support/package-builder/PackageUtils.py
2820c61a
 from CommandUtils import CommandUtils
 from Logger import Logger
 import os
 import shutil
 from constants import constants
518d6a6f
 import re
75a2daa5
 from time import sleep
3cc43c92
 import PullSources
90d8acae
 import json
 import collections
45c9260c
 from SpecData import SPECS
2820c61a
 
 class PackageUtils(object):
97a9151c
 
2820c61a
     def __init__(self,logName=None,logPath=None):
         if logName is None:
             self.logName = "PackageUtils"
         if logPath is None:
             logPath = constants.logPath
         self.logName=logName
         self.logPath=logPath
         self.logger=Logger.getLogger(logName,logPath)
542dc63c
         self.runInChrootCommand="./run-in-chroot.sh " + constants.sourcePath + " " + constants.rpmPath;
518d6a6f
         self.rpmBinary = "rpm"
         self.installRPMPackageOptions = "-Uvh"
         self.nodepsRPMPackageOptions = "--nodeps"
97a9151c
 
518d6a6f
         self.rpmbuildBinary = "rpmbuild"
45e37dfb
         self.rpmbuildBuildallOption = "-ba --clean"
518d6a6f
         self.rpmbuildNocheckOption = "--nocheck"
b5e09fac
         self.rpmbuildCheckOption ="-bi --clean"
518d6a6f
         self.queryRpmPackageOptions = "-qa"
         self.forceRpmPackageOptions = "--force"
7418d2bf
         self.replaceRpmPackageOptions = "--replacepkgs"
75a2daa5
         self.adjustGCCSpecScript="adjust-gcc-specs.sh"
542dc63c
         self.rpmFilesToInstallInAOneShot=""
         self.packagesToInstallInAOneShot=""
         self.noDepsRPMFilesToInstallInAOneShot=""
         self.noDepsPackagesToInstallInAOneShot=""
7418d2bf
         self.rpmFilesToReInstallInAOneShot=""
         self.noDepsRPMFilesToReInstallInAOneShot=""
97a9151c
 
2cfb758d
     def getRPMArch(self,rpmName):
2820c61a
         arch=""
         if rpmName.find("x86_64") != -1:
2cfb758d
             arch="x86_64"
2820c61a
         elif rpmName.find("noarch") != -1:
             arch="noarch"
2cfb758d
         return arch
 
     def getRPMDestDir(self,rpmName,rpmDir):
         arch = self.getRPMArch(rpmName)
2820c61a
         rpmDestDir=rpmDir+"/"+arch
         return rpmDestDir
97a9151c
 
2820c61a
     def copyRPM(self,rpmFile,destDir):
         cmdUtils = CommandUtils()
         rpmName=os.path.basename(rpmFile)
         rpmDestDir=self.getRPMDestDir(rpmName,destDir)
         rpmDestPath=rpmDestDir+"/"+rpmName
542dc63c
         if os.geteuid()==0:
             if not os.path.isdir(rpmDestDir):
                 cmdUtils.runCommandInShell("mkdir -p "+rpmDestDir)
             shutil.copyfile(rpmFile,  rpmDestPath)
2820c61a
         return rpmDestPath
97a9151c
 
2820c61a
     def installRPM(self,package,chrootID,noDeps=False,destLogPath=None):
542dc63c
 #        self.logger.info("Installing rpm for package:"+package)
 #        self.logger.debug("No deps:"+str(noDeps))
97a9151c
 
2820c61a
         rpmfile=self.findRPMFileForGivenPackage(package)
         if rpmfile is None:
518d6a6f
             self.logger.error("No rpm file found for package:"+package)
b5e09fac
             raise Exception("Missing rpm file: "+package)
2820c61a
 
         rpmDestFile = self.copyRPM(rpmfile, chrootID+constants.topDirPath+"/RPMS")
         rpmFile=rpmDestFile.replace(chrootID,"")
542dc63c
         if noDeps:
             self.noDepsRPMFilesToInstallInAOneShot += " " + rpmFile
             self.noDepsPackagesToInstallInAOneShot += " " + package
         else:
             self.rpmFilesToInstallInAOneShot += " " + rpmFile
             self.packagesToInstallInAOneShot += " " + package
97a9151c
 
542dc63c
     def installRPMSInAOneShot(self,chrootID,destLogPath):
2820c61a
         chrootCmd=self.runInChrootCommand+" "+chrootID
518d6a6f
         rpmInstallcmd=self.rpmBinary+" "+ self.installRPMPackageOptions
