support/package-builder/builder.py
87815216
 #!/usr/bin/env python3
3ad60002
 
0f3948ba
 from argparse import ArgumentParser
2820c61a
 import os.path
0f1fdc4b
 import platform
87815216
 import collections
 import traceback
 import sys
 import json
326d5ca8
 import copy
2820c61a
 from CommandUtils import CommandUtils
 from Logger import Logger
 from constants import constants
5062126c
 from PackageManager import PackageManager
45c9260c
 from SpecData import SPECS
97a9151c
 from PackageInfo import PackageInfo
b650a058
 
2820c61a
 def main():
8f56b626
     parser = ArgumentParser()
87815216
     parser.add_argument("-s", "--spec-path", dest="specPath", default="../../SPECS")
     parser.add_argument("-x", "--source-path", dest="sourcePath",
                         default="../../stage/SOURCES")
     parser.add_argument("-r", "--rpm-path", dest="rpmPath",
                         default="../../stage/RPMS")
     parser.add_argument("-i", "--install-package", dest="installPackage",
                         default=False, action="store_true")
     parser.add_argument("-p", "--publish-RPMS-path", dest="publishRPMSPath",
                         default="../../stage/PUBLISHRPMS")
     parser.add_argument("-e", "--publish-XRPMS-path", dest="publishXRPMSPath",
                         default="../../stage/PUBLISHXRPMS")
     parser.add_argument("-l", "--log-path", dest="logPath", default="../../stage/LOGS")
26b55679
     parser.add_argument("-y", "--log-level", dest="logLevel", default="error")
87815216
     parser.add_argument("-z", "--top-dir-path", dest="topDirPath", default="/usr/src/photon")
     parser.add_argument("-b", "--build-root-path", dest="buildRootPath", default="/mnt")
     parser.add_argument("-t", "--threads", dest="buildThreads",
                         default=1, type=int, help="Number of working threads")
     parser.add_argument("-m", "--tool-chain-stage", dest="toolChainStage", default="None")
     parser.add_argument("-c", "--pullsources-config", dest="pullsourcesConfig",
                         default="pullsources.conf")
9bc3518e
     parser.add_argument("-d", "--dist-tag", dest="dist", default="")
87815216
     parser.add_argument("-k", "--input-RPMS-path", dest="inputRPMSPath", default=None)
     parser.add_argument("-n", "--build-number", dest="buildNumber", default="0000000")
     parser.add_argument("-v", "--release-version", dest="releaseVersion", default="NNNnNNN")
     parser.add_argument("-u", "--enable-rpmcheck", dest="rpmCheck",
                         default=False, action="store_true")
     parser.add_argument("-a", "--source-rpm-path", dest="sourceRpmPath",
                         default="../../stage/SRPMS")
     parser.add_argument("-w", "--pkginfo-file", dest="pkgInfoFile",
                         default="../../stage/pkg_info.json")
     parser.add_argument("-g", "--pkg-build-option-file", dest="pkgBuildOptionFile",
                         default="../../common/data/pkg_build_options.json")
     parser.add_argument("-q", "--rpmcheck-stop-on-error", dest="rpmCheckStopOnError",
                         default=False, action="store_true")
     parser.add_argument("-bd", "--publish-build-dependencies", dest="publishBuildDependencies",
                         default=False)
620867de
     parser.add_argument("-pw", "--package-weights-path", dest="packageWeightsPath", default=None)
8f56b626
     parser.add_argument("-bt", "--build-type", dest="pkgBuildType", choices=['chroot', 'container'], default="chroot")
87815216
     parser.add_argument("-F", "--kat-build", dest="katBuild", default=None)
9bc3518e
     parser.add_argument("-pj", "--packages-json-input", dest="pkgJsonInput", default=None)
0f3948ba
     parser.add_argument("PackageName", nargs='?')
     options = parser.parse_args()
87815216
     cmdUtils = CommandUtils()
44991444
     if not os.path.isdir(options.logPath):
87815216
         cmdUtils.runCommandInShell("mkdir -p " + options.logPath)
2cfb758d
 
26b55679
     logger = Logger.getLogger("Main", options.logPath, options.logLevel)
87815216
     errorFlag = False
b650a058
     package = None
97a9151c
     pkgInfoJsonFile = options.pkgInfoFile
07f00ab3
     if not os.path.isdir(options.sourcePath):
87815216
         logger.error("Given Sources Path is not a directory:" + options.sourcePath)
07f00ab3
         errorFlag = True
     if not os.path.isdir(options.specPath):
87815216
         logger.error("Given Specs Path is not a directory:" + options.specPath)
07f00ab3
         errorFlag = True
     if not os.path.isdir(options.publishRPMSPath):
