Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_lsb_release_nomodules(self):
self._setup_for_distro(os.path.join(TESTDISTROS, 'lsb',
'ubuntu14_nomodules'))
distroi = distro.LinuxDistribution(True, 'non', 'non')
self.assertEqual(distroi.id(), 'ubuntu')
self.assertEqual(distroi.name(), 'Ubuntu')
self.assertEqual(distroi.name(pretty=True), 'Ubuntu 14.04.3 LTS')
self.assertEqual(distroi.version(), '14.04')
self.assertEqual(distroi.version(pretty=True), '14.04 (trusty)')
self.assertEqual(distroi.version(best=True), '14.04.3')
self.assertEqual(distroi.like(), '')
self.assertEqual(distroi.codename(), 'trusty')
def test_arch_release(self):
self._setup_for_distro(os.path.join(DISTROS, 'arch'))
distroi = distro.LinuxDistribution()
self.assertEqual(distroi.id(), 'arch')
self.assertEqual(distroi.name(), 'Arch Linux')
self.assertEqual(distroi.name(pretty=True), 'Arch Linux')
# Arch Linux has a continuous release concept:
self.assertEqual(distroi.version(), '')
self.assertEqual(distroi.version(pretty=True), '')
self.assertEqual(distroi.version(best=True), '')
self.assertEqual(distroi.like(), '')
self.assertEqual(distroi.codename(), '')
# Test the info from the searched distro release file
# Does not have one; The empty /etc/arch-release file is not
# considered a valid distro release file:
self.assertEqual(distroi.distro_release_file, '')
self.assertEqual(len(distroi.distro_release_info()), 0)
def test_linux_distribution(self):
_distro = distro.LinuxDistribution(False, self.ubuntu14_os_release)
i = _distro.linux_distribution()
assert i == ('Ubuntu', '14.04', 'Trusty Tahr')
def test_kvmibm1_dist_release(self):
distro_release = os.path.join(DISTROS, 'kvmibm1', 'etc',
'base-release')
distroi = distro.LinuxDistribution(False, 'non', distro_release)
self.assertEqual(distroi.id(), 'base')
self.assertEqual(distroi.name(), 'KVM for IBM z Systems')
self.assertEqual(distroi.name(pretty=True), 'KVM for IBM z Systems 1.1.1 (Z)')
self.assertEqual(distroi.version(), '1.1.1')
self.assertEqual(distroi.version(pretty=True), '1.1.1 (Z)')
self.assertEqual(distroi.version(best=True), '1.1.1')
self.assertEqual(distroi.like(), '')
self.assertEqual(distroi.codename(), 'Z')
def test_lsb_release_rc130(self):
self._setup_for_distro(os.path.join(TESTDISTROS, 'lsb', 'lsb_rc130'))
try:
distroi = distro.LinuxDistribution(True, 'non', 'non')
exc = None
except Exception as _exc:
exc = _exc
self.assertEqual(isinstance(exc, subprocess.CalledProcessError), True)
self.assertEqual(exc.returncode, 130)
def _test_attr(self, info_method, attr_method):
for dist in DISTROS:
self._setup_for_distro(os.path.join(DISTROS_DIR, dist))
_distro = distro.LinuxDistribution()
info = getattr(_distro, info_method)()
for key in info.keys():
try:
assert info[key] == getattr(_distro, attr_method)(key)
except AssertionError:
print("distro: {0}, key: {1}".format(dist, key))
def test_debian8_release(self):
self._setup_for_distro(os.path.join(DISTROS, 'debian8'))
distroi = distro.LinuxDistribution()
self.assertEqual(distroi.id(), 'debian')
self.assertEqual(distroi.name(), 'Debian GNU/Linux')
self.assertEqual(distroi.name(pretty=True), 'Debian GNU/Linux 8 (jessie)')
self.assertEqual(distroi.version(), '8')
self.assertEqual(distroi.version(pretty=True), '8 (jessie)')
self.assertEqual(distroi.version(best=True), '8.2')
self.assertEqual(distroi.like(), '')
self.assertEqual(distroi.codename(), 'jessie')
# Test the info from the searched distro release file
# Does not have one:
self.assertEqual(distroi.distro_release_file, '')
self.assertEqual(len(distroi.distro_release_info()), 0)
def test_rhel7_os_release(self):
os_release = os.path.join(DISTROS, 'rhel7', 'etc', 'os-release')
distroi = distro.LinuxDistribution(False, os_release, 'non')
self.assertEqual(distroi.id(), 'rhel')
self.assertEqual(distroi.name(), 'Red Hat Enterprise Linux Server')
self.assertEqual(
distroi.name(pretty=True),
'Red Hat Enterprise Linux Server 7.0 (Maipo)')
self.assertEqual(distroi.version(), '7.0')
self.assertEqual(distroi.version(pretty=True), '7.0 (Maipo)')
self.assertEqual(distroi.version(best=True), '7.0')
self.assertEqual(distroi.like(), 'fedora')
self.assertEqual(distroi.codename(), 'Maipo')
matches = _DISTRO_RELEASE_CONTENT_REVERSED_PATTERN.match(
line.strip()[::-1])
distro_info = {}
if matches:
# regexp ensures non-None
distro_info['name'] = matches.group(3)[::-1]
if matches.group(2):
distro_info['version_id'] = matches.group(2)[::-1]
if matches.group(1):
distro_info['codename'] = matches.group(1)[::-1]
elif line:
distro_info['name'] = line.strip()
return distro_info
_distro = LinuxDistribution()
def main():
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
parser = argparse.ArgumentParser(description="OS distro info tool")
parser.add_argument(
'--json',
'-j',
help="Output in machine readable format",
action="store_true")
args = parser.parse_args()
if args.json:
ret = pipmain(["install", "--user"] + packages)
logging.debug("Pip return code was %d", ret)
if not ret:
import importlib
for package in packages:
try:
globals()[package] = importlib.import_module(package)
except ImportError:
logging.error("Failed to import %s", package)
logging.warning("Install appeared succesful - subsequent run may succeed")
logging.debug("%s", e, exc_info=True, stack_info=True)
return False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
globals()["DISTRO"] = distro.LinuxDistribution().id()
return not ret