1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# coding=utf-8 |
3
|
|
|
from __future__ import division, print_function, unicode_literals |
4
|
|
|
import importlib |
5
|
|
|
from sacred.utils import modules_exist |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class MissingDependencyMock(object): |
9
|
|
|
def __init__(self, depends_on): |
10
|
|
|
self.depends_on = depends_on |
11
|
|
|
|
12
|
|
|
def __getattribute__(self, item): |
13
|
|
|
dep = object.__getattribute__(self, 'depends_on') |
14
|
|
|
if isinstance(dep, (list, tuple)): |
15
|
|
|
raise ImportError('Depends on missing {!r} packages.'.format(dep)) |
16
|
|
|
else: |
17
|
|
|
raise ImportError('Depends on missing {!r} package.'.format(dep)) |
18
|
|
|
|
19
|
|
|
def __call__(self, *args, **kwargs): |
20
|
|
|
dep = object.__getattribute__(self, 'depends_on') |
21
|
|
|
if isinstance(dep, (list, tuple)): |
22
|
|
|
raise ImportError('Depends on missing {!r} packages.'.format(dep)) |
23
|
|
|
else: |
24
|
|
|
raise ImportError('Depends on missing {!r} package.'.format(dep)) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def optional_import(*package_names): |
28
|
|
|
try: |
29
|
|
|
packages = [importlib.import_module(pn) for pn in package_names] |
30
|
|
|
return True, packages[0] |
31
|
|
|
except ImportError: |
32
|
|
|
return False, None |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
# Get libc in a cross-platform way and use it to also flush the c stdio buffers |
36
|
|
|
# credit to J.F. Sebastians SO answer from here: |
37
|
|
|
# http://stackoverflow.com/a/22434262/1388435 |
38
|
|
|
try: |
39
|
|
|
import ctypes |
40
|
|
|
from ctypes.util import find_library |
41
|
|
|
except ImportError: |
42
|
|
|
has_libc, libc = False, None |
43
|
|
|
else: |
44
|
|
|
try: |
45
|
|
|
has_libc, libc = True, ctypes.cdll.msvcrt # Windows |
46
|
|
|
except OSError: |
47
|
|
|
has_libc, libc = True, ctypes.cdll.LoadLibrary(find_library('c')) |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
has_numpy, np = optional_import('numpy') |
51
|
|
|
has_yaml, yaml = optional_import('yaml') |
52
|
|
|
has_pandas, pandas = optional_import('pandas') |
53
|
|
|
|
54
|
|
|
has_sqlalchemy = modules_exist('sqlalchemy') |
55
|
|
|
has_mako = modules_exist('mako') |
56
|
|
|
has_gitpython = modules_exist('git') |
57
|
|
|
has_tinydb = modules_exist('tinydb', 'tinydb_serialization', 'hashfs') |
58
|
|
|
has_requests = modules_exist('requests') |
59
|
|
|
has_tensorflow = modules_exist("tensorflow") |
60
|
|
|
has_telegram = modules_exist('telegram') |
61
|
|
|
|