-import ast
import re
DISTRO_CODENAME_MAP = {
codename,
))
- @classmethod
- def from_python(cls, python_val):
- """
- Parse output from platform.linux_distribution() and populate attributes
-
- Given a tuple or str()'ed tuple like this:
- ('Ubuntu', '14.04', 'trusty')
-
- Attributes will be:
- name = 'ubuntu'
- version = '14.04'
- codename = 'trusty'
- Additionally, we set the package type:
- package_type = 'deb'
- """
- if not isinstance(python_val, tuple):
- python_val = ast.literal_eval(python_val)
-
- (name, version, codename) = python_val
- name = name.lower().replace(' ', '')
- if name.startswith('redhat'):
- name = 'rhel'
- elif name.startswith('centos'):
- name = 'centos'
- elif name.startswith('fedora'):
- name = 'fedora'
- elif name.startswith('opensuse'):
- name = 'opensuse'
- obj = cls(name=name, version=version, codename=codename.lower())
- return obj
-
@classmethod
def from_lsb_release(cls, lsb_release_str):
"""
@property
def os(self):
if not hasattr(self, '_os'):
- proc = self.run(
- args=[
- 'python', '-c',
- 'import platform; print platform.linux_distribution()'],
- stdout=StringIO(), stderr=StringIO(), check_status=False)
- if proc.exitstatus == 0:
- self._os = OS.from_python(proc.stdout.getvalue().strip())
- return self._os
-
proc = self.run(args=['cat', '/etc/os-release'], stdout=StringIO(),
stderr=StringIO(), check_status=False)
if proc.exitstatus == 0:
BUG_REPORT_URL="http://bugs.debian.org/"
""")
- str_ubuntu_12_04_python = "('Ubuntu', '12.04', 'precise')"
-
str_ubuntu_12_04_lsb_release = dedent("""
Distributor ID: Ubuntu
Description: Ubuntu 12.04.4 LTS
Codename: Santiago
""")
- str_rhel_7_python = "('Red Hat Enterprise Linux Server', '7.0', 'Maipo')"
-
str_rhel_7_lsb_release = dedent("""
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: RedHatEnterpriseServer
assert os.codename == 'wheezy'
assert os.package_type == 'deb'
- def test_ubuntu_12_04_python(self):
- os = OS.from_python(self.str_ubuntu_12_04_python)
- assert os.name == 'ubuntu'
- assert os.version == '12.04'
- assert os.codename == 'precise'
- assert os.package_type == 'deb'
-
def test_ubuntu_12_04_lsb_release(self):
os = OS.from_lsb_release(self.str_ubuntu_12_04_lsb_release)
assert os.name == 'ubuntu'
assert os.codename == 'santiago'
assert os.package_type == 'rpm'
- def test_rhel_7_python(self):
- os = OS.from_python(self.str_rhel_7_python)
- assert os.name == 'rhel'
- assert os.version == '7.0'
- assert os.codename == 'maipo'
- assert os.package_type == 'rpm'
-
def test_rhel_7_lsb_release(self):
os = OS.from_lsb_release(self.str_rhel_7_lsb_release)
assert os.name == 'rhel'