542dc63c
         if self.noDepsRPMFilesToInstallInAOneShot != "":
             self.logger.info("Installing nodeps rpms: " + self.noDepsPackagesToInstallInAOneShot)
             logFile=chrootID+constants.topDirPath+"/LOGS/install_rpms_nodeps.log"
             cmdUtils = CommandUtils()
             cmd = rpmInstallcmd+" "+self.nodepsRPMPackageOptions + " " + self.noDepsRPMFilesToInstallInAOneShot
             returnVal = cmdUtils.runCommandInShell(cmd, logFile, chrootCmd)
             if destLogPath is not None:
                 shutil.copy2(logFile, destLogPath)
             if not returnVal:
                 self.logger.error("Unable to install rpms")
                 raise Exception("RPM installation failed")
         if self.rpmFilesToInstallInAOneShot != "":
             self.logger.info("Installing rpms: " + self.packagesToInstallInAOneShot)
             logFile=chrootID+constants.topDirPath+"/LOGS/install_rpms.log"
             cmdUtils = CommandUtils()
             cmd=rpmInstallcmd+" "+self.rpmFilesToInstallInAOneShot
             returnVal = cmdUtils.runCommandInShell(cmd, logFile, chrootCmd)
             if destLogPath is not None:
                 shutil.copy2(logFile, destLogPath)
             if not returnVal:
                 self.logger.error("Unable to install rpms")
                 raise Exception("RPM installation failed")
97a9151c
 
7418d2bf
     def verifyShaAndGetSourcePath(self, source, package):
2820c61a
         cmdUtils = CommandUtils()
7418d2bf
         # Fetch/verify sources if sha1 not None.
45c9260c
         sha1 = SPECS.getData().getSHA1(package, source)
7418d2bf
         if sha1 is not None:
             PullSources.get(source, sha1, constants.sourcePath, constants.pullsourcesConfig, self.logger)
 
         sourcePath = cmdUtils.findFile(source,constants.sourcePath)
         if sourcePath is None or len(sourcePath) == 0:
             sourcePath = cmdUtils.findFile(source,constants.specPath)
2820c61a
             if sourcePath is None or len(sourcePath) == 0:
7fba6943
                 if sha1 is None:
7418d2bf
                     self.logger.error("No sha1 found or missing source for "+source)
                     raise Exception("No sha1 found or missing source for "+source)
                 else:
                     self.logger.error("Missing source: "+source+". Cannot find sources for package: "+package)
                     raise Exception("Missing source")
         else:
             if sha1 is None:
                 self.logger.error("No sha1 found for "+source)
                 raise Exception("No sha1 found")
         if len(sourcePath) > 1:
             self.logger.error("Multiple sources found for source:"+source+"\n"+ ",".join(sourcePath) +"\nUnable to determine one.")
             raise Exception("Multiple sources found")
         return sourcePath
 
     def copySourcesTobuildroot(self,listSourceFiles,package,destDir):
         for source in listSourceFiles:
             sourcePath = self.verifyShaAndGetSourcePath(source, package)
2689a7f2
             self.logger.info("Copying... Source path :" + source + " Source filename: " + sourcePath[0])
90d8acae
             shutil.copy2(sourcePath[0], destDir)
97a9151c
 
1cf11562
     def copyAdditionalBuildFiles(self,listAdditionalFiles,chrootID):
90d8acae
         cmdUtils = CommandUtils()
1cf11562
         for additionalFile in listAdditionalFiles:
             source = additionalFile["src"].encode('utf-8')
             destDir = chrootID + additionalFile["dst"].encode('utf-8')
90d8acae
             if os.path.exists(source):
1cf11562
                 if os.path.isfile(source):
                     shutil.copy(source, destDir)
                 else:
                     shutil.copytree(source, destDir)
97a9151c
 
7418d2bf
     def getAdditionalBuildFiles(self, package, pkgBuildOptionFile):
         listAdditionalFiles = []
         macros = []
         jsonData = open(pkgBuildOptionFile)
         pkg_build_option_json = json.load(jsonData, object_pairs_hook=collections.OrderedDict)
         jsonData.close()
         pkgs_sorted = pkg_build_option_json.items()
         for pkg in pkgs_sorted:
             p = str(pkg[0].encode('utf-8'))
             if p == package:
                 filelist = pkg[1]["files"]
                 for f in filelist:
                     listAdditionalFiles.append(f)
                 macrolist = pkg[1]["macros"]
                 for macro in macrolist:
                     macros.append(str(macro.encode('utf-8')))
         return listAdditionalFiles, macros
 
90d8acae
     def buildRPMSForGivenPackage(self,package,chrootID,listBuildOptionPackages,pkgBuildOptionFile,destLogPath=None):
