def validate_and_prepare_common_arguments(args): # flatten lists of lists to a list containing elements lists_to_flatten = ["input_files", "blacklisted_parameters"] for list_to_flatten in lists_to_flatten: utils.flatten_list_of_lists(args, list_to_flatten) # if input is a single file, we expect output to be a file (and not a dir that already exists) if len(args.input_files) == 1: if os.path.isdir(args.output_destination): raise ApplicationException( "If a single input file is provided, output (%s) is expected to be a file " "and not a folder.\n" % args.output_destination) # if input is a list of files, we expect output to be a folder if len(args.input_files) > 1: if not os.path.isdir(args.output_destination): raise ApplicationException( "If several input files are provided, output (%s) is expected to be an " "existing directory.\n" % args.output_destination) # check that the provided input files, if provided, contain a valid file path input_arguments_to_check = [ "xsd_location", "input_files", "hardcoded_parameters" ] for argument_name in input_arguments_to_check: utils.validate_argument_is_valid_path(args, argument_name) # add the parameter hardcoder args.parameter_hardcoder = utils.parse_hardcoded_parameters( args.hardcoded_parameters)
def validate_and_prepare_args(args): # check that only one of skip_tools_file and required_tools_file has been provided if args.skip_tools_file is not None and args.required_tools_file is not None: raise ApplicationException( "You have provided both a file with tools to ignore and a file with required tools.\n" "Only one of -s/--skip-tools, -r/--required-tools can be provided." ) # flatten macros_files to make sure that we have a list containing file names and not a list of lists utils.flatten_list_of_lists(args, "macros_files") # check that the arguments point to a valid, existing path input_variables_to_check = [ "skip_tools_file", "required_tools_file", "macros_files", "formats_file" ] for variable_name in input_variables_to_check: utils.validate_argument_is_valid_path(args, variable_name) # check that the provided output files, if provided, contain a valid file path (i.e., not a folder) output_variables_to_check = [ "data_types_destination", "tool_conf_destination" ] for variable_name in output_variables_to_check: file_name = getattr(args, variable_name) if file_name is not None and os.path.isdir(file_name): raise ApplicationException( "The provided output file name (%s) points to a directory." % file_name) if not args.macros_files: # list is empty, provide the default value logger.warning("Using default macros from galaxy/macros.xml", 0) args.macros_files = ["galaxy/macros.xml"]
def parse_macros_files(macros_file_names): macros_to_expand = list() for macros_file_name in macros_file_names: try: macros_file = open(macros_file_name) logger.info("Loading macros from %s" % macros_file_name, 0) root = parse(macros_file).getroot() for xml_element in root.findall("xml"): name = xml_element.attrib["name"] if name in macros_to_expand: logger.warning( "Macro %s has already been found. Duplicate found in file %s." % (name, macros_file_name), 0) else: logger.info("Macro %s found" % name, 1) macros_to_expand.append(name) except ParseError, e: raise ApplicationException("The macros file " + macros_file_name + " could not be parsed. Cause: " + str(e)) except IOError, e: raise ApplicationException("The macros file " + macros_file_name + " could not be opened. Cause: " + str(e))
def validate_against_schema(ctd_file, schema): try: parser = etree.XMLParser(schema=schema) etree.parse(ctd_file, parser=parser) except etree.XMLSyntaxError as e: raise ApplicationException("Invalid CTD file %s. Reason: %s" % (ctd_file, str(e)))
def validate_argument_is_valid_path(args, argument_name): paths_to_check = [] # check if we are handling a single file or a list of files member_value = getattr(args, argument_name) if member_value is not None: if isinstance(member_value, list): for file_name in member_value: paths_to_check.append(str(file_name).strip()) else: paths_to_check.append(str(member_value).strip()) for path_to_check in paths_to_check: try: validate_path_exists(path_to_check) except ApplicationException: raise ApplicationException( "Argument %s: The provided output file name (%s) points to a directory." % (argument_name, path_to_check))
def validate_argument_is_directory(args, argument_name): file_name = getattr(args, argument_name) if file_name is not None and os.path.isdir(file_name): raise ApplicationException( "The provided output file name (%s) points to a directory." % file_name)
def validate_path_exists(path): if not os.path.isfile(path) or not os.path.exists(path): raise ApplicationException( "The provided path (%s) does not exist or is not a valid file path." % path)
" could not be parsed. Cause: " + str(e)) except IOError, e: raise ApplicationException("The macros file " + macros_file_name + " could not be opened. Cause: " + str(e)) # we depend on "stdio", "requirements" and "advanced_options" to exist on all the given macros files missing_needed_macros = [] for required_macro in REQUIRED_MACROS: if required_macro not in macros_to_expand: missing_needed_macros.append(required_macro) if missing_needed_macros: raise ApplicationException( "The following required macro(s) were not found in any of the given macros files: %s, " "see galaxy/macros.xml for an example of a valid macros file." % ", ".join(missing_needed_macros)) # we do not need to "expand" the advanced_options macro macros_to_expand.remove(ADVANCED_OPTIONS_MACRO_NAME) return macros_to_expand def parse_file_formats(formats_file): supported_formats = {} if formats_file is not None: line_number = 0 with open(formats_file) as f: for line in f: line_number += 1 if line is None or not line.strip() or line.strip().startswith(