|
| 1 | +import yaml |
| 2 | +import os |
| 3 | +import copy |
| 4 | +import argparse |
| 5 | +import logging |
| 6 | +import string |
| 7 | + |
| 8 | +logging.basicConfig(level=logging.INFO) |
| 9 | + |
| 10 | + |
| 11 | +def section_id_chr(c): |
| 12 | + return (c if c in string.ascii_letters + string.digits else '_').lower() |
| 13 | + |
| 14 | + |
| 15 | +def section_label_to_id(label): |
| 16 | + return ''.join(map(section_id_chr, label)) |
| 17 | + |
| 18 | + |
| 19 | +def update_file(fn, install_repository_dependencies, install_resolver_dependencies): |
| 20 | + with open(fn, 'r') as handle: |
| 21 | + unlocked = yaml.safe_load(handle) |
| 22 | + # If a lock file exists, load it from that file |
| 23 | + if os.path.exists(fn + '.lock'): |
| 24 | + with open(fn + '.lock', 'r') as handle: |
| 25 | + locked = yaml.safe_load(handle) |
| 26 | + else: |
| 27 | + # Otherwise just clone the "unlocked" list. |
| 28 | + locked = copy.deepcopy(unlocked) |
| 29 | + |
| 30 | + # We will place entries in a cleaned lockfile, removing defunct entries, etc. |
| 31 | + clean_lockfile = copy.deepcopy(locked) |
| 32 | + clean_lockfile['tools'] = [] |
| 33 | + |
| 34 | + # As here we add any new tools in. |
| 35 | + for tool in unlocked['tools']: |
| 36 | + # If we have an existing locked copy, we'll just use that. |
| 37 | + locked_tools = [x for x in locked['tools'] if x['name'] == tool['name'] and x['owner'] == tool['owner']] |
| 38 | + |
| 39 | + # If there are no copies of it seen in the lockfile, we'll just copy it |
| 40 | + # over directly, without a reivision. Another script will fix that. |
| 41 | + if len(locked) == 0: |
| 42 | + # new tool, just add directly. |
| 43 | + clean_lockfile['tools'].append(tool) |
| 44 | + continue |
| 45 | + |
| 46 | + # Otherwise we have one or more locked versions so we'll harmonise + |
| 47 | + # reduce. Revisions are the only thing that could be variable. |
| 48 | + # Name/section/owner should not be. If they are, we take original human |
| 49 | + # edited .yaml file as source of truth. |
| 50 | + revisions = [] |
| 51 | + for locked_tool in locked_tools: |
| 52 | + for revision in locked_tool.get('revisions', []): |
| 53 | + revisions.append(revision) |
| 54 | + |
| 55 | + new_tool = { |
| 56 | + 'name': tool['name'], |
| 57 | + 'owner': tool['owner'], |
| 58 | + 'revisions': sorted(list(set(map(str, revisions)))), # Cast to list for yaml serialization |
| 59 | + } |
| 60 | + |
| 61 | + if 'tool_shed_url' in tool: |
| 62 | + ts_url = tool['tool_shed_url'] |
| 63 | + logging.warning('Non-default Tool Shed URL for %s/%s: %s', tool['owner'], tool['name'], ts_url) |
| 64 | + new_tool['tool_shed_url'] = ts_url |
| 65 | + |
| 66 | + # Set the section - id supercedes label/name |
| 67 | + if 'tool_panel_section_id' in unlocked: |
| 68 | + new_tool['tool_panel_section_id'] = unlocked['tool_panel_section_id'] |
| 69 | + elif 'tool_panel_section_label' in unlocked: |
| 70 | + new_tool['tool_panel_section_id'] = section_label_to_id(unlocked['tool_panel_section_label']) |
| 71 | + |
| 72 | + for section_definition in ('tool_panel_section_id', 'tool_panel_section_label'): |
| 73 | + if section_definition in unlocked: |
| 74 | + new_tool[section_definition] = unlocked[section_definition] |
| 75 | + break |
| 76 | + else: |
| 77 | + raise Exception( |
| 78 | + "Unlocked tool definition must include 'tool_panel_section_id' or " |
| 79 | + "'tool_panel_section_label': %s" % str(unlocked) |
| 80 | + ) |
| 81 | + |
| 82 | + clean_lockfile['tools'].append(new_tool) |
| 83 | + |
| 84 | + # Set appropriate installation flags to true |
| 85 | + clean_lockfile.update({ |
| 86 | + "install_repository_dependencies": install_repository_dependencies, |
| 87 | + "install_resolver_dependencies": install_resolver_dependencies, |
| 88 | + "install_tool_dependencies": False, # These are TS deps, not Conda |
| 89 | + }) |
| 90 | + |
| 91 | + with open(fn + '.lock', 'w') as handle: |
| 92 | + yaml.dump(clean_lockfile, handle, default_flow_style=False) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + parser = argparse.ArgumentParser() |
| 97 | + parser.add_argument('--no-install-repository-dependencies', action='store_true', default=False, |
| 98 | + help="Don't install TS repository dependencies") |
| 99 | + parser.add_argument('--no-install-resolver-dependencies', action='store_true', default=True, |
| 100 | + help="Don't install tool dependencies via Galaxy dependency resolver (e.g. conda)") |
| 101 | + parser.add_argument('fn', type=argparse.FileType('r'), help="Tool.yaml file") |
| 102 | + args = parser.parse_args() |
| 103 | + logging.info("Processing %s", args.fn.name) |
| 104 | + update_file(args.fn.name, not args.no_install_repository_dependencies, not args.no_install_resolver_dependencies) |
0 commit comments