diff options
Diffstat (limited to 'src/software/openlmi/software/util/__init__.py')
-rw-r--r-- | src/software/openlmi/software/util/__init__.py | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/software/openlmi/software/util/__init__.py b/src/software/openlmi/software/util/__init__.py index 2ebe827..e48ea29 100644 --- a/src/software/openlmi/software/util/__init__.py +++ b/src/software/openlmi/software/util/__init__.py @@ -1,3 +1,4 @@ +# -*- encoding: utf-8 -*- # Software Management Providers # # Copyright (C) 2012 Red Hat, Inc. All rights reserved. @@ -18,3 +19,136 @@ # # Authors: Michal Minar <miminar@redhat.com> # + +"""Common utilities for LMI_Software* providers +""" + +import platform +import re + +RE_EVRA = re.compile( + r'^(?P<epoch>\d+):(?P<ver>[^-]+)-(?P<rel>.+)\.(?P<arch>[^.]+)$') +RE_NEVRA = re.compile( + r'^(?P<name>.+)-(?P<evra>(?P<epoch>\d+):(?P<ver>[^-]+)' + r'-(?P<rel>.+)\.(?P<arch>[^.]+))$') +RE_NEVRA_OPT_EPOCH = re.compile( + r'^(?P<name>.+)-(?P<evra>((?P<epoch>\d+):)?(?P<ver>[^-]+)' + r'-(?P<rel>.+)\.(?P<arch>[^.]+))$') +RE_ENVRA = re.compile( + r'^(?P<epoch>\d+):(?P<name>.+)-(?P<evra>(?P<ver>[^-]+)' + r'-(?P<rel>.+)\.(?P<arch>[^.]+))$') + +def _get_distname(): + """ + @return name of linux distribution + """ + if hasattr(platform, 'linux_distribution'): + return platform.linux_distribution( + full_distribution_name=False)[0].lower() + else: + return platform.dist()[0].lower() + + +def get_target_operating_system(): + """ + @return (val, text). + Where val is a number from ValueMap of TargetOperatingSystem property + of CIM_SoftwareElement class and text is its testual representation. + """ + + system = platform.system() + if system.lower() == 'linux': + try: + val, dist = \ + { 'redhat' : (79, 'RedHat Enterprise Linux') + , 'suse' : (81, 'SUSE') + , 'mandriva' : (88, 'Mandriva') + , 'ubuntu' : (93, 'Ubuntu') + , 'debian' : (95, 'Debian') + }[_get_distname()] + except KeyError: + linrel = platform.uname()[2] + if linrel.startswith('2.4'): + val, dist = (97, 'Linux 2.4.x') + elif linrel.startswith('2.6'): + val, dist = (99, 'Linux 2.6.x') + else: + return (36, 'LINUX') # no check for x86_64 + if platform.machine() == 'x86_64': + val += 1 + dist += ' 64-Bit' + return (val, dist) + elif system.lower() in ('macosx', 'darwin'): + return (2, 'MACOS') + # elif system.lower() == 'windows': # no general value + else: + return (0, 'Unknown') + +def check_target_operating_system(system): + """ + @return if param system matches current target operating system + """ + if isinstance(system, basestring): + system = int(system) + if not isinstance(system, (int, long)): + raise TypeError("system must be either string or integer, not %s" % + system.__class__.__name__) + tos = get_target_operating_system() + if system == tos: + return True + if system == 36: # linux + if platform.system().lower() == "linux": + return True + if ( system >= 97 and system <= 100 # linux 2.x.x + and platform.uname()[2].startswith('2.4' if system < 99 else '2.6') + # check machine + and ( bool(platform.machine().endswith('64')) + == bool(not (system % 2)))): + return True + return False + +def nevra2filter(nevra): + """ + Takes either regexp match object resulting from RE_NEVRA match or + a nevra string. + @return dictionary with package filter key-value pairs made from nevra + """ + if isinstance(nevra, basestring): + match = RE_NEVRA_OPT_EPOCH.match(nevra) + elif nevra.__class__.__name__.lower() == "sre_match": + match = nevra + else: + raise TypeError("nevra must be either string or regexp match object") + epoch = match.group("epoch") + if not epoch or match.group("epoch") == "(none)": + epoch = "0" + return { "name" : match.group("name") + , "epoch" : epoch + , "version" : match.group("ver") + , "release" : match.group("rel") + , "arch" : match.group("arch") + } + +def make_nevra(name, epoch, ver, rel, arch, with_epoch='NOT_ZERO'): + """ + @param with_epoch may be one of: + "NOT_ZERO" - include epoch only if it's not zero + "ALWAYS" - include epoch always + "NEVER" - do not include epoch at all + """ + estr = '' + if with_epoch.lower() == "always": + estr = epoch + elif with_epoch.lower() == "not_zero": + if epoch != "0": + estr = epoch + if len(estr): + estr += ":" + return "%s-%s%s-%s.%s" % (name, estr, ver, rel, arch) + +def pkg2nevra(pkg, with_epoch='NOT_ZERO'): + """ + @return nevra string made of pkg + """ + return make_nevra(pkg.name, pkg.epoch, pkg.version, + pkg.release, pkg.arch, with_epoch) |