1
0
mirror of synced 2024-11-22 09:14:23 +01:00

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>
This commit is contained in:
Antoine Tenart 2019-05-11 11:20:05 +02:00 committed by jvoisin
parent 51ab2db279
commit f19f6ed8b6
3 changed files with 52 additions and 20 deletions

View File

@ -30,35 +30,65 @@ UNSUPPORTED_EXTENSIONS = {
} }
DEPENDENCIES = { DEPENDENCIES = {
'Cairo': 'cairo', 'Cairo': {
'PyGobject': 'gi', 'module': 'cairo',
'GdkPixbuf from PyGobject': 'gi.repository.GdkPixbuf', 'required': True,
'Poppler from PyGobject': 'gi.repository.Poppler', },
'GLib from PyGobject': 'gi.repository.GLib', 'PyGobject': {
'Mutagen': 'mutagen', 'module': 'gi',
'required': True,
},
'GdkPixbuf from PyGobject': {
'module': 'gi.repository.GdkPixbuf',
'required': True,
},
'Poppler from PyGobject': {
'module': 'gi.repository.Poppler',
'required': True,
},
'GLib from PyGobject': {
'module': 'gi.repository.GLib',
'required': True,
},
'Mutagen': {
'module': 'mutagen',
'required': True,
},
} }
CMD_DEPENDENCIES = { CMD_DEPENDENCIES = {
'Exiftool': exiftool._get_exiftool_path, 'Exiftool': {
'Ffmpeg': video._get_ffmpeg_path, 'cmd': exiftool._get_exiftool_path,
'required': False,
},
'Ffmpeg': {
'cmd': video._get_ffmpeg_path,
'required': False,
},
} }
def check_dependencies() -> Dict[str, bool]: def check_dependencies() -> Dict[str, Dict[str, bool]]:
ret = collections.defaultdict(bool) # type: Dict[str, bool] ret = collections.defaultdict(bool) # type: Dict[str, bool]
for key, value in DEPENDENCIES.items(): for key, value in DEPENDENCIES.items():
ret[key] = True ret[key] = {
'found': True,
'required': value['required'],
}
try: try:
importlib.import_module(value) importlib.import_module(value['module'])
except ImportError: # pragma: no cover except ImportError: # pragma: no cover
ret[key] = False # pragma: no cover ret[key]['found'] = False
for k, v in CMD_DEPENDENCIES.items(): for k, v in CMD_DEPENDENCIES.items():
ret[k] = True ret[k] = {
'found': True,
'required': v['required'],
}
try: try:
v() v['cmd']()
except RuntimeError: # pragma: no cover except RuntimeError: # pragma: no cover
ret[k] = False ret[k]['found'] = False
return ret return ret

5
mat2
View File

@ -165,9 +165,10 @@ def main() -> int:
show_parsers() show_parsers()
return 0 return 0
elif args.check_dependencies: elif args.check_dependencies:
print("Dependencies required for MAT2 %s:" % __version__) print("Dependencies for MAT2 %s:" % __version__)
for key, value in sorted(check_dependencies().items()): for key, value in sorted(check_dependencies().items()):
print('- %s: %s' % (key, 'yes' if value else 'no')) print('- %s: %s %s' % (key, 'yes' if value['found'] else 'no',
'(optional)' if not value['required'] else ''))
else: else:
arg_parser.print_help() arg_parser.print_help()
return 0 return 0

View File

@ -16,7 +16,8 @@ class TestCheckDependencies(unittest.TestCase):
def test_deps(self): def test_deps(self):
ret = check_dependencies() ret = check_dependencies()
for key, value in ret.items(): for key, value in ret.items():
self.assertTrue(value, "The value for %s is False" % key) if value['required']:
self.assertTrue(value['found'], "The value for %s is False" % key)
class TestParserFactory(unittest.TestCase): class TestParserFactory(unittest.TestCase):