tahoe-lafs/src/allmydata/__init__.py

146 lines
5.2 KiB
Python
Raw Normal View History

"""
Decentralized storage grid.
maintainer web site: U{http://allmydata.com/}
community web site: U{http://allmydata.org/}
"""
__version__ = "unknown"
try:
from _version import __version__
except ImportError:
# We're running in a tree that hasn't run "./setup.py darcsver", and didn't
# come with a _version.py, so we don't know what our version is. This should
# not happen very often.
pass
hush_pyflakes = __version__
del hush_pyflakes
import _auto_deps
_auto_deps.require_auto_deps()
import platform, re, subprocess
_distributor_id_cmdline_re = re.compile("(?:Distributor ID)?:?\s*(.*)", re.I)
_release_cmdline_re = re.compile("(?:Release)?:?\s*(.*)", re.I)
_distributor_id_file_re = re.compile("(?:DISTRIB_ID.*=)?\s*(.*)", re.I)
_release_file_re = re.compile("(?:DISTRIB_RELEASE.*=)?\s*(.*)", re.I)
def get_linux_distro():
""" Tries to determine the name of the Linux OS distribution name.
The function tries to execute "lsb_release", as standardized in 2001:
http://refspecs.freestandards.org/LSB_1.0.0/gLSB/lsbrelease.html
The current version of the standard is here:
http://refspecs.freestandards.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/lsbrelease.html
If executing "lsb_release" raises no exception, and returns exit code 0, and
both the "distributor id" and "release" results are non-empty after being
stripped of whitespace, then return a two-tuple containing the information
that lsb_release emitted, as strings. Why do we execute lsb_release as our
first strategy? Because it is the standard.
If executing "lsb_release" doesn't work, then try to parse a file named
/etc/lsb-release to get the same information. Why do that? Because some
distributions (at least Debian/Ubuntu) have /etc/lsb-release in the
"base-files" package (Priority: required) but /usr/bin/lsb_release in the
"lsb-release" package (Priority: important), so it is possible that
/etc/lsb-release is there even if /usr/bin/lsb_release isn't.
If that doesn't work, then invoke platform.dist() and return the first two
elements of the tuple returned by that function.
Returns a tuple (distname,version). Distname is what LSB calls a
"distributor id", e.g. "Ubuntu". Version is what LSB calls a "release",
e.g. "8.04".
A version of this has been submitted to python as a patch for the standard
library module "platform":
http://bugs.python.org/issue3937
"""
_distname = None
_version = None
try:
p = subprocess.Popen(["lsb_release", "--id"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
rc = p.wait()
if rc == 0:
m = _distributor_id_cmdline_re.search(p.stdout.read())
if m:
_distname = m.group(1).strip()
p = subprocess.Popen(["lsb_release", "--release"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
rc = p.wait()
if rc == 0:
m = _release_cmdline_re.search(p.stdout.read())
if m:
_version = m.group(1).strip()
except EnvironmentError:
pass
if _distname and _version:
return (_distname, _version)
try:
etclsbrel = open("/etc/lsb-release", "rU")
for line in etclsbrel:
m = _distributor_id_file_re.search(line)
if m:
_distname = m.group(1).strip()
m = _release_file_re.search(line)
if m:
_version = m.group(1).strip()
except EnvironmentError:
pass
if _distname and _version:
return (_distname, _version)
return platform.dist()[:2]
def get_platform():
# Our version of platform.platform(), telling us both less and more than the
# Python Standard Library's version does.
# We omit details such as the Linux kernel version number, but we add a
# more detailed and correct rendition of the Linux distribution and
# distribution-version.
if "linux" in platform.system().lower():
return platform.system()+"-"+"_".join(get_linux_distro())+"-"+platform.machine()+"-"+"_".join([x for x in platform.architecture() if x])
else:
return platform.platform()
def get_package_versions():
import OpenSSL, allmydata, foolscap, nevow, platform, pycryptopp, setuptools, simplejson, twisted, zfec
return {
'pyopenssl': OpenSSL.__version__,
'allmydata': allmydata.__version__,
'foolscap': foolscap.__version__,
'nevow': nevow.__version__,
'pycryptopp': pycryptopp.__version__,
'setuptools': setuptools.__version__,
'simplejson': simplejson.__version__,
'twisted': twisted.__version__,
'zfec': zfec.__version__,
'python': platform.python_version(),
'platform': get_platform()
}
def get_package_versions_string():
versions = get_package_versions()
res = []
for p in ["allmydata", "foolscap", "pycryptopp", "zfec", "twisted", "nevow", "python", "platform"]:
if versions.has_key(p):
res.append(str(p) + ": " + str(versions[p]))
del versions[p]
else:
res.append(str(p) + ": UNKNOWN")
for p, v in versions.iteritems():
res.append(str(p) + ": " + str(v))
return ', '.join(res)