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