]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blobdiff - src/allmydata/__init__.py
Suppress DeprecationWarning about twisted.internet.interfaces.IFinishableConsumer...
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / __init__.py
index 8977bac20962bd8aae45a3eb71117950aa83aa4a..51af4fe5ddb870955f76510e931709aebc6235b5 100644 (file)
@@ -1,7 +1,7 @@
 """
 Decentralized storage grid.
 
-community web site: U{http://tahoe-lafs.org/}
+community web site: U{https://tahoe-lafs.org/}
 """
 
 class PackagingError(EnvironmentError):
@@ -29,10 +29,10 @@ except ImportError:
 
 # __full_version__ is the one that you ought to use when identifying yourself in the
 # "application" part of the Tahoe versioning scheme:
-# http://allmydata.org/trac/tahoe/wiki/Versioning
+# https://tahoe-lafs.org/trac/tahoe-lafs/wiki/Versioning
 __full_version__ = __appname__ + '/' + str(__version__)
 
-import os, platform, re, subprocess, sys
+import os, platform, re, subprocess, sys, traceback
 _distributor_id_cmdline_re = re.compile("(?:Distributor ID:)\s*(.*)", re.I)
 _release_cmdline_re = re.compile("(?:Release:)\s*(.*)", re.I)
 
@@ -133,13 +133,19 @@ def get_platform():
 
 
 from allmydata.util import verlib
-def normalized_version(verstr):
-    return verlib.NormalizedVersion(verlib.suggest_normalized_version(verstr))
+def normalized_version(verstr, what=None):
+    try:
+        return verlib.NormalizedVersion(verlib.suggest_normalized_version(verstr))
+    except (StandardError, verlib.IrrationalVersionError):
+        cls, value, trace = sys.exc_info()
+        raise PackagingError, ("could not parse %s due to %s: %s"
+                               % (what or repr(verstr), cls.__name__, value)), trace
 
 
 def get_package_versions_and_locations():
     import warnings
-    from _auto_deps import package_imports, deprecation_messages, deprecation_imports
+    from _auto_deps import package_imports, global_deprecation_messages, deprecation_messages, \
+        user_warning_messages, runtime_warning_messages, warning_imports
 
     def package_dir(srcfile):
         return os.path.dirname(os.path.dirname(os.path.normcase(os.path.realpath(srcfile))))
@@ -149,26 +155,24 @@ def get_package_versions_and_locations():
     # or any other bug that causes sys.path to be set up incorrectly. Therefore we
     # must import the packages in order to check their versions and paths.
 
-    # This warning is generated by twisted, PyRex, and possibly other packages,
-    # but can happen at any time, not only when they are imported. See
-    # http://tahoe-lafs.org/trac/tahoe-lafs/ticket/1129 .
-    warnings.filterwarnings("ignore", category=DeprecationWarning,
-        message="BaseException.message has been deprecated as of Python 2.6",
-        append=True)
-
-    # This is to suppress various DeprecationWarnings that occur when modules are imported.
-    # See http://allmydata.org/trac/tahoe/ticket/859 and http://divmod.org/trac/ticket/2994 .
+    # This is to suppress various DeprecationWarnings, UserWarnings, and RuntimeWarnings
+    # (listed in _auto_deps.py).
 
-    for msg in deprecation_messages:
+    for msg in global_deprecation_messages + deprecation_messages:
         warnings.filterwarnings("ignore", category=DeprecationWarning, message=msg, append=True)
+    for msg in user_warning_messages:
+        warnings.filterwarnings("ignore", category=UserWarning, message=msg, append=True)
+    for msg in runtime_warning_messages:
+        warnings.filterwarnings("ignore", category=RuntimeWarning, message=msg, append=True)
     try:
-        for modulename in deprecation_imports:
+        for modulename in warning_imports:
             try:
                 __import__(modulename)
             except ImportError:
                 pass
     finally:
-        for ign in deprecation_messages:
+        # Leave suppressions for global_deprecation_messages active.
+        for ign in runtime_warning_messages + user_warning_messages + deprecation_messages:
             warnings.filters.pop()
 
     packages = []
@@ -182,52 +186,61 @@ def get_package_versions_and_locations():
                 __import__(modulename)
                 module = sys.modules[modulename]
             except ImportError:
-                packages.append((pkgname, (None, modulename)))
+                etype, emsg, etrace = sys.exc_info()
+                trace_info = (etype, str(emsg), ([None] + traceback.extract_tb(etrace))[-1])
+                packages.append( (pkgname, (None, None, trace_info)) )
             else:
-                if 'sqlite' in pkgname:
-                    packages.append( (pkgname,  (get_version(module, 'version'),        package_dir(module.__file__))) )
-                    packages.append( ('sqlite', (get_version(module, 'sqlite_version'), package_dir(module.__file__))) )
-                else:
-                    packages.append( (pkgname,  (get_version(module, '__version__'),    package_dir(module.__file__))) )
+                comment = None
+                if pkgname == 'setuptools' and hasattr(module, '_distribute'):
+                    # distribute does not report its version in any module variables
+                    comment = 'distribute'
+                packages.append( (pkgname, (get_version(module, '__version__'), package_dir(module.__file__), comment)) )
         elif pkgname == 'python':
-            packages.append( (pkgname, (platform.python_version(), sys.executable)) )
+            packages.append( (pkgname, (platform.python_version(), sys.executable, None)) )
         elif pkgname == 'platform':
-            packages.append( (pkgname, (get_platform(), None)) )
+            packages.append( (pkgname, (get_platform(), None, None)) )
 
     return packages
 
 
 def check_requirement(req, vers_and_locs):
     # TODO: check [] options
-    # We support only disjunctions of >= and ==
+    # We support only disjunctions of <=, >=, and ==
 
     reqlist = req.split(',')
-    name = reqlist[0].split('>=')[0].split('==')[0].strip(' ').split('[')[0]
+    name = reqlist[0].split('<=')[0].split('>=')[0].split('==')[0].strip(' ').split('[')[0]
     if name not in vers_and_locs:
         raise PackagingError("no version info for %s" % (name,))
     if req.strip(' ') == name:
         return
-    (actual, location) = vers_and_locs[name]
+    (actual, location, comment) = vers_and_locs[name]
     if actual is None:
-        raise ImportError("could not import %r for requirement %r" % (location, req))
+        # comment is (type, message, (filename, line number, function name, text)) for the original ImportError
+        raise ImportError("for requirement %r: %s" % (req, comment))
     if actual == 'unknown':
         return
-    actualver = normalized_version(actual)
+    actualver = normalized_version(actual, what="actual version %r of %s from %r" % (actual, name, location))
 
     for r in reqlist:
-        s = r.split('>=')
+        s = r.split('<=')
         if len(s) == 2:
             required = s[1].strip(' ')
-            if actualver >= normalized_version(required):
-                return  # minimum requirement met
+            if actualver <= normalized_version(required, what="required maximum version %r in %r" % (required, req)):
+                return  # maximum requirement met
         else:
-            s = r.split('==')
+            s = r.split('>=')
             if len(s) == 2:
                 required = s[1].strip(' ')
-                if actualver == normalized_version(required):
-                    return  # exact requirement met
+                if actualver >= normalized_version(required, what="required minimum version %r in %r" % (required, req)):
+                    return  # minimum requirement met
             else:
-                raise PackagingError("no version info or could not understand requirement %r" % (req,))
+                s = r.split('==')
+                if len(s) == 2:
+                    required = s[1].strip(' ')
+                    if actualver == normalized_version(required, what="required exact version %r in %r" % (required, req)):
+                        return  # exact requirement met
+                else:
+                    raise PackagingError("no version info or could not understand requirement %r" % (req,))
 
     msg = ("We require %s, but could only find version %s.\n" % (req, actual))
     if location and location != 'unknown':
@@ -246,55 +259,71 @@ def cross_check_pkg_resources_versus_import():
     import pkg_resources
     from _auto_deps import install_requires
 
-    errors = []
-    not_pkg_resourceable = set(['sqlite', 'sqlite3', 'python', 'platform', __appname__.lower()])
-    not_import_versionable = set(['zope.interface', 'mock', 'pyasn1'])
-    ignorable = set(['argparse', 'pyutil', 'zbase32'])
-
     pkg_resources_vers_and_locs = dict([(p.project_name.lower(), (str(p.version), p.location))
                                         for p in pkg_resources.require(install_requires)])
 
