]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/__init__.py
setup: if invoking lsb_release doesn't work (which it doesn't on our etch buildslave...
[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, and
40     both the "distributor id" and "release" results are non-empty after being
41     stripped of whitespace, then return a two-tuple containing the information
42     that lsb_release emitted, as strings.  Else, invoke platform.dist() and
43     return the first two elements of the tuple returned by that function.
44
45     Returns a tuple (distname,version). Distname is what LSB calls a
46     "distributor id", e.g. "Ubuntu".  Version is what LSB calls a "release",
47     e.g. "8.04".
48
49     A version of this has been submitted to python as a patch for the standard
50     library module "platform":
51
52     http://bugs.python.org/issue3937
53     """
54     _distname = ""
55     _version = ""
56     try:
57         p = subprocess.Popen(["lsb_release", "--id"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
58         rc = p.wait()
59         if rc == 0:
60             m = _distributor_id.search(p.stdout.read())
61             if m:
62                 _distname = m.group(1).strip()
63
64         p = subprocess.Popen(["lsb_release", "--release"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
65         rc = p.wait()
66         if rc == 0:
67             m = _release.search(p.stdout.read())
68             if m:
69                 _version = m.group(1).strip()
70     except EnvironmentError:
71         pass
72
73     if _distname and _version:
74         return (_distname, _version)
75     else:
76         return platform.dist()[:2]
77
78 def get_platform():
79     # Our version of platform.platform(), telling us both less and more than the
80     # Python Standard Library's version does.
81     # We omit details such as the Linux kernel version number, but we add a
82     # more detailed and correct rendition of the Linux distribution and
83     # distribution-version.
84     if "linux" in platform.system().lower():
85         return platform.system()+"-"+"_".join(get_linux_distro())+"-"+platform.machine()+"-"+"_".join([x for x in platform.architecture() if x])
86     else:
87         return platform.platform()
88
89 def get_package_versions():
90     import OpenSSL, allmydata, foolscap, nevow, platform, pycryptopp, setuptools, simplejson, twisted, zfec
91
92     return {
93         'pyopenssl': OpenSSL.__version__,
94         'allmydata': allmydata.__version__,
95         'foolscap': foolscap.__version__,
96         'nevow': nevow.__version__,
97         'pycryptopp': pycryptopp.__version__,
98         'setuptools': setuptools.__version__,
99         'simplejson': simplejson.__version__,
100         'twisted': twisted.__version__,
101         'zfec': zfec.__version__,
102         'python': platform.python_version(),
103         'platform': get_platform()
104         }
105
106 def get_package_versions_string():
107     versions = get_package_versions()
108     res = []
109     for p in ["allmydata", "foolscap", "pycryptopp", "zfec", "twisted", "nevow", "python", "platform"]:
110         if versions.has_key(p):
111             res.append(str(p) + ": " + str(versions[p]))
112             del versions[p]
113         else:
114             res.append(str(p) + ": UNKNOWN")
115     for p, v in versions.iteritems():
116         res.append(str(p) + ": " + str(v))
117     return ', '.join(res)