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