2820c61a |
import os |
0f1fdc4b |
import platform |
2820c61a |
import shutil |
518d6a6f |
import re |
b8ab7fb4 |
import random
import string |
87815216 |
from CommandUtils import CommandUtils
from Logger import Logger
from constants import constants
import PullSources |
45c9260c |
from SpecData import SPECS |
e45f5730 |
from distutils.version import LooseVersion |
2820c61a |
class PackageUtils(object): |
97a9151c |
|
87815216 |
def __init__(self, logName=None, logPath=None): |
2820c61a |
if logName is None:
self.logName = "PackageUtils"
if logPath is None:
logPath = constants.logPath |
87815216 |
self.logName = logName
self.logPath = logPath
self.logger = Logger.getLogger(logName, logPath)
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" |
87815216 |
self.rpmbuildCheckOption = "-bi --clean" |
518d6a6f |
self.queryRpmPackageOptions = "-qa"
self.forceRpmPackageOptions = "--force" |
7418d2bf |
self.replaceRpmPackageOptions = "--replacepkgs" |
87815216 |
self.adjustGCCSpecScript = "adjust-gcc-specs.sh"
self.rpmFilesToInstallInAOneShot = ""
self.packagesToInstallInAOneShot = ""
self.noDepsRPMFilesToInstallInAOneShot = ""
self.noDepsPackagesToInstallInAOneShot = ""
self.rpmFilesToReInstallInAOneShot = ""
self.noDepsRPMFilesToReInstallInAOneShot = ""
|
4a93976f |
def installRPM(self, package,version, chrootID, noDeps=False, destLogPath=None): |
542dc63c |
# self.logger.info("Installing rpm for package:"+package)
# self.logger.debug("No deps:"+str(noDeps)) |
97a9151c |
|
4a93976f |
rpmfile = self.findRPMFileForGivenPackage(package,version) |
2820c61a |
if rpmfile is None: |
87815216 |
self.logger.error("No rpm file found for package:" + package)
raise Exception("Missing rpm file: " + package) |
2820c61a |
|
326d5ca8 |
rpmDestFile = self._copyRPM(rpmfile, chrootID + constants.topDirPath + "/RPMS") |
87815216 |
rpmFile = rpmDestFile.replace(chrootID, "") |
542dc63c |
if noDeps:
self.noDepsRPMFilesToInstallInAOneShot += " " + rpmFile
self.noDepsPackagesToInstallInAOneShot += " " + package
else:
self.rpmFilesToInstallInAOneShot += " " + rpmFile
self.packagesToInstallInAOneShot += " " + package |
97a9151c |
|
87815216 |
def installRPMSInAOneShot(self, chrootID, destLogPath):
chrootCmd = self.runInChrootCommand + " " + chrootID
rpmInstallcmd = self.rpmBinary + " " + self.installRPMPackageOptions |
7f9d2e12 |
cmdUtils = CommandUtils() |
542dc63c |
if self.noDepsRPMFilesToInstallInAOneShot != "": |
87815216 |
self.logger.info("Installing nodeps rpms: " +
self.noDepsPackagesToInstallInAOneShot)
logFile = destLogPath + "/install_rpms_nodeps.log"
cmd = (rpmInstallcmd+" "+self.nodepsRPMPackageOptions + " " +
self.noDepsRPMFilesToInstallInAOneShot) |
542dc63c |
returnVal = cmdUtils.runCommandInShell(cmd, logFile, chrootCmd)
if not returnVal: |
7f9d2e12 |
self.logger.debug("Command Executed:" + cmd) |
542dc63c |
self.logger.error("Unable to install rpms")
raise Exception("RPM installation failed")
if self.rpmFilesToInstallInAOneShot != "":
self.logger.info("Installing rpms: " + self.packagesToInstallInAOneShot) |
87815216 |
logFile = destLogPath+"/install_rpms.log"
cmd = rpmInstallcmd+" "+self.rpmFilesToInstallInAOneShot |
542dc63c |
returnVal = cmdUtils.runCommandInShell(cmd, logFile, chrootCmd)
if not returnVal: |
7f9d2e12 |
self.logger.debug("Command Executed:" + cmd) |
542dc63c |
self.logger.error("Unable to install rpms")
raise Exception("RPM installation failed") |
97a9151c |
|
5d822c2d |
def buildRPMSForGivenPackage(self, package, chrootID, destLogPath=None, index=0): |
87815216 |
self.logger.info("Building rpm's for package:" + package) |
2820c61a |
|
5d822c2d |
listSourcesFiles = SPECS.getData().getSources(package, index)
listPatchFiles = SPECS.getData().getPatches(package, index)
specFile = SPECS.getData().getSpecFile(package, index) |
45c9260c |
specName = SPECS.getData().getSpecName(package) + ".spec" |
97a9151c |
|
87815216 |
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 |
|
b8ab7fb4 |
# FIXME: some sources are located in SPECS/.. how to mount?
# if os.geteuid()==0: |
5d822c2d |
self._copySourcesTobuildroot(listSourcesFiles, package, chrootSourcePath, index)
self._copySourcesTobuildroot(listPatchFiles, package, chrootSourcePath, index) |
90d8acae |
macros = [] |
085306ae |
|
326d5ca8 |
listAdditionalFiles, macros = self._getAdditionalBuildFiles(package)
self._copyAdditionalBuildFiles(listAdditionalFiles, chrootID) |
90d8acae |
|
9b2f8b85 |
#Adding rpm macros |
343d89e8 |
listRPMMacros = constants.userDefinedMacros |
326d5ca8 |
for macroName, value in listRPMMacros.items():
macros.append(macroName + " " + value) |
9b2f8b85 |
|
87815216 |
listRPMFiles = []
listSRPMFiles = [] |
3ad2cb4c |
try: |
326d5ca8 |
listRPMFiles, listSRPMFiles = self._buildRPM(chrootSpecPath + specName,
chrootLogsFilePath, chrootCmd,
package, macros) |
87815216 |
self.logger.info("Successfully built rpm:" + package) |
3ad2cb4c |
except Exception as e: |
87815216 |
self.logger.error("Failed while building rpm:" + package) |
3ad2cb4c |
raise e
finally:
if destLogPath is not None: |
87815216 |
if (constants.rpmCheck and
package in constants.testForceRPMS and
SPECS.getData().isCheckAvailable(package)):
cmd = ("sed -i '/^Executing(%check):/,/^Processing files:/{//!b};d' " +
chrootLogsFilePath)
logFile = destLogPath + "/adjustTestFile.log" |
b5e09fac |
returnVal = CommandUtils().runCommandInShell(cmd, logFile) |
87815216 |
testLogFile = destLogPath + "/" + package + "-test.log" |
b5e09fac |
shutil.copyfile(chrootLogsFilePath, testLogFile)
else:
shutil.copy2(chrootLogsFilePath, destLogPath) |
2cfb758d |
self.logger.info("RPM build is successful") |
b5e09fac |
|
2820c61a |
for rpmFile in listRPMFiles: |
326d5ca8 |
self._copyRPM(chrootID + "/" + rpmFile, constants.rpmPath) |
97a9151c |
|
d2526915 |
for srpmFile in listSRPMFiles: |
326d5ca8 |
srpmDestFile = self._copyRPM(chrootID + "/" + srpmFile, constants.sourceRpmPath) |
97a9151c |
|
4a93976f |
def findRPMFileForGivenPackage(self, package,version="*", index=0): |
2820c61a |
cmdUtils = CommandUtils() |
4a93976f |
release = "*"
if version == "*":
version = SPECS.getData().getVersion(package, index)
release = SPECS.getData().getRelease(package, index) |
87815216 |
listFoundRPMFiles = sum([cmdUtils.findFile(package + "-" + version + "-" + release + "." +
platform.machine()+".rpm",
constants.rpmPath),
cmdUtils.findFile(package + "-" + version + "-" + release +
".noarch.rpm",
constants.rpmPath)], []) |
42ffccb5 |
if constants.inputRPMSPath is not None: |
87815216 |
listFoundRPMFiles = sum([cmdUtils.findFile(package + "-" + version + "-" + release +
"." + platform.machine()+".rpm",
constants.inputRPMSPath),
cmdUtils.findFile(package + "-" + version + "-" + release +
".noarch.rpm", constants.inputRPMSPath)],
listFoundRPMFiles)
if len(listFoundRPMFiles) == 1: |
2820c61a |
return listFoundRPMFiles[0] |
87815216 |
if len(listFoundRPMFiles) == 0: |
2820c61a |
return None |
87815216 |
if len(listFoundRPMFiles) > 1:
self.logger.error("Found multiple rpm files for given package in rpm directory." +
"Unable to determine the rpm file for package:" + package) |
518d6a6f |
raise Exception("Multiple rpm files found") |
97a9151c |
|
87815216 |
def findPackageInfoFromRPMFile(self, rpmfile):
rpmfile = os.path.basename(rpmfile)
rpmfile = rpmfile.replace("." + platform.machine() + ".rpm", "")
rpmfile = rpmfile.replace(".noarch.rpm", "")
releaseindex = rpmfile.rfind("-") |
af3575c9 |
if releaseindex == -1: |
87815216 |
self.logger.error("Invalid rpm file:" + rpmfile) |
af3575c9 |
raise Exception("Invalid RPM") |
87815216 |
versionindex = rpmfile[0:releaseindex].rfind("-") |
af3575c9 |
if versionindex == -1: |
87815216 |
self.logger.error("Invalid rpm file:" + rpmfile) |
af3575c9 |
raise Exception("Invalid RPM") |
87815216 |
packageName = rpmfile[0:versionindex]
version = rpmfile[versionindex + 1:releaseindex]
release = rpmfile[releaseindex + 1:]
return packageName, version, release |
2cfb758d |
|
518d6a6f |
def findInstalledRPMPackages(self, chrootID): |
87815216 |
cmd = self.rpmBinary + " " + self.queryRpmPackageOptions
chrootCmd = self.runInChrootCommand + " " + chrootID
cmdUtils = CommandUtils()
result = cmdUtils.runCommandInShell2(cmd, chrootCmd) |
610ab83e |
if result is not None: |
87815216 |
return result.decode().split() |
518d6a6f |
return result |
75a2daa5 |
|
5d822c2d |
def adjustGCCSpecs(self, package, chrootID, logPath, index=0):
opt = " " + SPECS.getData().getSecurityHardeningOption(package, index) |
87815216 |
cmdUtils = CommandUtils()
cpcmd = ("cp " + self.adjustGCCSpecScript + " " + chrootID +
"/tmp/" + self.adjustGCCSpecScript)
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
|
87815216 |
self.logger.debug(cmdUtils.runCommandInShell2("ls -la " + chrootID +
"/tmp/" + self.adjustGCCSpecScript))
self.logger.debug(cmdUtils.runCommandInShell2("lsof " + chrootID + "/tmp/" +
self.adjustGCCSpecScript)) |
610ab83e |
self.logger.debug(cmdUtils.runCommandInShell2("ps ax"))
self.logger.error("Failed while adjusting gcc specs") |
36c2f510 |
raise Exception("Failed while adjusting gcc specs") |
7418d2bf |
|
4a93976f |
def prepRPMforInstallInContainer(self, package,version, containerID, noDeps=False, destLogPath=None):
rpmfile = self.findRPMFileForGivenPackage(package,version) |
7418d2bf |
if rpmfile is None:
self.logger.error("No rpm file found for package: " + package)
raise Exception("Missing rpm file")
|
326d5ca8 |
rpmDestFile = self._getRPMPathInContainer(rpmfile, containerID) |
7418d2bf |
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): |
87815216 |
rpmInstallcmd = (self.rpmBinary + " " + self.installRPMPackageOptions + " " +
self.forceRpmPackageOptions) |
7418d2bf |
if self.noDepsRPMFilesToInstallInAOneShot != "": |
87815216 |
self.logger.info("PackageUtils-installRPMSInAOneShotInContainer: " +
"Installing nodeps rpms: " + |
7418d2bf |
self.noDepsPackagesToInstallInAOneShot)
logFile = destLogPath + "/install_rpms_nodeps.log" |
87815216 |
cmd = (rpmInstallcmd + " " + self.nodepsRPMPackageOptions + " " +
self.noDepsRPMFilesToInstallInAOneShot) |
7418d2bf |
cmd = "/bin/bash -l -c '" + cmd + "'" |
87815216 |
#self.logger.debug("VDBG-PU-installRPMSInAOneShotInContainer: " +
# "Install nodeps cmd: " + cmd) |
7418d2bf |
#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") |
87815216 |
with open(logFile, 'w') as logfile:
logfile.write(installLog.decode()) |
7418d2bf |
if self.noDepsRPMFilesToReInstallInAOneShot != "": |
87815216 |
cmd = (rpmInstallcmd + " " + self.nodepsRPMPackageOptions + " " +
self.forceRpmPackageOptions + " " +
self.noDepsRPMFilesToReInstallInAOneShot) |
7418d2bf |
cmd = "/bin/bash -l -c '" + cmd + "'" |
87815216 |
#self.logger.debug("VDBG-PU-installRPMSInAOneShotInContainer: " +
# "ReInstall nodeps cmd: " + cmd) |
7418d2bf |
#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") |
87815216 |
with open(logFile, 'a') as logfile:
logfile.write(installLog.decode()) |
7418d2bf |
if self.rpmFilesToInstallInAOneShot != "": |
87815216 |
self.logger.info("PackageUtils-installRPMSInAOneShotInContainer: Installing rpms: " + |
7418d2bf |
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") |
87815216 |
with open(logFile, 'w') as logfile:
logfile.write(installLog.decode()) |
7418d2bf |
if self.rpmFilesToReInstallInAOneShot != "": |
87815216 |
cmd = (rpmInstallcmd + " " + self.forceRpmPackageOptions + " " +
self.rpmFilesToReInstallInAOneShot) |
7418d2bf |
cmd = "/bin/bash -l -c '" + cmd + "'" |
87815216 |
#self.logger.debug("VDBG-PU-installRPMSInAOneShotInContainer: " +
# "ReInstall cmd: " + cmd) |
7418d2bf |
#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") |
87815216 |
with open(logFile, 'a') as logfile:
logfile.write(installLog.decode()) |
7418d2bf |
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: |
87815216 |
return result.decode().split() |
7418d2bf |
return result
|
5d822c2d |
def adjustGCCSpecsInContainer(self, package, containerID, logPath, index=0):
opt = " " + SPECS.getData().getSecurityHardeningOption(package, index) |
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: |
87815216 |
with open(logFile, 'w') as logfile:
logfile.write(scriptLog.decode()) |
7418d2bf |
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")
|
5d822c2d |
def buildRPMSForGivenPackageInContainer(self, package, containerID, destLogPath=None, index=0): |
87815216 |
self.logger.info("Building rpm's for package " + package + " in container " +
containerID.short_id) |
7418d2bf |
|
5d822c2d |
listSourcesFiles = SPECS.getData().getSources(package, index)
listPatchFiles = SPECS.getData().getPatches(package, index)
specFile = SPECS.getData().getSpecFile(package, index) |
45c9260c |
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")
|
56c77555 |
#FIXME: some sources are located in SPECS/.. how to mount?
# if os.geteuid()==0: |
7418d2bf |
#TODO: mount it in, don't copy
macros = [] |
5d822c2d |
self._copySourcesToContainer(listSourcesFiles, package, containerID, sourcePath, index) |
7418d2bf |
#TODO: mount it in, don't copy |
5d822c2d |
self._copySourcesToContainer(listPatchFiles, package, containerID, sourcePath, index) |
326d5ca8 |
listAdditionalFiles, macros = self._getAdditionalBuildFiles(package)
self._copyAdditionalBuildFilesToContainer(listAdditionalFiles, containerID) |
7418d2bf |
# Add rpm macros |
343d89e8 |
listRPMMacros = constants.userDefinedMacros |
326d5ca8 |
for macroName, value in listRPMMacros.items():
macros.append(macroName + " " + value) |
7418d2bf |
# Build RPMs |
87815216 |
listRPMFiles = []
listSRPMFiles = [] |
7418d2bf |
try: |
326d5ca8 |
listRPMFiles, listSRPMFiles = self._buildRPMinContainer( |
87815216 |
specPath + specName,
rpmLogFile,
destLogFile,
containerID,
package,
macros)
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" |
87815216 |
if (constants.rpmCheck and
package in constants.testForceRPMS and |
5d822c2d |
SPECS.getData().isCheckAvailable(package, index)): |
87815216 |
cmd = "sed -i '/^Executing(%check):/,/^Processing files:/{//!b};d' " + rpmLog
logFile = destLogPath + "/adjustTestFile.log" |
0e4f2bff |
returnVal = CommandUtils().runCommandInShell(cmd, logFile) |
87815216 |
testLogFile = destLogPath + "/" + package + "-test.log" |
0e4f2bff |
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) |
326d5ca8 |
rpmDestDir = self._getRPMDestDir(rpmName, constants.rpmPath) |
7418d2bf |
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) |
326d5ca8 |
srpmDestDir = self._getRPMDestDir(os.path.basename(srpmFile), constants.sourceRpmPath) |
7418d2bf |
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.")
|
326d5ca8 |
def _getRPMArch(self, rpmName):
arch = ""
if "x86_64" in rpmName:
arch = "x86_64"
elif "aarch64" in rpmName:
arch = "aarch64"
elif "noarch" in rpmName:
arch = "noarch"
return arch
def _getRPMDestDir(self, rpmName, rpmDir):
arch = self._getRPMArch(rpmName)
rpmDestDir = rpmDir + "/" + arch
return rpmDestDir
|
e45f5730 |
def _getProperVersion(self,package,parseSpecObj):
listOfVersionObjs=SPECS.getData().getSpecObj(package)
for num in listOfVersionObjs:
if parseSpecObj.compare == 'gte':
if LooseVersion(num.version) >= LooseVersion(parseSpecObj.version):
return num.version
elif parseSpecObj.compare == 'lte':
if LooseVersion(num.version) <= LooseVersion(parseSpecObj.version):
return num.version
elif parseSpecObj.compare == 'eq':
if LooseVersion(num.version) == LooseVersion(parseSpecObj.version):
return num.version
elif parseSpecObj.compare == 'lt':
if LooseVersion(num.version) < LooseVersion(parseSpecObj.version):
return num.version
elif parseSpecObj.compare == 'gt':
if LooseVersion(num.version) > LooseVersion(parseSpecObj.version):
return num.version
return "*"
|
326d5ca8 |
def _copyRPM(self, rpmFile, destDir):
cmdUtils = CommandUtils()
rpmName = os.path.basename(rpmFile)
rpmDestDir = self._getRPMDestDir(rpmName, destDir)
# shutil is not atomic. copy & move to ensure atomicity.
rpmDestPath = rpmDestDir + "/" + rpmName
rpmDestPathTemp = (rpmDestDir + "/." +
''.join([random.choice(string.ascii_letters +
string.digits) for n in range(10)]))
if os.geteuid() == 0:
if not os.path.isdir(rpmDestDir):
cmdUtils.runCommandInShell("mkdir -p " + rpmDestDir)
shutil.copyfile(rpmFile, rpmDestPathTemp)
shutil.move(rpmDestPathTemp, rpmDestPath)
return rpmDestPath
|
5d822c2d |
def _verifyShaAndGetSourcePath(self, source, package, index=0): |
326d5ca8 |
cmdUtils = CommandUtils()
# Fetch/verify sources if sha1 not None. |
5d822c2d |
sha1 = SPECS.getData().getSHA1(package, source, index) |
326d5ca8 |
if sha1 is not None: |
a00911af |
PullSources.get(package, source, sha1, constants.sourcePath,
constants.pullsourcesConfig, self.logger) |
326d5ca8 |
sourcePath = cmdUtils.findFile(source, constants.sourcePath)
if not sourcePath:
sourcePath = cmdUtils.findFile(source, constants.specPath)
if not sourcePath:
if sha1 is None:
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
|
5d822c2d |
def _copySourcesTobuildroot(self, listSourceFiles, package, destDir, index=0): |
326d5ca8 |
for source in listSourceFiles: |
5d822c2d |
sourcePath = self._verifyShaAndGetSourcePath(source, package, index) |
326d5ca8 |
self.logger.info("Copying... Source path :" + source +
" Source filename: " + sourcePath[0])
shutil.copy2(sourcePath[0], destDir)
def _copyAdditionalBuildFiles(self, listAdditionalFiles, chrootID):
cmdUtils = CommandUtils()
for additionalFile in listAdditionalFiles:
source = additionalFile["src"]
destDir = chrootID + additionalFile["dst"]
self.logger.info("Copying additional Source build files :" + source)
if os.path.exists(source):
if os.path.isfile(source):
shutil.copy(source, destDir)
else:
shutil.copytree(source, destDir)
def _getAdditionalBuildFiles(self, package):
listAdditionalFiles = []
macros = []
if package in constants.buildOptions.keys():
pkg = constants.buildOptions[package]
filelist = pkg["files"]
for f in filelist:
listAdditionalFiles.append(f)
macrolist = pkg["macros"]
for macro in macrolist:
macros.append(macro)
return listAdditionalFiles, macros
def _buildRPM(self, specFile, logFile, chrootCmd, package, macros):
rpmBuildcmd = self.rpmbuildBinary + " " + self.rpmbuildBuildallOption
if constants.rpmCheck and package in constants.testForceRPMS:
self.logger.info("#" * (68 + 2 * len(package)))
if not SPECS.getData().isCheckAvailable(package):
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
cmdUtils = CommandUtils()
self.logger.info("Building rpm....")
self.logger.info(rpmBuildcmd)
returnVal = cmdUtils.runCommandInShell(rpmBuildcmd, logFile, chrootCmd)
if constants.rpmCheck and package in constants.testForceRPMS:
if not SPECS.getData().isCheckAvailable(package):
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 = []
with open(logFile, 'r') as logfile:
fileContents = logfile.readlines()
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().endswith(".rpm") and
"/RPMS/" in listcontents[1]):
listRPMFiles.append(listcontents[1])
if ((len(listcontents) == 2) and
listcontents[1].strip().endswith(".src.rpm") and
"/SRPMS/" in listcontents[1]):
listSRPMFiles.append(listcontents[1])
return listRPMFiles, listSRPMFiles
|
5d822c2d |
def _copySourcesToContainer(self, listSourceFiles, package, containerID, destDir, index=0): |
326d5ca8 |
cmdUtils = CommandUtils()
for source in listSourceFiles: |
5d822c2d |
sourcePath = self._verifyShaAndGetSourcePath(source, package, index) |
326d5ca8 |
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"]
destDir = additionalFile["dst"]
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 additional source file: " + source)
copyCmd += " " + destPath
else:
self.logger.info("Copying additional 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 += platform.machine()+"/"
rpmPath += rpmName
return rpmPath
def _buildRPMinContainer(self, specFile, rpmLogFile, destLogFile, containerID, package, macros): |
7418d2bf |
|
7f9d2e12 |
rpmBuildCmd = self.rpmbuildBinary + " " + self.rpmbuildBuildallOption |
7418d2bf |
if constants.rpmCheck and package in constants.testForceRPMS: |
87815216 |
self.logger.info("#" * (68 + 2 * len(package))) |
5d822c2d |
if not SPECS.getData().isCheckAvailable(package, index): |
87815216 |
self.logger.info("####### " + package +
" MakeCheck is not available. Skipping MakeCheck TEST for " +
package + " #######")
rpmBuildCmd = self.rpmbuildBinary + " --clean" |
7418d2bf |
else: |
87815216 |
self.logger.info("####### " + package +
" MakeCheck is available. Running MakeCheck TEST for " +
package + " #######")
rpmBuildCmd = self.rpmbuildBinary + " " + self.rpmbuildCheckOption
self.logger.info("#" * (68 + 2 * len(package))) |
7418d2bf |
else: |
87815216 |
rpmBuildCmd += " "+self.rpmbuildNocheckOption |
7418d2bf |
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: |
5d822c2d |
if not SPECS.getData().isCheckAvailable(package, index): |
87815216 |
constants.testLogger.info(package + " : N/A") |
7418d2bf |
elif returnVal: |
87815216 |
constants.testLogger.info(package + " : PASS") |
7418d2bf |
else: |
87815216 |
constants.testLogger.error(package + " : FAIL") |
7418d2bf |
if constants.rpmCheck:
if not returnVal and constants.rpmCheckStopOnError: |
87815216 |
self.logger.error("Checking rpm is failed " + specFile) |
7418d2bf |
raise Exception("RPM check failed")
else:
if not returnVal: |
87815216 |
self.logger.error("Building rpm is failed " + specFile) |
7418d2bf |
raise Exception("RPM build failed")
#Extracting rpms created from log file |
87815216 |
listRPMFiles = []
listSRPMFiles = [] |
326d5ca8 |
with open(destLogFile, 'r') as logfile:
rpmBuildLogLines = logfile.readlines()
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().endswith(".rpm") and
"/RPMS/" in listcontents[1]):
listRPMFiles.append(listcontents[1])
if ((len(listcontents) == 2) and
listcontents[1].strip().endswith(".src.rpm") and
"/SRPMS/" in listcontents[1]):
listSRPMFiles.append(listcontents[1]) |
7418d2bf |
#if not listRPMFiles:
# self.logger.error("Building rpm failed for " + specFile)
# raise Exception("RPM Build failed")
return listRPMFiles, listSRPMFiles |