]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/__init__.py
switch all foolscap imports to use foolscap.api or foolscap.logging
[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 __appname__ = "unknown"
19 try:
20     from _appname import __appname__
21 except ImportError:
22     # We're running in a tree that hasn't run "./setup.py".  This shouldn't happen.
23     pass
24
25 # __full_version__ is the one that you ought to use when identifying yourself in the
26 # "application" part of the Tahoe versioning scheme:
27 # http://allmydata.org/trac/tahoe/wiki/Versioning
28 __full_version__ = __appname__ + '/' + str(__version__)
29
30 hush_pyflakes = __version__
31 del hush_pyflakes
32
33 import _auto_deps
34 _auto_deps.require_auto_deps()
35
36 import os, platform, re, subprocess, sys
37 _distributor_id_cmdline_re = re.compile("(?:Distributor ID:)\s*(.*)", re.I)
38 _release_cmdline_re = re.compile("(?:Release:)\s*(.*)", re.I)
39
40 _distributor_id_file_re = re.compile("(?:DISTRIB_ID\s*=)\s*(.*)", re.I)
41 _release_file_re = re.compile("(?:DISTRIB_RELEASE\s*=)\s*(.*)", re.I)
42
43 global _distname,_version
44 _distname = None
45 _version = None
46
47 def get_linux_distro():
48     """ Tries to determine the name of the Linux OS distribution name.
49
50     First, try to parse a file named "/etc/lsb-release".  If it exists, and
51     contains the "DISTRIB_ID=" line and the "DISTRIB_RELEASE=" line, then return
52     the strings parsed from that file.
53
54     If that doesn't work, then invoke platform.dist().
55
56     If that doesn't work, then try to execute "lsb_release", as standardized in
57     2001:
58
59     http://refspecs.freestandards.org/LSB_1.0.0/gLSB/lsbrelease.html
60
61     The current version of the standard is here:
62
63     http://refspecs.freestandards.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/lsbrelease.html
64
65     that lsb_release emitted, as strings.
66
67     Returns a tuple (distname,version). Distname is what LSB calls a
68     "distributor id", e.g. "Ubuntu".  Version is what LSB calls a "release",
69     e.g. "8.04".
70
71     A version of this has been submitted to python as a patch for the standard
72     library module "platform":
73
74     http://bugs.python.org/issue3937
75     """
76     global _distname,_version
77     if _distname and _version:
78         return (_distname, _version)
79
80     try:
81         etclsbrel = open("/etc/lsb-release", "rU")
82         for line in etclsbrel:
83             m = _distributor_id_file_re.search(line)
84             if m:
85                 _distname = m.group(1).strip()
86                 if _distname and _version:
87                     return (_distname, _version)
88             m = _release_file_re.search(line)
89             if m:
90                 _version = m.group(1).strip()
91                 if _distname and _version:
92                     return (_distname, _version)
93     except EnvironmentError:
94             pass
95
96     (_distname, _version) = platform.dist()[:2]
97     if _distname and _version:
98         return (_distname, _version)
99
100     try:
101         p = subprocess.Popen(["lsb_release", "--all"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
102         rc = p.wait()
103         if rc == 0:
104             for line in p.stdout.readlines():
105                 m = _distributor_id_cmdline_re.search(line)
106                 if m:
107                     _distname = m.group(1).strip()
108                     if _distname and _version:
109                         return (_distname, _version)
110
111                 m = _release_cmdline_re.search(p.stdout.read())
112                 if m:
113                     _version = m.group(1).strip()
114                     if _distname and _version:
115                         return (_distname, _version)
116     except EnvironmentError:
117         pass
118
119     if os.path.exists("/etc/arch-release"):
120         return ("Arch_Linux", "")
121
122     return (_distname,_version)
123
124 def get_platform():
125     # Our version of platform.platform(), telling us both less and more than the
126     # Python Standard Library's version does.
127     # We omit details such as the Linux kernel version number, but we add a
128     # more detailed and correct rendition of the Linux distribution and
129     # distribution-version.
130     if "linux" in platform.system().lower():
131         return platform.system()+"-"+"_".join(get_linux_distro())+"-"+platform.machine()+"-"+"_".join([x for x in platform.architecture() if x])
132     else:
133         return platform.platform()
134
135 def get_package_versions_and_locations():
136     # because there are a few dependencies that are outside setuptools's ken (Python and
137     # platform), and because setuptools might fail to find something even though import finds
138     # it:
139     import OpenSSL, allmydata, foolscap.api, nevow, platform, pycryptopp, setuptools, simplejson, twisted, zfec, zope.interface
140
141     d1 = {
142         'pyOpenSSL': (OpenSSL.__version__, os.path.dirname(OpenSSL.__file__)),
143         'allmydata-tahoe': (allmydata.__version__, os.path.dirname(allmydata.__file__)),
144         'foolscap': (foolscap.api.__version__, os.path.dirname(foolscap.__file__)),
145         'Nevow': (nevow.__version__, os.path.dirname(nevow.__file__)),
146         'pycryptopp': (pycryptopp.__version__, os.path.dirname(pycryptopp.__file__)),
147         'setuptools': (setuptools.__version__, os.path.dirname(setuptools.__file__)),
148         'simplejson': (simplejson.__version__, os.path.dirname(simplejson.__file__)),
149         'zope.interface': ('unknown', os.path.dirname(zope.interface.__file__)),
150         'Twisted': (twisted.__version__, os.path.dirname(twisted.__file__)),
151         'zfec': (zfec.__version__, os.path.dirname(zfec.__file__)),
152         'python': (platform.python_version(), sys.executable),
153         'platform': (get_platform(), None),
154         }
155
156     # But we prefer to get all the dependencies as known by setuptools:
157     import pkg_resources
158     try:
159         d2 = _auto_deps.get_package_versions_from_setuptools()
160     except pkg_resources.DistributionNotFound:
161         # See docstring in _auto_deps.require_auto_deps() to explain why it makes sense to ignore this exception.
162         pass
163     else:
164         d1.update(d2)
165
166     return d1
167
168 def get_package_versions():
169     return dict([(k, v) for k, (v, l) in get_package_versions_and_locations().iteritems()])
170
171 def get_package_locations():
172     return dict([(k, l) for k, (v, l) in get_package_versions_and_locations().iteritems()])
173
174 def get_package_versions_string(show_paths=False):
175     vers_and_locs = get_package_versions_and_locations()
176     res = []
177     for p in ["allmydata-tahoe", "foolscap", "pycryptopp", "zfec", "Twisted", "Nevow", "zope.interface", "python", "platform"]:
178         (ver, loc) = vers_and_locs.get(p, ('UNKNOWN', 'UNKNOWN'))
179         info = str(p) + ": " + str(ver)
180         if show_paths:
181             info = info + " (%s)" % str(loc)
182         res.append(info)
183         if vers_and_locs.has_key(p):
184             del vers_and_locs[p]
185
186     for p, (v, loc) in vers_and_locs.iteritems():
187         info = str(p) + ": " + str(v)
188         if show_paths:
189             info = info + " (%s)" % str(loc)
190         res.append(info)
191     return ', '.join(res)