Source code for mojo.libdist.CommandLineOptions

"""
CommandLineOptions
==================
Contains class 'CommandLineOptions' which offers all methods necessary to parse the command line.
For parsing you have to call the method parse. It uses a Parser created by the method getParser
to read in the command line.
"""


import argparse
import os

from ..bricabrac import fileIO
from .. import getMojoVersion
from . import libdistData


[docs]class CommandLineOptions: """ Processed command line options :param argv: command line arguments :type argv: list """ def __init__(self, argv): """ :param argv: command line arguments :type argv: list """ # path to XML file containing build configuration specifications. self.configFilePath = "" # number of parallel builds self.nBuilds = 1 # path to local source self.sourceCodePath = "" # path to the output directory self.outputPath = "" # name of the settings to build self.settings = [] # verbosity self.verbosity = 2 # keepOutFile self.keepOutput = False # save a copy of argv self.argv = list(argv)
[docs] def parse(self): """ Parse command line using a parser created by getParser The command line options are saved in the CommandLineOptions object """ parser = self.getParser() parser.parse_args(self.argv, namespace=self) self.outputPath = os.path.abspath(self.outputDirectory)
[docs] def getParser(self): """ Creates parser which is used by parse :return: parser object to parse command line arguments :rtype: ArgumentParser """ parser = argparse.ArgumentParser() parser.add_argument("--configFile", dest="configFile", help="path of the XML file", action=ConfigFileAction, required=True) parser.add_argument("--nBuilds", dest="nBuilds", help=f"number of parallel builds (default = {libdistData.N_BUILDS}) (max number of parallel processes is nBuilds * nProcsPerBuild)", type=int, default=libdistData.N_BUILDS) parser.add_argument("--nProcsPerBuild", dest="nProcsPerBuild", help=f"number of processors per build (default = {libdistData.MAKE_NPROCS}) (max number of parallel processes is nBuilds * nProcsPerBuild)", type=int, default=libdistData.MAKE_NPROCS) parser.add_argument("--path", dest="sourceCodePath", help="path to local source", required=True) parser.add_argument("--output", dest="outputDirectory", help="path to the output directory", default=libdistData.DEFAULT_LIBDIST_OUTPUT_DIRECTORY) parser.add_argument("--set", dest="settings", nargs="*", help="config settings to build, default: build all", default=[]) parser.add_argument("--verbose", dest="verbosity", type=int, default=2, choices=list(range(0, 4)), help="control quantity of output, possible values: 0,1,2,3") parser.add_argument("--keepOutput", dest="keepOutput", action='store_true', help="do not remove the output files\ created by the jobmanagement also if all jobs succeeded") parser.add_argument("--version", "-v", action="version", version=getMojoVersion()) return parser
def __str__(self): """ Print all informations stored in the object. :return: informations stored in CommandLineOptions :rtype: string """ return ("Command Line Options\n\n" f"Config file: {self.configFilePath}\n" f"Number of processes: {self.nBuilds}\n" f"Sourcecode path: {self.sourceCodePath}\n" f"Output path: {self.outputPath}\n" f"Settings: {self.settings}" )
[docs]class ConfigFileAction(argparse.Action): """ class to control if the given or default path exists if the path already exists an error is raised if not the path is saved in the CommandLineOptions object """ def __call__(self, parser, namespace, values, option_string=None): try: namespace.configFilePath = fileIO.getAbsoluteFilePath(values) except RuntimeError: raise IOError("Cant't find XML in given path")
[docs]def parseCommandLine(argv): """ creates CommandLineOptions instance and uses it to parse the given command line :param argv: command line arguments :type argv: list :return: command line options :rtype: CommandLineOptions """ commandLineOptions = CommandLineOptions(argv) commandLineOptions.parse() return commandLineOptions