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