1# https://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6 

7import sys 

8import os.path as op 

9from importlib import import_module 

10 

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} 

27 

28 

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']) 

33 

34 

35def dependencies(group): 

36 

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 

46 

47 return d 

48 

49 

50def str_dependencies(): 

51 lines = [] 

52 lines.append('dependencies:') 

53 for group in ['required', 'optional']: 

54 lines.append(' %s:' % group) 

55 

56 for package, version in dependencies(group).items(): 

57 lines.append(' %s: %s' % ( 

58 package, 

59 version or 'N/A')) 

60 

61 return '\n'.join(lines) 

62 

63 

64def print_dependencies(): 

65 print(str_dependencies()) 

66 

67 

68class MissingPyrockoDependency(ImportError): 

69 pass 

70 

71 

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] 

77 

78 raise MissingPyrockoDependency(''' 

79 

80Missing Pyrocko requirement: %s 

81 

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: 

87 

88 %s 

89''' % ( 

90 package_name, 

91 package_name, 

92 package_name, 

93 module_name, 

94 str(e))) 

95 

96 

97def require_all(group): 

98 for (_, module_name, _) in _pyrocko_deps[group]: 

99 require(module_name) 

100 

101 

102def find_pyrocko_installations(): 

103 found = [] 

104 seen = set() 

105 orig_sys_path = list(sys.path) 

106 while sys.path: 

107 

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) 

114 

115 found.append(x) 

116 seen.add(dpath) 

117 

118 del sys.modules['pyrocko'] 

119 del sys.modules['pyrocko.info'] 

120 except (ImportError, AttributeError): 

121 pass 

122 

123 sys.path.pop(0) 

124 

125 sys.path = orig_sys_path 

126 return found 

127 

128 

129def str_installations(found): 

130 lines = [ 

131 'Python library path (sys.path): \n %s\n' % '\n '.join(sys.path)] 

132 

133 dates = sorted([xx[0] for xx in found]) 

134 i = 1 

135 

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)' 

141 

142 if installed_date == dates[-1]: 

143 oldnew = ' (newest)' 

144 

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)) 

155 

156 i += 1 

157 

158 return '\n'.join(lines) 

159 

160 

161def print_installations(): 

162 found = find_pyrocko_installations() 

163 print(str_installations(found)) 

164 

165 

166if __name__ == '__main__': 

167 print_dependencies() 

168 print() 

169 print_installations()