def validateConfig(self): """ __validateConfig__ Checking if needed input parameters are there. Not all the commands require a configuration. """ ## Check that the configuration object has the sections we expect it to have. ## (WMCore already checks that attributes added to the configuration object are of type ConfigSection.) ## Even if not all configuration sections need to be there, we anyway request ## the user to add all the sections in the configuration file. if not hasattr(self.configuration, 'General'): msg = "Invalid CRAB configuration: Section 'General' is missing." return False, msg if not hasattr(self.configuration, 'JobType'): msg = "Invalid CRAB configuration: Section 'JobType' is missing." return False, msg if not hasattr(self.configuration, 'Data'): msg = "Invalid CRAB configuration: Section 'Data' is missing." return False, msg if not hasattr(self.configuration, 'Site'): msg = "Invalid CRAB configuration: Section 'Site' is missing." return False, msg ## Some parameters may have been renamed. Check here if the configuration file has an old ## parameter defined, and in that case tell the user what is the new parameter name. for old_param, new_param in renamedParams.iteritems(): if len(old_param.split('.')) != 2 or len(new_param['newParam'].split('.')) != 2: continue old_param_section, old_param_name = old_param.split('.') if hasattr(self.configuration, old_param_section) and hasattr(getattr(self.configuration, old_param_section), old_param_name): msg = "Invalid CRAB configuration: Parameter %s has been renamed to %s" % (old_param, new_param['newParam']) if new_param['version'] != None: msg += " starting from CRAB %s" % (new_param['version']) msg += "; please change your configuration file accordingly." return False, msg ## Check if there are unknown parameters (and try to suggest the correct parameter name). all_config_params = configParametersInfo.keys() SpellChecker.DICTIONARY = SpellChecker.train(all_config_params) for section in self.configuration.listSections_(): for attr in getattr(self.configuration, section).listSections_(): param = (section + '.' + attr) if not SpellChecker.is_correct(param): msg = "Invalid CRAB configuration: Parameter %s is not known." % (param) if SpellChecker.correct(param) != param: msg += " Maybe you mean %s?" % (SpellChecker.correct(param)) return False, msg ## Check that each parameter specified in the configuration file is of the ## type specified in the configuration map. ## Check that, if a parameter is a required one and it has no default value, ## then it must be specified in the configuration file. for paramName, paramInfo in configParametersInfo.iteritems(): requiredTypeName = paramInfo['type'] try: requiredType = getattr(types, requiredTypeName) except AttributeError: msg = "Invalid type %s specified in CRABClient configuration mapping for parameter %s." % (requiredTypeName, paramName) return False, msg attrs = paramName.split('.') obj = self.configuration while attrs and obj is not None: obj = getattr(obj, attrs.pop(0), None) if obj is not None: if not isinstance(obj, requiredType): msg = "Invalid CRAB configuration: Parameter %s requires a value of type %s (while a value of type %s was given)." \ % (paramName, str(requiredType), str(type(obj))) if paramName == "Data.userInputFiles": msg += "\nIn CRAB v3.3.14 the configuration parameter Data.userInputFiles has been modified to directly take a (python) list of primary input files." msg += " Previously it was taking the name of a local text file where the primary input files were listed." msg += " One can still use a text file and convert its content into a python list by doing Data.userInputFiles = list(open('my_list_of_files.txt'))" return False, msg elif requiredType == list: if not all(isinstance(arg, str) for arg in obj): msg = "Invalid CRAB configuration: Parameter %s has to be a list of strings." % (paramName) return False, msg elif getParamDefaultValue(paramName) is None and paramInfo['required']: msg = "Invalid CRAB configuration: Parameter %s is missing." % (paramName) return False, msg return True, "Valid configuration"
def validateConfig(self): """ __validateConfig__ Checking if needed input parameters are there. Not all the commands require a configuration. """ ## Check that the configuration object has the sections we expect it to have. ## (WMCore already checks that attributes added to the configuration object are of type ConfigSection.) ## Even if not all configuration sections need to be there, we anyway request ## the user to add all the sections in the configuration file. if not hasattr(self.configuration, 'General'): msg = "Invalid CRAB configuration: Section 'General' is missing." return False, msg if not hasattr(self.configuration, 'JobType'): msg = "Invalid CRAB configuration: Section 'JobType' is missing." return False, msg if not hasattr(self.configuration, 'Data'): msg = "Invalid CRAB configuration: Section 'Data' is missing." return False, msg if not hasattr(self.configuration, 'Site'): msg = "Invalid CRAB configuration: Section 'Site' is missing." return False, msg ## Some parameters may have been renamed. Check here if the configuration file has an old ## parameter defined, and in that case tell the user what is the new parameter name. for old_param, new_param in renamedParams.iteritems(): if len(old_param.split('.')) != 2 or len( new_param['newParam'].split('.')) != 2: continue old_param_section, old_param_name = old_param.split('.') if hasattr(self.configuration, old_param_section) and hasattr( getattr(self.configuration, old_param_section), old_param_name): msg = "Invalid CRAB configuration: Parameter %s has been renamed to %s" % ( old_param, new_param['newParam']) if new_param['version'] != None: msg += " starting from CRAB %s" % (new_param['version']) msg += "; please change your configuration file accordingly." return False, msg ## Check if there are unknown parameters (and try to suggest the correct parameter name). all_config_params = configParametersInfo.keys() SpellChecker.DICTIONARY = SpellChecker.train(all_config_params) for section in self.configuration.listSections_(): for attr in getattr(self.configuration, section).listSections_(): param = (section + '.' + attr) if not SpellChecker.is_correct(param): msg = "Invalid CRAB configuration: Parameter %s is not known." % ( param) if SpellChecker.correct(param) != param: msg += " Maybe you mean %s?" % ( SpellChecker.correct(param)) return False, msg ## Check that each parameter specified in the configuration file is of the ## type specified in the configuration map. ## Check that, if a parameter is a required one and it has no default value, ## then it must be specified in the configuration file. for paramName, paramInfo in configParametersInfo.iteritems(): requiredTypeName = paramInfo['type'] try: requiredType = getattr(types, requiredTypeName) except AttributeError: msg = "Invalid type %s specified in CRABClient configuration mapping for parameter %s." % ( requiredTypeName, paramName) return False, msg attrs = paramName.split('.') obj = self.configuration while attrs and obj is not None: obj = getattr(obj, attrs.pop(0), None) if obj is not None: if not isinstance(obj, requiredType): msg = "Invalid CRAB configuration: Parameter %s requires a value of type %s (while a value of type %s was given)." \ % (paramName, str(requiredType), str(type(obj))) if paramName == "Data.totalUnits" and isinstance( obj, float): continue if paramName == "Data.userInputFiles": msg += "\nIn CRAB v3.3.14 the configuration parameter Data.userInputFiles has been modified to directly take a (python) list of primary input files." msg += " Previously it was taking the name of a local text file where the primary input files were listed." msg += " One can still use a text file and convert its content into a python list by doing Data.userInputFiles = list(open('my_list_of_files.txt'))" return False, msg elif requiredType == list: if not all(isinstance(arg, str) for arg in obj): msg = "Invalid CRAB configuration: Parameter %s has to be a list of strings." % ( paramName) return False, msg elif getParamDefaultValue( paramName) is None and paramInfo['required']: msg = "Invalid CRAB configuration: Parameter %s is missing." % ( paramName) return False, msg return True, "Valid configuration"