source: trunk/src/allmydata/__init__.py @ 6215ebd

Last change on this file since 6215ebd was 6215ebd, checked in by david-sarah <david-sarah@…>, at 2010-02-01T00:44:29Z

cli: suppress DeprecationWarnings? emitted from importing nevow and twisted. Fixes #859

  • Property mode set to 100644
File size: 8.6 KB
Line 
1"""
2Decentralized storage grid.
3
4maintainer web site: U{http://allmydata.com/}
5
6community web site: U{http://allmydata.org/}
7"""
8
9# This is just to suppress DeprecationWarnings from nevow and twisted.
10# See http://allmydata.org/trac/tahoe/ticket/859 and
11# http://divmod.org/trac/ticket/2994 .
12import warnings
13warnings.filterwarnings("ignore", category=DeprecationWarning,
14    message="object.__new__\(\) takes no parameters",
15    append=True)
16warnings.filterwarnings("ignore", category=DeprecationWarning,
17    message="The popen2 module is deprecated.  Use the subprocess module.",
18    append=True)
19warnings.filterwarnings("ignore", category=DeprecationWarning,
20    message="the md5 module is deprecated; use hashlib instead",
21    append=True)
22warnings.filterwarnings("ignore", category=DeprecationWarning,
23    message="the sha module is deprecated; use the hashlib module instead",
24    append=True)
25try:
26    import nevow
27    from twisted.persisted import sob
28    from twisted.python import filepath
29    hush_pyflakes = (nevow, sob, filepath)
30    del hush_pyflakes
31finally:
32    warnings.filters.pop()
33    warnings.filters.pop()
34    warnings.filters.pop()
35    warnings.filters.pop()
36
37__version__ = "unknown"
38try:
39    from _version import __version__
40except ImportError:
41    # We're running in a tree that hasn't run "./setup.py darcsver", and didn't
42    # come with a _version.py, so we don't know what our version is. This should
43    # not happen very often.
44    pass
45
46__appname__ = "unknown"
47try:
48    from _appname import __appname__
49except ImportError:
50    # We're running in a tree that hasn't run "./setup.py".  This shouldn't happen.
51    pass
52
53# __full_version__ is the one that you ought to use when identifying yourself in the
54# "application" part of the Tahoe versioning scheme:
55# http://allmydata.org/trac/tahoe/wiki/Versioning
56__full_version__ = __appname__ + '/' + str(__version__)
57
58import _auto_deps
59_auto_deps.require_auto_deps()
60
61import os, platform, re, subprocess, sys
62_distributor_id_cmdline_re = re.compile("(?:Distributor ID:)\s*(.*)", re.I)
63_release_cmdline_re = re.compile("(?:Release:)\s*(.*)", re.I)
64
65_distributor_id_file_re = re.compile("(?:DISTRIB_ID\s*=)\s*(.*)", re.I)
66_release_file_re = re.compile("(?:DISTRIB_RELEASE\s*=)\s*(.*)", re.I)
67
68global _distname,_version
69_distname = None
70_version = None
71
72def get_linux_distro():
73    """ Tries to determine the name of the Linux OS distribution name.
74
75    First, try to parse a file named "/etc/lsb-release".  If it exists, and
76    contains the "DISTRIB_ID=" line and the "DISTRIB_RELEASE=" line, then return
77    the strings parsed from that file.
78
79    If that doesn't work, then invoke platform.dist().
80
81    If that doesn't work, then try to execute "lsb_release", as standardized in
82    2001:
83
84    http://refspecs.freestandards.org/LSB_1.0.0/gLSB/lsbrelease.html
85
86    The current version of the standard is here:
87
88    http://refspecs.freestandards.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/lsbrelease.html
89
90    that lsb_release emitted, as strings.
91
92    Returns a tuple (distname,version). Distname is what LSB calls a
93    "distributor id", e.g. "Ubuntu".  Version is what LSB calls a "release",
94    e.g. "8.04".
95
96    A version of this has been submitted to python as a patch for the standard
97    library module "platform":
98
99    http://bugs.python.org/issue3937
100    """
101    global _distname,_version
102    if _distname and _version:
103        return (_distname, _version)
104
105    try:
106        etclsbrel = open("/etc/lsb-release", "rU")
107        for line in etclsbrel:
108            m = _distributor_id_file_re.search(line)
109            if m:
110                _distname = m.group(1).strip()
111                if _distname and _version:
112                    return (_distname, _version)
113            m = _release_file_re.search(line)
114            if m:
115                _version = m.group(1).strip()
116                if _distname and _version:
117                    return (_distname, _version)
118    except EnvironmentError:
119            pass
120
121    (_distname, _version) = platform.dist()[:2]
122    if _distname and _version:
123        return (_distname, _version)
124
125    try:
126        p = subprocess.Popen(["lsb_release", "--all"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
127        rc = p.wait()
128        if rc == 0:
129            for line in p.stdout.readlines():
130                m = _distributor_id_cmdline_re.search(line)
131                if m:
132                    _distname = m.group(1).strip()
133                    if _distname and _version:
134                        return (_distname, _version)
135
136                m = _release_cmdline_re.search(p.stdout.read())
137                if m:
138                    _version = m.group(1).strip()
139                    if _distname and _version:
140                        return (_distname, _version)
141    except EnvironmentError:
142        pass
143
144    if os.path.exists("/etc/arch-release"):
145        return ("Arch_Linux", "")
146
147    return (_distname,_version)
148
149def get_platform():
150    # Our version of platform.platform(), telling us both less and more than the
151    # Python Standard Library's version does.
152    # We omit details such as the Linux kernel version number, but we add a
153    # more detailed and correct rendition of the Linux distribution and
154    # distribution-version.
155    if "linux" in platform.system().lower():
156        return platform.system()+"-"+"_".join(get_linux_distro())+"-"+platform.machine()+"-"+"_".join([x for x in platform.architecture() if x])
157    else:
158        return platform.platform()
159
160def get_package_versions_and_locations():
161    # because there are a few dependencies that are outside setuptools's ken
162    # (Python and platform, and sqlite3 if you are on Python >= 2.5), and
163    # because setuptools might fail to find something even though import
164    # finds it:
165    import OpenSSL, allmydata, foolscap.api, nevow, platform, pycryptopp, setuptools, simplejson, twisted, zfec, zope.interface
166    pysqlitever = None
167    pysqlitefile = None
168    sqlitever = None
169    try:
170        import sqlite3
171    except ImportError:
172        try:
173            from pysqlite2 import dbapi2
174        except ImportError:
175            pass
176        else:
177            pysqlitever = dbapi2.version
178            pysqlitefile = os.path.dirname(dbapi2.__file__)
179            sqlitever = dbapi2.sqlite_version
180    else:
181        pysqlitever = sqlite3.version
182        pysqlitefile = os.path.dirname(sqlite3.__file__)
183        sqlitever = sqlite3.sqlite_version
184
185    d1 = {
186        'pyOpenSSL': (OpenSSL.__version__, os.path.dirname(OpenSSL.__file__)),
187        'allmydata-tahoe': (allmydata.__version__, os.path.dirname(allmydata.__file__)),
188        'foolscap': (foolscap.api.__version__, os.path.dirname(foolscap.__file__)),
189        'Nevow': (nevow.__version__, os.path.dirname(nevow.__file__)),
190        'pycryptopp': (pycryptopp.__version__, os.path.dirname(pycryptopp.__file__)),
191        'setuptools': (setuptools.__version__, os.path.dirname(setuptools.__file__)),
192        'simplejson': (simplejson.__version__, os.path.dirname(simplejson.__file__)),
193        'pysqlite': (pysqlitever, pysqlitefile),
194        'sqlite': (sqlitever, 'unknown'),
195        'zope.interface': ('unknown', os.path.dirname(zope.interface.__file__)),
196        'Twisted': (twisted.__version__, os.path.dirname(twisted.__file__)),
197        'zfec': (zfec.__version__, os.path.dirname(zfec.__file__)),
198        'python': (platform.python_version(), sys.executable),
199        'platform': (get_platform(), None),
200        }
201
202    # But we prefer to get all the dependencies as known by setuptools:
203    import pkg_resources
204    try:
205        d2 = _auto_deps.get_package_versions_from_setuptools()
206    except pkg_resources.DistributionNotFound:
207        # See docstring in _auto_deps.require_auto_deps() to explain why it makes sense to ignore this exception.
208        pass
209    else:
210        d1.update(d2)
211
212    return d1
213
214def get_package_versions():
215    return dict([(k, v) for k, (v, l) in get_package_versions_and_locations().iteritems()])
216
217def get_package_locations():
218    return dict([(k, l) for k, (v, l) in get_package_versions_and_locations().iteritems()])
219
220def get_package_versions_string(show_paths=False):
221    vers_and_locs = get_package_versions_and_locations()
222    res = []
223    for p in ["allmydata-tahoe", "foolscap", "pycryptopp", "zfec", "Twisted", "Nevow", "zope.interface", "python", "platform"]:
224        (ver, loc) = vers_and_locs.get(p, ('UNKNOWN', 'UNKNOWN'))
225        info = str(p) + ": " + str(ver)
226        if show_paths:
227            info = info + " (%s)" % str(loc)
228        res.append(info)
229        if vers_and_locs.has_key(p):
230            del vers_and_locs[p]
231
232    for p, (v, loc) in vers_and_locs.iteritems():
233        info = str(p) + ": " + str(v)
234        if show_paths:
235            info = info + " (%s)" % str(loc)
236        res.append(info)
237    return ', '.join(res)
Note: See TracBrowser for help on using the repository browser.