2 Decentralized storage grid.
4 community web site: U{https://tahoe-lafs.org/}
7 class PackagingError(EnvironmentError):
9 Raised when there is an error in packaging of Tahoe-LAFS or its
10 dependencies which makes it impossible to proceed safely.
14 __version__ = "unknown"
16 from allmydata._version import __version__
18 # We're running in a tree that hasn't run update_version, and didn't
19 # come with a _version.py, so we don't know what our version is.
20 # This should not happen very often.
23 full_version = "unknown"
26 from allmydata._version import full_version, branch
28 # We're running in a tree that hasn't run update_version, and didn't
29 # come with a _version.py, so we don't know what our full version or
30 # branch is. This should not happen very often.
33 __appname__ = "unknown"
35 from allmydata._appname import __appname__
37 # We're running in a tree that hasn't run "./setup.py". This shouldn't happen.
40 # __full_version__ is the one that you ought to use when identifying yourself in the
41 # "application" part of the Tahoe versioning scheme:
42 # https://tahoe-lafs.org/trac/tahoe-lafs/wiki/Versioning
43 __full_version__ = __appname__ + '/' + str(__version__)
45 import os, platform, re, subprocess, sys, traceback
46 _distributor_id_cmdline_re = re.compile("(?:Distributor ID:)\s*(.*)", re.I)
47 _release_cmdline_re = re.compile("(?:Release:)\s*(.*)", re.I)
49 _distributor_id_file_re = re.compile("(?:DISTRIB_ID\s*=)\s*(.*)", re.I)
50 _release_file_re = re.compile("(?:DISTRIB_RELEASE\s*=)\s*(.*)", re.I)
52 global _distname,_version
56 def get_linux_distro():
57 """ Tries to determine the name of the Linux OS distribution name.
59 First, try to parse a file named "/etc/lsb-release". If it exists, and
60 contains the "DISTRIB_ID=" line and the "DISTRIB_RELEASE=" line, then return
61 the strings parsed from that file.
63 If that doesn't work, then invoke platform.dist().
65 If that doesn't work, then try to execute "lsb_release", as standardized in
68 http://refspecs.freestandards.org/LSB_1.0.0/gLSB/lsbrelease.html
70 The current version of the standard is here:
72 http://refspecs.freestandards.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/lsbrelease.html
74 that lsb_release emitted, as strings.
76 Returns a tuple (distname,version). Distname is what LSB calls a
77 "distributor id", e.g. "Ubuntu". Version is what LSB calls a "release",
80 A version of this has been submitted to python as a patch for the standard
81 library module "platform":
83 http://bugs.python.org/issue3937
85 global _distname,_version
86 if _distname and _version:
87 return (_distname, _version)
90 etclsbrel = open("/etc/lsb-release", "rU")
91 for line in etclsbrel:
92 m = _distributor_id_file_re.search(line)
94 _distname = m.group(1).strip()
95 if _distname and _version:
96 return (_distname, _version)
97 m = _release_file_re.search(line)
99 _version = m.group(1).strip()
100 if _distname and _version:
101 return (_distname, _version)
102 except EnvironmentError:
105 (_distname, _version) = platform.dist()[:2]
106 if _distname and _version:
107 return (_distname, _version)
109 if os.path.isfile("/usr/bin/lsb_release") or os.path.isfile("/bin/lsb_release"):
111 p = subprocess.Popen(["lsb_release", "--all"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
114 for line in p.stdout.readlines():
115 m = _distributor_id_cmdline_re.search(line)
117 _distname = m.group(1).strip()
118 if _distname and _version:
119 return (_distname, _version)
121 m = _release_cmdline_re.search(p.stdout.read())
123 _version = m.group(1).strip()
124 if _distname and _version:
125 return (_distname, _version)
126 except EnvironmentError:
129 if os.path.exists("/etc/arch-release"):
130 return ("Arch_Linux", "")
132 return (_distname,_version)
135 # Our version of platform.platform(), telling us both less and more than the
136 # Python Standard Library's version does.
137 # We omit details such as the Linux kernel version number, but we add a
138 # more detailed and correct rendition of the Linux distribution and
139 # distribution-version.
140 if "linux" in platform.system().lower():
141 return platform.system()+"-"+"_".join(get_linux_distro())+"-"+platform.machine()+"-"+"_".join([x for x in platform.architecture() if x])
143 return platform.platform()
146 from allmydata.util import verlib
147 def normalized_version(verstr, what=None):
149 suggested = verlib.suggest_normalized_version(verstr) or verstr
150 return verlib.NormalizedVersion(suggested)
151 except (StandardError, verlib.IrrationalVersionError):
152 cls, value, trace = sys.exc_info()
153 raise PackagingError, ("could not parse %s due to %s: %s"
154 % (what or repr(verstr), cls.__name__, value)), trace
156 def get_openssl_version():
158 from OpenSSL import SSL
159 return extract_openssl_version(SSL)
161 return ("unknown", None, None)
163 def extract_openssl_version(ssl_module):
164 openssl_version = ssl_module.SSLeay_version(ssl_module.SSLEAY_VERSION)
165 if openssl_version.startswith('OpenSSL '):
166 openssl_version = openssl_version[8 :]
168 (version, _, comment) = openssl_version.partition(' ')
171 openssl_cflags = ssl_module.SSLeay_version(ssl_module.SSLEAY_CFLAGS)
172 if '-DOPENSSL_NO_HEARTBEATS' in openssl_cflags.split(' '):
173 comment += ", no heartbeats"
177 return (version, None, comment if comment else None)
179 def get_package_versions_and_locations():
181 from _auto_deps import package_imports, global_deprecation_messages, deprecation_messages, \
182 runtime_warning_messages, warning_imports, ignorable
184 def package_dir(srcfile):
185 return os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(srcfile))))
187 # pkg_resources.require returns the distribution that pkg_resources attempted to put
188 # on sys.path, which can differ from the one that we actually import due to #1258,
189 # or any other bug that causes sys.path to be set up incorrectly. Therefore we
190 # must import the packages in order to check their versions and paths.
192 # This is to suppress all UserWarnings and various DeprecationWarnings and RuntimeWarnings
193 # (listed in _auto_deps.py).
195 warnings.filterwarnings("ignore", category=UserWarning, append=True)
197 for msg in global_deprecation_messages + deprecation_messages:
198 warnings.filterwarnings("ignore", category=DeprecationWarning, message=msg, append=True)
199 for msg in runtime_warning_messages:
200 warnings.filterwarnings("ignore", category=RuntimeWarning, message=msg, append=True)
202 for modulename in warning_imports:
204 __import__(modulename)
208 # Leave suppressions for UserWarnings and global_deprecation_messages active.
209 for ign in runtime_warning_messages + deprecation_messages:
210 warnings.filters.pop()
214 def get_version(module):
215 if hasattr(module, '__version__'):
216 return str(getattr(module, '__version__'))
217 elif hasattr(module, 'version'):
218 ver = getattr(module, 'version')
219 if isinstance(ver, tuple):
220 return '.'.join(map(str, ver))
226 for pkgname, modulename in [(__appname__, 'allmydata')] + package_imports:
229 __import__(modulename)
230 module = sys.modules[modulename]
232 etype, emsg, etrace = sys.exc_info()
233 trace_info = (etype, str(emsg), ([None] + traceback.extract_tb(etrace))[-1])
234 packages.append( (pkgname, (None, None, trace_info)) )
237 if pkgname == __appname__:
238 comment = "%s: %s" % (branch, full_version)
239 elif pkgname == 'setuptools' and hasattr(module, '_distribute'):
240 # distribute does not report its version in any module variables
241 comment = 'distribute'
242 packages.append( (pkgname, (get_version(module), package_dir(module.__file__), comment)) )
243 elif pkgname == 'python':
244 packages.append( (pkgname, (platform.python_version(), sys.executable, None)) )
245 elif pkgname == 'platform':
246 packages.append( (pkgname, (get_platform(), None, None)) )
247 elif pkgname == 'OpenSSL':
248 packages.append( (pkgname, get_openssl_version()) )
250 cross_check_errors = []
252 if not hasattr(sys, 'frozen'):
254 from _auto_deps import install_requires
256 pkg_resources_vers_and_locs = dict([(p.project_name.lower(), (str(p.version), p.location))
257 for p in pkg_resources.require(install_requires)])
259 imported_packages = set([p.lower() for (p, _) in packages])
262 for pr_name, (pr_ver, pr_loc) in pkg_resources_vers_and_locs.iteritems():
263 if pr_name not in imported_packages and pr_name not in ignorable:
264 extra_packages.append( (pr_name, (pr_ver, pr_loc, "according to pkg_resources")) )
266 cross_check_errors = cross_check(pkg_resources_vers_and_locs, packages)
267 packages += extra_packages
269 return packages, cross_check_errors
272 def check_requirement(req, vers_and_locs):
273 # We support only conjunctions of <=, >=, and !=
275 reqlist = req.split(',')
276 name = reqlist[0].split('<=')[0].split('>=')[0].split('!=')[0].strip(' ').split('[')[0]
277 if name not in vers_and_locs:
278 raise PackagingError("no version info for %s" % (name,))
279 if req.strip(' ') == name:
281 (actual, location, comment) = vers_and_locs[name]
283 # comment is (type, message, (filename, line number, function name, text)) for the original ImportError
284 raise ImportError("for requirement %r: %s" % (req, comment))
285 if actual == 'unknown':
287 actualver = normalized_version(actual, what="actual version %r of %s from %r" % (actual, name, location))
289 if not match_requirement(req, reqlist, actualver):
290 msg = ("We require %s, but could only find version %s.\n" % (req, actual))
291 if location and location != 'unknown':
292 msg += "The version we found is from %r.\n" % (location,)
293 msg += ("To resolve this problem, uninstall that version, either using your\n"
294 "operating system's package manager or by moving aside the directory.")
295 raise PackagingError(msg)
298 def match_requirement(req, reqlist, actualver):
302 required = s[1].strip(' ')
303 if not (actualver <= normalized_version(required, what="required maximum version %r in %r" % (required, req))):
304 return False # maximum requirement not met
308 required = s[1].strip(' ')
309 if not (actualver >= normalized_version(required, what="required minimum version %r in %r" % (required, req))):
310 return False # minimum requirement not met
314 required = s[1].strip(' ')
315 if not (actualver != normalized_version(required, what="excluded version %r in %r" % (required, req))):
316 return False # not-equal requirement not met
318 raise PackagingError("no version info or could not understand requirement %r" % (req,))
323 def cross_check(pkg_resources_vers_and_locs, imported_vers_and_locs_list):
324 """This function returns a list of errors due to any failed cross-checks."""
326 from _auto_deps import not_import_versionable
329 not_pkg_resourceable = ['python', 'platform', __appname__.lower(), 'openssl']
331 for name, (imp_ver, imp_loc, imp_comment) in imported_vers_and_locs_list:
333 if name not in not_pkg_resourceable:
334 if name not in pkg_resources_vers_and_locs:
335 if name == "setuptools" and "distribute" in pkg_resources_vers_and_locs:
336 pr_ver, pr_loc = pkg_resources_vers_and_locs["distribute"]
337 if not (os.path.normpath(os.path.realpath(pr_loc)) == os.path.normpath(os.path.realpath(imp_loc))
338 and imp_comment == "distribute"):
339 errors.append("Warning: dependency 'setuptools' found to be version %r of 'distribute' from %r "
340 "by pkg_resources, but 'import setuptools' gave version %r [%s] from %r. "
341 "A version mismatch is expected, but a location mismatch is not."
342 % (pr_ver, pr_loc, imp_ver, imp_comment or 'probably *not* distribute', imp_loc))
344 errors.append("Warning: dependency %r (version %r imported from %r) was not found by pkg_resources."
345 % (name, imp_ver, imp_loc))
348 pr_ver, pr_loc = pkg_resources_vers_and_locs[name]
349 if imp_ver is None and imp_loc is None:
350 errors.append("Warning: dependency %r could not be imported. pkg_resources thought it should be possible "
351 "to import version %r from %r.\nThe exception trace was %r."
352 % (name, pr_ver, pr_loc, imp_comment))
355 # If the pkg_resources version is identical to the imported version, don't attempt
356 # to normalize them, since it is unnecessary and may fail (ticket #2499).
357 if imp_ver != 'unknown' and pr_ver == imp_ver:
361 pr_normver = normalized_version(pr_ver)
363 errors.append("Warning: version number %r found for dependency %r by pkg_resources could not be parsed. "
364 "The version found by import was %r from %r. "
365 "pkg_resources thought it should be found at %r. "
366 "The exception was %s: %s"
367 % (pr_ver, name, imp_ver, imp_loc, pr_loc, e.__class__.__name__, e))
369 if imp_ver == 'unknown':
370 if name not in not_import_versionable:
371 errors.append("Warning: unexpectedly could not find a version number for dependency %r imported from %r. "
372 "pkg_resources thought it should be version %r at %r."
373 % (name, imp_loc, pr_ver, pr_loc))
376 imp_normver = normalized_version(imp_ver)
378 errors.append("Warning: version number %r found for dependency %r (imported from %r) could not be parsed. "
379 "pkg_resources thought it should be version %r at %r. "
380 "The exception was %s: %s"
381 % (imp_ver, name, imp_loc, pr_ver, pr_loc, e.__class__.__name__, e))
383 if pr_ver == 'unknown' or (pr_normver != imp_normver):
384 if not os.path.normpath(os.path.realpath(pr_loc)) == os.path.normpath(os.path.realpath(imp_loc)):
385 errors.append("Warning: dependency %r found to have version number %r (normalized to %r, from %r) "
386 "by pkg_resources, but version %r (normalized to %r, from %r) by import."
387 % (name, pr_ver, str(pr_normver), pr_loc, imp_ver, str(imp_normver), imp_loc))
392 _vers_and_locs_list, _cross_check_errors = get_package_versions_and_locations()
395 def get_error_string(errors, debug=False):
396 from allmydata._auto_deps import install_requires
398 msg = "\n%s\n" % ("\n".join(errors),)
401 "For debugging purposes, the PYTHONPATH was\n"
403 "install_requires was\n"
405 "sys.path after importing pkg_resources was\n"
407 % (os.environ.get('PYTHONPATH'), install_requires, (os.pathsep+"\n ").join(sys.path)) )
410 def check_all_requirements():
411 """This function returns a list of errors due to any failed checks."""
413 from allmydata._auto_deps import install_requires
417 # We require at least 2.6 on all platforms.
418 # (On Python 3, we'll have failed long before this point.)
419 if sys.version_info < (2, 6):
421 version_string = ".".join(map(str, sys.version_info))
423 version_string = repr(sys.version_info)
424 fatal_errors.append("Tahoe-LAFS currently requires Python v2.6 or greater (but less than v3), not %s"
427 vers_and_locs = dict(_vers_and_locs_list)
428 for requirement in install_requires:
430 check_requirement(requirement, vers_and_locs)
431 except (ImportError, PackagingError), e:
432 fatal_errors.append("%s: %s" % (e.__class__.__name__, e))
435 raise PackagingError(get_error_string(fatal_errors + _cross_check_errors, debug=True))
437 check_all_requirements()
440 def get_package_versions():
441 return dict([(k, v) for k, (v, l, c) in _vers_and_locs_list])
443 def get_package_locations():
444 return dict([(k, l) for k, (v, l, c) in _vers_and_locs_list])
446 def get_package_versions_string(show_paths=False, debug=False):
448 for p, (v, loc, comment) in _vers_and_locs_list:
449 info = str(p) + ": " + str(v)
451 info = info + " [%s]" % str(comment)
453 info = info + " (%s)" % str(loc)
456 output = "\n".join(res) + "\n"
458 if _cross_check_errors:
459 output += get_error_string(_cross_check_errors, debug=debug)