diff options
| author | Antoine Tenart | 2019-05-11 11:20:05 +0200 |
|---|---|---|
| committer | jvoisin | 2019-05-13 23:35:26 +0200 |
| commit | f19f6ed8b6ded81d2a1cc9d2fe606f71fcd0e27a (patch) | |
| tree | d5982c72a91f2cd6a6d10e466b4b86e9937ff143 | |
| parent | 51ab2db2793e290f8df4791bf552e3a2531de8d4 (diff) | |
Rework the dependency checks to distinguish required/optional ones
Rework the dependencies definition to include a 'required' flags, which
is passed by the check_dependencies helper to the callers, so that they
can distinguish between required and optional dependencies.
This help in two ways:
- The unit test for the dependencies was now failing when an optional
one was missing, due to a previous rework.
- Mat2's --check-dependencies was referring to "required dependencies"
and was misleading for the user as some of them could be optional.
Signed-off-by: Antoine Tenart <antoine.tenart@ack.tf>
| -rw-r--r-- | libmat2/__init__.py | 64 | ||||
| -rwxr-xr-x | mat2 | 5 | ||||
| -rw-r--r-- | tests/test_libmat2.py | 3 |
3 files changed, 52 insertions, 20 deletions
diff --git a/libmat2/__init__.py b/libmat2/__init__.py index 41c2395..501baaa 100644 --- a/libmat2/__init__.py +++ b/libmat2/__init__.py | |||
| @@ -30,35 +30,65 @@ UNSUPPORTED_EXTENSIONS = { | |||
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | DEPENDENCIES = { | 32 | DEPENDENCIES = { |
| 33 | 'Cairo': 'cairo', | 33 | 'Cairo': { |
| 34 | 'PyGobject': 'gi', | 34 | 'module': 'cairo', |
| 35 | 'GdkPixbuf from PyGobject': 'gi.repository.GdkPixbuf', | 35 | 'required': True, |
| 36 | 'Poppler from PyGobject': 'gi.repository.Poppler', | 36 | }, |
| 37 | 'GLib from PyGobject': 'gi.repository.GLib', | 37 | 'PyGobject': { |
| 38 | 'Mutagen': 'mutagen', | 38 | 'module': 'gi', |
| 39 | } | 39 | 'required': True, |
| 40 | }, | ||
| 41 | 'GdkPixbuf from PyGobject': { | ||
| 42 | 'module': 'gi.repository.GdkPixbuf', | ||
| 43 | 'required': True, | ||
| 44 | }, | ||
| 45 | 'Poppler from PyGobject': { | ||
| 46 | 'module': 'gi.repository.Poppler', | ||
| 47 | 'required': True, | ||
| 48 | }, | ||
| 49 | 'GLib from PyGobject': { | ||
| 50 | 'module': 'gi.repository.GLib', | ||
| 51 | 'required': True, | ||
| 52 | }, | ||
| 53 | 'Mutagen': { | ||
| 54 | 'module': 'mutagen', | ||
| 55 | 'required': True, | ||
| 56 | }, | ||
| 57 | } | ||
| 40 | 58 | ||
| 41 | CMD_DEPENDENCIES = { | 59 | CMD_DEPENDENCIES = { |
| 42 | 'Exiftool': exiftool._get_exiftool_path, | 60 | 'Exiftool': { |
| 43 | 'Ffmpeg': video._get_ffmpeg_path, | 61 | 'cmd': exiftool._get_exiftool_path, |
| 44 | } | 62 | 'required': False, |
| 63 | }, | ||
| 64 | 'Ffmpeg': { | ||
| 65 | 'cmd': video._get_ffmpeg_path, | ||
| 66 | 'required': False, | ||
| 67 | }, | ||
| 68 | } | ||
| 45 | 69 | ||
| 46 | def check_dependencies() -> Dict[str, bool]: | 70 | def check_dependencies() -> Dict[str, Dict[str, bool]]: |
| 47 | ret = collections.defaultdict(bool) # type: Dict[str, bool] | 71 | ret = collections.defaultdict(bool) # type: Dict[str, bool] |
| 48 | 72 | ||
| 49 | for key, value in DEPENDENCIES.items(): | 73 | for key, value in DEPENDENCIES.items(): |
| 50 | ret[key] = True | 74 | ret[key] = { |
| 75 | 'found': True, | ||
| 76 | 'required': value['required'], | ||
| 77 | } | ||
| 51 | try: | 78 | try: |
| 52 | importlib.import_module(value) | 79 | importlib.import_module(value['module']) |
| 53 | except ImportError: # pragma: no cover | 80 | except ImportError: # pragma: no cover |
| 54 | ret[key] = False # pragma: no cover | 81 | ret[key]['found'] = False |
| 55 | 82 | ||
| 56 | for k, v in CMD_DEPENDENCIES.items(): | 83 | for k, v in CMD_DEPENDENCIES.items(): |
| 57 | ret[k] = True | 84 | ret[k] = { |
| 85 | 'found': True, | ||
| 86 | 'required': v['required'], | ||
| 87 | } | ||
| 58 | try: | 88 | try: |
| 59 | v() | 89 | v['cmd']() |
| 60 | except RuntimeError: # pragma: no cover | 90 | except RuntimeError: # pragma: no cover |
| 61 | ret[k] = False | 91 | ret[k]['found'] = False |
| 62 | 92 | ||
| 63 | return ret | 93 | return ret |
| 64 | 94 | ||
| @@ -165,9 +165,10 @@ def main() -> int: | |||
| 165 | show_parsers() | 165 | show_parsers() |
| 166 | return 0 | 166 | return 0 |
| 167 | elif args.check_dependencies: | 167 | elif args.check_dependencies: |
| 168 | print("Dependencies required for MAT2 %s:" % __version__) | 168 | print("Dependencies for MAT2 %s:" % __version__) |
| 169 | for key, value in sorted(check_dependencies().items()): | 169 | for key, value in sorted(check_dependencies().items()): |
| 170 | print('- %s: %s' % (key, 'yes' if value else 'no')) | 170 | print('- %s: %s %s' % (key, 'yes' if value['found'] else 'no', |
| 171 | '(optional)' if not value['required'] else '')) | ||
| 171 | else: | 172 | else: |
| 172 | arg_parser.print_help() | 173 | arg_parser.print_help() |
| 173 | return 0 | 174 | return 0 |
diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py index de5f67c..64e679f 100644 --- a/tests/test_libmat2.py +++ b/tests/test_libmat2.py | |||
| @@ -16,7 +16,8 @@ class TestCheckDependencies(unittest.TestCase): | |||
| 16 | def test_deps(self): | 16 | def test_deps(self): |
| 17 | ret = check_dependencies() | 17 | ret = check_dependencies() |
| 18 | for key, value in ret.items(): | 18 | for key, value in ret.items(): |
| 19 | self.assertTrue(value, "The value for %s is False" % key) | 19 | if value['required']: |
| 20 | self.assertTrue(value['found'], "The value for %s is False" % key) | ||
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | class TestParserFactory(unittest.TestCase): | 23 | class TestParserFactory(unittest.TestCase): |