-    for name, (imp_ver, imp_loc) in _vers_and_locs_list:
+    return cross_check(pkg_resources_vers_and_locs, _vers_and_locs_list)
+
+
+def cross_check(pkg_resources_vers_and_locs, imported_vers_and_locs_list):
+    """This function returns a list of errors due to any failed cross-checks."""
+
+    errors = []
+    not_pkg_resourceable = set(['python', 'platform', __appname__.lower()])
+    not_import_versionable = set(['zope.interface', 'mock', 'pyasn1'])
+    ignorable = set(['argparse', 'pyutil', 'zbase32', 'distribute', 'twisted-web', 'twisted-core'])
+
+    for name, (imp_ver, imp_loc, imp_comment) in imported_vers_and_locs_list:
         name = name.lower()
         if name not in not_pkg_resourceable:
             if name not in pkg_resources_vers_and_locs:
-                errors.append("Warning: dependency %s (version %s imported from %r) was not found by pkg_resources."
-                              % (name, imp_ver, imp_loc))
+                if name == "setuptools" and "distribute" in pkg_resources_vers_and_locs:
+                    pr_ver, pr_loc = pkg_resources_vers_and_locs["distribute"]
+                    if not (os.path.normpath(os.path.realpath(pr_loc)) == os.path.normpath(os.path.realpath(imp_loc))
+                            and imp_comment == "distribute"):
+                        errors.append("Warning: dependency 'setuptools' found to be version %r of 'distribute' from %r "
+                                      "by pkg_resources, but 'import setuptools' gave version %r [%s] from %r. "
+                                      "A version mismatch is expected, but a location mismatch is not."
+                                      % (pr_ver, pr_loc, imp_ver, imp_comment or 'probably *not* distribute', imp_loc))
+                else:
+                    errors.append("Warning: dependency %r (version %r imported from %r) was not found by pkg_resources."
+                                  % (name, imp_ver, imp_loc))
+                continue
 
             pr_ver, pr_loc = pkg_resources_vers_and_locs[name]
             try:
                 pr_normver = normalized_version(pr_ver)
             except Exception, e:
-                errors.append("Warning: version number %s found for dependency %s by pkg_resources could not be parsed. "
-                              "The version found by import was %s from %r. "
+                errors.append("Warning: version number %r found for dependency %r by pkg_resources could not be parsed. "
+                              "The version found by import was %r from %r. "
                               "pkg_resources thought it should be found at %r. "
                               "The exception was %s: %s"
-                              % (pr_ver, name, imp_ver, imp_loc, pr_loc, e.__class__.name, e))
+                              % (pr_ver, name, imp_ver, imp_loc, pr_loc, e.__class__.__name__, e))
             else:
                 if imp_ver == 'unknown':
                     if name not in not_import_versionable:
