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