# -*- encoding: utf-8 -*- # Software Management Providers # # Copyright (C) 2012-2013 Red Hat, Inc. All rights reserved. # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # # Authors: Michal Minar # """Common utilities for LMI_Software* providers """ import platform import re import signal RE_EVRA = re.compile( r'^(?P\d+):(?P[^-]+)-(?P.+)\.(?P[^.]+)$') RE_NEVRA = re.compile( r'^(?P.+)-(?P(?P\d+):(?P[^-]+)' r'-(?P.+)\.(?P[^.]+))$') RE_NEVRA_OPT_EPOCH = re.compile( r'^(?P.+)-(?P((?P\d+):)?(?P[^-]+)' r'-(?P.+)\.(?P[^.]+))$') RE_ENVRA = re.compile( r'^(?P\d+):(?P.+)-(?P(?P[^-]+)' r'-(?P.+)\.(?P[^.]+))$') 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) def get_signal_name(signal_num): """ @return name of signal for signal_num argument """ if not isinstance(signal_num, (int, long)): raise TypeError("signal_num must be an integer") try: return dict((v, k) for k, v in signal.__dict__.items())[signal_num] except KeyError: return "UNKNOWN_SIGNAL(%d)" % signal_num