-                        errors.append("Warning: unexpectedly could not find a version number for dependency %s imported from %r. "
-                                      "pkg_resources thought it should be version %s at %r."
+                        errors.append("Warning: unexpectedly could not find a version number for dependency %r imported from %r. "
+                                      "pkg_resources thought it should be version %r at %r."
                                       % (name, imp_loc, pr_ver, pr_loc))
                 else:
                     try:
                         imp_normver = normalized_version(imp_ver)
                     except Exception, e:
-                        errors.append("Warning: version number %s found for dependency %s (imported from %r) could not be parsed. "
-                                      "pkg_resources thought it should be version %s at %r. "
+                        errors.append("Warning: version number %r found for dependency %r (imported from %r) could not be parsed. "
+                                      "pkg_resources thought it should be version %r at %r. "
                                       "The exception was %s: %s"
-                                      % (imp_ver, name, imp_loc, pr_ver, pr_loc, e.__class__.name, e))
+                                      % (imp_ver, name, imp_loc, pr_ver, pr_loc, e.__class__.__name__, e))
                     else:
                         if pr_ver == 'unknown' or (pr_normver != imp_normver):
                             if not os.path.normpath(os.path.realpath(pr_loc)) == os.path.normpath(os.path.realpath(imp_loc)):
-                                errors.append("Warning: dependency %s found to have version number %s (normalized to %s, from %r) "
-                                              "by pkg_resources, but version %s (normalized to %s, from %r) by import."
+                                errors.append("Warning: dependency %r found to have version number %r (normalized to %r, from %r) "
+                                              "by pkg_resources, but version %r (normalized to %r, from %r) by import."
                                               % (name, pr_ver, str(pr_normver), pr_loc, imp_ver, str(imp_normver), imp_loc))
 
-    imported_packages = set([p.lower() for (p, _) in _vers_and_locs_list])
+    imported_packages = set([p.lower() for (p, _) in imported_vers_and_locs_list])
     for pr_name, (pr_ver, pr_loc) in pkg_resources_vers_and_locs.iteritems():
         if pr_name not in imported_packages and pr_name not in ignorable:
-            errors.append("Warning: dependency %s (version %s) found by pkg_resources not found by import."
+            errors.append("Warning: dependency %r (version %r) found by pkg_resources not found by import."
                           % (pr_name, pr_ver))
 
     return errors
@@ -322,30 +351,21 @@ def check_all_requirements():
 
     errors = []
 
-    # we require 2.4.4 on non-UCS-2, non-Redhat builds to avoid <http://www.python.org/news/security/PSF-2006-001/>
-    # we require 2.4.3 on non-UCS-2 Redhat, because 2.4.3 is common on Redhat-based distros and will have patched the above bug
-    # we require at least 2.4.2 in any case to avoid a bug in the base64 module: <http://bugs.python.org/issue1171487>
-    if sys.maxunicode == 65535:
-        if sys.version_info < (2, 4, 2) or sys.version_info[0] > 2:
-            errors.append("Tahoe-LAFS current requires Python v2.4.2 or greater "
-                          "for a UCS-2 build (but less than v3), not %r" %
-                          (sys.version_info,))
-    elif platform.platform().lower().find('redhat') >= 0:
-        if sys.version_info < (2, 4, 3) or sys.version_info[0] > 2:
-            errors.append("Tahoe-LAFS current requires Python v2.4.3 or greater "
-                          "on Redhat-based distributions (but less than v3), not %r" %
-                          (sys.version_info,))
-    else:
-        if sys.version_info < (2, 4, 4) or sys.version_info[0] > 2:
-            errors.append("Tahoe-LAFS current requires Python v2.4.4 or greater "
-                          "for a non-UCS-2 build (but less than v3), not %r" %
-                          (sys.version_info,))
+    # We require at least 2.5 on all platforms.
+    # (On Python 3, we'll have failed long before this point.)
+    if sys.version_info < (2, 5):
+        try:
+            version_string = ".".join(map(str, sys.version_info))
+        except Exception:
+            version_string = repr(sys.version_info)
+        errors.append("Tahoe-LAFS current requires Python v2.5 or greater (but less than v3), not %s"
+                      % (version_string,))
 
     vers_and_locs = dict(_vers_and_locs_list)
     for requirement in install_requires:
         try:
             check_requirement(requirement, vers_and_locs)
-        except Exception, e:
+        except (ImportError, PackagingError), e:
             errors.append("%s: %s" % (e.__class__.__name__, e))
 
     if errors:
@@ -355,15 +375,17 @@ check_all_requirements()
 
 
 def get_package_versions():
-    return dict([(k, v) for k, (v, l) in _vers_and_locs_list])
+    return dict([(k, v) for k, (v, l, c) in _vers_and_locs_list])
 
 def get_package_locations():
-    return dict([(k, l) for k, (v, l) in _vers_and_locs_list])
+    return dict([(k, l) for k, (v, l, c) in _vers_and_locs_list])
 
 def get_package_versions_string(show_paths=False, debug=False):
     res = []
-    for p, (v, loc) in _vers_and_locs_list:
+    for p, (v, loc, comment) in _vers_and_locs_list:
         info = str(p) + ": " + str(v)
+        if comment:
+            info = info + " [%s]" % str(comment)
         if show_paths:
             info = info + " (%s)" % str(loc)
         res.append(info)