diff options
| author | jvoisin | 2018-07-10 20:49:54 +0200 |
|---|---|---|
| committer | jvoisin | 2018-07-10 21:24:26 +0200 |
| commit | d5861e46537f3e94abd26f63a3a7ad5b69d25e77 (patch) | |
| tree | b7d01595ad77fd210142afaaa8de28dfd919db3c | |
| parent | 22e3918f67b3b3517312406c70a1e71641afc7ae (diff) | |
Implement a check for dependencies in mat2
Example use:
```
$ mat2 -c
Dependencies required for MAT2 0.1.3:
- Cairo: yes
- Exiftool: yes
- GdkPixbuf from PyGobject: yes
- Mutagen: yes
- Poppler from PyGobject: yes
- PyGobject: yes
```
This should close #35
| -rw-r--r-- | libmat2/__init__.py | 33 | ||||
| -rwxr-xr-x | mat2 | 14 | ||||
| -rw-r--r-- | tests/test_climat2.py | 9 | ||||
| -rw-r--r-- | tests/test_libmat2.py | 8 |
4 files changed, 59 insertions, 5 deletions
diff --git a/libmat2/__init__.py b/libmat2/__init__.py index d910215..cd65bfc 100644 --- a/libmat2/__init__.py +++ b/libmat2/__init__.py | |||
| @@ -1,5 +1,13 @@ | |||
| 1 | #!/bin/env python3 | 1 | #!/bin/env python3 |
| 2 | 2 | ||
| 3 | import os | ||
| 4 | import collections | ||
| 5 | import importlib | ||
| 6 | from typing import Dict | ||
| 7 | |||
| 8 | # make pyflakes happy | ||
| 9 | assert Dict | ||
| 10 | |||
| 3 | # A set of extension that aren't supported, despite matching a supported mimetype | 11 | # A set of extension that aren't supported, despite matching a supported mimetype |
| 4 | UNSUPPORTED_EXTENSIONS = { | 12 | UNSUPPORTED_EXTENSIONS = { |
| 5 | '.asc', | 13 | '.asc', |
| @@ -17,3 +25,28 @@ UNSUPPORTED_EXTENSIONS = { | |||
| 17 | '.xsd', | 25 | '.xsd', |
| 18 | '.xsl', | 26 | '.xsl', |
| 19 | } | 27 | } |
| 28 | |||
| 29 | DEPENDENCIES = { | ||
| 30 | 'cairo': 'Cairo', | ||
| 31 | 'gi': 'PyGobject', | ||
| 32 | 'gi.repository.GdkPixbuf': 'GdkPixbuf from PyGobject', | ||
| 33 | 'gi.repository.Poppler': 'Poppler from PyGobject', | ||
| 34 | 'mutagen': 'Mutagen', | ||
| 35 | } | ||
| 36 | |||
| 37 | def check_dependencies() -> dict: | ||
| 38 | ret = collections.defaultdict(bool) # type: Dict[str, bool] | ||
| 39 | |||
| 40 | exiftool = '/usr/bin/exiftool' | ||
| 41 | ret['Exiftool'] = False | ||
| 42 | if os.path.isfile(exiftool) and os.access(exiftool, os.X_OK): # pragma: no cover | ||
| 43 | ret['Exiftool'] = True | ||
| 44 | |||
| 45 | for key, value in DEPENDENCIES.items(): | ||
| 46 | ret[value] = True | ||
| 47 | try: | ||
| 48 | importlib.import_module(key) | ||
| 49 | except ImportError: # pragma: no cover | ||
| 50 | ret[value] = False # pragma: no cover | ||
| 51 | |||
| 52 | return ret | ||
| @@ -9,7 +9,7 @@ import argparse | |||
| 9 | import multiprocessing | 9 | import multiprocessing |
| 10 | 10 | ||
| 11 | try: | 11 | try: |
| 12 | from libmat2 import parser_factory, UNSUPPORTED_EXTENSIONS | 12 | from libmat2 import parser_factory, UNSUPPORTED_EXTENSIONS, check_dependencies |
| 13 | except ValueError as e: | 13 | except ValueError as e: |
| 14 | print(e) | 14 | print(e) |
| 15 | sys.exit(1) | 15 | sys.exit(1) |
| @@ -36,6 +36,9 @@ def create_arg_parser(): | |||
| 36 | version='MAT2 %s' % __version__) | 36 | version='MAT2 %s' % __version__) |
| 37 | parser.add_argument('-l', '--list', action='store_true', | 37 | parser.add_argument('-l', '--list', action='store_true', |
| 38 | help='list all supported fileformats') | 38 | help='list all supported fileformats') |
| 39 | parser.add_argument('-c', '--check-dependencies', action='store_true', | ||
| 40 | help='check if MAT2 has all the dependencies it needs') | ||
| 41 | |||
| 39 | 42 | ||
| 40 | info = parser.add_mutually_exclusive_group() | 43 | info = parser.add_mutually_exclusive_group() |
| 41 | info.add_argument('-s', '--show', action='store_true', | 44 | info.add_argument('-s', '--show', action='store_true', |
| @@ -108,9 +111,14 @@ def main(): | |||
| 108 | args = arg_parser.parse_args() | 111 | args = arg_parser.parse_args() |
| 109 | 112 | ||
| 110 | if not args.files: | 113 | if not args.files: |
| 111 | if not args.list: | 114 | if args.list: |
| 115 | show_parsers() | ||
| 116 | elif args.check_dependencies: | ||
| 117 | print("Dependencies required for MAT2 %s:" % __version__) | ||
| 118 | for key, value in sorted(check_dependencies().items()): | ||
| 119 | print('- %s: %s' % (key, 'yes' if value else 'no')) | ||
| 120 | else: | ||
| 112 | return arg_parser.print_help() | 121 | return arg_parser.print_help() |
| 113 | show_parsers() | ||
| 114 | return 0 | 122 | return 0 |
| 115 | 123 | ||
| 116 | elif args.show: | 124 | elif args.show: |
diff --git a/tests/test_climat2.py b/tests/test_climat2.py index 9948057..99f9b9c 100644 --- a/tests/test_climat2.py +++ b/tests/test_climat2.py | |||
| @@ -8,12 +8,12 @@ class TestHelp(unittest.TestCase): | |||
| 8 | def test_help(self): | 8 | def test_help(self): |
| 9 | proc = subprocess.Popen(['./mat2', '--help'], stdout=subprocess.PIPE) | 9 | proc = subprocess.Popen(['./mat2', '--help'], stdout=subprocess.PIPE) |
| 10 | stdout, _ = proc.communicate() | 10 | stdout, _ = proc.communicate() |
| 11 | self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-s | -L] [files [files ...]]', stdout) | 11 | self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-c] [-s | -L] [files [files ...]]', stdout) |
| 12 | 12 | ||
| 13 | def test_no_arg(self): | 13 | def test_no_arg(self): |
| 14 | proc = subprocess.Popen(['./mat2'], stdout=subprocess.PIPE) | 14 | proc = subprocess.Popen(['./mat2'], stdout=subprocess.PIPE) |
| 15 | stdout, _ = proc.communicate() | 15 | stdout, _ = proc.communicate() |
| 16 | self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-s | -L] [files [files ...]]', stdout) | 16 | self.assertIn(b'usage: mat2 [-h] [-v] [-l] [-c] [-s | -L] [files [files ...]]', stdout) |
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | class TestVersion(unittest.TestCase): | 19 | class TestVersion(unittest.TestCase): |
| @@ -22,6 +22,11 @@ class TestVersion(unittest.TestCase): | |||
| 22 | stdout, _ = proc.communicate() | 22 | stdout, _ = proc.communicate() |
| 23 | self.assertTrue(stdout.startswith(b'MAT2 ')) | 23 | self.assertTrue(stdout.startswith(b'MAT2 ')) |
| 24 | 24 | ||
| 25 | class TestDependencies(unittest.TestCase): | ||
| 26 | def test_dependencies(self): | ||
| 27 | proc = subprocess.Popen(['./mat2', '--check-dependencies'], stdout=subprocess.PIPE) | ||
| 28 | stdout, _ = proc.communicate() | ||
| 29 | self.assertTrue(b'MAT2' in stdout) | ||
| 25 | 30 | ||
| 26 | class TestReturnValue(unittest.TestCase): | 31 | class TestReturnValue(unittest.TestCase): |
| 27 | def test_nonzero(self): | 32 | def test_nonzero(self): |
diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py index cb37985..fa7e281 100644 --- a/tests/test_libmat2.py +++ b/tests/test_libmat2.py | |||
| @@ -7,6 +7,14 @@ import zipfile | |||
| 7 | import tempfile | 7 | import tempfile |
| 8 | 8 | ||
| 9 | from libmat2 import pdf, images, audio, office, parser_factory, torrent, harmless | 9 | from libmat2 import pdf, images, audio, office, parser_factory, torrent, harmless |
| 10 | from libmat2 import check_dependencies | ||
| 11 | |||
| 12 | |||
| 13 | class TestCheckDependencies(unittest.TestCase): | ||
| 14 | def test_deps(self): | ||
| 15 | ret = check_dependencies() | ||
| 16 | for key, value in ret.items(): | ||
| 17 | self.assertTrue(value) | ||
| 10 | 18 | ||
| 11 | 19 | ||
| 12 | class TestParserFactory(unittest.TestCase): | 20 | class TestParserFactory(unittest.TestCase): |
