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