87815216
         logger.error("Given RPMS Path is not a directory:" + options.publishRPMSPath)
07f00ab3
         errorFlag = True
062d56d1
     if not os.path.isdir(options.publishXRPMSPath):
87815216
         logger.error("Given X RPMS Path is not a directory:" + options.publishXRPMSPath)
062d56d1
         errorFlag = True
87815216
     if not os.path.isdir(options.publishRPMSPath + "/" + platform.machine()):
         logger.error("Given RPMS Path is missing " + platform.machine()+
                      " sub-directory:"+options.publishRPMSPath)
07f00ab3
         errorFlag = True
0f1fdc4b
     if not os.path.isdir(options.publishXRPMSPath+"/" + platform.machine()):
87815216
         logger.error("Given X RPMS Path is missing "+platform.machine()+
                      " sub-directory:"+options.publishXRPMSPath)
062d56d1
         errorFlag = True
07f00ab3
     if not os.path.isdir(options.publishRPMSPath+"/noarch"):
87815216
         logger.error("Given RPMS Path is missing noarch sub-directory:"+
                      options.publishRPMSPath)
07f00ab3
         errorFlag = True
062d56d1
     if not os.path.isdir(options.publishXRPMSPath+"/noarch"):
87815216
         logger.error("Given X RPMS Path is missing noarch sub-directory:"+
                      options.publishXRPMSPath)
062d56d1
         errorFlag = True
8f56b626
 
5062126c
 
42ffccb5
     if options.inputRPMSPath is not None and not os.path.isdir(options.inputRPMSPath):
062d56d1
         logger.error("Given input RPMS Path is not a directory:"+options.inputRPMSPath)
42ffccb5
         errorFlag = True
5062126c
 
620867de
     if options.packageWeightsPath is not None and not os.path.isfile(options.packageWeightsPath):
         logger.error("Given input Weights file is not a file:"+options.packageWeightsPath)
         errorFlag = True
 
9bc3518e
     if options.pkgJsonInput is not None and not os.path.isfile(options.pkgJsonInput):
         logger.error("Given input packages file is not a file:"+options.pkgJsonInput)
         errorFlag = True
 
87815216
     if options.installPackage:
0f3948ba
         if not options.PackageName:
1fd3eff3
             logger.error("Please provide package name")
             errorFlag = True
         else:
87815216
             package = options.PackageName
5062126c
 
07f00ab3
     if errorFlag:
         logger.error("Found some errors. Please fix input options and re-run it.")
         return False
5062126c
 
2820c61a
     if not os.path.isdir(options.rpmPath):
0f1fdc4b
         cmdUtils.runCommandInShell("mkdir -p "+options.rpmPath+"/"+platform.machine())
518d6a6f
         cmdUtils.runCommandInShell("mkdir -p "+options.rpmPath+"/noarch")
d2526915
 
     if not os.path.isdir(options.sourceRpmPath):
         cmdUtils.runCommandInShell("mkdir -p "+options.sourceRpmPath)
5062126c
 
d23d369f
     if not os.path.isdir(options.buildRootPath):
085306ae
         cmdUtils.runCommandInShell("mkdir -p " + options.buildRootPath)
5062126c
 
26b55679
     logger.debug("Source Path :"+options.sourcePath)
     logger.debug("Spec Path :" + options.specPath)
     logger.debug("Rpm Path :" + options.rpmPath)
     logger.debug("Log Path :" + options.logPath)
     logger.debug("Log Level :" + options.logLevel)
     logger.debug("Top Dir Path :" + options.topDirPath)
     logger.debug("Publish RPMS Path :" + options.publishRPMSPath)
     logger.debug("Publish X RPMS Path :" + options.publishXRPMSPath)
b5e09fac
 
97a9151c
     if options.installPackage:
26b55679
         logger.debug("Package to build:" + package)
5062126c
 
085306ae
     get_packages_with_build_options(options.pkgBuildOptionFile)
8bcb2fdd
 
07f00ab3
     try:
accc8120
 
         constants.setSpecPath(options.specPath)
         constants.setSourcePath(options.sourcePath)
         constants.setRpmPath(options.rpmPath)
         constants.setSourceRpmPath(options.sourceRpmPath)
         constants.setTopDirPath(options.topDirPath)
         constants.setLogPath(options.logPath)
26b55679
         constants.setLogLevel(options.logLevel)
accc8120
         constants.setDist(options.dist)
         constants.setBuildNumber(options.buildNumber)
         constants.setReleaseVersion(options.releaseVersion)
         constants.setPrevPublishRPMRepo(options.publishRPMSPath)
         constants.setPrevPublishXRPMRepo(options.publishXRPMSPath)
         constants.setBuildRootPath(options.buildRootPath)
