Esempio n. 1
0
    def getRunOptions(self, primary_section, version=None):
        """
        primary client code interface to the finished product.
        do not override this method

        This returns a tuple of the (1) a class holding all of the
        primary run options gathered from the primary section of the ini
        file and command-line options and (2) an inifile hash-of-hashes
        reflecting all sections of the ini file.
        """
        def updateIniSections(data, newData):
            for k in newData.keys():
                if k not in data: data[k] = {}
                for kk in newData[k].keys():
                    data[k][kk] = newData[k][kk]

        # first level of options are those hard coded into the python code as defaults,
        # these have the lowest precedence:
        #
        iniSections = {primary_section: self.getOptionDefaults()}

        # next is the 'global' ini file, in the same directory as the configure
        # script:
        cmdlineScriptName = os.path.basename(sys.argv[0])
        configFileName = cmdlineScriptName + ".ini"

        cmdlineScriptDir = os.path.abspath(os.path.dirname(sys.argv[0]))
        globalConfigPath = os.path.join(cmdlineScriptDir, configFileName)
        updateIniSections(iniSections, getIniSections(globalConfigPath))

        # finally there is the local ini path:
        localConfigPath = os.path.join(os.path.abspath('.'), configFileName)
        updateIniSections(iniSections, getIniSections(localConfigPath))

        parser = self._getOptionParser(iniSections[primary_section],
                                       configFileName,
                                       version=version)
        (options, args) = parser.parse_args()

        if options.isAllHelp:
            parser = self._getOptionParser(iniSections[primary_section],
                                           configFileName,
                                           True,
                                           version=version)
            parser.print_help()
            sys.exit(2)

        if len(args):  # or (len(sys.argv) == 1):
            parser.print_help()
            sys.exit(2)

        try:
            # sanitize arguments before writing defaults, check for missing arguments after:
            #
            self.validateAndSanitizeExistingOptions(options)

            # write options object back into full iniSections object:
            #
            for k, v in vars(options).iteritems():
                if k == "isAllHelp": continue
                if k == "isWriteConfig": continue
                iniSections[primary_section][k] = v

            # write ini file back out if required:
            if options.isWriteConfig == True:
                dumpIniSections(configFileName, iniSections)
                sys.exit(0)

            self.validateOptionExistence(options)

        except OptParseException, e:
            noArgOrError(parser, str(e))
Esempio n. 2
0
    def getRunOptions(self, primary_section, version=None, configHelp=None):
        """
        primary client code interface to the finished product.
        do not override this method

        This returns a tuple of the (1) a class holding all of the
        primary run options gathered from the primary section of the ini
        file and command-line options and (2) an inifile hash-of-hashes
        reflecting all sections of the ini file.
        """
        def updateIniSections(data, newData):
            for k in newData.keys():
                if k not in data: data[k] = {}
                for kk in newData[k].keys():
                    data[k][kk] = newData[k][kk]

        # first level of options are those hard coded into the python code as defaults,
        # these have the lowest precedence:
        #
        iniSections = {primary_section: self.getOptionDefaults()}

        # next is the 'global' ini file, in the same directory as the configure
        # script:
        realArg0 = os.path.realpath(sys.argv[0])
        cmdlineScriptName = os.path.basename(realArg0)
        configFileName = cmdlineScriptName + ".ini"

        cmdlineScriptDir = os.path.abspath(os.path.dirname(realArg0))
        globalConfigPath = os.path.join(cmdlineScriptDir, configFileName)
        updateIniSections(iniSections, getIniSections(globalConfigPath))

        parser = self._getOptionParser(iniSections[primary_section],
                                       configFileName,
                                       cmdlineScriptDir,
                                       version=version,
                                       configHelp=configHelp)
        (options, args) = parser.parse_args()

        try:
            if options.userConfigPath:
                if not os.path.isfile(options.userConfigPath):
                    raise OptParseException("Can't find config file: '%s'" %
                                            (options.userConfigPath))

                updateIniSections(iniSections,
                                  getIniSections(options.userConfigPath))

                # reparse with updated default values:
                parser = self._getOptionParser(iniSections[primary_section],
                                               configFileName,
                                               cmdlineScriptDir,
                                               version=version,
                                               configHelp=configHelp)
                (options, args) = parser.parse_args()
            else:
                if not os.path.isfile(globalConfigPath):
                    raise OptParseException(
                        "Can't find default config file: '%s'" %
                        (globalConfigPath))

            if options.isAllHelp:
                # this second call to getOptionParser is only here to provide the extended help option:
                parser = self._getOptionParser(iniSections[primary_section],
                                               configFileName,
                                               cmdlineScriptDir,
                                               True,
                                               version=version,
                                               configHelp=configHelp)
                parser.print_help()
                sys.exit(2)

            nargs = len(args)
            if nargs:
                plural = ""
                if nargs > 1: plural = "s"
                raise OptParseException("%i unrecognized argument%s:\n%s" %
                                        (nargs, plural, "\n".join(
                                            ["'" + arg + "'"
                                             for arg in args])))

            self.validateAndSanitizeOptions(options)

            # write options object back into full iniSections object:
            #
            for k, v in vars(options).iteritems():
                if k == "isAllHelp": continue
                iniSections[primary_section][k] = v

        except OptParseException as e:
            noArgOrError(parser, str(e))

        return options, iniSections
