From ae2bb02fa22118fb69987e16040c74098b0220b4 Mon Sep 17 00:00:00 2001
From: jvoisin <julien.voisin@dustri.org>
Date: Tue, 10 Jul 2018 20:49:54 +0200
Subject: [PATCH] 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
---
 libmat2/__init__.py | 33 +++++++++++++++++++++++++++++++++
 mat2                | 14 +++++++++++---
 2 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/libmat2/__init__.py b/libmat2/__init__.py
index d910215..74b07ef 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):
+        ret['Exiftool'] = True
+
+    for key, value in DEPENDENCIES.items():
+        ret[value] = True
+        try:
+            importlib.import_module(key)
+        except ImportError:
+            ret[value] = False
+
+    return ret
diff --git a/mat2 b/mat2
index 7c7b652..efc0478 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:
-- 
GitLab