From 9c1459bf0e93f45131f97abcf38d9597289f3afb Mon Sep 17 00:00:00 2001 From: Mike Hiretsky Date: Mon, 18 Oct 2010 18:02:53 +0400 Subject: [PATCH] Improve find best distributive image. Also added method for find latest stage. --- pym/cl_distr.py | 55 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/pym/cl_distr.py b/pym/cl_distr.py index e1fcffe..bd6b07f 100644 --- a/pym/cl_distr.py +++ b/pym/cl_distr.py @@ -22,8 +22,9 @@ import types from time import sleep import re import sys +import operator from cl_utils import runOsCommand,isMount,removeDir,typeFile,pathJoin, \ - process,getRunCommands + process,getRunCommands,getTupleVersion from shutil import copyfile,copytree from cl_template import _terms from subprocess import Popen,PIPE,STDOUT @@ -172,21 +173,29 @@ class DistributiveRepository: return match.groupdict() def _getAvailableDistributives(self,system=None,shortname=None,march=None, - version=None): + version=None,op_compare=None): """Get all distributives by filter""" + if op_compare is None: + op_compare = operator.eq + if version: + version = getTupleVersion(version) def distfilter(dist): d = self._getdistrinfo(dist) if not d: return False # check filter conditions - if system and self.system(d['name']) != system or \ - not "name" in d or not "ver" in d or \ - shortname and d['name'].lower() != shortname.lower() or \ - march and d['march'] != march or \ - version and d['ver'] != version: + if system and self.system(d['name']) != system: return False - else: - return True + if not "name" in d or not "ver" in d: + return False + if shortname and d['name'].lower() != shortname.lower(): + return False + if march and d['march'] != march: + return False + if version and not op_compare(getTupleVersion(d['ver']), version): + return False + return True + def listdistr(pathname): if path.exists(path.join(pathname,'etc/make.profile')): return [pathname] @@ -253,10 +262,11 @@ class DistributiveRepository: return self.extcomparator(ext1,ext2) def getBestDistributive(self,system=None,shortname=None,march=None, - version=None,discardType=[]): + version=None,discardType=[], op_compare=None): """Get the actualest distributive""" availDistrs = self._getAvailableDistributives(system,shortname, - march,version) + march,version, + op_compare) availDistrs = filter(lambda x:x[1] and "ext" in x[1] and not x[1]["ext"] in discardType, map(lambda x:(x,self._getdistrinfo(x)), @@ -268,6 +278,29 @@ class DistributiveRepository: else: return None + def _findLatestFile(self,dirs,reMatch,keyfunc): + """Find latest file in dirs, which match by reMatch, + comparable part get by keyfunc""" + existsdirs = filter(path.exists,dirs) + listimgs = reduce(lambda x,y:x + \ + map(lambda x:reMatch.search(path.join(y,x)), + os.listdir(y)), + existsdirs,[]) + listimgs = filter(lambda x:x, listimgs) + if listimgs: + return max(listimgs,key=keyfunc).group() + return "" + + def getBestStage(self,march=None): + """Get latest stage by march""" + if march: + convert = {'x86_64':'amd64'} + march = convert.get(march,march) + reStage = re.compile(r'^.*/stage3-%s-(\d+)\.tar\.bz2$'%march,re.S) + else: + reStage = re.compile(r'^.*/stage3-[^-]+-(\d+)\.tar\.bz2$',re.S) + return self._findLatestFile(self.dirs,reStage,lambda x:x.groups()[0]) + class Distributive(object, SignalInterrupt): """Distributive object. Parent object for all distributive.""" mountError = _("Cann't mount") + " %s:\n%s"