#!/usr/bin/env python import os import sys import getopt from subprocess import Popen, PIPE, STDOUT KDIR="/usr/src/linux-headers-" ACTION=None CURDIR=os.path.realpath('.') PKGPATCHES=CURDIR+"/patches/" DESTDIR="/" TARGET=None debug=False try: opts, args = getopt.getopt(sys.argv[1:], ":hd", ["gencontrol", "debug", "compile", "install", "destdir=", "target="]) except getopt.error, msg: print msg print "for command line options use build.py --help" sys.exit(2) # process options for o, a in opts: if o in ("-d", "--debug"): debug = True if o == "--gencontrol": ACTION = "gencontrol" if o == "--compile": ACTION = "compile" if o == "--install": ACTION = "install" if o == "--destdir": DESTDIR = a if o == "--target": TARGET = a if o in ("-h", "--help"): usage() sys.exit() def print_debug(txt): if debug: print "DEBUG: %s" %txt def sh(cmd, verbose=True, canfail=False): print_debug("sh: %s" %cmd) installing=True p = Popen(cmd, shell=True, bufsize=0, stdout=PIPE, stderr=STDOUT, close_fds=True) while installing: if p.poll() != None: installing=False line=p.stdout.readline() if line.strip() == '': continue print_debug( line.replace('\n', '') ) if canfail: if p.returncode == 2: print("ERROR: Command return code 2 and canfail is True") sys.exit(1) print_debug( "\n\n" ) class Builder: def __init__(self): self.config={} self.KERNELS=None self.MODULES=None self.PKGNAME=None self.PKGVERSION=None self.PKGDISTRO=None self.PKGBINDEPS=[] self.PKGSOURCEDEPS=[] def getdata(self): """ read debian/changelog """ if not os.path.isfile("debian/changelog"): print "debian/changelog not found" sys.exit(2) f=open("debian/changelog",'r') data=f.readline() f.close() (self.PKGNAME, self.PKGVERSION, self.PKGDISTRO, self.URGENCY)=data.split() self.PKGVERSION=self.PKGVERSION.replace('(','').replace(')','') self.PKGDISTRO=self.PKGDISTRO.replace(';','') if TARGET: self.PKGDISTRO=TARGET def getkernels(self): if not os.path.isfile("common.mk"): print "common.mk not found" sys.exit(2) f=open("common.mk", 'r') data=f.readlines() f.close() #print "searching KERNEL_%s" %(self.PKGDISTRO) for line in data: if line.find("KERNEL_%s" %(self.PKGDISTRO) ) == 0: self.KERNELS=line.split('=')[1].split() def getmodules(self): if not os.path.isfile("common.mk"): print "common.mk not found" sys.exit(2) f=open("common.mk", 'r') data=f.readlines() f.close() for line in data: if line.find("TCOS_MODULES_%s" %(self.PKGDISTRO) ) == 0: self.MODULES=line.split('=')[1].replace('"','').split() def generatedeps(self, kernel): self.PKGSOURCEDEPS=[] self.PKGBINDEPS=[] for module in self.MODULES: if module == "cdfs": self.PKGSOURCEDEPS.append("%s-src" %module) else: self.PKGSOURCEDEPS.append("%s-source" %module) self.PKGBINDEPS.append("%s-modules-%s" %(module, kernel) ) def clean(self): sh("rm -rf build/") def patchmodule(self,modname,kversion): """ patch sources """ file1="%s%s.patch" %(PKGPATCHES,modname) file2="%s%s-%s.patch" %(PKGPATCHES,modname,kversion) #print_debug("patching(%s), looking for '%s' and '%s'" %(modname, file1, file2)) if os.path.isfile(file1): print (" **PATCH** %s" %file1) sh("cd build/%s && patch -p1 < %s" %(kversion,file1), canfail=True) if os.path.isfile(file2): print (" **PATCH** %s" %file2) sh("cd build/%s && patch -p1 < %s" %(kversion,file2), canfail=True) def buildmodule(self, modname, kversion): """ build_tcos_module: cd $(MAINDIR) mkdir -p build tar -jxf /usr/src/$(MO).tar.bz2 -C $(CURDIR)/build/ make pre_patch MO=$(MO) cd build/modules/$(MO) && \ $(MAKE) KDIR=$(KSRC) KVER=$(KVERS) """ sh("mkdir -p %s/build/%s" %(CURDIR,kversion) ) sh("tar -jxf /usr/src/%s.tar.bz2 -C %s/build/%s" %(modname,CURDIR,kversion)) self.patchmodule(modname,kversion) print(" *** Compiling module %s for kernel %s ..." %(modname,kversion) ) if modname == "aufs": sh("cd build/%s/modules/%s && make ARCH=i386 -C %s M=%s/build/%s/modules/%s modules" %(kversion, modname, KDIR+kversion, CURDIR, kversion, modname)) else: sh("cd build/%s/modules/%s && make ARCH=i386 KDIR=%s KVER=%s KVERS=%s" %(kversion, modname, KDIR+kversion, kversion, kversion)) def installmodule(self, modname, kversion): """ install_module: cd build/modules/$(MO) && \ mkdir -p $(DESTDIR) && \ $(MAKE) KDIR=$(KSRC) KVER=$(KVERS) DESTDIR=$(DESTDIR) install """ destdir=DESTDIR+ "/" + self.PKGNAME + "-" + kversion print ("Installing %s of kernel %s in DESTDIR=%s" %(modname, kversion, destdir)) sh("mkdir -p %s" %(destdir)) if modname == "aufs": # add install: target in Makefile f=open("%s/build/%s/modules/aufs/Makefile"%(CURDIR,kversion), 'a') f.write("\n\ninstall:") f.write("\n\tinstall -D -m 0644 aufs.ko $(DESTDIR)/lib/modules/$(KVER)/kernel/fs/aufs/aufs.ko") f.close() make="make ARCH=i386 KVER=%s KVERS=%s M=%s/build/%s/modules/aufs DESTDIR=%s install" %(kversion,kversion,CURDIR,kversion,destdir) else: make="make ARCH=i386 KDIR=%s KVER=%s KVERS=%s DESTDIR=%s install" %(KDIR+kversion, kversion, kversion, destdir) cmd="cd build/%s/modules/%s && " %(kversion, modname) + make #print cmd sh( cmd , canfail=True) def parsecontrol(self): f=open("debian/source.modules.in", 'r') data=f.readlines() f.close() newdata=[] headers=[] for kernel in self.KERNELS: headers.append("linux-headers-%s" %kernel) for line in data: line=line.replace("_HEADERS_PKGS_", ", ".join(headers) ) line=line.replace("_SOURCE_PKGS_", ", ".join(self.PKGSOURCEDEPS) ) newdata.append(line) for kernel in self.KERNELS: arch=kernel.split('-')[-1] if arch == "bigmem": arch=kernel.split('-')[-2]+"-"+kernel.split('-')[-1] conflicts=[] for module in self.MODULES: conflicts.append("%s-modules-%s" %(module, kernel)) f=open("debian/control.modules.in", 'r') data=f.readlines() f.close() for line in data: line=line.replace("_KVERS_", kernel) line=line.replace("_BIN_PKGS_", ", ".join(conflicts) ) line=line.replace("_ARCH_", arch) newdata.append(line) #print "".join(newdata) f=open("debian/control", 'w') f.write("".join(newdata)) f.close() print " *** Created debian/control " for kernel in self.KERNELS: newdata=[] f=open("debian/tcos-extra-modules-_KVERS_.postinst",'r') data=f.readlines() f.close() for line in data: line=line.replace("_KVERS_", kernel) newdata.append(line) f=open("debian/tcos-extra-modules-%s.postinst" %kernel,'w') f.write("".join(newdata)) f.close() print " *** Created debian/tcos-extra-modules-%s.postinst" %kernel newdata=[] f=open("debian/tcos-extra-modules-_KVERS_.postrm",'r') data=f.readlines() f.close() for line in data: line=line.replace("_KVERS_", kernel) newdata.append(line) f=open("debian/tcos-extra-modules-%s.postrm" %kernel,'w') f.write("".join(newdata)) f.close() print " *** Created debian/tcos-extra-modules-%s.postrm" %kernel if __name__ == "__main__": app=Builder() #app.clean() app.getdata() app.getkernels() app.getmodules() app.generatedeps("2.6.22-1-486") if ACTION == "gencontrol": app.parsecontrol() elif ACTION == "compile": for kernel in app.KERNELS: print " *** working on kernel %s" %kernel for module in app.MODULES: print " *** working on module %s" %module app.buildmodule(module, kernel) elif ACTION == "install": for kernel in app.KERNELS: print " *** working on kernel %s" %kernel for module in app.MODULES: print " *** working on module %s" %module app.installmodule(module, kernel) else: print "ERROR: unknow action" print """ ./build.py gencontrol ./build.py compile ./build.py install """ #app=Builder() #app.getdata() #app.getkernels() #app.getmodules() #KVERS="2.6.29-2-686" #app.generatedeps(KVERS) #app.buildmodule("aufs", KVERS) #app.installmodule("aufs", KVERS)