From 5b81189ab146e6949937142ec4843b0992d73ef2 Mon Sep 17 00:00:00 2001 From: Mike Hiretsky Date: Mon, 27 Sep 2010 18:05:15 +0400 Subject: [PATCH] Add cl-image script. Added fill methods for all variables. --- pym/cl_fill_builder.py | 103 +++++++++++++++++++++++++++++++++-------- pym/cl_image_cmd.py | 74 +++++++++++++++++++++++++++++ pym/cl_vars_builder.py | 2 +- scripts/cl-image | 54 +++++++++++++++++++++ 4 files changed, 213 insertions(+), 20 deletions(-) create mode 100644 pym/cl_image_cmd.py create mode 100644 scripts/cl-image diff --git a/pym/cl_fill_builder.py b/pym/cl_fill_builder.py index a26b774..6163b0e 100644 --- a/pym/cl_fill_builder.py +++ b/pym/cl_fill_builder.py @@ -17,12 +17,15 @@ import os import re import cl_overriding -from cl_datavars import glob_attr +from cl_vars_share import varsShare from cl_kernel import cl_kernel -from os import access, R_OK +from os import access, R_OK,W_OK from os import path +from cl_distr import Distributive +from cl_utils import getTupleVersion +from operator import itemgetter -class fillVars(object, glob_attr): +class fillVars(object, varsShare): def get_cl_kernel_src_path(self): defaultKernelPath = '/usr/src' @@ -147,60 +150,122 @@ class fillVars(object, glob_attr): def get_cl_builder_iso_action(self): """Need perform templates for builder:iso""" - pass + if self.Get('cl_action')=='iso': + return "up" + return "" def get_cl_builder_squash_action(self): """Need perform templates for builder:squahs""" - pass + if self.Get('cl_action')=='squash': + return "up" + return "" def get_cl_builder_squash_path(self): """Directory of system for livecd.squashfs""" - pass + return "/mnt/builder" def get_cl_builder_iso_path(self): """Directory for iso building""" - pass + return "/var/calculate/tmp/iso" def get_cl_builder_kernel(self): """Kernel image""" - pass + bootdir = path.join(self.Get('cl_builder_squash_path'),'boot') + modulesdir = path.join(self.Get('cl_builder_squash_path'),'lib/modules') + validKernel = os.listdir(modulesdir) + kernelFiles = self.getFilesByType(bootdir,"Linux kernel") + kernelsWithVer = \ + map(lambda x:(x[0],(getTupleVersion("".join(x[1].groups()[0:3:2])), + path.getmtime(x[0]))), + filter(lambda x:x[1] and x[1].group() in validKernel, + map(lambda x:(x[0],self.reFindVer.search(x[1])), + kernelFiles))) + if kernelsWithVer: + return path.split(max(kernelsWithVer,key=itemgetter(1))[0])[-1] + return "" def get_cl_builder_initrd_install(self): - """Initramfs image""" - pass + return self.getInitrd(self.Get('os_builder_arch_machine'), + self.Get('os_builder_linux_shortname'), + self.Get('cl_builder_squash_path'), + self.Get('cl_builder_kernel'), + suffix="install") or "" def get_os_builder_linux_name(self): """Build system name""" - pass + linuxShortName = self.Get("os_builder_linux_shortname") + return self.dictLinuxName.get(linuxShortName,"Linux") def get_os_builder_linux_subname(self): """Build system subname""" - pass + linuxShortName = self.Get("os_builder_linux_shortname") + return self.dictLinuxSubName.get(linuxShortName,"") def get_os_builder_linux_shortname(self): """Build system shortname""" - pass + systemRoot = self.Get('cl_builder_squash_path') + return self.getShortnameByMakeprofile(systemRoot) or \ + self.getShortnameByIni(systemRoot) or \ + self.detectOtherShortname(systemRoot) or \ + "Linux" def get_os_builder_linux_ver(self): """Build system ver""" - pass + linuxShortName = self.Get("os_builder_linux_shortname") + rootSystem = self.Get('cl_builder_squash_path') + return self.getVersionFromMetapackage(rootSystem,linuxShortName) or \ + self.getVersionFromCalculateIni(rootSystem) or \ + self.getVersionFromGentooFiles(rootSystem) or \ + self.getVersionFromUname() or "0" def get_os_builder_arch_machine(self): """Build system arch""" - pass + rootSystem = self.Get('cl_builder_squash_path') + lib64path = path.join(rootSystem,"lib64") + return 'x86_64' if path.lexists(lib64path) else "i686" def get_os_builder_linux_system(self): """Build linux system type""" - pass + shortName = self.Get('os_builder_linux_shortname') + return self.dictNameSystem.get(shortName,"") def get_cl_builder_cdname(self): """Cd size specified by name (DVD/CD)""" - pass + squashfile = path.join(self.Get('cl_builder_iso_path'), + self.Get('cl_builder_current_squash')) + if os.access(squashfile,R_OK): + isosize = path.getsize(squashfile)*1.05 + if isosize > 700*1024*1024: + return "DVD" + else: + return "CD" + return "" def get_cl_builder_current_squash(self): """Livecd.squashfs name (may be livecd.squashfs.2 and etc)""" - pass + def getSquashNum(reMatch): + if reMatch.groups()[1] and reMatch.groups()[1].isdigit(): + return int(reMatch.groups()[1]) + else: + return 0 + directory = self.Get('cl_builder_iso_path') + if not os.access(directory,R_OK): + return "" + squashfiles = filter(lambda x:x, + map(Distributive.reLive.search, + os.listdir(directory))) + if squashfiles: + return "livecd.squashfs.%d"% \ + (getSquashNum(max(squashfiles, key=getSquashNum).group())+1) + return "livecd.squashfs" def get_cl_builder_image(self): """Iso image full path""" - pass + paths = filter(lambda x:os.access(x,W_OK), + self.Get('cl_builder_image_path')) + if paths: + return path.join(paths[0],"%(shortname)s-%(ver)s-%(march)s.iso"% + {'shortname':self.Get('os_builder_linux_shortname').lower(), + 'ver':self.Get('os_builder_linux_ver'), + 'march':self.Get('os_builder_arch_machine')}) + return "" diff --git a/pym/cl_image_cmd.py b/pym/cl_image_cmd.py new file mode 100644 index 0000000..b67817f --- /dev/null +++ b/pym/cl_image_cmd.py @@ -0,0 +1,74 @@ +#-*- coding: utf-8 -*- + +# Copyright 2010 Calculate Ltd. http://www.calculate-linux.org +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from cl_builder import cl_builder, __app__, __version__ +from cl_opt import opt +from cl_share_cmd import share_cmd +import os +from os import path +from os import access,R_OK +import re +import sys + +from cl_lang import lang +lang().setLanguage(sys.modules[__name__]) + +DESCRIPTION = _("The Calculate Linux image builder") +CMD_OPTIONS = [] +USAGE = _("%prog [options] iso|squash") + +class image_cmd(share_cmd): + """Class for work with cl_builder by console""" + def __init__(self): + self.optobj = opt(package=__app__, + version=__version__, + description=DESCRIPTION, + usage=USAGE, + option_list= CMD_OPTIONS + opt.variable_control + + opt.color_control, + check_values=self.checkOpts) + self.logicObj = cl_builder() + self.commands = ["iso","squash"] + #self.optionsInitrdIncompatible = ["o","no_clean","m","mdadm","lvm", + # "k", "e","dmraid","c","ebuild", + # "symlink"] + + def checkOpts(self, values, args): + """Check values all specified options.""" + if len(args) > 1: + self.optobj.error(_("unrecognized option") + ": %s"% "".join(args)) + if len(args) < 1 and not values.v: + self.optobj.error(_("missing command argument")) + if args and not args[0] in self.commands: + self.optobj.error(_("invalid command: '%s' (choose from %s)")% + (args[0],", ".join(map(lambda x:"'%s'"%x,self.commands)))) + self.logicObj.clVars.Set('cl_action',args[0],True) + + self.optobj.checkVarSyntax(values) + return (values, args) + + def performActionByCommand(self,options,args): + action = {'iso':self.makeIso, + 'squash':self.makeSquash} + action[args[0]](options) + + def makeIso(self,options): + """Set specified kernel to default""" + return True + + def makeSquash(self,options): + """Set specified kernel to default""" + return True diff --git a/pym/cl_vars_builder.py b/pym/cl_vars_builder.py index a158016..8ee2abc 100644 --- a/pym/cl_vars_builder.py +++ b/pym/cl_vars_builder.py @@ -96,7 +96,7 @@ class Data: os_builder_linux_shortname = {} # build system ver - os_builder_linux_ver = {} + os_builder_linux_ver = {'mode':'r'} # build system arch os_builder_arch_machine = {} diff --git a/scripts/cl-image b/scripts/cl-image new file mode 100644 index 0000000..9310ecf --- /dev/null +++ b/scripts/cl-image @@ -0,0 +1,54 @@ +#!/usr/bin/python +#-*- coding: utf-8 -*- + +# Copyright 2010 Calculate Ltd. http://www.calculate-linux.org +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import sys +import os +from os import path +sys.path.insert(0, path.abspath('/usr/lib/calculate-2.2/calculate-lib/pym')) +sys.path.insert(0, path.abspath('/usr/lib/calculate-2.2/calculate-install/pym')) +sys.path.insert(0, path.abspath('/usr/lib/calculate-2.2/calculate-builder/pym')) + +from cl_image_cmd import image_cmd + +from cl_lang import lang +tr = lang() +tr.setGlobalDomain('cl_builder') +tr.setLanguage(sys.modules[__name__]) + +if __name__ == "__main__": + image = image_cmd() + image.logicObj.initVars() + # set lang + ret = image.optobj.parse_args() + if ret is False: + sys.exit(1) + options, args = ret + # set color/nocolor for display messages + image.setPrintNoColor(options) + # set values to variables + if not image.setVars(options): + sys.exit(1) + # print variables + if options.v or options.filter or options.xml: + image.printVars(options) + sys.exit(0) + # check root + if not image.isRoot(): + sys.exit(1) + if not image.performActionByCommand(options,args): + sys.exit(1) + sys.exit(0)