def __init__(self, definition): """Creates an error interface from the given definition. If the definition is invalid, a :class:`job.configuration.interface.exceptions.InvalidInterfaceDefinition` exception will be thrown. :param definition: The interface definition :type definition: dict """ if definition is None: definition = {} self.definition = definition try: validate(definition, ERROR_INTERFACE_SCHEMA) except ValidationError as validation_error: raise InvalidInterfaceDefinition(validation_error) self._populate_default_values() if self.definition['version'] != '1.0': raise InvalidInterfaceDefinition( '%s is an unsupported version number' % self.definition['version'])
def __init__(self, definition): """Creates a job interface from the given definition. If the definition is invalid, a :class:`job.configuration.interface.exceptions.InvalidInterfaceDefinition` exception will be thrown. :param definition: The interface definition :type definition: dict """ self.definition = definition self._param_names = set() # Tuples used for validation with other classes self._property_validation_dict = {} # str->bool self._input_file_validation_dict = {} # str->tuple self._output_file_validation_list = [] self._output_file_manifest_dict = {} # str->bool try: validate(definition, JOB_INTERFACE_SCHEMA) except ValidationError as validation_error: raise InvalidInterfaceDefinition(validation_error) self._populate_default_values() if self.definition['version'] != '1.0': raise InvalidInterfaceDefinition( '%s is an unsupported version number' % self.definition['version']) self._check_param_name_uniqueness() self._validate_command_arguments() self._create_validation_dicts()
def create_legacy_error_mapping(error_dict): """Returns the error mapping for the given legacy error dict :param error_dict: The legacy error dict :type error_dict: dict :returns: The error mapping :rtype: :class:`job.error.mapping.JobErrorMapping` """ if error_dict is None: error_dict = {} mapping = JobErrorMapping(None) mapping.error_dict = error_dict if 'version' not in error_dict: error_dict['version'] = '1.0' if error_dict['version'] != '1.0': raise InvalidInterfaceDefinition( 'Invalid error interface version: %s' % error_dict['version']) if 'exit_codes' not in error_dict: error_dict['exit_codes'] = {} if not isinstance(error_dict['exit_codes'], dict): raise InvalidInterfaceDefinition('Invalid error interface') for exit_code, error_name in error_dict['exit_codes'].items(): exit_code = int(exit_code) error = JobError(None, error_name) mapping.add_mapping(exit_code, error) return mapping
def _check_param_name_uniqueness(self): """Ensures all the parameter names are unique, and throws a :class:`job.configuration.interface.exceptions.InvalidInterfaceDefinition` if they are not unique """ for input_data in self.definition['input_data']: if input_data['name'] in self._param_names: raise InvalidInterfaceDefinition('shared resource & input_data names must be unique') self._param_names.add(input_data['name']) for shared_resource in self.definition['shared_resources']: if shared_resource['name'] in self._param_names: raise InvalidInterfaceDefinition('shared resource & input_data names must be unique') self._param_names.add(shared_resource['name'])
def _validate_command_arguments(self): '''Ensure the command string is valid, and any parameters used are actually in the input_data or shared_resources. Will raise a :exception:`job.configuration.data.exceptions.InvalidInterfaceDefinition` if the arguments are not valid ''' command_arguments = self.definition['command_arguments'] param_pattern = '\$\{(?:[^\}]*:)?([^\}]*)\}' for param in re.findall(param_pattern, command_arguments): found_match = False for input_data in self.definition['input_data']: if input_data['name'] == param: found_match = True break if not found_match: for shared_resource in self.definition['shared_resources']: if shared_resource['name'] == param: found_match = True break #Look for system properties if param == 'job_output_dir': found_match = True if not found_match: msg = 'The %s parameter was not found in any inputs, shared_resources, or system variables' % param raise InvalidInterfaceDefinition(msg)
def _check_env_var_uniqueness(self): """Ensures all the enviornmental variable names are unique, and throws a :class:`job.configuration.interface.exceptions.InvalidInterfaceDefinition` if they are not unique """ env_vars = [env_var['name'] for env_var in self.definition['env_vars']] if len(env_vars) != len(set(env_vars)): raise InvalidInterfaceDefinition('Environment variable names must be unique')
def _check_setting_name_uniqueness(self): """Ensures all the settings names are unique, and throws a :class:`job.configuration.interface.exceptions.InvalidInterfaceDefinition` if they are not unique """ for setting in self.definition['settings']: if setting['name'] in self._param_names: raise InvalidInterfaceDefinition('Setting names must be unique') self._param_names.add(setting['name'])
def _check_mount_name_uniqueness(self): """Ensures all the mount names are unique, and throws a :class:`job.configuration.interface.exceptions.InvalidInterfaceDefinition` if they are not unique """ for mount in self.definition['mounts']: if mount['name'] in self._mount_names: raise InvalidInterfaceDefinition('Mount names must be unique') self._mount_names.add(mount['name'])
def convert_interface(interface): """Convert the previous Job interface schema to the 1.0 schema :param interface: The previous interface :type interface: dict :return: converted interface :rtype: dict """ raise InvalidInterfaceDefinition('%s is an unsupported version number' % interface['version'])
def _check_param_name_uniqueness(self): '''Ensures all the parameter names are unique throws a :class:`job.configuration.interface.exceptions.InvalidInterfaceDefinition` if they are not unique :return: command arguments for the given properties :rtype: str ''' for input_data in self.definition['input_data']: if input_data['name'] in self._param_names: raise InvalidInterfaceDefinition( 'shared resource & input_data names must be unique') self._param_names.add(input_data['name']) for shared_resource in self.definition['shared_resources']: if shared_resource['name'] in self._param_names: raise InvalidInterfaceDefinition( 'shared resource & input_data names must be unique') self._param_names.add(shared_resource['name'])
def _validate_mount_paths(self): """Ensures that all mount paths are valid :raises :class:`job.configuration.data.exceptions.InvalidInterfaceDefinition`: If a mount path is invalid """ for mount in self.definition['mounts']: name = mount['name'] path = mount['path'] if not os.path.isabs(path): raise InvalidInterfaceDefinition('%s mount must have an absolute path' % name)
def validate(self): '''Validates the error mappings to ensure that all referenced errors actually exist. :returns: A list of warnings discovered during validation. :rtype: list[:class:`job.configuration.data.job_data.ValidationWarning`] :raises :class:`job.configuration.interface.exceptions.InvalidInterfaceDefinition`: If there is a missing error. ''' error_names = self.get_error_names() error_map = {error.name: error for error in Error.objects.filter(name__in=error_names)} for error_name in error_names: if error_name not in error_map: raise InvalidInterfaceDefinition('Missing error model reference: %s' % error_name) return []
def __init__(self, definition, do_validate=True): """Creates a job interface from the given definition. If the definition is invalid, a :class:`job.configuration.interface.exceptions.InvalidInterfaceDefinition` exception will be thrown. :param definition: The interface definition :type definition: dict :param do_validate: Whether to perform validation on the JSON schema :type do_validate: bool """ self.definition = definition self._mount_names = set() self._param_names = set() # Tuples used for validation with other classes self._property_validation_dict = {} # str->bool self._input_file_validation_dict = {} # str->tuple self._output_file_validation_list = [] self._output_file_manifest_dict = {} # str->bool if 'version' not in self.definition: self.definition['version'] = SCHEMA_VERSION if self.definition['version'] != SCHEMA_VERSION: self.convert_interface(definition) try: if do_validate: validate(definition, JOB_INTERFACE_SCHEMA) except ValidationError as validation_error: raise InvalidInterfaceDefinition(validation_error) self._populate_default_values() self._check_env_var_uniqueness() self._check_param_name_uniqueness() self._check_setting_name_uniqueness() self._check_mount_name_uniqueness() self._validate_command_arguments() self._validate_mount_paths() self._create_validation_dicts()