]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/__init__.py
setup: fix missing import -- thanks, pyflakes
[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 os, 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 global _distname,_version
32 _distname = None
33 _version = None
34
35 def get_linux_distro():
36     """ Tries to determine the name of the Linux OS distribution name.
37
38     First, try to parse a file named "/etc/lsb-release".  If it exists, and
39     contains the "DISTRIB_ID=" line and the "DISTRIB_RELEASE=" line, then return
40     the strings parsed from that file.
41
42     If that doesn't work, then invoke platform.dist().
43
44     If that doesn't work, then try to execute "lsb_release", as standardized in
45     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     that lsb_release emitted, as strings.
54
55     Returns a tuple (distname,version). Distname is what LSB calls a
56     "distributor id", e.g. "Ubuntu".  Version is what LSB calls a "release",
57     e.g. "8.04".
58
59     A version of this has been submitted to python as a patch for the standard
60     library module "platform":
61
62     http://bugs.python.org/issue3937
63     """
64     global _distname,_version
65     if _distname and _version:
66         return (_distname, _version)
67
68     try:
69         etclsbrel = open("/etc/lsb-release", "rU")
70         for line in etclsbrel:
71             m = _distributor_id_file_re.search(line)
72             if m:
73                 _distname = m.group(1).strip()
74                 if _distname and _version:
75                     return (_distname, _version)
76             m = _release_file_re.search(line)
77             if m:
78                 _version = m.group(1).strip()
79                 if _distname and _version:
80                     return (_distname, _version)
81     except EnvironmentError:
82             pass
83
84     (_distname, _version) = platform.dist()[:2]
85     if _distname and _version:
86         return (_distname, _version)
87
88     try:
89         p = subprocess.Popen(["lsb_release", "--all"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
90         rc = p.wait()
91         if rc == 0:
92             for line in p.stdout.readlines():
93                 m = _distributor_id_cmdline_re.search(line)
94                 if m:
95                     _distname = m.group(1).strip()
96                     if _distname and _version:
97                         return (_distname, _version)
98
99                 m = _release_cmdline_re.search(p.stdout.read())
100                 if m:
101                     _version = m.group(1).strip()
102                     if _distname and _version:
103                         return (_distname, _version)
104     except EnvironmentError:
105         pass
106
107     if os.path.exists("/etc/arch-release"):
108         return ("Arch_Linux", "")
109
110     return (_distname,_version)
111
112 def get_platform():
113     # Our version of platform.platform(), telling us both less and more than the
114     # Python Standard Library's version does.
115     # We omit details such as the Linux kernel version number, but we add a
116     # more detailed and correct rendition of the Linux distribution and
117     # distribution-version.
118     if "linux" in platform.system().lower():
119         return platform.system()+"-"+"_".join(get_linux_distro())+"-"+platform.machine()+"-"+"_".join([x for x in platform.architecture() if x])
120     else:
121         return platform.platform()
122
123 def get_package_versions():
124     import OpenSSL, allmydata, foolscap, nevow, platform, pycryptopp, setuptools, simplejson, twisted, zfec
125
126     return {
127         'pyopenssl': OpenSSL.__version__,
128         'allmydata': allmydata.__version__,
129         'foolscap': foolscap.__version__,
130         'nevow': nevow.__version__,
131         'pycryptopp': pycryptopp.__version__,
132         'setuptools': setuptools.__version__,
133         'simplejson': simplejson.__version__,
134         'twisted': twisted.__version__,
135         'zfec': zfec.__version__,
136         'python': platform.python_version(),
137         'platform': get_platform()
138         }
139
140 def get_package_versions_string():
141     versions = get_package_versions()
142     res = []
143     for p in ["allmydata", "foolscap", "pycryptopp", "zfec", "twisted", "nevow", "python", "platform"]:
144         if versions.has_key(p):
145             res.append(str(p) + ": " + str(versions[p]))
146             del versions[p]
147         else:
148             res.append(str(p) + ": UNKNOWN")
149     for p, v in versions.iteritems():
150         res.append(str(p) + ": " + str(v))
151     return ', '.join(res)