# -*- encoding: utf-8 -*- # Software Management Providers # # Copyright (C) 2012 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 # """ Define job classes representing kinds of jobs of worker process. """ import threading from openlmi.software import util from openlmi.software.yumdb.packageinfo import PackageInfo class YumJob(object): #pylint: disable=R0903 """ Base class for any job, that is processable by YumWorker process. It contains at least a jobid attribute, that must be unique for each job, it's counted from zero a incremented after each creation. """ __slots__ = ('jobid', ) # jobs can be created concurrently from multiple threads, that's # why we need to make its creation thread safe _JOB_ID_LOCK = threading.Lock() _JOB_ID = 0 @staticmethod def _get_job_id(): """ Generates new job ids. It should be called only from constructor of YumJob. Ensures, that each job has a unique number. @return number of jobs created since program start -1 """ with YumJob._JOB_ID_LOCK: val = YumJob._JOB_ID YumJob._JOB_ID += 1 return val def __init__(self): self.jobid = self._get_job_id() @property def job_kwargs(self): """ Jobs are in worker represented be methods with arguments. Those can be obtained from job by calling this property. @return dictionary of keyword arguments of job """ kwargs = {} cls = self.__class__ while not cls in (YumJob, object): for slot in cls.__slots__: if not slot in kwargs: kwargs[slot] = getattr(self, slot) cls = cls.__bases__[0] kwargs.pop('jobid', None) return kwargs def __getstate__(self): ret = self.job_kwargs ret.update(jobid=self.jobid) return ret def __setstate__(self, state): for k, value in state.items(): setattr(self, k, value) class YumBeginSession(YumJob): #pylint: disable=R0903 """ Begin session on YumWorker which ensures that yum database is locked during its lifetime. Sessions can be nested, but the number of YumEndSession jobs must be processed to make the database unlocked. """ pass class YumEndSession(YumJob): #pylint: disable=R0903 """ End the session started with YumBeginSession. If the last active session is ended, database will be unlocked. """ pass class YumGetPackageList(YumJob): #pylint: disable=R0903 """ Job requesing a list of packages. Arguments: kind - supported values are in SUPPORTED_KINDS tuple allow_duplicates - whether multiple packages can be present in result for single (name, arch) of package differing in their version Worker replies with [pkg1, pkg2, ...]. """ __slots__ = ('kind', 'allow_duplicates', 'sort') SUPPORTED_KINDS = ('installed', 'available', 'all') def __init__(self, kind, allow_duplicates, sort=False): YumJob.__init__(self) if not isinstance(kind, basestring): raise TypeError("kind must be a string") if not kind in self.SUPPORTED_KINDS: raise ValueError("kind must be one of {%s}" % ", ".join(self.SUPPORTED_KINDS)) self.kind = kind self.allow_duplicates = bool(allow_duplicates) self.sort = bool(sort) class YumFilterPackages(YumGetPackageList): #pylint: disable=R0903 """ Job similar to YumGetPackageList, but allowing to specify filter on packages. Arguments (plus those in YumGetPackageList): name, epoch, version, release, arch, nevra, envra, evra Some of those are redundant, but filtering is optimized for speed, so supplying all of them won't affect performance. Worker replies with [pkg1, pkg2, ...]. """ __slots__ = ( 'name', 'epoch', 'version', 'release', 'arch', 'nevra', 'envra', 'evra') def __init__(self, kind, allow_duplicates, sort=False, name=None, epoch=None, version=None, release=None, arch=None, nevra=None, evra=None, envra=None): if nevra is not None and not util.RE_NEVRA.match(nevra): raise ValueError("Invalid nevra: %s" % nevra) if evra is not None and not util.RE_EVRA.match(evra): raise ValueError("Invalid evra: %s" % evra) if envra is not None and not util.RE_ENVRA.match(evra): raise ValueError("Invalid envra: %s" % envra) YumGetPackageList.__init__(self, kind, allow_duplicates, sort) self.name = name self.epoch = None if epoch is None else str(epoch) self.version = version self.release = release self.arch = arch self.nevra = nevra self.evra = evra self.envra = envra class YumSpecificPackageJob(YumJob): #pylint: disable=R0903 """ Abstract job taking instance of yumdb.PackageInfo as argument. Arguments: pkg - plays different role depending on job subclass """ __slots__ = ('pkg', ) def __init__(self, pkg): if not isinstance(pkg, PackageInfo): raise TypeError("pkg must be instance of PackageInfo") YumJob.__init__(self) self.pkg = pkg class YumInstallPackage(YumSpecificPackageJob): #pylint: disable=R0903 """ Job requesting installation of specific package. pkg argument should be available. Worker replies with new instance of package. """ pass class YumRemovePackage(YumSpecificPackageJob): #pylint: disable=R0903 """ Job requesting removal of specific package. pkg argument should be installed. """ pass class YumUpdateToPackage(YumSpecificPackageJob): #pylint: disable=R0903 """ Job requesting update to provided specific package. Package is updated to epoch, version and release of this provided available package. Worker replies with new instance of package. """ pass class YumUpdatePackage(YumSpecificPackageJob): #pylint: disable=R0903 """ Job requesting update of package, optionally reducing possible candidate packages to ones with specific evr. Arguments: to_epoch, to_version, to_release The arguments more given, the more complete filter of candidates. Worker replies with new instance of package. """ __slots__ = ('to_epoch', 'to_version', 'to_release') def __init__(self, pkg, to_epoch=None, to_version=None, to_release=None): if not isinstance(pkg, PackageInfo): raise TypeError("pkg must be instance of yumdb.PackageInfo") YumSpecificPackageJob.__init__(self, pkg) self.to_epoch = to_epoch self.to_version = to_version self.to_release = to_release class YumCheckPackage(YumSpecificPackageJob): #pylint: disable=R0903 """ Request verification information for instaled package and its files. Worker replies with new instance of yumdb.PackageCheck. """ def __init__(self, pkg): YumSpecificPackageJob.__init__(self, pkg) if not pkg.installed: raise ValueError("package must be installed to check it")