]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/__init__.py
0b0c5f816cc392920822cc1c496bf74cd15b812c
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / __init__.py
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 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.
21     pass
22
23 full_version = "unknown"
24 branch = "unknown"
25 try:
26     from allmydata._version import full_version, branch
27 except ImportError:
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.
31     pass
32
33 __appname__ = "unknown"
34 try:
35     from allmydata._appname import __appname__
36 except ImportError:
37     # We're running in a tree that hasn't run "./setup.py".  This shouldn't happen.
38     pass
39
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__)
44
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)
48
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)
51
52 global _distname,_version
53 _distname = None
54 _version = None
55
56 def get_linux_distro():
57     """ Tries to determine the name of the Linux OS distribution name.
58
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.
62
63     If that doesn't work, then invoke platform.dist().
64
65     If that doesn't work, then try to execute "lsb_release", as standardized in
66     2001:
67
68     http://refspecs.freestandards.org/LSB_1.0.0/gLSB/lsbrelease.html
69
70     The current version of the standard is here:
71
72     http://refspecs.freestandards.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/lsbrelease.html
73
74     that lsb_release emitted, as strings.
75
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",
78     e.g. "8.04".
79
80     A version of this has been submitted to python as a patch for the standard
81     library module "platform":
82
83     http://bugs.python.org/issue3937
84     """
85     global _distname,_version
86     if _distname and _version:
87         return (_distname, _version)
88
89     try:
90         etclsbrel = open("/etc/lsb-release", "rU")
91         for line in etclsbrel:
92             m = _distributor_id_file_re.search(line)
93             if m:
94                 _distname = m.group(1).strip()
95                 if _distname and _version:
96                     return (_distname, _version)
97             m = _release_file_re.search(line)
98             if m:
99                 _version = m.group(1).strip()
100                 if _distname and _version:
101                     return (_distname, _version)
102     except EnvironmentError:
103         pass
104
105     (_distname, _version) = platform.dist()[:2]
106     if _distname and _version:
107         return (_distname, _version)
108
109     if os.path.isfile("/usr/bin/lsb_release") or os.path.isfile("/bin/lsb_release"):
110         try:
111             p = subprocess.Popen(["lsb_release", "--all"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
112             rc = p.wait()
113             if rc == 0:
114                 for line in p.stdout.readlines():
115                     m = _distributor_id_cmdline_re.search(line)
116                     if m:
117                         _distname = m.group(1).strip()
118                         if _distname and _version:
119                             return (_distname, _version)
120
121                     m = _release_cmdline_re.search(p.stdout.read())
122                     if m:
123                         _version = m.group(1).strip()
124                         if _distname and _version:
125                             return (_distname, _version)
126         except EnvironmentError:
127             pass
128
129     if os.path.exists("/etc/arch-release"):
130         return ("Arch_Linux", "")
131
132     return (_distname,_version)
133
134 def get_platform():
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])
142     else:
143         return platform.platform()
144
145
146 from allmydata.util import verlib
147 def normalized_version(verstr, what=None):
148     try:
149         return verlib.NormalizedVersion(verlib.suggest_normalized_version(verstr))
150     except (StandardError, verlib.IrrationalVersionError):
151         cls, value, trace = sys.exc_info()
152         raise PackagingError, ("could not parse %s due to %s: %s"
153                                % (what or repr(verstr), cls.__name__, value)), trace
154
155 def get_openssl_version():
156     try:
157         from OpenSSL import SSL
158         return extract_openssl_version(SSL)
159     except Exception:
160         return ("unknown", None, None)
161
162 def extract_openssl_version(ssl_module):
163     openssl_version = ssl_module.SSLeay_version(ssl_module.SSLEAY_VERSION)
164     if openssl_version.startswith('OpenSSL '):
165         openssl_version = openssl_version[8 :]
166
167     (version, _, comment) = openssl_version.partition(' ')
168
169     try:
170         openssl_cflags = ssl_module.SSLeay_version(ssl_module.SSLEAY_CFLAGS)
171         if '-DOPENSSL_NO_HEARTBEATS' in openssl_cflags.split(' '):
172             comment += ", no heartbeats"
173     except Exception:
174         pass
175
176     return (version, None, comment if comment else None)
177
178 def get_package_versions_and_locations():
179     import warnings
180     from _auto_deps import package_imports, global_deprecation_messages, deprecation_messages, \
181         runtime_warning_messages, warning_imports, ignorable
182
183     def package_dir(srcfile):
184         return os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(srcfile))))
185
186     # pkg_resources.require returns the distribution that pkg_resources attempted to put
187     # on sys.path, which can differ from the one that we actually import due to #1258,
188     # or any other bug that causes sys.path to be set up incorrectly. Therefore we
189     # must import the packages in order to check their versions and paths.
190
191     # This is to suppress all UserWarnings and various DeprecationWarnings and RuntimeWarnings
192     # (listed in _auto_deps.py).
193
194     warnings.filterwarnings("ignore", category=UserWarning, append=True)
195
196     for msg in global_deprecation_messages + deprecation_messages:
197         warnings.filterwarnings("ignore", category=DeprecationWarning, message=msg, append=True)
198     for msg in runtime_warning_messages:
199         warnings.filterwarnings("ignore", category=RuntimeWarning, message=msg, append=True)
200     try:
201         for modulename in warning_imports:
202             try:
203                 __import__(modulename)
204             except ImportError:
205                 pass
206     finally:
207         # Leave suppressions for UserWarnings and global_deprecation_messages active.
208         for ign in runtime_warning_messages + deprecation_messages:
209             warnings.filters.pop()
210
211     packages = []
212
213     def get_version(module):
214         if hasattr(module, '__version__'):
215             return str(getattr(module, '__version__'))
216         elif hasattr(module, 'version'):
217             ver = getattr(module, 'version')
218             if isinstance(ver, tuple):
219                 return '.'.join(map(str, ver))
220             else:
221                 return str(ver)
222         else:
223             return 'unknown'
224
225     for pkgname, modulename in [(__appname__, 'allmydata')] + package_imports:
226         if modulename:
227             try:
228                 __import__(modulename)
229                 module = sys.modules[modulename]
230             except ImportError:
231                 etype, emsg, etrace = sys.exc_info()
232                 trace_info = (etype, str(emsg), ([None] + traceback.extract_tb(etrace))[-1])
233                 packages.append( (pkgname, (None, None, trace_info)) )
234             else:
235                 comment = None
236                 if pkgname == __appname__:
237                     comment = "%s: %s" % (branch, full_version)
238                 elif pkgname == 'setuptools' and hasattr(module, '_distribute'):
239                     # distribute does not report its version in any module variables
240                     comment = 'distribute'
241                 packages.append( (pkgname, (get_version(module), package_dir(module.__file__), comment)) )
242         elif pkgname == 'python':
243             packages.append( (pkgname, (platform.python_version(), sys.executable, None)) )
244         elif pkgname == 'platform':
245             packages.append( (pkgname, (get_platform(), None, None)) )
246         elif pkgname == 'OpenSSL':
247             packages.append( (pkgname, get_openssl_version()) )
248
249     cross_check_errors = []
250
251     if not hasattr(sys, 'frozen'):
252         import pkg_resources
253         from _auto_deps import install_requires
254
255         pkg_resources_vers_and_locs = dict([(p.project_name.lower(), (str(p.version), p.location))
256                                             for p in pkg_resources.require(install_requires)])
257
258         imported_packages = set([p.lower() for (p, _) in packages])
259         extra_packages = []
260
261         for pr_name, (pr_ver, pr_loc) in pkg_resources_vers_and_locs.iteritems():
262             if pr_name not in imported_packages and pr_name not in ignorable:
263                 extra_packages.append( (pr_name, (pr_ver, pr_loc, "according to pkg_resources")) )
264
265         cross_check_errors = cross_check(pkg_resources_vers_and_locs, packages)
266         packages += extra_packages
267
268     return packages, cross_check_errors
269
270
271 def check_requirement(req, vers_and_locs):
272     # We support only conjunctions of <=, >=, and !=
273
274     reqlist = req.split(',')
275     name = reqlist[0].split('<=')[0].split('>=')[0].split('!=')[0].strip(' ').split('[')[0]
276     if name not in vers_and_locs:
277         raise PackagingError("no version info for %s" % (name,))
278     if req.strip(' ') == name:
279         return
280     (actual, location, comment) = vers_and_locs[name]
281     if actual is None:
282         # comment is (type, message, (filename, line number, function name, text)) for the original ImportError
283         raise ImportError("for requirement %r: %s" % (req, comment))
284     if actual == 'unknown':
285         return
286     actualver = normalized_version(actual, what="actual version %r of %s from %r" % (actual, name, location))
287
288     if not match_requirement(req, reqlist, actualver):
289         msg = ("We require %s, but could only find version %s.\n" % (req, actual))
290         if location and location != 'unknown':
291             msg += "The version we found is from %r.\n" % (location,)
292         msg += ("To resolve this problem, uninstall that version, either using your\n"
293                 "operating system's package manager or by moving aside the directory.")
294         raise PackagingError(msg)
295
296
297 def match_requirement(req, reqlist, actualver):
298     for r in reqlist:
299         s = r.split('<=')
300         if len(s) == 2:
301             required = s[1].strip(' ')
302             if not (actualver <= normalized_version(required, what="required maximum version %r in %r" % (required, req))):
303                 return False  # maximum requirement not met
304         else:
305             s = r.split('>=')
306             if len(s) == 2:
307                 required = s[1].strip(' ')
308                 if not (actualver >= normalized_version(required, what="required minimum version %r in %r" % (required, req))):
309                     return False  # minimum requirement not met
310             else:
311                 s = r.split('!=')
312                 if len(s) == 2:
313                     required = s[1].strip(' ')
314                     if not (actualver != normalized_version(required, what="excluded version %r in %r" % (required, req))):
315                         return False  # not-equal requirement not met
316                 else:
317                     raise PackagingError("no version info or could not understand requirement %r" % (req,))
318
319     return True
320
321
322 def cross_check(pkg_resources_vers_and_locs, imported_vers_and_locs_list):
323     """This function returns a list of errors due to any failed cross-checks."""
324
325     from _auto_deps import not_import_versionable
326
327     errors = []
328     not_pkg_resourceable = ['python', 'platform', __appname__.lower(), 'openssl']
329
330     for name, (imp_ver, imp_loc, imp_comment) in imported_vers_and_locs_list:
331         name = name.lower()
332         if name not in not_pkg_resourceable:
333             if name not in pkg_resources_vers_and_locs:
334                 if name == "setuptools" and "distribute" in pkg_resources_vers_and_locs:
335                     pr_ver, pr_loc = pkg_resources_vers_and_locs["distribute"]
336                     if not (os.path.normpath(os.path.realpath(pr_loc)) == os.path.normpath(os.path.realpath(imp_loc))
337                             and imp_comment == "distribute"):
338                         errors.append("Warning: dependency 'setuptools' found to be version %r of 'distribute' from %r "
339                                       "by pkg_resources, but 'import setuptools' gave version %r [%s] from %r. "
340                                       "A version mismatch is expected, but a location mismatch is not."
341                                       % (pr_ver, pr_loc, imp_ver, imp_comment or 'probably *not* distribute', imp_loc))
342                 else:
343                     errors.append("Warning: dependency %r (version %r imported from %r) was not found by pkg_resources."
344                                   % (name, imp_ver, imp_loc))
345                 continue
346
347             pr_ver, pr_loc = pkg_resources_vers_and_locs[name]
348             if imp_ver is None and imp_loc is None:
349                 errors.append("Warning: dependency %r could not be imported. pkg_resources thought it should be possible "
350                               "to import version %r from %r.\nThe exception trace was %r."
351                               % (name, pr_ver, pr_loc, imp_comment))
352                 continue
353
354             try:
355                 pr_normver = normalized_version(pr_ver)
356             except Exception, e:
357                 errors.append("Warning: version number %r found for dependency %r by pkg_resources could not be parsed. "
358                               "The version found by import was %r from %r. "
359                               "pkg_resources thought it should be found at %r. "
360                               "The exception was %s: %s"
361                               % (pr_ver, name, imp_ver, imp_loc, pr_loc, e.__class__.__name__, e))
362             else:
363                 if imp_ver == 'unknown':
364                     if name not in not_import_versionable:
365                         errors.append("Warning: unexpectedly could not find a version number for dependency %r imported from %r. "
366                                       "pkg_resources thought it should be version %r at %r."
367                                       % (name, imp_loc, pr_ver, pr_loc))
368                 else:
369                     try:
370                         imp_normver = normalized_version(imp_ver)
371                     except Exception, e:
372                         errors.append("Warning: version number %r found for dependency %r (imported from %r) could not be parsed. "
373                                       "pkg_resources thought it should be version %r at %r. "
374                                       "The exception was %s: %s"
375                                       % (imp_ver, name, imp_loc, pr_ver, pr_loc, e.__class__.__name__, e))
376                     else:
377                         if pr_ver == 'unknown' or (pr_normver != imp_normver):
378                             if not os.path.normpath(os.path.realpath(pr_loc)) == os.path.normpath(os.path.realpath(imp_loc)):
379                                 errors.append("Warning: dependency %r found to have version number %r (normalized to %r, from %r) "
380                                               "by pkg_resources, but version %r (normalized to %r, from %r) by import."
381                                               % (name, pr_ver, str(pr_normver), pr_loc, imp_ver, str(imp_normver), imp_loc))
382
383     return errors
384
385
386 _vers_and_locs_list, _cross_check_errors = get_package_versions_and_locations()
387
388
389 def get_error_string(errors, debug=False):
390     from allmydata._auto_deps import install_requires
391
392     msg = "\n%s\n" % ("\n".join(errors),)
393     if debug:
394         msg += ("\n"
395                 "For debugging purposes, the PYTHONPATH was\n"
396                 "  %r\n"
397                 "install_requires was\n"
398                 "  %r\n"
399                 "sys.path after importing pkg_resources was\n"
400                 "  %s\n"
401                 % (os.environ.get('PYTHONPATH'), install_requires, (os.pathsep+"\n  ").join(sys.path)) )
402     return msg
403
404 def check_all_requirements():
405     """This function returns a list of errors due to any failed checks."""
406
407     from allmydata._auto_deps import install_requires
408
409     fatal_errors = []
410
411     # We require at least 2.6 on all platforms.
412     # (On Python 3, we'll have failed long before this point.)
413     if sys.version_info < (2, 6):
414         try:
415             version_string = ".".join(map(str, sys.version_info))
416         except Exception:
417             version_string = repr(sys.version_info)
418         fatal_errors.append("Tahoe-LAFS currently requires Python v2.6 or greater (but less than v3), not %s"
419                             % (version_string,))
420
421     vers_and_locs = dict(_vers_and_locs_list)
422     for requirement in install_requires:
423         try:
424             check_requirement(requirement, vers_and_locs)
425         except (ImportError, PackagingError), e:
426             fatal_errors.append("%s: %s" % (e.__class__.__name__, e))
427
428     if fatal_errors:
429         raise PackagingError(get_error_string(fatal_errors + _cross_check_errors, debug=True))
430
431 check_all_requirements()
432
433
434 def get_package_versions():
435     return dict([(k, v) for k, (v, l, c) in _vers_and_locs_list])
436
437 def get_package_locations():
438     return dict([(k, l) for k, (v, l, c) in _vers_and_locs_list])
439
440 def get_package_versions_string(show_paths=False, debug=False):
441     res = []
442     for p, (v, loc, comment) in _vers_and_locs_list:
443         info = str(p) + ": " + str(v)
444         if comment:
445             info = info + " [%s]" % str(comment)
446         if show_paths:
447             info = info + " (%s)" % str(loc)
448         res.append(info)
449
450     output = "\n".join(res) + "\n"
451
452     if _cross_check_errors:
453         output += get_error_string(_cross_check_errors, debug=debug)
454
455     return output