Esempio n. 3
0
    def getRunOptions(self, primary_section, version = None, configHelp=None) :
        """
        primary client code interface to the finished product.
        do not override this method

        This returns a tuple of the (1) a class holding all of the
        primary run options gathered from the primary section of the ini
        file and command-line options and (2) an inifile hash-of-hashes
        reflecting all sections of the ini file.
        """

        def updateIniSections(data,newData) :
            for k in newData.keys() :
                if k not in data : data[k] = {}
                for kk in newData[k].keys() :
                    data[k][kk] = newData[k][kk]


        # first level of options are those hard coded into the python code as defaults,
        # these have the lowest precedence:
        #
        iniSections = { primary_section : self.getOptionDefaults() }

        # next is the 'global' ini file, in the same directory as the configure
        # script:
        cmdlineScriptName=os.path.basename(sys.argv[0])
        configFileName=cmdlineScriptName+".ini"

        cmdlineScriptDir=os.path.abspath(os.path.dirname(sys.argv[0]))
        globalConfigPath=os.path.join(cmdlineScriptDir,configFileName)
        updateIniSections(iniSections,getIniSections(globalConfigPath))

        parser=self._getOptionParser(iniSections[primary_section],configFileName, cmdlineScriptDir,
                                     version=version, configHelp=configHelp)
        (options,args) = parser.parse_args()

        if options.userConfigPath :
            if not os.path.isfile(options.userConfigPath) :
                raise OptParseException("Can't find config file: '%s'" % (options.userConfigPath))

            updateIniSections(iniSections,getIniSections(options.userConfigPath))

            # reparse with updated default values:
            parser=self._getOptionParser(iniSections[primary_section],configFileName, cmdlineScriptDir,
                                         version=version, configHelp=configHelp)
            (options,args) = parser.parse_args()

        if options.isAllHelp :
            # this second call to getOptionParser is only here to provide the extended help option:
            parser=self._getOptionParser(iniSections[primary_section],configFileName, cmdlineScriptDir, True,
                                         version=version, configHelp=configHelp)
            parser.print_help()
            sys.exit(2)

        if len(args) : # or (len(sys.argv) == 1):
            parser.print_help()
            sys.exit(2)

        try :
            # sanitize arguments before writing defaults, check for missing arguments after:
            #
            self.validateAndSanitizeExistingOptions(options)

            # write options object back into full iniSections object:
            #
            for k,v in vars(options).iteritems() :
                if k == "isAllHelp" : continue
                iniSections[primary_section][k] = v

            self.validateOptionExistence(options)

        except OptParseException, e :
            noArgOrError(parser,str(e))