]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/__init__.py
setup: simplify the implementation of allmydata.get_package_versions() and add "platf...
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / __init__.py
1 """
2 Decentralized storage grid.
3
4 maintainer web site: U{http://allmydata.com/}
5
6 community web site: U{http://allmydata.org/}
7 """
8
9 __version__ = "unknown"
10 try:
11     from _version import __version__
12 except ImportError:
13     # We're running in a tree that hasn't run "./setup.py darcsver", and didn't
14     # come with a _version.py, so we don't know what our version is. This should
15     # not happen very often.
16     pass
17
18 hush_pyflakes = __version__
19 del hush_pyflakes
20
21 import _auto_deps
22 _auto_deps.require_auto_deps()
23
24 import platform, re, subprocess
25 _distributor_id = re.compile("(?:Distributor ID)?:?\s*(.*)", re.I)
26 _release = re.compile("(?:Release)?:?\s*(.*)", re.I)
27
28 def get_linux_distro():
29     """ Tries to determine the name of the Linux OS distribution name.
30
31     The function tries to execute "lsb_release", as standardized in 2001:
32
33     http://refspecs.freestandards.org/LSB_1.0.0/gLSB/lsbrelease.html
34
35     The current version of the standard is here:
36
37     http://refspecs.freestandards.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/lsbrelease.html
38
39     If executing "lsb_release" raises no exception, and returns exit code 0,
40     then return a two-tuple containing the information that lsb_release emitted
41     as strings.
42
43     Returns a tuple (distname,version). Distname is what LSB calls a
44     "distributor id", e.g. "Ubuntu".  Version is what LSB calls a "release",
45     e.g. "8.04".
46
47     A version of this has been submitted to python as a patch for the standard
48     library module "platform":
49
50     http://bugs.python.org/issue3937
51     """
52     _distname = ""
53     _version = ""
54     try:
55         p = subprocess.Popen(["lsb_release", "--id"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
56         rc = p.wait()
57         if rc == 0:
58             m = _distributor_id.search(p.stdout.read())
59             if m:
60                 _distname = m.group(1).strip()
61
62         p = subprocess.Popen(["lsb_release", "--release"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
63         rc = p.wait()
64         if rc == 0:
65             m = _release.search(p.stdout.read())
66             if m:
67                 _version = m.group(1).strip()
68     except EnvironmentError:
69         pass
70
71     return (_distname, _version)
72
73 def get_platform():
74     # Our version of platform.platform(), telling us both less and more than the
75     # Python Standard Library's version does.
76     # We omit details such as the Linux kernel version number, but we add a
77     # more detailed and correct rendition of the Linux distribution and
78     # distribution-version.
79     if "linux" in platform.system().lower():
80         return platform.system()+"-"+"_".join(get_linux_distro())+"-"+platform.machine()+"-"+"_".join([x for x in platform.architecture() if x])
81     else:
82         return platform.platform()
83
84 def get_package_versions():
85     import OpenSSL, allmydata, foolscap, nevow, platform, pycryptopp, setuptools, simplejson, twisted, zfec
86
87     return {
88         'pyopenssl': OpenSSL.__version__,
89         'allmydata': allmydata.__version__,
90         'foolscap': foolscap.__version__,
91         'nevow': nevow.__version__,
92         'pycryptopp': pycryptopp.__version__,
93         'setuptools': setuptools.__version__,
94         'simplejson': simplejson.__version__,
95         'twisted': twisted.__version__,
96         'zfec': zfec.__version__,
97         'python': platform.python_version(),
98         'platform': get_platform()
99         }
100
101 def get_package_versions_string():
102     versions = get_package_versions()
103     res = []
104     for p in ["allmydata", "foolscap", "pycryptopp", "zfec", "twisted", "nevow", "python", "platform"]:
105         if versions.has_key(p):
106             res.append(str(p) + ": " + str(versions[p]))
107             del versions[p]
108         else:
109             res.append(str(p) + ": UNKNOWN")
110     for p, v in versions.iteritems():
111         res.append(str(p) + ": " + str(v))
112     return ', '.join(res)