518d6a6f
         self.logger.info("Building rpm's for package:"+package)
2820c61a
 
45c9260c
         listSourcesFiles = SPECS.getData().getSources(package)
         listPatchFiles =  SPECS.getData().getPatches(package)
         specFile = SPECS.getData().getSpecFile(package)
         specName = SPECS.getData().getSpecName(package) + ".spec"
97a9151c
 
2820c61a
         chrootSourcePath=chrootID+constants.topDirPath+"/SOURCES/"
         chrootSpecPath=constants.topDirPath+"/SPECS/"
         chrootLogsFilePath=chrootID+constants.topDirPath+"/LOGS/"+package+".log"
         chrootCmd=self.runInChrootCommand+" "+chrootID
         shutil.copyfile(specFile, chrootID+chrootSpecPath+specName )
97a9151c
 
e8435c63
 # FIXME: some sources are located in SPECS/.. how to mount?
 #        if os.geteuid()==0:
         self.copySourcesTobuildroot(listSourcesFiles,package,chrootSourcePath)
2820c61a
         self.copySourcesTobuildroot(listPatchFiles,package,chrootSourcePath)
90d8acae
 
         macros = []
         if package in listBuildOptionPackages:
7418d2bf
             listAdditionalFiles, macros = self.getAdditionalBuildFiles(package, pkgBuildOptionFile)
1cf11562
             self.copyAdditionalBuildFiles(listAdditionalFiles,chrootID)
90d8acae
 
9b2f8b85
         #Adding rpm macros
45c9260c
         listRPMMacros = SPECS.getData().getRPMMacros()
9b2f8b85
         for macroName in listRPMMacros.keys():
             macros.append(macroName+" "+listRPMMacros[macroName])
 
3ad2cb4c
         listRPMFiles=[]
d2526915
         listSRPMFiles=[]
3ad2cb4c
         try:
4b59c19b
             listRPMFiles,listSRPMFiles = self.buildRPM(chrootSpecPath +specName,chrootLogsFilePath,chrootCmd,package,macros)
e4eff00f
             self.logger.info("Successfully built rpm:"+package)
3ad2cb4c
         except Exception as e:
             self.logger.error("Failed while building rpm:"+package)
             raise e
         finally:
             if destLogPath is not None:
45c9260c
                 if constants.rpmCheck and package in constants.testForceRPMS and SPECS.getData().isCheckAvailable(package):
b5e09fac
                     cmd="sed -i '/^Executing(%check):/,/^Processing files:/{//!b};d' "+ chrootLogsFilePath
                     logFile = destLogPath+"/adjustTestFile.log"
                     returnVal = CommandUtils().runCommandInShell(cmd, logFile)
                     testLogFile = destLogPath+"/"+package+"-test.log"
                     shutil.copyfile(chrootLogsFilePath, testLogFile)
                 else:
                     shutil.copy2(chrootLogsFilePath, destLogPath)
2cfb758d
         self.logger.info("RPM build is successful")
b5e09fac
 
2820c61a
         for rpmFile in listRPMFiles:
             self.copyRPM(chrootID+"/"+rpmFile, constants.rpmPath)
97a9151c
 
d2526915
         for srpmFile in listSRPMFiles:
97a9151c
             srpmDestFile = self.copyRPM(chrootID+"/"+srpmFile, constants.sourceRpmPath)
d2526915
 
90d8acae
     def buildRPM(self,specFile,logFile,chrootCmd,package,macros):
97a9151c
 
7418d2bf
         rpmbuildDistOption = '--define \\\"dist %s\\\"' % constants.dist
         rpmBuildcmd=self.rpmbuildBinary+" "+self.rpmbuildBuildallOption+" "+rpmbuildDistOption
b5e09fac
 
         if constants.rpmCheck and package in constants.testForceRPMS:
             self.logger.info("#"*(68+2*len(package)))
45c9260c
             if not SPECS.getData().isCheckAvailable(package):
b5e09fac
                 self.logger.info("####### "+package+" MakeCheck is not available. Skipping MakeCheck TEST for "+package+ " #######")
                 rpmBuildcmd=self.rpmbuildBinary+" --clean"
             else:
                 self.logger.info("####### "+package+" MakeCheck is available. Running MakeCheck TEST for "+package+ " #######")
                 rpmBuildcmd=self.rpmbuildBinary+" "+self.rpmbuildCheckOption
             self.logger.info("#"*(68+2*len(package)))
         else:
            rpmBuildcmd+=" "+self.rpmbuildNocheckOption
 
