Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/deps.py: 24%

80 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-10-04 09:52 +0000

1# https://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6''' 

7Library dependency checkup utilities. 

8''' 

9 

10import sys 

11import os.path as op 

12from importlib import import_module 

13import logging 

14 

15logger = logging.getLogger('pyrocko.deps') 

16 

17 

18_pyrocko_deps = { 

19 'required': [ 

20 ('python', 'sys', lambda x: '.'.join(map(str, x.version_info[:3]))), 

21 ('pyyaml', 'yaml', '__version__'), 

22 ('numpy', 'numpy', '__version__'), 

23 ('scipy', 'scipy', '__version__'), 

24 ('matplotlib', 'matplotlib', '__version__'), 

25 ('requests', 'requests', '__version__'), 

26 ], 

27 'optional': [ 

28 ('PyQt5', 'PyQt5.Qt', 'PYQT_VERSION_STR'), 

29 ('PyQtWebEngine', 'PyQt5.QtWebEngine', 'PYQT_WEBENGINE_VERSION_STR'), 

30 ('vtk', 'vtk', 'VTK_VERSION'), 

31 ('pyserial', 'serial', '__version__'), 

32 ('kite', 'kite', '__version__'), 

33 ], 

34} 

35 

36 

37_module_to_package_name = dict( 

38 (module_name, package_name) 

39 for (package_name, module_name, _) 

40 in _pyrocko_deps['required'] + _pyrocko_deps['optional']) 

41 

42 

43def dependencies(group): 

44 

45 d = {} 

46 for (package_name, module_name, version_attr) in _pyrocko_deps[group]: 

47 try: 

48 mod = import_module(module_name) 

49 d[package_name] = getattr(mod, version_attr) \ 

50 if isinstance(version_attr, str) \ 

51 else version_attr(mod) 

52 except ImportError: 

53 d[package_name] = None 

54 

55 return d 

56 

57 

58def str_dependencies(): 

59 lines = [] 

60 lines.append('dependencies:') 

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

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

63 

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

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

66 package, 

67 version or 'N/A')) 

68 

69 return '\n'.join(lines) 

70 

71 

72def print_dependencies(): 

73 print(str_dependencies()) 

74 

75 

76class MissingPyrockoDependency(ImportError): 

77 pass 

78 

79 

80def require(module_name): 

81 try: 

82 return import_module(module_name) 

83 except ImportError as e: 

84 package_name = _module_to_package_name[module_name] 

85 

86 raise MissingPyrockoDependency(''' 

87 

88Missing Pyrocko requirement: %s 

89 

90The Python package '%s' is required to run this program, but it doesn't seem to 

91be available or an error occured while importing it. Use your package manager 

92of choice (apt / yum / conda / pip) to install it. To verify that '%s' is 

93available in your Python environment, check that "python -c \'import %s\'" 

94exits without error. The original error was: 

95 

96 %s 

97''' % ( 

98 package_name, 

99 package_name, 

100 package_name, 

101 module_name, 

102 str(e))) from None 

103 

104 

105def import_optional(module_name, needed_for): 

106 try: 

107 return import_module(module_name) 

108 except ImportError as e: 

109 package_name = _module_to_package_name[module_name] 

110 

111 logger.info( 

112 'Optional Pyrocko dependency "%s" is not available ' 

113 '(needed for %s): %s' % ( 

114 package_name, 

115 needed_for, 

116 str(e))) 

117 

118 return None 

119 

120 

121def require_all(group): 

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

123 require(module_name) 

124 

125 

126def find_pyrocko_installations(): 

127 found = [] 

128 seen = set() 

129 orig_sys_path = list(sys.path) 

130 while sys.path: 

131 

132 try: 

133 import pyrocko 

134 dpath = op.dirname(op.abspath(pyrocko.__file__)) 

135 if dpath not in seen: 

136 x = (pyrocko.installed_date, dpath, 

137 pyrocko.long_version, 

138 pyrocko.src_path if hasattr(pyrocko, 'src_path') else '?') 

139 

140 found.append(x) 

141 seen.add(dpath) 

142 

143 del sys.modules['pyrocko'] 

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

145 except (ImportError, AttributeError): 

146 pass 

147 

148 sys.path.pop(0) 

149 

150 sys.path = orig_sys_path 

151 return found 

152 

153 

154def str_installations(found): 

155 lines = [ 

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

157 

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

159 i = 1 

160 

161 for (installed_date, installed_path, long_version, src_path) in found: 

162 oldnew = '' 

163 if len(dates) >= 2: 

164 if installed_date == dates[0]: 

165 oldnew = ' (oldest)' 

166 

167 if installed_date == dates[-1]: 

168 oldnew = ' (newest)' 

169 

170 lines.append('''Pyrocko installation #%i %s: 

171 date installed: %s%s 

172 version: %s 

173 path: %s 

174 src path: %s 

175''' % ( 

176 i, '(used)' if i == 1 else '(not used)', 

177 installed_date, 

178 oldnew, 

179 long_version, 

180 installed_path, 

181 src_path)) 

182 

183 i += 1 

184 

185 return '\n'.join(lines) 

186 

187 

188def print_installations(): 

189 found = find_pyrocko_installations() 

190 print(str_installations(found)) 

191 

192 

193if __name__ == '__main__': 

194 print_dependencies() 

195 print() 

196 print_installations()