]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/__init__.py
setup: try parsing /etc/lsb-release first, then invoking lsb_release, because the...
[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_cmdline_re = re.compile("(?:Distributor ID:)\s*(.*)", re.I)
26 _release_cmdline_re = re.compile("(?:Release:)\s*(.*)", re.I)
27
28 _distributor_id_file_re = re.compile("(?:DISTRIB_ID\s*=)\s*(.*)", re.I)
29 _release_file_re = re.compile("(?:DISTRIB_RELEASE\s*=)\s*(.*)", re.I)
30
31 def get_linux_distro():
32     """ Tries to determine the name of the Linux OS distribution name.
33
34     First, try to parse a file named "/etc/lsb-release".  If it exists, and
35     contains the "DISTRIB_ID=" line and the "DISTRIB_RELEASE=" line, then return
36     the strings parsed from that file.  The reason we try this first is because
37     it is faster than the official method of invoking "lsb_release" (which takes
38     half a second on my high-performance Athlon64 Ubuntu workstation).  Also
39     because some distributions (at least Debian/Ubuntu) have /etc/lsb-release in
40     the "base-files" package (Priority: required) but /usr/bin/lsb_release in
41     the "lsb-release" package (Priority: important), so it is possible that
42     /etc/lsb-release is there even if /usr/bin/lsb_release isn't.
43
44     If parsing /etc/lsb-release doesn't work, then try to execute "lsb_release",
45     as standardized in 2001:
46
47     http://refspecs.freestandards.org/LSB_1.0.0/gLSB/lsbrelease.html
48
49     The current version of the standard is here:
50
51     http://refspecs.freestandards.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/lsbrelease.html
52
53     If executing "lsb_release" raises no exception, and returns exit code 0, and
54     both the "distributor id" and "release" results are non-empty after being
55     stripped of whitespace, then return a two-tuple containing the information
56     that lsb_release emitted, as strings.
57
58     If that doesn't work, then invoke platform.dist() and return the first two
59     elements of the tuple returned by that function.
60
61     Returns a tuple (distname,version). Distname is what LSB calls a
62     "distributor id", e.g. "Ubuntu".  Version is what LSB calls a "release",
63     e.g. "8.04".
64
65     A version of this has been submitted to python as a patch for the standard
66     library module "platform":
67
68     http://bugs.python.org/issue3937
69     """
70     _distname = None
71     _version = None
72
73     try:
74         etclsbrel = open("/etc/lsb-release", "rU")
75         for line in etclsbrel:
76             m = _distributor_id_file_re.search(line)
77             if m:
78                 _distname = m.group(1).strip()
79                 if _distname and _version:
80                     return (_distname, _version)
81             m = _release_file_re.search(line)
82             if m:
83                 _version = m.group(1).strip()
84                 if _distname and _version:
85                     return (_distname, _version)
86     except EnvironmentError:
87             pass
88
89     try:
90         p = subprocess.Popen(["lsb_release", "--all"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
91         rc = p.wait()
92         if rc == 0:
93             for line in p.stdout.readlines():
94                 m = _distributor_id_cmdline_re.search(line)
95                 if m:
96                     _distname = m.group(1).strip()
97                     if _distname and _version:
98                         return (_distname, _version)
99
100                 m = _release_cmdline_re.search(p.stdout.read())
101                 if m:
102                     _version = m.group(1).strip()
103                     if _distname and _version:
104                         return (_distname, _version)
105     except EnvironmentError:
106         pass
107
108     return platform.dist()[:2]
109
110 def get_platform():
111     # Our version of platform.platform(), telling us both less and more than the
112     # Python Standard Library's version does.
113     # We omit details such as the Linux kernel version number, but we add a
114     # more detailed and correct rendition of the Linux distribution and
115     # distribution-version.
116     if "linux" in platform.system().lower():
117         return platform.system()+"-"+"_".join(get_linux_distro())+"-"+platform.machine()+"-"+"_".join([x for x in platform.architecture() if x])
118     else:
119         return platform.platform()
120
121 def get_package_versions():
122     import OpenSSL, allmydata, foolscap, nevow, platform, pycryptopp, setuptools, simplejson, twisted, zfec
123
124     return {
125         'pyopenssl': OpenSSL.__version__,
126         'allmydata': allmydata.__version__,
127         'foolscap': foolscap.__version__,
128         'nevow': nevow.__version__,
129         'pycryptopp': pycryptopp.__version__,
130         'setuptools': setuptools.__version__,
131         'simplejson': simplejson.__version__,
132         'twisted': twisted.__version__,
133         'zfec': zfec.__version__,
134         'python': platform.python_version(),
135         'platform': get_platform()
136         }
137
138 def get_package_versions_string():
139     versions = get_package_versions()
140     res = []
141     for p in ["allmydata", "foolscap", "pycryptopp", "zfec", "twisted", "nevow", "python", "platform"]:
142         if versions.has_key(p):
143             res.append(str(p) + ": " + str(versions[p]))
144             del versions[p]
145         else:
146             res.append(str(p) + ": UNKNOWN")
147     for p, v in versions.iteritems():
148         res.append(str(p) + ": " + str(v))
149     return ', '.join(res)