90d8acae
         for macro in macros:
             rpmBuildcmd+=' --define \\\"%s\\\"' % macro
518d6a6f
         rpmBuildcmd+=" "+specFile
97a9151c
 
2820c61a
         cmdUtils = CommandUtils()
42ffccb5
         self.logger.info("Building rpm....")
2c153d29
         self.logger.info(rpmBuildcmd)
518d6a6f
         returnVal = cmdUtils.runCommandInShell(rpmBuildcmd, logFile, chrootCmd)
b5e09fac
         if constants.rpmCheck and package in constants.testForceRPMS:
45c9260c
             if not SPECS.getData().isCheckAvailable(package):
b5e09fac
                 constants.testLogger.info(package+" : N/A")
             elif returnVal:
                 constants.testLogger.info(package+" : PASS")
             else:
                 constants.testLogger.error(package+" : FAIL" )
 
         if constants.rpmCheck:
             if not returnVal and constants.rpmCheckStopOnError:
                 self.logger.error("Checking rpm is failed "+specFile)
                 raise Exception("RPM check failed")
         else:
             if not returnVal:
                 self.logger.error("Building rpm is failed "+specFile)
                 raise Exception("RPM build failed")
 
518d6a6f
         #Extracting rpms created from log file
         logfile=open(logFile,'r')
         fileContents=logfile.readlines()
         logfile.close()
         listRPMFiles=[]
d2526915
         listSRPMFiles=[]
518d6a6f
         for i in range(0,len(fileContents)):
             if re.search("^Wrote:",fileContents[i]):
                 listcontents=fileContents[i].split()
                 if (len(listcontents) == 2) and listcontents[1].strip()[-4:] == ".rpm" and listcontents[1].find("/RPMS/") != -1:
                     listRPMFiles.append(listcontents[1])
d2526915
                 if (len(listcontents) == 2) and listcontents[1].strip()[-8:] == ".src.rpm" and listcontents[1].find("/SRPMS/") != -1:
                     listSRPMFiles.append(listcontents[1])
97a9151c
         return listRPMFiles,listSRPMFiles
 
2820c61a
     def findRPMFileForGivenPackage(self,package):
         cmdUtils = CommandUtils()
45c9260c
         version = SPECS.getData().getVersion(package)
         release = SPECS.getData().getRelease(package)
cd022cc1
         listFoundRPMFiles = sum([cmdUtils.findFile(package+"-"+version+"-"+release+".x86_64.rpm",constants.rpmPath),
                             cmdUtils.findFile(package+"-"+version+"-"+release+".noarch.rpm",constants.rpmPath)], [])
42ffccb5
         if constants.inputRPMSPath is not None:
             listFoundRPMFiles = sum([cmdUtils.findFile(package+"-"+version+"-"+release+".x86_64.rpm",constants.inputRPMSPath),
97a9151c
                             cmdUtils.findFile(package+"-"+version+"-"+release+".noarch.rpm",constants.inputRPMSPath)], listFoundRPMFiles)
2820c61a
         if len(listFoundRPMFiles) == 1 :
             return listFoundRPMFiles[0]
         if len(listFoundRPMFiles) == 0 :
             return None
         if len(listFoundRPMFiles) > 1 :
518d6a6f
             self.logger.error("Found multiple rpm files for given package in rpm directory.Unable to determine the rpm file for package:"+package)
             raise Exception("Multiple rpm files found")
97a9151c
 
2820c61a
     def findPackageNameFromRPMFile(self,rpmfile):
         rpmfile=os.path.basename(rpmfile)
         releaseindex=rpmfile.rfind("-")
         if releaseindex == -1:
             self.logger.error("Invalid rpm file:"+rpmfile)
518d6a6f
             raise Exception("Invalid RPM")
2820c61a
         versionindex=rpmfile[0:releaseindex].rfind("-")
         if versionindex == -1:
             self.logger.error("Invalid rpm file:"+rpmfile)
518d6a6f
             raise Exception("Invalid RPM")
2820c61a
         packageName=rpmfile[0:versionindex]
97a9151c
         return packageName
af3575c9
 
     def findPackageInfoFromRPMFile(self,rpmfile):
         rpmfile=os.path.basename(rpmfile)
         rpmfile=rpmfile.replace(".x86_64.rpm","")
         rpmfile=rpmfile.replace(".noarch.rpm","")
         releaseindex=rpmfile.rfind("-")
         if releaseindex == -1:
             self.logger.error("Invalid rpm file:"+rpmfile)
             raise Exception("Invalid RPM")
         versionindex=rpmfile[0:releaseindex].rfind("-")
         if versionindex == -1:
             self.logger.error("Invalid rpm file:"+rpmfile)
             raise Exception("Invalid RPM")
         packageName=rpmfile[0:versionindex]
         version=rpmfile[versionindex+1:releaseindex]
         release=rpmfile[releaseindex+1:]
         return packageName,version,release