aac331d9
         constants.setPullSourcesURL(get_baseurl(options.pullsourcesConfig))
accc8120
         constants.setInputRPMSPath(options.inputRPMSPath)
         constants.setRPMCheck(options.rpmCheck)
         constants.setRpmCheckStopOnError(options.rpmCheckStopOnError)
         constants.setPublishBuildDependencies(options.publishBuildDependencies)
         constants.setPackageWeightsPath(options.packageWeightsPath)
         constants.setKatBuild(options.katBuild)
 
         constants.initialize()
45c9260c
         # parse SPECS folder
87815216
         SPECS()
accc8120
         if options.toolChainStage == "stage1":
db36e75e
             pkgManager = PackageManager()
             pkgManager.buildToolChain()
         elif options.toolChainStage == "stage2":
             pkgManager = PackageManager()
6a4bde38
             pkgManager.buildToolChainPackages(options.buildThreads)
db36e75e
         elif options.installPackage:
4b1a41cb
             buildSpecifiedPackages([package], options.buildThreads, options.pkgBuildType)
9bc3518e
         elif options.pkgJsonInput:
4b1a41cb
             buildPackagesInJson(options.pkgJsonInput, options.buildThreads,
                                 options.pkgBuildType, pkgInfoJsonFile, logger)
07f00ab3
         else:
4b1a41cb
             buildPackagesForAllSpecs(options.buildThreads, options.pkgBuildType,
                                      pkgInfoJsonFile, logger)
07f00ab3
     except Exception as e:
         logger.error("Caught an exception")
         logger.error(str(e))
d355ea7e
         # print stacktrace
         traceback.print_exc()
8846e378
         sys.exit(1)
     sys.exit(0)
07f00ab3
 
920a9773
 
4b1a41cb
 def buildSpecifiedPackages(listPackages, buildThreads, pkgBuildType, pkgInfoJsonFile=None, logger=None):
b5e09fac
     if constants.rpmCheck:
326d5ca8
         constants.setTestForceRPMS(copy.copy(listPackages))
4b1a41cb
     pkgManager = PackageManager(pkgBuildType=pkgBuildType)
     pkgManager.buildPackages(listPackages, buildThreads)
 
     if pkgInfoJsonFile is not None:
         # Generating package info file which is required by installer
         if logger is not None:
             logger.debug("Writing Package info to the file:" + pkgInfoJsonFile)
         pkgInfo = PackageInfo()
         pkgInfo.loadPackagesData()
         pkgInfo.writePkgListToFile(pkgInfoJsonFile)
07f00ab3
 
4b1a41cb
 
 def buildPackagesInJson(pkgJsonInput, buildThreads, pkgBuildType, pkgInfoJsonFile, logger):
9bc3518e
     listPackages = []
     with open(pkgJsonInput) as jsonData:
         pkg_list_json = json.load(jsonData)
         listPackages = pkg_list_json["packages"]
4b1a41cb
     buildSpecifiedPackages(listPackages, buildThreads, pkgBuildType, pkgInfoJsonFile, logger)
9bc3518e
 
accc8120
 
4b1a41cb
 def buildPackagesForAllSpecs(buildThreads, pkgBuildType, pkgInfoJsonFile, logger):
45c9260c
     listPackages = SPECS.getData().getListPackages()
4b1a41cb
     buildSpecifiedPackages(listPackages, buildThreads, pkgBuildType, pkgInfoJsonFile, logger)
90d8acae
 
accc8120
 
90d8acae
 def get_packages_with_build_options(pkg_build_options_file):
     if os.path.exists(pkg_build_options_file):
085306ae
         with open(pkg_build_options_file) as jsonData:
             pkg_build_option_json = json.load(jsonData, object_pairs_hook=collections.OrderedDict)
9bc3518e
             constants.setBuildOptions(pkg_build_option_json)
accc8120
 
aac331d9
 def get_baseurl(conf_file):
     with open(conf_file) as jsonFile:
         config = json.load(jsonFile)
     return config['baseurl']
 
5d05cfcc
 def get_all_package_names(build_install_option):
     base_path = os.path.dirname(build_install_option)
     packages = []
 
326d5ca8
     with open(build_install_option) as jsonData:
         option_list_json = json.load(jsonData, object_pairs_hook=collections.OrderedDict)
         options_sorted = option_list_json.items()
 
         for install_option in options_sorted:
             filename = os.path.join(base_path, install_option[1]["file"])
             with open(filename) as pkgJsonData:
                 package_list_json = json.load(pkgJsonData)
             packages = packages + package_list_json["packages"]
5d05cfcc
 
     return packages
 
accc8120
 
87815216
 if __name__ == "__main__":
2fcd8ac0
     main()