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