2cfb758d
 
518d6a6f
     def findInstalledRPMPackages(self, chrootID):
         cmd = self.rpmBinary+" "+self.queryRpmPackageOptions
         chrootCmd=self.runInChrootCommand+" "+chrootID
         cmdUtils=CommandUtils()
         result=cmdUtils.runCommandInShell2(cmd, chrootCmd)
610ab83e
         if result is not None:
             return result.split()
518d6a6f
         return result
75a2daa5
 
     def adjustGCCSpecs(self, package, chrootID, logPath):
45c9260c
         opt = " " + SPECS.getData().getSecurityHardeningOption(package)
75a2daa5
         cmdUtils=CommandUtils()
8d0a7e93
         cpcmd="cp "+ self.adjustGCCSpecScript+" "+chrootID+"/tmp/"+self.adjustGCCSpecScript
75a2daa5
         cmd = "/tmp/"+self.adjustGCCSpecScript+opt
         logFile = logPath+"/adjustGCCSpecScript.log"
         chrootCmd=self.runInChrootCommand+" "+chrootID
8d0a7e93
         returnVal = cmdUtils.runCommandInShell(cpcmd, logFile)
         if not returnVal:
42ffccb5
             self.logger.error("Error during copying the file adjust gcc spec")
36c2f510
             raise Exception("Failed while copying adjust gcc spec file")
610ab83e
         returnVal = cmdUtils.runCommandInShell(cmd, logFile, chrootCmd)
         if returnVal:
             return
 
         self.logger.debug(cmdUtils.runCommandInShell2("ls -la " + chrootID + "/tmp/" + self.adjustGCCSpecScript))
         self.logger.debug(cmdUtils.runCommandInShell2("lsof " + chrootID + "/tmp/" + self.adjustGCCSpecScript))
         self.logger.debug(cmdUtils.runCommandInShell2("ps ax"))
 
         self.logger.error("Failed while adjusting gcc specs")
36c2f510
         raise Exception("Failed while adjusting gcc specs")
