diff --git a/libmat2/__init__.py b/libmat2/__init__.py index d910215a6e3cbd07b2c35c3c95c4bce161590609..cd65bfca44b8c7a905ff968d3c8edc2c1166b899 100644 --- a/libmat2/__init__.py +++ b/libmat2/__init__.py @@ -1,5 +1,13 @@ #!/bin/env python3 +import os +import collections +import importlib +from typing import Dict + +# make pyflakes happy +assert Dict + # A set of extension that aren't supported, despite matching a supported mimetype UNSUPPORTED_EXTENSIONS = { '.asc', @@ -17,3 +25,28 @@ UNSUPPORTED_EXTENSIONS = { '.xsd', '.xsl', } + +DEPENDENCIES = { + 'cairo': 'Cairo', + 'gi': 'PyGobject', + 'gi.repository.GdkPixbuf': 'GdkPixbuf from PyGobject', + 'gi.repository.Poppler': 'Poppler from PyGobject', + 'mutagen': 'Mutagen', + } + +def check_dependencies() -> dict: + ret = collections.defaultdict(bool) # type: Dict[str, bool] + + exiftool = '/usr/bin/exiftool' + ret['Exiftool'] = False + if os.path.isfile(exiftool) and os.access(exiftool, os.X_OK): # pragma: no cover + ret['Exiftool'] = True + + for key, value in DEPENDENCIES.items(): + ret[value] = True + try: + importlib.import_module(key) + except ImportError: # pragma: no cover + ret[value] = False # pragma: no cover + + return ret diff --git a/mat2 b/mat2 index 7c7b6526c813b198ffeb2773a043f7e68bee902c..efc0478e79ff45920d1c81121c6c3f37ba3f0f60 100755 --- a/mat2 +++ b/mat2 @@ -9,7 +9,7 @@ import argparse import multiprocessing try: - from libmat2 import parser_factory, UNSUPPORTED_EXTENSIONS + from libmat2 import parser_factory, UNSUPPORTED_EXTENSIONS, check_dependencies except ValueError as e: print(e) sys.exit(1) @@ -36,6 +36,9 @@ def create_arg_parser(): version='MAT2 %s' % __version__) parser.add_argument('-l', '--list', action='store_true', help='list all supported fileformats') + parser.add_argument('-c', '--check-dependencies', action='store_true', + help='check if MAT2 has all the dependencies it needs') + info = parser.add_mutually_exclusive_group() info.add_argument('-s', '--show', action='store_true', @@ -108,9 +111,14 @@ def main(): args = arg_parser.parse_args() if not args.files: - if not args.list: + if args.list: + show_parsers() + elif args.check_dependencies: + print("Dependencies required for MAT2 %s:" % __version__) + for key, value in sorted(check_dependencies().items()): + print('- %s: %s' % (key, 'yes' if value else 'no')) + else: return arg_parser.print_help() - show_parsers() return 0 elif args.show: diff --git a/tests/test_climat2.py b/tests/test_climat2.py index 994805717d3d6251cf77a952f1ab1310d3f3e06d..99f9b9cad5b381dc74eb77d92ccc5108061b56ca 100644 --- a/tests/test_climat2.py +++ b/tests/test_climat2.py @@ -8,12 +8,12 @@ class TestHelp(unittest.TestCase): def test_help(self): proc = subprocess.Popen(['./mat2', '--help'], stdout=subprocess.PIPE) stdout, _ = proc.communicate() - self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-s | -L] [files [files ...]]', stdout) + self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-c] [-s | -L] [files [files ...]]', stdout) def test_no_arg(self): proc = subprocess.Popen(['./mat2'], stdout=subprocess.PIPE) stdout, _ = proc.communicate() - self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-s | -L] [files [files ...]]', stdout) + self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-c] [-s | -L] [files [files ...]]', stdout) class TestVersion(unittest.TestCase): @@ -22,6 +22,11 @@ class TestVersion(unittest.TestCase): stdout, _ = proc.communicate() self.assertTrue(stdout.startswith(b'MAT2 ')) +class TestDependencies(unittest.TestCase): + def test_dependencies(self): + proc = subprocess.Popen(['./mat2', '--check-dependencies'], stdout=subprocess.PIPE) + stdout, _ = proc.communicate() + self.assertTrue(b'MAT2' in stdout) class TestReturnValue(unittest.TestCase): def test_nonzero(self): diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py index cb379858b62ef48315f559f50310203422185fee..fa7e28172116e6f119386272e65eb013e18b6270 100644 --- a/tests/test_libmat2.py +++ b/tests/test_libmat2.py @@ -7,6 +7,14 @@ import zipfile import tempfile from libmat2 import pdf, images, audio, office, parser_factory, torrent, harmless +from libmat2 import check_dependencies + + +class TestCheckDependencies(unittest.TestCase): + def test_deps(self): + ret = check_dependencies() + for key, value in ret.items(): + self.assertTrue(value) class TestParserFactory(unittest.TestCase):