support/package-builder/PackageBuilder.py
2820c61a
 from PackageUtils import PackageUtils
 from Logger import Logger
 from ChrootUtils import ChrootUtils
 from ToolChainUtils import ToolChainUtils
 from CommandUtils import CommandUtils
 import os.path
 from constants import constants
387ea52d
 import shutil
2820c61a
 
 class PackageBuilder(object):
     
90d8acae
     def __init__(self,mapPackageToCycles,listAvailableCyclicPackages,listBuildOptionPackages,pkgBuildOptionFile,logName=None,logPath=None):
2820c61a
         if logName is None:
             logName = "PackageBuilder"
         if logPath is None:
             logPath = constants.logPath
         self.logName=logName
         self.logPath=logPath
         self.logger=Logger.getLogger(logName,logPath)
         self.mapPackageToCycles = mapPackageToCycles
         self.listAvailableCyclicPackages = listAvailableCyclicPackages
         self.listNodepsPackages = ["glibc","gmp","zlib","file","binutils","mpfr","mpc","gcc","ncurses","util-linux","groff","perl","texinfo","rpm","openssl","go"]
90d8acae
         self.listBuildOptionPackages=listBuildOptionPackages
         self.pkgBuildOptionFile=pkgBuildOptionFile
387ea52d
         
a5e4be9f
     def prepareBuildRoot(self,chrootName,isToolChainPackage=False):
518d6a6f
         chrootID=None
         try:
             chrUtils = ChrootUtils(self.logName,self.logPath)
65584c99
             returnVal,chrootID = chrUtils.createChroot(chrootName)
2689a7f2
             self.logger.debug("Created new chroot: " + chrootID)
4720acad
             if not returnVal:
                 raise Exception("Unable to prepare build root")
             tUtils=ToolChainUtils(self.logName,self.logPath)
939126ed
 #            if isToolChainPackage:
 #                tUtils.installCoreToolChainPackages(chrootID)
 #            else:
             tUtils.installToolChain(chrootID)
4720acad
         except Exception as e:
             if chrootID is not None:
2689a7f2
                 self.logger.debug("Deleting chroot: " + chrootID)
4720acad
                 chrUtils.destroyChroot(chrootID)
             raise e
         return chrootID
     
2820c61a
     def findPackageNameFromRPMFile(self,rpmfile):
         rpmfile=os.path.basename(rpmfile)
         releaseindex=rpmfile.rfind("-")
         if releaseindex == -1:
             self.logger.error("Invalid rpm file:"+rpmfile)
             return None
         versionindex=rpmfile[0:releaseindex].rfind("-")
         if versionindex == -1:
             self.logger.error("Invalid rpm file:"+rpmfile)
             return None
         packageName=rpmfile[0:versionindex]
         return packageName
     
     def findInstalledPackages(self,chrootID):
518d6a6f
         pkgUtils = PackageUtils(self.logName,self.logPath)
         listInstalledRPMs=pkgUtils.findInstalledRPMPackages(chrootID)
2820c61a
         listInstalledPackages=[]
         for installedRPM in listInstalledRPMs:
             packageName=self.findPackageNameFromRPMFile(installedRPM)
             if packageName is not None:
                 listInstalledPackages.append(packageName)
         return listInstalledPackages
     
a5e4be9f
     def buildPackageThreadAPI(self,package,outputMap, threadName,):
518d6a6f
         try:
             self.buildPackage(package)
             outputMap[threadName]=True
         except Exception as e:
             self.logger.error(e)
             outputMap[threadName]=False
2820c61a
         
     def buildPackage(self,package):
         #should initialize a logger based on package name
         chrUtils = ChrootUtils(self.logName,self.logPath)
65584c99
         chrootName="build-"+package
         chrootID=None
a5e4be9f
         isToolChainPackage=False
         if package in constants.listToolChainPackages:
             isToolChainPackage=True
518d6a6f
         try:
a5e4be9f
             chrootID = self.prepareBuildRoot(chrootName,isToolChainPackage)
518d6a6f
             destLogPath=constants.logPath+"/build-"+package
             if not os.path.isdir(destLogPath):
                 cmdUtils = CommandUtils()
                 cmdUtils.runCommandInShell("mkdir -p "+destLogPath)
             
             listInstalledPackages=self.findInstalledPackages(chrootID)
             self.logger.info("List of installed packages")
             self.logger.info(listInstalledPackages)
             listDependentPackages=self.findBuildTimeRequiredPackages(package)
             
542dc63c
             pkgUtils = PackageUtils(self.logName,self.logPath)
518d6a6f
             if len(listDependentPackages) != 0:
                 self.logger.info("Installing the build time dependent packages......")
                 for pkg in listDependentPackages:
542dc63c
                     self.installPackage(pkgUtils, pkg,chrootID,destLogPath,listInstalledPackages)
                 pkgUtils.installRPMSInAOneShot(chrootID,destLogPath)
518d6a6f
                 self.logger.info("Finished installing the build time dependent packages......")
75a2daa5
             pkgUtils.adjustGCCSpecs(package, chrootID, destLogPath)
90d8acae
             pkgUtils.buildRPMSForGivenPackage(package,chrootID,self.listBuildOptionPackages,self.pkgBuildOptionFile,destLogPath)
518d6a6f
             self.logger.info("Successfully built the package:"+package)
         except Exception as e:
2689a7f2
             self.logger.error("Failed while building package:" + package)
             self.logger.debug("Chroot with ID: " + chrootID + " not deleted for debugging.")
9b6f052e
             logFileName = os.path.join(destLogPath, package + ".log")
             fileLog = os.popen('tail -n 20 ' + logFileName).read()
             self.logger.debug(fileLog)
518d6a6f
             raise e
2689a7f2
         if chrootID is not None:
             chrUtils.destroyChroot(chrootID)
518d6a6f
         
2820c61a
         
     def findRunTimeRequiredRPMPackages(self,rpmPackage):
         listRequiredPackages=constants.specData.getRequiresForPackage(rpmPackage)
518d6a6f
         return listRequiredPackages
2820c61a
     
     def findBuildTimeRequiredPackages(self,package):
         listRequiredPackages=constants.specData.getBuildRequiresForPackage(package)
518d6a6f
         return listRequiredPackages
2820c61a
     
542dc63c
     def installPackage(self,pkgUtils,package,chrootID,destLogPath,listInstalledPackages):
2820c61a
         if package in listInstalledPackages:
518d6a6f
             return
542dc63c
         self.installDependentRunTimePackages(pkgUtils,package,chrootID,destLogPath,listInstalledPackages)
2820c61a
         noDeps=False
         if self.mapPackageToCycles.has_key(package):
             noDeps = True
         if package in self.listNodepsPackages:
             noDeps=True
a5e4be9f
         if package in constants.noDepsPackageList:
             noDeps=True
518d6a6f
         pkgUtils.installRPM(package,chrootID,noDeps,destLogPath)
2820c61a
         listInstalledPackages.append(package)
 
542dc63c
     def installDependentRunTimePackages(self,pkgUtils,package,chrootID,destLogPath,listInstalledPackages):
518d6a6f
         listRunTimeDependentPackages=self.findRunTimeRequiredRPMPackages(package)
2820c61a
         if len(listRunTimeDependentPackages) != 0:
             for pkg in listRunTimeDependentPackages:
                 if self.mapPackageToCycles.has_key(pkg) and pkg not in self.listAvailableCyclicPackages:
                     continue
                 if pkg in listInstalledPackages:
                     continue
542dc63c
                 self.installPackage(pkgUtils,pkg,chrootID,destLogPath,listInstalledPackages)