7418d2bf
 
     def copySourcesToContainer(self, listSourceFiles, package, containerID, destDir):
         cmdUtils = CommandUtils()
         for source in listSourceFiles:
             sourcePath = self.verifyShaAndGetSourcePath(source, package)
             self.logger.info("Copying source file: " + sourcePath[0])
             copyCmd = "docker cp " + sourcePath[0] + " " + containerID.short_id + ":" + destDir
             cmdUtils.runCommandInShell(copyCmd)
 
     def copyAdditionalBuildFilesToContainer(self, listAdditionalFiles, containerID):
         cmdUtils = CommandUtils()
         #self.logger.debug("VDBG-PU-copyAdditionalBuildFilesToContainer id: " +containerID.short_id)
         #self.logger.debug(listAdditionalFiles)
         for additionalFile in listAdditionalFiles:
             source = additionalFile["src"].encode('utf-8')
             destDir = additionalFile["dst"].encode('utf-8')
             destPath = containerID.short_id + ":" + destDir
             #TODO: exit status of exec_run
             containerID.exec_run("mkdir -p " + destDir)
             if os.path.exists(source):
                 copyCmd = "docker cp " + source
                 if os.path.isfile(source):
                     self.logger.info("Copying addl source file: " + source)
                     copyCmd += " " + destPath
                 else:
                     self.logger.info("Copying addl source file tree: " + source)
                     copyCmd +=  "/. " + destPath
                 #TODO: cmd error code
                 cmdUtils.runCommandInShell(copyCmd)
 
     def getRPMPathInContainer(self, rpmFile, containerID):
         rpmName = os.path.basename(rpmFile)
         #TODO: Container path from constants
         if "PUBLISHRPMS" in rpmFile:
             rpmPath = "/publishrpms/"
         elif "PUBLISHXRPMS" in rpmFile:
             rpmPath = "/publishxrpms/"
         else:
             rpmPath = constants.topDirPath + "/RPMS/"
         if "noarch" in rpmFile:
             rpmPath += "noarch/"
         else:
             rpmPath += "x86_64/"
         rpmPath += rpmName
         return rpmPath
 
     def prepRPMforInstallInContainer(self, package, containerID, noDeps=False, destLogPath=None):
         rpmfile = self.findRPMFileForGivenPackage(package)
         if rpmfile is None:
             self.logger.error("No rpm file found for package: " + package)
             raise Exception("Missing rpm file")
 
         rpmDestFile = self.getRPMPathInContainer(rpmfile, containerID)
         if noDeps:
             self.noDepsRPMFilesToInstallInAOneShot += " " + rpmDestFile
             self.noDepsPackagesToInstallInAOneShot += " " + package
             if package in constants.listReInstallPackages:
                 self.noDepsRPMFilesToReInstallInAOneShot += " " + rpmDestFile
         else:
             self.rpmFilesToInstallInAOneShot += " " + rpmDestFile
             self.packagesToInstallInAOneShot += " " + package
             if package in constants.listReInstallPackages:
                 self.rmpFilesToReInstallInAOneShot += " " + rpmDestFile
 
     def installRPMSInAOneShotInContainer(self, containerID, destLogPath):
         rpmInstallcmd = self.rpmBinary + " " + self.installRPMPackageOptions + " " + self.forceRpmPackageOptions
 
         if self.noDepsRPMFilesToInstallInAOneShot != "":
             self.logger.info("PackageUtils-installRPMSInAOneShotInContainer: Installing nodeps rpms: " + \
                              self.noDepsPackagesToInstallInAOneShot)
             logFile = destLogPath + "/install_rpms_nodeps.log"
             cmd = rpmInstallcmd + " " + self.nodepsRPMPackageOptions + " " + self.noDepsRPMFilesToInstallInAOneShot
             cmd = "/bin/bash -l -c '" + cmd + "'"
             #self.logger.debug("VDBG-PU-installRPMSInAOneShotInContainer: Install nodeps cmd: " + cmd)
             #TODO: Error code from exec_run
             installLog = containerID.exec_run(cmd)
             if not installLog:
                 self.logger.error("Unable to install nodeps rpms")
                 raise Exception("nodeps RPM installation failed")
             logfile = open(logFile, 'w')
             logfile.write(installLog)
             logfile.close()
 
             if self.noDepsRPMFilesToReInstallInAOneShot != "":
                 cmd = rpmInstallcmd + " " + self.nodepsRPMPackageOptions + " " + self.forceRpmPackageOptions + " " + self.noDepsRPMFilesToReInstallInAOneShot
                 cmd = "/bin/bash -l -c '" + cmd + "'"
                 #self.logger.debug("VDBG-PU-installRPMSInAOneShotInContainer: ReInstall nodeps cmd: " + cmd)
                 #TODO: Error code from exec_run
                 installLog = containerID.exec_run(cmd)
                 if not installLog:
                     self.logger.error("Unable to re-install nodeps rpms")
                     raise Exception("nodeps RPM re-installation failed")
                 logfile = open(logFile, 'a')
                 logfile.write(installLog)
                 logfile.close()
 
         if self.rpmFilesToInstallInAOneShot != "":
             self.logger.info("PackageUtils-installRPMSInAOneShotInContainer: Installing rpms: " + \
                              self.packagesToInstallInAOneShot)
             logFile = destLogPath + "/install_rpms.log"
             cmd = rpmInstallcmd + " " + self.rpmFilesToInstallInAOneShot
             cmd = "/bin/bash -l -c '" + cmd + "'"
             #self.logger.debug("VDBG-PU-installRPMSInAOneShotInContainer: Install cmd: " + cmd)
             #TODO: Error code from exec_run
             installLog = containerID.exec_run(cmd)
             if not installLog:
                 self.logger.error("Unable to install rpms")
                 raise Exception("RPM installation failed")
             logfile = open(logFile, 'w')
             logfile.write(installLog)
             logfile.close()
 
             if self.rpmFilesToReInstallInAOneShot != "":
                 cmd = rpmInstallcmd + " " + self.forceRpmPackageOptions + " " + self.rpmFilesToReInstallInAOneShot
                 cmd = "/bin/bash -l -c '" + cmd + "'"
                 #self.logger.debug("VDBG-PU-installRPMSInAOneShotInContainer: ReInstall cmd: " + cmd)
                 #TODO: Error code from exec_run
                 installLog = containerID.exec_run(cmd)
                 if not installLog:
                     self.logger.error("Unable to re-install rpms")
                     raise Exception("RPM re-installation failed")
                 logfile = open(logFile, 'a')
                 logfile.write(installLog)
                 logfile.close()
 
     def findInstalledRPMPackagesInContainer(self, containerID):
         cmd = self.rpmBinary + " " + self.queryRpmPackageOptions
         cmd = "/bin/bash -l -c '" + cmd + "'"
         #TODO: Error code from exec_run
         result = containerID.exec_run(cmd)
         if result is not None:
             return result.split()
         return result
 
     def adjustGCCSpecsInContainer(self, package, containerID, logPath):
