diff --git a/subdomains/scripts/parse_tools_to_produce_yml_files.py b/subdomains/scripts/parse_tools_to_produce_yml_files.py index 8ba2d0c3..613a9b47 100644 --- a/subdomains/scripts/parse_tools_to_produce_yml_files.py +++ b/subdomains/scripts/parse_tools_to_produce_yml_files.py @@ -2,37 +2,39 @@ installation. """ -import yaml +import argparse import os import re -import argparse import tools_au +import yaml parser = argparse.ArgumentParser( - description='Create installation yaml files from a Yaml file containing' - ' the panel_view/Toolkit data') -parser.add_argument('--tk', action="store", dest='toolkit', - help='Path of the toolkit.yaml file') -parser.add_argument('--yml-folder', action="store", dest='dest', - help='Folder containing the yml files with tool installed' - ' in the target instance') + description="Create installation yaml files from a Yaml file containing the panel_view/Toolkit data" +) +parser.add_argument("--tk", action="store", dest="toolkit", help="Path of the toolkit.yaml file") +parser.add_argument( + "--yml-folder", + action="store", + dest="dest", + help="Folder containing the yml files with tool installed" " in the target instance", +) args = parser.parse_args() print(args.toolkit) print(args.dest) -with open(args.toolkit, 'r') as stream: +with open(args.toolkit) as stream: data_loaded = yaml.safe_load(stream) -for i in range(3, len(data_loaded['items']) - 2): - section = data_loaded['items'][i]['id'] - name = data_loaded['items'][i]['name'] +for i in range(3, len(data_loaded["items"]) - 2): + section = data_loaded["items"][i]["id"] + name = data_loaded["items"][i]["name"] f = open(section + ".yml", "w") f.write("tool_panel_section_label: " + name + "\ntools:\n") - for j in data_loaded['items'][i]['items']: # for loop on toolkit sections - path = j['id'] - infos = path.replace('toolshed.g2.bx.psu.edu/repos/', "") + for j in data_loaded["items"][i]["items"]: # for loop on toolkit sections + path = j["id"] + infos = path.replace("toolshed.g2.bx.psu.edu/repos/", "") # extract the owner (group1) and the name of the tool (group2) pattern = re.compile(r"([^\/]+)\/([^\/]+)\/") for match in pattern.finditer(infos): # for each tool in the seection @@ -40,7 +42,7 @@ tool = match.group(2) # verify the tool is not already installed installed = os.popen('grep -r "' + tool + '" ' + args.dest).read() - if installed == '': # if tool not installed + if installed == "": # if tool not installed f.write("- name: " + tool + "\n") f.write(" owner: " + owner + "\n") f.close() diff --git a/subdomains/scripts/tools_au.py b/subdomains/scripts/tools_au.py index 87edb7e9..c860bf67 100644 --- a/subdomains/scripts/tools_au.py +++ b/subdomains/scripts/tools_au.py @@ -1,49 +1,59 @@ """Convert YAML files for install on AU. -Assumes that wdir contains tool yaml files. +Assumes that wdir contains tool yaml files produced by the +parse_tools_to_produce_yml_files.py script, they should look like: +.yml +.yml.lock +.yml +.yml.lock +... + +See subdomains/singlecell/tool_panel for example. """ -import yaml from pathlib import Path +import yaml + WDIR = Path(__file__).parent EXCLUDE_KEYS = [ - 'install_repository_dependencies', - 'tool_panel_section_label', + "install_repository_dependencies", + "tool_panel_section_label", ] -OUTPUT_FILE = WDIR / 'usegalaxy.org.au.yml' +OUTPUT_FILE = WDIR / "usegalaxy.org.au.yml" -def parse(): - data = {'tools': []} - for f in WDIR.glob('*.yml.lock'): - if not f.name.endswith('.yml.lock'): +def parse() -> None: + """Parse tools list from YAML files.""" + data: dict = {"tools": []} + for f in WDIR.glob("*.yml.lock"): + if not f.name.endswith(".yml.lock"): continue print(f"Extracting data from {f}...") - with open(f, 'r') as handle: + with open(f) as handle: section_data = yaml.safe_load(handle) - section_data['tools'] = [ - _transcribe_to_au(x) for x in section_data['tools'] - ] + section_data["tools"] = [_transcribe_to_au(x) for x in section_data["tools"]] for key in EXCLUDE_KEYS: section_data.pop(key) - data['tools'] += section_data['tools'] - for key in ('install_resolver_dependencies', 'install_tool_dependencies'): + data["tools"] += section_data["tools"] + for key in ("install_resolver_dependencies", "install_tool_dependencies"): data[key] = section_data[key] write_data(data) - print(f'Data transcribed and written to {OUTPUT_FILE}') + print(f"Data transcribed and written to {OUTPUT_FILE}") -def _transcribe_to_au(tool): - tool['tool_panel_section_label'] = 'Single-Cell' - tool.pop('tool_panel_section_id') +def _transcribe_to_au(tool: dict) -> dict: + """Convert keys to AU installation format.""" + tool["tool_panel_section_label"] = "Single-Cell" + tool.pop("tool_panel_section_id") return tool -def write_data(data): - with open(OUTPUT_FILE, 'w') as handle: +def write_data(data: dict) -> None: + """Write data to YAML output file.""" + with open(OUTPUT_FILE, "w") as handle: yaml.dump(data, handle, default_flow_style=False) -if __name__ == '__main__': +if __name__ == "__main__": parse()