Repository navigation
Expand file tree
/
Copy pathsplit_tool_yml.py
More file actions
120 lines (90 loc) · 3.19 KB
/
Copy pathsplit_tool_yml.py
File metadata and controls
120 lines (90 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
import yaml
from collections import defaultdict
import re
import os
import string
import argparse
def slugify(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
rval = ''
for c in value:
rval += (c if c in string.ascii_letters + string.digits else '_').lower()
return rval
def strip_superflous(cat):
"""
Re-arranges the ephemeris returned yml format for tools to the usegalaxy-tools minimal tool yml format
i.e. Takes a list like:
- name: substitution_rates
owner: devteam
revisions:
- d1b35bcdaacc
tool_panel_section_label: Regional Variation
tool_shed_url: toolshed.g2.bx.psu.edu
- name: indels_3way
owner: devteam
revisions:
- 5ad24b81dd10
tool_panel_section_label: Regional Variation
tool_shed_url: toolshed.g2.bx.psu.edu
...
and returns:
tool_panel_section_label: Regional Variation
- name: substitution_rates
owner: devteam
- name: indels_3way
owner: devteam
...
"""
out = {'tool_panel_section_label': cat[0]['tool_panel_section_label']}
for tool in cat:
del tool['tool_panel_section_label']
del tool['tool_panel_section_id']
del tool['revisions']
if tool['tool_shed_url'] == 'toolshed.g2.bx.psu.edu':
del tool['tool_shed_url']
out['tools'] = cat
return out
def main():
VERSION = 0.2
parser = argparse.ArgumentParser(description="Splits up a Ephemeris `get_tool_list` yml file for a Galaxy server into individual files for each Section Label.")
parser.add_argument("-i", "--infile", help="The returned `get_tool_list` yml file to split.")
parser.add_argument("-o", "--outdir", help="The output directory to put the split files into. Defaults to infile without the .yml.")
parser.add_argument("-l", "--lockfiles", action='store_true', help="Produce lock files instead of plain yml files.")
parser.add_argument("--version", action='store_true')
parser.add_argument("--verbose", action='store_true')
args = parser.parse_args()
if args.version:
print("split_tool_yml.py version: %.1f" % VERSION)
return
filename = args.infile
a = yaml.safe_load(open(filename, 'r'))
outdir = re.sub(r'\.yml', '', filename)
if args.outdir:
outdir = args.outdir
if args.verbose:
print('Outdir: %s' % outdir)
if not os.path.isdir(outdir):
os.mkdir(outdir)
tools = a['tools']
categories = defaultdict(list)
for tool in tools:
categories[tool['tool_panel_section_label']].append(tool)
for cat in categories:
fname = str(cat)
good_fname = outdir + "/" + slugify(fname) + ".yml"
if args.lockfiles:
good_fname += ".lock"
tool_yaml = {'tools': categories[cat]}
else:
tool_yaml = strip_superflous(categories[cat])
if args.verbose:
print("Working on: %s" % good_fname)
with open(good_fname, 'w') as outfile:
yaml.dump(tool_yaml, outfile, default_flow_style=False)
return
if __name__ == "__main__":
main()