1 | """ |
---|
2 | Decentralized storage grid. |
---|
3 | |
---|
4 | community web site: U{https://tahoe-lafs.org/} |
---|
5 | """ |
---|
6 | |
---|
7 | class PackagingError(EnvironmentError): |
---|
8 | """ |
---|
9 | Raised when there is an error in packaging of Tahoe-LAFS or its |
---|
10 | dependencies which makes it impossible to proceed safely. |
---|
11 | """ |
---|
12 | pass |
---|
13 | |
---|
14 | __version__ = "unknown" |
---|
15 | try: |
---|
16 | from allmydata._version import __version__ |
---|
17 | except ImportError: |
---|
18 | # We're running in a tree that hasn't run "./setup.py darcsver", and didn't |
---|
19 | # come with a _version.py, so we don't know what our version is. This should |
---|
20 | # not happen very often. |
---|
21 | pass |
---|
22 | |
---|
23 | __appname__ = "unknown" |
---|
24 | try: |
---|
25 | from allmydata._appname import __appname__ |
---|
26 | except ImportError: |
---|
27 | # We're running in a tree that hasn't run "./setup.py". This shouldn't happen. |
---|
28 | pass |
---|
29 | |
---|
30 | # __full_version__ is the one that you ought to use when identifying yourself in the |
---|
31 | # "application" part of the Tahoe versioning scheme: |
---|
32 | # https://tahoe-lafs.org/trac/tahoe-lafs/wiki/Versioning |
---|
33 | __full_version__ = __appname__ + '/' + str(__version__) |
---|
34 | |
---|
35 | import os, platform, re, subprocess, sys, traceback |
---|
36 | _distributor_id_cmdline_re = re.compile("(?:Distributor ID:)\s*(.*)", re.I) |
---|
37 | _release_cmdline_re = re.compile("(?:Release:)\s*(.*)", re.I) |
---|
38 | |
---|
39 | _distributor_id_file_re = re.compile("(?:DISTRIB_ID\s*=)\s*(.*)", re.I) |
---|
40 | _release_file_re = re.compile("(?:DISTRIB_RELEASE\s*=)\s*(.*)", re.I) |
---|
41 | |
---|
42 | global _distname,_version |
---|
43 | _distname = None |
---|
44 | _version = None |
---|
45 | |
---|
46 | def get_linux_distro(): |
---|
47 | """ Tries to determine the name of the Linux OS distribution name. |
---|
48 | |
---|
49 | First, try to parse a file named "/etc/lsb-release". If it exists, and |
---|
50 | contains the "DISTRIB_ID=" line and the "DISTRIB_RELEASE=" line, then return |
---|
51 | the strings parsed from that file. |
---|
52 | |
---|
53 | If that doesn't work, then invoke platform.dist(). |
---|
54 | |
---|
55 | If that doesn't work, then try to execute "lsb_release", as standardized in |
---|
56 | 2001: |
---|
57 | |
---|
58 | http://refspecs.freestandards.org/LSB_1.0.0/gLSB/lsbrelease.html |
---|
59 | |
---|
60 | The current version of the standard is here: |
---|
61 | |
---|
62 | http://refspecs.freestandards.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/lsbrelease.html |
---|
63 | |
---|
64 | that lsb_release emitted, as strings. |
---|
65 | |
---|
66 | Returns a tuple (distname,version). Distname is what LSB calls a |
---|
67 | "distributor id", e.g. "Ubuntu". Version is what LSB calls a "release", |
---|
68 | e.g. "8.04". |
---|
69 | |
---|
70 | A version of this has been submitted to python as a patch for the standard |
---|
71 | library module "platform": |
---|
72 | |
---|
73 | http://bugs.python.org/issue3937 |
---|
74 | """ |
---|
75 | global _distname,_version |
---|
76 | if _distname and _version: |
---|
77 | return (_distname, _version) |
---|
78 | |
---|
79 | try: |
---|
80 | etclsbrel = open("/etc/lsb-release", "rU") |
---|
81 | for line in etclsbrel: |
---|
82 | m = _distributor_id_file_re.search(line) |
---|
83 | if m: |
---|
84 | _distname = m.group(1).strip() |
---|
85 | if _distname and _version: |
---|
86 | return (_distname, _version) |
---|
87 | m = _release_file_re.search(line) |
---|
88 | if m: |
---|
89 | _version = m.group(1).strip() |
---|
90 | if _distname and _version: |
---|
91 | return (_distname, _version) |
---|
92 | except EnvironmentError: |
---|
93 | pass |
---|
94 | |
---|
95 | (_distname, _version) = platform.dist()[:2] |
---|
96 | if _distname and _version: |
---|
97 | return (_distname, _version) |
---|
98 | |
---|
99 | try: |
---|
100 | p = subprocess.Popen(["lsb_release", "--all"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
---|
101 | rc = p.wait() |
---|
102 | if rc == 0: |
---|
103 | for line in p.stdout.readlines(): |
---|
104 | m = _distributor_id_cmdline_re.search(line) |
---|
105 | if m: |
---|
106 | _distname = m.group(1).strip() |
---|
107 | if _distname and _version: |
---|
108 | return (_distname, _version) |
---|
109 | |
---|
110 | m = _release_cmdline_re.search(p.stdout.read()) |
---|
111 | if m: |
---|
112 | _version = m.group(1).strip() |
---|
113 | if _distname and _version: |
---|
114 | return (_distname, _version) |
---|
115 | except EnvironmentError: |
---|
116 | pass |
---|
117 | |
---|
118 | if os.path.exists("/etc/arch-release"): |
---|
119 | return ("Arch_Linux", "") |
---|
120 | |
---|
121 | return (_distname,_version) |
---|
122 | |
---|
123 | def get_platform(): |
---|
124 | # Our version of platform.platform(), telling us both less and more than the |
---|
125 | # Python Standard Library's version does. |
---|
126 | # We omit details such as the Linux kernel version number, but we add a |
---|
127 | # more detailed and correct rendition of the Linux distribution and |
---|
128 | # distribution-version. |
---|
129 | if "linux" in platform.system().lower(): |
---|
130 | return platform.system()+"-"+"_".join(get_linux_distro())+"-"+platform.machine()+"-"+"_".join([x for x in platform.architecture() if x]) |
---|
131 | else: |
---|
132 | return platform.platform() |
---|
133 | |
---|
134 | |
---|
135 | from allmydata.util import verlib |
---|
136 | def normalized_version(verstr, what=None): |
---|
137 | try: |
---|
138 | return verlib.NormalizedVersion(verlib.suggest_normalized_version(verstr)) |
---|
139 | except (StandardError, verlib.IrrationalVersionError): |
---|
140 | cls, value, trace = sys.exc_info() |
---|
141 | raise PackagingError, ("could not parse %s due to %s: %s" |
---|
142 | % (what or repr(verstr), cls.__name__, value)), trace |
---|
143 | |
---|
144 | |
---|
145 | def get_package_versions_and_locations(): |
---|
146 | import warnings |
---|
147 | from _auto_deps import package_imports, global_deprecation_messages, deprecation_messages, \ |
---|
148 | user_warning_messages, runtime_warning_messages, warning_imports |
---|
149 | |
---|
150 | def package_dir(srcfile): |
---|
151 | return os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(srcfile)))) |
---|
152 | |
---|
153 | # pkg_resources.require returns the distribution that pkg_resources attempted to put |
---|
154 | # on sys.path, which can differ from the one that we actually import due to #1258, |
---|
155 | # or any other bug that causes sys.path to be set up incorrectly. Therefore we |
---|
156 | # must import the packages in order to check their versions and paths. |
---|
157 | |
---|
158 | # This is to suppress various DeprecationWarnings, UserWarnings, and RuntimeWarnings |
---|
159 | # (listed in _auto_deps.py). |
---|
160 | |
---|
161 | for msg in global_deprecation_messages + deprecation_messages: |
---|
162 | warnings.filterwarnings("ignore", category=DeprecationWarning, message=msg, append=True) |
---|
163 | for msg in user_warning_messages: |
---|
164 | warnings.filterwarnings("ignore", category=UserWarning, message=msg, append=True) |
---|
165 | for msg in runtime_warning_messages: |
---|
166 | warnings.filterwarnings("ignore", category=RuntimeWarning, message=msg, append=True) |
---|
167 | try: |
---|
168 | for modulename in warning_imports: |
---|
169 | try: |
---|
170 | __import__(modulename) |
---|
171 | except ImportError: |
---|
172 | pass |
---|
173 | finally: |
---|
174 | # Leave suppressions for global_deprecation_messages active. |
---|
175 | for ign in runtime_warning_messages + user_warning_messages + deprecation_messages: |
---|
176 | warnings.filters.pop() |
---|
177 | |
---|
178 | packages = [] |
---|
179 | |
---|
180 | def get_version(module, attr): |
---|
181 | return str(getattr(module, attr, 'unknown')) |
---|
182 | |
---|
183 | for pkgname, modulename in [(__appname__, 'allmydata')] + package_imports: |
---|
184 | if modulename: |
---|
185 | try: |
---|
186 | __import__(modulename) |
---|
187 | module = sys.modules[modulename] |
---|
188 | except ImportError: |
---|
189 | etype, emsg, etrace = sys.exc_info() |
---|
190 | trace_info = (etype, str(emsg), ([None] + traceback.extract_tb(etrace))[-1]) |
---|
191 | packages.append( (pkgname, (None, None, trace_info)) ) |
---|
192 | else: |
---|
193 | comment = None |
---|
194 | if pkgname == 'setuptools' and hasattr(module, '_distribute'): |
---|
195 | # distribute does not report its version in any module variables |
---|
196 | comment = 'distribute' |
---|
197 | packages.append( (pkgname, (get_version(module, '__version__'), package_dir(module.__file__), comment)) ) |
---|
198 | elif pkgname == 'python': |
---|
199 | packages.append( (pkgname, (platform.python_version(), sys.executable, None)) ) |
---|
200 | elif pkgname == 'platform': |
---|
201 | packages.append( (pkgname, (get_platform(), None, None)) ) |
---|
202 | |
---|
203 | return packages |
---|
204 | |
---|
205 | |
---|
206 | def check_requirement(req, vers_and_locs): |
---|
207 | # TODO: check [] options |
---|
208 | # We support only disjunctions of <=, >=, and == |
---|
209 | |
---|
210 | reqlist = req.split(',') |
---|
211 | name = reqlist[0].split('<=')[0].split('>=')[0].split('==')[0].strip(' ').split('[')[0] |
---|
212 | if name not in vers_and_locs: |
---|
213 | raise PackagingError("no version info for %s" % (name,)) |
---|
214 | if req.strip(' ') == name: |
---|
215 | return |
---|
216 | (actual, location, comment) = vers_and_locs[name] |
---|
217 | if actual is None: |
---|
218 | # comment is (type, message, (filename, line number, function name, text)) for the original ImportError |
---|
219 | raise ImportError("for requirement %r: %s" % (req, comment)) |
---|
220 | if actual == 'unknown': |
---|
221 | return |
---|
222 | actualver = normalized_version(actual, what="actual version %r of %s from %r" % (actual, name, location)) |
---|
223 | |
---|
224 | for r in reqlist: |
---|
225 | s = r.split('<=') |
---|
226 | if len(s) == 2: |
---|
227 | required = s[1].strip(' ') |
---|
228 | if actualver <= normalized_version(required, what="required maximum version %r in %r" % (required, req)): |
---|
229 | return # maximum requirement met |
---|
230 | else: |
---|
231 | s = r.split('>=') |
---|
232 | if len(s) == 2: |
---|
233 | required = s[1].strip(' ') |
---|
234 | if actualver >= normalized_version(required, what="required minimum version %r in %r" % (required, req)): |
---|
235 | return # minimum requirement met |
---|
236 | else: |
---|
237 | s = r.split('==') |
---|
238 | if len(s) == 2: |
---|
239 | required = s[1].strip(' ') |
---|
240 | if actualver == normalized_version(required, what="required exact version %r in %r" % (required, req)): |
---|
241 | return # exact requirement met |
---|
242 | else: |
---|
243 | raise PackagingError("no version info or could not understand requirement %r" % (req,)) |
---|
244 | |
---|
245 | msg = ("We require %s, but could only find version %s.\n" % (req, actual)) |
---|
246 | if location and location != 'unknown': |
---|
247 | msg += "The version we found is from %r.\n" % (location,) |
---|
248 | msg += ("To resolve this problem, uninstall that version, either using your\n" |
---|
249 | "operating system's package manager or by moving aside the directory.") |
---|
250 | raise PackagingError(msg) |
---|
251 | |
---|
252 | |
---|
253 | _vers_and_locs_list = get_package_versions_and_locations() |
---|
254 | |
---|
255 | |
---|
256 | def cross_check_pkg_resources_versus_import(): |
---|
257 | """This function returns a list of errors due to any failed cross-checks.""" |
---|
258 | |
---|
259 | import pkg_resources |
---|
260 | from _auto_deps import install_requires |
---|
261 | |
---|
262 | pkg_resources_vers_and_locs = dict([(p.project_name.lower(), (str(p.version), p.location)) |
---|
263 | for p in pkg_resources.require(install_requires)]) |
---|
264 | |
---|
265 | return cross_check(pkg_resources_vers_and_locs, _vers_and_locs_list) |
---|
266 | |
---|
267 | |
---|
268 | def cross_check(pkg_resources_vers_and_locs, imported_vers_and_locs_list): |
---|
269 | """This function returns a list of errors due to any failed cross-checks.""" |
---|
270 | |
---|
271 | errors = [] |
---|
272 | not_pkg_resourceable = set(['python', 'platform', __appname__.lower()]) |
---|
273 | not_import_versionable = set(['zope.interface', 'mock', 'pyasn1']) |
---|
274 | ignorable = set(['argparse', 'pyutil', 'zbase32', 'distribute', 'twisted-web', 'twisted-core', 'twisted-conch']) |
---|
275 | |
---|
276 | for name, (imp_ver, imp_loc, imp_comment) in imported_vers_and_locs_list: |
---|
277 | name = name.lower() |
---|
278 | if name not in not_pkg_resourceable: |
---|
279 | if name not in pkg_resources_vers_and_locs: |
---|
280 | if name == "setuptools" and "distribute" in pkg_resources_vers_and_locs: |
---|
281 | pr_ver, pr_loc = pkg_resources_vers_and_locs["distribute"] |
---|
282 | if not (os.path.normpath(os.path.realpath(pr_loc)) == os.path.normpath(os.path.realpath(imp_loc)) |
---|
283 | and imp_comment == "distribute"): |
---|
284 | errors.append("Warning: dependency 'setuptools' found to be version %r of 'distribute' from %r " |
---|
285 | "by pkg_resources, but 'import setuptools' gave version %r [%s] from %r. " |
---|
286 | "A version mismatch is expected, but a location mismatch is not." |
---|
287 | % (pr_ver, pr_loc, imp_ver, imp_comment or 'probably *not* distribute', imp_loc)) |
---|
288 | else: |
---|
289 | errors.append("Warning: dependency %r (version %r imported from %r) was not found by pkg_resources." |
---|
290 | % (name, imp_ver, imp_loc)) |
---|
291 | continue |
---|
292 | |
---|
293 | pr_ver, pr_loc = pkg_resources_vers_and_locs[name] |
---|
294 | if imp_ver is None and imp_loc is None: |
---|
295 | errors.append("Warning: dependency %r could not be imported. pkg_resources thought it should be possible " |
---|
296 | "to import version %r from %r.\nThe exception trace was %r." |
---|
297 | % (name, pr_ver, pr_loc, imp_comment)) |
---|
298 | continue |
---|
299 | |
---|
300 | try: |
---|
301 | pr_normver = normalized_version(pr_ver) |
---|
302 | except Exception, e: |
---|
303 | errors.append("Warning: version number %r found for dependency %r by pkg_resources could not be parsed. " |
---|
304 | "The version found by import was %r from %r. " |
---|
305 | "pkg_resources thought it should be found at %r. " |
---|
306 | "The exception was %s: %s" |
---|
307 | % (pr_ver, name, imp_ver, imp_loc, pr_loc, e.__class__.__name__, e)) |
---|
308 | else: |
---|
309 | if imp_ver == 'unknown': |
---|
310 | if name not in not_import_versionable: |
---|
311 | errors.append("Warning: unexpectedly could not find a version number for dependency %r imported from %r. " |
---|
312 | "pkg_resources thought it should be version %r at %r." |
---|
313 | % (name, imp_loc, pr_ver, pr_loc)) |
---|
314 | else: |
---|
315 | try: |
---|
316 | imp_normver = normalized_version(imp_ver) |
---|
317 | except Exception, e: |
---|
318 | errors.append("Warning: version number %r found for dependency %r (imported from %r) could not be parsed. " |
---|
319 | "pkg_resources thought it should be version %r at %r. " |
---|
320 | "The exception was %s: %s" |
---|
321 | % (imp_ver, name, imp_loc, pr_ver, pr_loc, e.__class__.__name__, e)) |
---|
322 | else: |
---|
323 | if pr_ver == 'unknown' or (pr_normver != imp_normver): |
---|
324 | if not os.path.normpath(os.path.realpath(pr_loc)) == os.path.normpath(os.path.realpath(imp_loc)): |
---|
325 | errors.append("Warning: dependency %r found to have version number %r (normalized to %r, from %r) " |
---|
326 | "by pkg_resources, but version %r (normalized to %r, from %r) by import." |
---|
327 | % (name, pr_ver, str(pr_normver), pr_loc, imp_ver, str(imp_normver), imp_loc)) |
---|
328 | |
---|
329 | imported_packages = set([p.lower() for (p, _) in imported_vers_and_locs_list]) |
---|
330 | for pr_name, (pr_ver, pr_loc) in pkg_resources_vers_and_locs.iteritems(): |
---|
331 | if pr_name not in imported_packages and pr_name not in ignorable: |
---|
332 | errors.append("Warning: dependency %r (version %r) found by pkg_resources not found by import." |
---|
333 | % (pr_name, pr_ver)) |
---|
334 | |
---|
335 | return errors |
---|
336 | |
---|
337 | |
---|
338 | def get_error_string(errors, debug=False): |
---|
339 | from allmydata._auto_deps import install_requires |
---|
340 | |
---|
341 | msg = "\n%s\n" % ("\n".join(errors),) |
---|
342 | if debug: |
---|
343 | msg += ("\n" |
---|
344 | "For debugging purposes, the PYTHONPATH was\n" |
---|
345 | " %r\n" |
---|
346 | "install_requires was\n" |
---|
347 | " %r\n" |
---|
348 | "sys.path after importing pkg_resources was\n" |
---|
349 | " %s\n" |
---|
350 | % (os.environ.get('PYTHONPATH'), install_requires, (os.pathsep+"\n ").join(sys.path)) ) |
---|
351 | return msg |
---|
352 | |
---|
353 | def check_all_requirements(): |
---|
354 | """This function returns a list of errors due to any failed checks.""" |
---|
355 | |
---|
356 | from allmydata._auto_deps import install_requires |
---|
357 | |
---|
358 | errors = [] |
---|
359 | |
---|
360 | # We require at least 2.6 on all platforms. |
---|
361 | # (On Python 3, we'll have failed long before this point.) |
---|
362 | if sys.version_info < (2, 6): |
---|
363 | try: |
---|
364 | version_string = ".".join(map(str, sys.version_info)) |
---|
365 | except Exception: |
---|
366 | version_string = repr(sys.version_info) |
---|
367 | errors.append("Tahoe-LAFS currently requires Python v2.6 or greater (but less than v3), not %s" |
---|
368 | % (version_string,)) |
---|
369 | |
---|
370 | vers_and_locs = dict(_vers_and_locs_list) |
---|
371 | for requirement in install_requires: |
---|
372 | try: |
---|
373 | check_requirement(requirement, vers_and_locs) |
---|
374 | except (ImportError, PackagingError), e: |
---|
375 | errors.append("%s: %s" % (e.__class__.__name__, e)) |
---|
376 | |
---|
377 | if errors: |
---|
378 | raise PackagingError(get_error_string(errors, debug=True)) |
---|
379 | |
---|
380 | check_all_requirements() |
---|
381 | |
---|
382 | |
---|
383 | def get_package_versions(): |
---|
384 | return dict([(k, v) for k, (v, l, c) in _vers_and_locs_list]) |
---|
385 | |
---|
386 | def get_package_locations(): |
---|
387 | return dict([(k, l) for k, (v, l, c) in _vers_and_locs_list]) |
---|
388 | |
---|
389 | def get_package_versions_string(show_paths=False, debug=False): |
---|
390 | res = [] |
---|
391 | for p, (v, loc, comment) in _vers_and_locs_list: |
---|
392 | info = str(p) + ": " + str(v) |
---|
393 | if comment: |
---|
394 | info = info + " [%s]" % str(comment) |
---|
395 | if show_paths: |
---|
396 | info = info + " (%s)" % str(loc) |
---|
397 | res.append(info) |
---|
398 | |
---|
399 | output = "\n".join(res) + "\n" |
---|
400 | |
---|
401 | if not hasattr(sys, 'frozen'): |
---|
402 | errors = cross_check_pkg_resources_versus_import() |
---|
403 | if errors: |
---|
404 | output += get_error_string(errors, debug=debug) |
---|
405 | |
---|
406 | return output |
---|