45c9260c
         opt = " " + SPECS.getData().getSecurityHardeningOption(package)
8996b6b6
         adjustCmd = "/" + self.adjustGCCSpecScript + opt
7418d2bf
         adjustCmd = "/bin/bash -l -c '" + adjustCmd + "'"
         logFile = logPath + "/adjustGCCSpecScript.log"
 
         #TODO: Error code from exec_run
         scriptLog = containerID.exec_run(adjustCmd)
         if scriptLog:
             logfile = open(logFile, 'w')
             logfile.write(scriptLog)
             logfile.close()
             return
 
         self.logger.debug(containerID.exec_run("ls -la /tmp/" + self.adjustGCCSpecScript))
         self.logger.debug(containerID.exec_run("lsof /tmp/" + self.adjustGCCSpecScript))
         self.logger.debug(containerID.exec_run("ps ax"))
         self.logger.error("Failed while adjusting gcc specs")
         raise Exception("Failed while adjusting gcc specs")
 
     def buildRPMSForGivenPackageInContainer(self, package, containerID, listBuildOptionPackages,
                                             pkgBuildOptionFile, destLogPath=None):
         self.logger.info("Building rpm's for package " + package + " in container " + containerID.short_id)
 
45c9260c
         listSourcesFiles = SPECS.getData().getSources(package)
         listPatchFiles = SPECS.getData().getPatches(package)
         specFile = SPECS.getData().getSpecFile(package)
         specName = SPECS.getData().getSpecName(package) + ".spec"
7418d2bf
         sourcePath = constants.topDirPath + "/SOURCES/"
         specPath = constants.topDirPath + "/SPECS/"
         rpmLogFile = constants.topDirPath + "/LOGS/" + package + ".log"
         destLogFile = destLogPath + "/" + package + ".log"
         cmdUtils = CommandUtils()
 
         #TODO: mount it in, don't copy
         cpSpecCmd = "docker cp " + specFile + " " + containerID.short_id \
                         + ":" + specPath + specName
         returnVal = cmdUtils.runCommandInShell(cpSpecCmd)
         if not returnVal:
             self.logger.error("Error copying source SPEC file to container")
             raise Exception("Failed copying source SPEC to container")
 
 # FIXME: some sources are located in SPECS/.. how to mount?
 #        if os.geteuid()==0:
         #TODO: mount it in, don't copy
         macros = []
         self.copySourcesToContainer(listSourcesFiles, package, containerID, sourcePath)
         #TODO: mount it in, don't copy
         self.copySourcesToContainer(listPatchFiles, package, containerID, sourcePath)
         if package in listBuildOptionPackages:
             listAdditionalFiles, macros = self.getAdditionalBuildFiles(package, pkgBuildOptionFile)
             self.copyAdditionalBuildFilesToContainer(listAdditionalFiles, containerID)
 
         # Add rpm macros
45c9260c
         listRPMMacros = SPECS.getData().getRPMMacros()
7418d2bf
         for macroName in listRPMMacros.keys():
             macros.append(macroName + " " + listRPMMacros[macroName])
 
         # Build RPMs
         listRPMFiles=[]
         listSRPMFiles=[]
         try:
             listRPMFiles, listSRPMFiles = self.buildRPMinContainer(
                                                     specPath + specName,
                                                     rpmLogFile,
                                                     destLogFile,
                                                     containerID,
                                                     package,
                                                     macros)
0e4f2bff
             self.logger.info("Successfully built rpm:"+package)
7418d2bf
         except Exception as e:
             self.logger.error("Failed while building rpm: " + package)
             raise e
0e4f2bff
         finally:
             if destLogPath is not None:
                 rpmLog = destLogPath + "/" + package + ".log"
45c9260c
                 if constants.rpmCheck and package in constants.testForceRPMS and SPECS.getData().isCheckAvailable(package):
0e4f2bff
                     cmd="sed -i '/^Executing(%check):/,/^Processing files:/{//!b};d' "+ rpmLog
                     logFile = destLogPath+"/adjustTestFile.log"
                     returnVal = CommandUtils().runCommandInShell(cmd, logFile)
                     testLogFile = destLogPath+"/"+package+"-test.log"
                     shutil.copyfile(rpmLog, testLogFile)
