Skip to content

Instantly share code, notes, and snippets.

@rbbratta
Created April 19, 2013 17:02
Show Gist options
  • Save rbbratta/5421681 to your computer and use it in GitHub Desktop.
Save rbbratta/5421681 to your computer and use it in GitHub Desktop.
distro_lib_prototype
# Copyright(c) 2013 Intel Corporation.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
#
# The full GNU General Public License is included in this distribution in
# the file called "COPYING".
import platform
import sys
import os
from subprocess import Popen, PIPE, CalledProcessError, check_call
import common
from autotest.client import utils
from autotest.client.site_lib.service_lib import SpecificServiceManager, auto_create_specific_service_command_generator
from autotest.client.site_lib.site_utils import SmartDict
def get_os():
dist, version, _ = platform.dist()
if 'redhat' in dist:
sut_os = RHEL6.version_factory(version)
elif 'fedora' in dist:
sut_os = Fedora.version_factory(version)
elif 'SuSE' in dist:
sut_os = SLES11.version_factory()
elif 'Ubuntu' in dist:
sut_os = Ubuntu()
elif 'debian' in dist:
sut_os = Debian()
else:
sut_os = Linux()
return sut_os
def find_newest_file(files):
stats = sorted(
(os.path.getmtime(f), f) for f in files if os.path.isfile(f))
return stats[-1][1]
class Linux(object):
pass
class FedoraCommon(Linux):
# TODO: why do this here? Why not add attributes afterwords?
# Just one big function that has all the dep logic
components = SmartDict({
'lldpad' : Fedora_LLDPAD(),
'fcoe_utils' : Fedora_FCOE_UTILS(),
'cgdcbxd' : Fedora_CGDCBXD(),
'libhbalinux' : Fedora_LIBHBALINUX(),
})
rpm_lib = RpmLib
@staticmethod
def find_most_recent_kernel_config():
config_files = utils.system_output(
"rpm -ql kernel | grep /boot/config").splitlines()
# how to find the kernel config that belongs to the distro kernel after having compiled a new kernel?
# use RPM to search for kernel RPM name
return find_newest_file(config_files)
class Fedora(FedoraCommon):
def __init__(self, version):
super(Fedora, self).__init__()
self.version = version
@classmethod
def version_factory(cls, version):
_versions = {"18": Fedora}
return _versions[version](version=version)
class RHEL6(FedoraCommon):
def __init__(self):
super(RHEL6, self).__init__()
@classmethod
def version_factory(cls, version):
_versions = {"6.4": RHEL6_4, "6.3": RHEL6_3}
return _versions[version]()
# kernel_name = os.uname()[2]
# kernel_version = int(re.search(r'-(\d+).el6', kernel_name).group(1))
# if kernel_version >= 358:
# return RHEL6_4()
# elif kernel_version >= 279:
# return RHEL6_3()
# else:
# raise ValueError(kernel_name)
class RHEL6_3(RHEL6):
def __init__(self):
super(RHEL6_3, self).__init__()
self.version = "6.3"
class RHEL6_4(RHEL6):
def __init__(self):
super(RHEL6_4, self).__init__()
self.version = "6.4"
class SLES11(Linux):
components = SmartDict({
'lldpad' : SLES_LLDPAD(),
'fcoe_utils' : SLES_FCOE_UTILS(),
'cgdcbxd' : SLES_CGDCBXD(),
'libhbalinux' : SLES_LIBHBALINUX(),
})
rpm_lib = RpmLib
@classmethod
def version_factory(cls):
release_file = open("/etc/SuSE-release")
try:
patchlevel = [l.strip().split('=', 1)[1].strip(
) for l in release_file if "PATCHLEVEL" in l][0]
# do we really need the service pack version?
# version = "{0}-SP{1}".format(version, patchlevel)
finally:
release_file.close()
if patchlevel == '3':
return SLES11_SP3()
else:
raise ValueError(patchlevel)
class SLES11_SP3(Linux):
def __init__(self):
super(SLES11_SP3, self).__init__()
self.version = "SLES11SP3"
@staticmethod
def find_most_recent_kernel_config():
config_files = utils.system_output(
"rpm -ql kernel-default-base | grep /boot/config").splitlines()
return find_newest_file(config_files)
class Ubuntu(Linux):
def __init__(self):
super(Ubuntu, self).__init__()
class Debian(Linux):
def __init__(self):
super(Debian, self).__init__()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment