1# https://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
7import sys
8import os.path as op
9from importlib import import_module
11_pyrocko_deps = {
12 'required': [
13 ('python', 'sys', lambda x: '.'.join(map(str, x.version_info[:3]))),
14 ('pyyaml', 'yaml', '__version__'),
15 ('numpy', 'numpy', '__version__'),
16 ('scipy', 'scipy', '__version__'),
17 ('matplotlib', 'matplotlib', '__version__'),
18 ('requests', 'requests', '__version__'),
19 ],
20 'optional': [
21 ('PyQt5', 'PyQt5.Qt', 'PYQT_VERSION_STR'),
22 ('PyQtWebEngine', 'PyQt5.QtWebEngine', 'PYQT_WEBENGINE_VERSION_STR'),
23 ('vtk', 'vtk', 'VTK_VERSION'),
24 ('pyserial', 'serial', '__version__'),
25 ],
26}
29_module_to_package_name = dict(
30 (module_name, package_name)
31 for (package_name, module_name, _)
32 in _pyrocko_deps['required'] + _pyrocko_deps['optional'])
35def dependencies(group):
37 d = {}
38 for (package_name, module_name, version_attr) in _pyrocko_deps[group]:
39 try:
40 mod = import_module(module_name)
41 d[package_name] = getattr(mod, version_attr) \
42 if isinstance(version_attr, str) \
43 else version_attr(mod)
44 except ImportError:
45 d[package_name] = None
47 return d
50def str_dependencies():
51 lines = []
52 lines.append('dependencies:')
53 for group in ['required', 'optional']:
54 lines.append(' %s:' % group)
56 for package, version in dependencies(group).items():
57 lines.append(' %s: %s' % (
58 package,
59 version or 'N/A'))
61 return '\n'.join(lines)
64def print_dependencies():
65 print(str_dependencies())
68class MissingPyrockoDependency(ImportError):
69 pass
72def require(module_name):
73 try:
74 return import_module(module_name)
75 except ImportError as e:
76 package_name = _module_to_package_name[module_name]
78 raise MissingPyrockoDependency('''
80Missing Pyrocko requirement: %s
82The Python package '%s' is required to run this program, but it doesn't seem to
83be available or an error occured while importing it. Use your package manager
84of choice (apt / yum / conda / pip) to install it. To verify that '%s' is
85available in your Python environment, check that "python -c \'import %s\'"
86exits without error. The original error was:
88 %s
89''' % (
90 package_name,
91 package_name,
92 package_name,
93 module_name,
94 str(e)))
97def require_all(group):
98 for (_, module_name, _) in _pyrocko_deps[group]:
99 require(module_name)
102def find_pyrocko_installations():
103 found = []
104 seen = set()
105 orig_sys_path = list(sys.path)
106 while sys.path:
108 try:
109 import pyrocko
110 dpath = op.dirname(op.abspath(pyrocko.__file__))
111 if dpath not in seen:
112 x = (pyrocko.installed_date, dpath,
113 pyrocko.long_version)
115 found.append(x)
116 seen.add(dpath)
118 del sys.modules['pyrocko']
119 del sys.modules['pyrocko.info']
120 except (ImportError, AttributeError):
121 pass
123 sys.path.pop(0)
125 sys.path = orig_sys_path
126 return found
129def str_installations(found):
130 lines = [
131 'Python library path (sys.path): \n %s\n' % '\n '.join(sys.path)]
133 dates = sorted([xx[0] for xx in found])
134 i = 1
136 for (installed_date, installed_path, long_version) in found:
137 oldnew = ''
138 if len(dates) >= 2:
139 if installed_date == dates[0]:
140 oldnew = ' (oldest)'
142 if installed_date == dates[-1]:
143 oldnew = ' (newest)'
145 lines.append('''Pyrocko installation #%i %s:
146 date installed: %s%s
147 version: %s
148 path: %s
149''' % (
150 i, '(used)' if i == 1 else '(not used)',
151 installed_date,
152 oldnew,
153 long_version,
154 installed_path))
156 i += 1
158 return '\n'.join(lines)
161def print_installations():
162 found = find_pyrocko_installations()
163 print(str_installations(found))
166if __name__ == '__main__':
167 print_dependencies()
168 print()
169 print_installations()