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