]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/__init__.py
setup: add pysqlite and sqlite to get_package_versions()
[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
137     # (Python and platform, and sqlite3 if you are on Python >= 2.5), and
138     # because setuptools might fail to find something even though import
139     # finds it:
140     import OpenSSL, allmydata, foolscap.api, nevow, platform, pycryptopp, setuptools, simplejson, twisted, zfec, zope.interface
141     pysqlitever = None
142     pysqlitefile = None
143     sqlitever = None
144     try:
145         import sqlite3
146     except ImportError:
147         try:
148             from pysqlite2 import dbapi2
149         except ImportError:
150             pass
151         else:
152             pysqlitever = dbapi2.version
153             pysqlitefile = os.path.dirname(dbapi2.__file__)
154             sqlitever = dbapi2.sqlite_version
155     else:
156         pysqlitever = sqlite3.version
157         pysqlitefile = os.path.dirname(sqlite3.__file__)
158         sqlitever = sqlite3.sqlite_version
159
160     d1 = {
161         'pyOpenSSL': (OpenSSL.__version__, os.path.dirname(OpenSSL.__file__)),
162         'allmydata-tahoe': (allmydata.__version__, os.path.dirname(allmydata.__file__)),
163         'foolscap': (foolscap.api.__version__, os.path.dirname(foolscap.__file__)),
164         'Nevow': (nevow.__version__, os.path.dirname(nevow.__file__)),
165         'pycryptopp': (pycryptopp.__version__, os.path.dirname(pycryptopp.__file__)),
166         'setuptools': (setuptools.__version__, os.path.dirname(setuptools.__file__)),
167         'simplejson': (simplejson.__version__, os.path.dirname(simplejson.__file__)),
168         'pysqlite': (pysqlitever, pysqlitefile),
169         'sqlite': (sqlitever, 'unknown'),
170         'zope.interface': ('unknown', os.path.dirname(zope.interface.__file__)),
171         'Twisted': (twisted.__version__, os.path.dirname(twisted.__file__)),
172         'zfec': (zfec.__version__, os.path.dirname(zfec.__file__)),
173         'python': (platform.python_version(), sys.executable),
174         'platform': (get_platform(), None),
175         }
176
177     # But we prefer to get all the dependencies as known by setuptools:
178     import pkg_resources
179     try:
180         d2 = _auto_deps.get_package_versions_from_setuptools()
181     except pkg_resources.DistributionNotFound:
182         # See docstring in _auto_deps.require_auto_deps() to explain why it makes sense to ignore this exception.
183         pass
184     else:
185         d1.update(d2)
186
187     return d1
188
189 def get_package_versions():
190     return dict([(k, v) for k, (v, l) in get_package_versions_and_locations().iteritems()])
191
192 def get_package_locations():
193     return dict([(k, l) for k, (v, l) in get_package_versions_and_locations().iteritems()])
194
195 def get_package_versions_string(show_paths=False):
196     vers_and_locs = get_package_versions_and_locations()
197     res = []
198     for p in ["allmydata-tahoe", "foolscap", "pycryptopp", "zfec", "Twisted", "Nevow", "zope.interface", "python", "platform"]:
199         (ver, loc) = vers_and_locs.get(p, ('UNKNOWN', 'UNKNOWN'))
200         info = str(p) + ": " + str(ver)
201         if show_paths:
202             info = info + " (%s)" % str(loc)
203         res.append(info)
204         if vers_and_locs.has_key(p):
205             del vers_and_locs[p]
206
207     for p, (v, loc) in vers_and_locs.iteritems():
208         info = str(p) + ": " + str(v)
209         if show_paths:
210             info = info + " (%s)" % str(loc)
211         res.append(info)
212     return ', '.join(res)