7418d2bf
         self.logger.info("RPM build is successful")
 
         # Verify RPM and SRPM files exist as success criteria
         for rpmFile in listRPMFiles:
             rpmName = os.path.basename(rpmFile)
             rpmDestDir = self.getRPMDestDir(rpmName, constants.rpmPath)
             rpmDestFile = rpmDestDir + "/" + rpmName
             if not os.path.isfile(rpmDestFile):
                 self.logger.error("Could not find RPM file: " + rpmDestFile)
                 raise Exception("Built RPM file not found.")
 
         for srpmFile in listSRPMFiles:
             srpmName = os.path.basename(srpmFile)
             srpmDestDir = self.getRPMDestDir(os.path.basename(srpmFile), constants.sourceRpmPath)
             srpmDestFile = srpmDestDir + "/" + srpmName
             if not os.path.isfile(srpmDestFile):
                 self.logger.error("Could not find RPM file: " + srpmDestFile)
                 raise Exception("Built SRPM file not found.")
 
     def buildRPMinContainer(self, specFile, rpmLogFile, destLogFile, containerID, package, macros):
 
         rpmbuildDistOption = '--define \"dist %s\"' % constants.dist
         rpmBuildCmd = self.rpmbuildBinary + " " + self.rpmbuildBuildallOption \
                           + " " + rpmbuildDistOption
 
         if constants.rpmCheck and package in constants.testForceRPMS:
             self.logger.info("#"*(68+2*len(package)))
45c9260c
             if not SPECS.getData().isCheckAvailable(package):
7418d2bf
                 self.logger.info("####### "+package+" MakeCheck is not available. Skipping MakeCheck TEST for "+package+ " #######")
                 rpmBuildCmd=self.rpmbuildBinary+" --clean"
             else:
                 self.logger.info("####### "+package+" MakeCheck is available. Running MakeCheck TEST for "+package+ " #######")
                 rpmBuildCmd=self.rpmbuildBinary+" "+self.rpmbuildCheckOption
             self.logger.info("#"*(68+2*len(package)))
         else:
            rpmBuildCmd+=" "+self.rpmbuildNocheckOption
 
         for macro in macros:
             rpmBuildCmd += ' --define \"%s\"' % macro
         rpmBuildCmd += " " + specFile
         rpmBuildCmd = "/bin/bash -l -c '" + rpmBuildCmd + " > " + rpmLogFile + " 2>&1'"
f3936267
         rpmBuildCmd = "docker exec -t " + str(containerID.short_id) + " " + rpmBuildCmd
7418d2bf
 
         cmdUtils = CommandUtils()
         self.logger.info("Building rpm for package: " + package)
         #TODO: Show running log of rpmbuildcmd
         #TODO: Get exit status of rpmBuildCmd
         #containerID.exec_run(rpmBuildCmd)
         returnVal = cmdUtils.runCommandInShell(rpmBuildCmd)
 
         if not os.path.isfile(destLogFile):
             self.logger.error("RPM build not file not found. Building rpm failed for: " + specFile)
             raise Exception("RPM Build failed")
 
         if constants.rpmCheck and package in constants.testForceRPMS:
45c9260c
             if not SPECS.getData().isCheckAvailable(package):
7418d2bf
                 constants.testLogger.info(package+" : N/A")
             elif returnVal:
                 constants.testLogger.info(package+" : PASS")
             else:
                 constants.testLogger.error(package+" : FAIL" )
 
         if constants.rpmCheck:
             if not returnVal and constants.rpmCheckStopOnError:
                 self.logger.error("Checking rpm is failed "+specFile)
                 raise Exception("RPM check failed")
         else:
             if not returnVal:
                 self.logger.error("Building rpm is failed "+specFile)
                 raise Exception("RPM build failed")
 
         #Extracting rpms created from log file
         listRPMFiles=[]
         listSRPMFiles=[]
         logfile = open(destLogFile, 'r')
         rpmBuildLogLines = logfile.readlines()
         logfile.close()
         for i in range(0, len(rpmBuildLogLines)):
             if re.search("^Wrote:", rpmBuildLogLines[i]):
                 listcontents = rpmBuildLogLines[i].split()
                 if (len(listcontents) == 2) and listcontents[1].strip()[-4:] == ".rpm" and listcontents[1].find("/RPMS/") != -1:
                     listRPMFiles.append(listcontents[1])
                 if (len(listcontents) == 2) and listcontents[1].strip()[-8:] == ".src.rpm" and listcontents[1].find("/SRPMS/") != -1:
                     listSRPMFiles.append(listcontents[1])
         #if not listRPMFiles:
         #    self.logger.error("Building rpm failed for " + specFile)
         #    raise Exception("RPM Build failed")
         return listRPMFiles, listSRPMFiles