#-*- 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 os import re import cl_overriding from cl_vars_share import varsShare from cl_kernel import cl_kernel 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, varsShare): def get_cl_kernel_src_path(self): defaultKernelPath = '/usr/src' priorityDir = path.join(defaultKernelPath,"linux") if not access(defaultKernelPath,R_OK): return "" kernel = cl_kernel() kernelDirs = filter(kernel._testKernelDirectory, map(lambda x:path.join(defaultKernelPath,x), os.listdir(defaultKernelPath))) if kernelDirs: if priorityDir in kernelDirs: return priorityDir else: return path.join(defaultKernelPath,kernelDirs[0]) return "" def get_cl_kernel_full_ver(self): if self.Get('cl_kernel_config') \ and os.access(self.Get('cl_kernel_config'),os.R_OK): localVer = self.getValueFromConfig( self.Get('cl_kernel_config'), 'CONFIG_LOCALVERSION') if not localVer: localVer="" else: localVer = "" kernelMakefile = path.join(self.Get('cl_kernel_src_path'),'Makefile') if os.access(kernelMakefile,os.R_OK): extraVer = "" for line in open(kernelMakefile,'r'): if "EXTRAVERSION" in line: extraVer = line.partition('=')[2].strip() break else: localVer = "" return "%s%s%s"%(self.Get('cl_kernel_ver'), extraVer, localVer) def get_cl_kernel_suffix(self): kernelFullVer = self.Get('cl_kernel_full_ver') fullVerWithoutCalculate = kernelFullVer.replace("-calculate","") return "%s-%s-%s"%(fullVerWithoutCalculate, self.Get('os_arch_machine'), self.Get('os_linux_shortname')) def get_cl_kernel_ver(self): kernelMakefile = path.join(self.Get('cl_kernel_src_path'),'Makefile') reVerPart = re.compile( "^(?:VERSION|^PATCHLEVEL|^SUBLEVEL)\s*=",re.I) if access(kernelMakefile,R_OK): try: makeVer = \ "%(VERSION)s.%(PATCHLEVEL)s.%(SUBLEVEL)s"% \ dict(map(lambda x:(x[0].strip(),x[2].strip()), map(lambda x:x.partition('='), filter(reVerPart.search, open(kernelMakefile,'r'))))) return makeVer except: pass return "" def get_cl_kernel_config(self): kernelDirs = self.Get('cl_kernel_config_path') if not access(kernelDirs,R_OK): return "" shortVerSearch = re.search("^\d+\.\d+.\d+",self.Get('cl_kernel_ver'),re.I) if shortVerSearch: shortVer = shortVerSearch.group() else: return "" configName = "config-%(system)s-%(march)s-%(ver)s" % \ {'system':self.Get('os_linux_system'), 'march':self.Get('os_arch_machine'), 'ver':shortVer} if path.exists(path.join(kernelDirs,configName)): return path.join(kernelDirs,configName) return "" def get_os_builder_makeopts(self): makeconf = '/etc/make.conf' if access(makeconf,R_OK): makeopts = self.getValueFromConfig('/etc/make.conf',"MAKEOPTS") if makeopts: return makeopts return "" def get_cl_kernel_boot_path(self): return path.join(self.Get('cl_kernel_install_path'),"boot") def get_cl_kernel_cache_path(self): cacheDir = self.getValueFromConfig('/etc/genkernel.conf','CACHE_DIR') if cacheDir == False: return "/var/cache/genkernel" else: return cacheDir reChroot = re.compile("^(?:_=.*bin/chroot|CHROOT=on)$",re.S) def isChroot(self,pid): """Recursive detect chroot mode""" if not os.access('/proc/%d/environ'%pid,R_OK): return False if filter(self.reChroot.match, open('/proc/%d/environ'%pid,'r').read().split('\x00')): return True else: ppid = filter(lambda x:x.startswith('PPid:'), open('/proc/%d/status'%pid,'r').readlines())[0] ppid = int(ppid.strip().partition('\t')[2]) if ppid == 0: return False else: return self.isChroot(ppid) def get_cl_chroot_status(self): """Detect chroot mode by mtab content""" try: return "on" if self.isChroot(os.getpid()) else "off" except: return "off" def get_cl_builder_iso_action(self): """Need perform templates for builder:iso""" if self.Get('cl_action')=='iso': return "up" return "" def get_cl_builder_squash_action(self): """Need perform templates for builder:squahs""" if self.Get('cl_action')=='squash': return "up" return "" def get_cl_builder_squash_path(self): """Directory of system for livecd.squashfs""" return "/mnt/builder" def get_cl_builder_iso_path(self): """Directory for iso building""" return "/var/calculate/tmp/iso" def get_cl_builder_kernel(self): """Kernel image""" 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): 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""" linuxShortName = self.Get("os_builder_linux_shortname") return self.dictLinuxName.get(linuxShortName,"Linux") def get_os_builder_linux_subname(self): """Build system subname""" linuxShortName = self.Get("os_builder_linux_shortname") return self.dictLinuxSubName.get(linuxShortName,"") def get_os_builder_linux_shortname(self): """Build system shortname""" 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""" 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""" 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""" 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)""" 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)""" 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""" 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 ""