annotate history_metadata_extractor.py @ 1:c7f4f2ac38f2 draft default tip

" master branch Updating"
author lain
date Tue, 09 Aug 2022 15:19:45 +0000
parents 426b0f85a311
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
1 #!/usr/bin/env python
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
2
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
3 ## **@AUTHOR**: Lain Pavot - lain.pavot@inrae.fr
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
4 ## **@DATE**: 22/06/2022
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
5
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
6
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
7 import json
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
8 import os
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
9 import sys
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
10
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
11
1
c7f4f2ac38f2 " master branch Updating"
lain
parents: 0
diff changeset
12 STATIC = os.path.join(sys.path[0], "static")
c7f4f2ac38f2 " master branch Updating"
lain
parents: 0
diff changeset
13 VENDOR = os.path.join(sys.path[0], "vendor")
c7f4f2ac38f2 " master branch Updating"
lain
parents: 0
diff changeset
14
c7f4f2ac38f2 " master branch Updating"
lain
parents: 0
diff changeset
15 with open(os.path.join(STATIC, "app.css")) as css:
0
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
16 CSS_STYLES = css.read()
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
17
1
c7f4f2ac38f2 " master branch Updating"
lain
parents: 0
diff changeset
18 with open(os.path.join(VENDOR, "bootstrap.min.css")) as bootstrap:
0
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
19 CSS_STYLES = f"{CSS_STYLES}\n{bootstrap.read()}"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
20
1
c7f4f2ac38f2 " master branch Updating"
lain
parents: 0
diff changeset
21 with open(os.path.join(STATIC, "app.js")) as js:
0
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
22 JAVASCRIPT = js.read()
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
23
1
c7f4f2ac38f2 " master branch Updating"
lain
parents: 0
diff changeset
24 with open(os.path.join(STATIC, "app.template.html")) as template:
0
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
25 PAGE_TEMPLATE = template.read()
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
26
1
c7f4f2ac38f2 " master branch Updating"
lain
parents: 0
diff changeset
27 with open(os.path.join(STATIC, "title.template.html")) as template:
0
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
28 TITLE_TEMPLATE = template.read()
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
29
1
c7f4f2ac38f2 " master branch Updating"
lain
parents: 0
diff changeset
30 with open(os.path.join(STATIC, "table.template.html")) as template:
0
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
31 TABLE_TEMPLATE = template.read()
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
32
1
c7f4f2ac38f2 " master branch Updating"
lain
parents: 0
diff changeset
33 with open(os.path.join(STATIC, "header_list.template.html")) as template:
0
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
34 HEADER_LIST_TEMPLATE = template.read()
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
35
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
36 HEADER_LIST_TEMPLATE = '\n'.join((
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
37 "<thead>",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
38 " <tr>",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
39 "{header_list}",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
40 " </tr>",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
41 "</thead>",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
42 ))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
43
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
44 HEADER_TEMPLATE = "<th scope=\"col\">{}</th>"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
45 COLUMN_TEMPLATE = "<th scope=\"row\">{}</th>"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
46
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
47 TABLE_LINE_LIST_TEMPLATE = '\n'.join((
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
48 "<tr class=\"{classes}\">",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
49 "{table_lines}",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
50 "</tr>",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
51 ))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
52 TABLE_LINE_TEMPLATE = "<td>{}</td>"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
53
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
54 INDENT = " "
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
55
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
56
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
57 HISTORY_CACHE = {}
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
58
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
59 def indent(text):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
60 if text.startswith("\n"):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
61 return text.replace("\n", f"\n{INDENT}")
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
62 else:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
63 return INDENT+text.replace("\n", f"\n{INDENT}")
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
64
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
65 def noempty(ls, as_list=True):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
66 if as_list:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
67 return [x for x in ls if x]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
68 return (x for x in ls if x)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
69
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
70 def join_noempty(ls, sep=';'):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
71 return sep.join(noempty(ls, as_list=False))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
72
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
73 def extract_dataset_attributes(dataset_attrs):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
74 for dataset_attr in dataset_attrs:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
75 HISTORY_CACHE[dataset_attr["encoded_id"]] = dataset_attr
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
76 HISTORY_CACHE["dataset_attrs"] = dataset_attrs
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
77
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
78 def convert_to_html(jobs_attrs, dataset_attrs=None):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
79 if dataset_attrs:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
80 extract_dataset_attributes(dataset_attrs)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
81 return PAGE_TEMPLATE.format(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
82 styles=CSS_STYLES.replace("\n<", "\n <"),
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
83 javascript=JAVASCRIPT,
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
84 title=indent(indent(get_title(jobs_attrs))),
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
85 table_list=indent(indent(get_table_list(jobs_attrs)))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
86 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
87
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
88 def get_title(jobs_attrs):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
89 galaxy_version = jobs_attrs[0]["galaxy_version"] or "Unknown version"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
90 return TITLE_TEMPLATE.format(galaxy_version=galaxy_version)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
91
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
92 def get_table_list(jobs_attrs):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
93 return '\n'.join((
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
94 convert_item_to_table(job_attr, dataset_id)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
95 for job_attr in jobs_attrs
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
96 for dataset_id_set in sorted(list((
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
97 job_attr["output_dataset_mapping"]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
98 or {1:"unknown"}
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
99 ).values()))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
100 for dataset_id in sorted(dataset_id_set)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
101 ))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
102
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
103 def convert_item_to_table(job_attr, dataset_id):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
104 encoded_jid = job_attr.get("encoded_id")
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
105 if HISTORY_CACHE:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
106 history = HISTORY_CACHE.get(dataset_id, {})
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
107 hid = history.get("hid", "DELETED")
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
108 else:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
109 hid = "?"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
110 exit_code = job_attr.get("exit_code")
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
111 if job_attr["exit_code"] == 0:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
112 status = f"Ok ({exit_code})"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
113 classes = "alert alert-success"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
114 else:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
115 status = f"Failed ({exit_code})"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
116 classes = "alert alert-danger"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
117 if hid == "DELETED":
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
118 classes += " history_metadata_extractor_deleted"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
119 tool_name = job_attr["tool_id"] or "unknown"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
120 if tool_name.count("/") >= 4:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
121 tool_name = job_attr["tool_id"].split("/")[-2]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
122 tool_name = tool_name + " - " + job_attr["tool_version"]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
123 tool_name = f"[{hid}] - {tool_name}"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
124 return TABLE_TEMPLATE.format(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
125 classes=classes,
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
126 tool_name=tool_name,
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
127 tool_status=status,
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
128 table=convert_parameters_to_html(job_attr)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
129 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
130
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
131 def convert_parameters_to_html(job_attr):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
132 params = job_attr["params"]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
133 params_enrichment(job_attr, params)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
134 keys = [
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
135 key for key in iter_parameter_keys(params)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
136 if key not in ("dbkey", "chromInfo", "__input_ext", "request_json")
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
137 ]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
138 return '\n'.join((
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
139 indent(get_table_header(params, ["value", "name", "extension", "hid"])),
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
140 indent(get_table_lines(params, keys)),
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
141 ))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
142
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
143 def params_enrichment(job_attr, params):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
144 if (
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
145 all(map(params.__contains__, ("request_json", "files")))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
146 and "encoded_id" in job_attr
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
147 ):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
148 params.update(json.loads(params.pop("request_json")))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
149 for i, target in enumerate(params.pop("targets")):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
150 files = target["elements"]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
151 params["files"][i]["hid"] = join_noempty(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
152 str(file["object_id"])
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
153 for file in files
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
154 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
155 params["files"][i]["name"] = join_noempty(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
156 str(file["name"])
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
157 for file in files
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
158 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
159 params["files"][i]["extension"] = join_noempty(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
160 str(file["ext"])
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
161 for file in files
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
162 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
163
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
164 def iter_parameter_keys(params):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
165 for key in params:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
166 param = params[key]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
167 if param_is_file(param):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
168 yield key
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
169 elif isinstance(param, dict):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
170 for subkey in iter_parameter_keys(param):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
171 if subkey not in ("__current_case__", ):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
172 yield f"{key}.{subkey}"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
173 else:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
174 yield key
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
175
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
176 def param_is_file(param):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
177 return is_file_v1(param) or is_file_v2(param)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
178
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
179 def is_file_v1(param):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
180 return (
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
181 isinstance(param, dict)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
182 and all(map(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
183 param.__contains__,
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
184 ("info", "peek", "name", "extension")
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
185 ))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
186 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
187
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
188 def is_file_v2(param):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
189 return (
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
190 isinstance(param, dict)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
191 and "values" in param
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
192 and isinstance(param["values"], list)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
193 and isinstance(param["values"][0], dict)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
194 and all(map(param["values"][0].__contains__, ("id", "src")))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
195 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
196
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
197 def get_table_header(params, keys):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
198 return HEADER_LIST_TEMPLATE.format(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
199 header_list=indent(indent('\n'.join(map(HEADER_TEMPLATE.format, [""]+keys))))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
200 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
201
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
202 def get_table_lines(params, keys):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
203 return ''.join(table_lines_iterator(params, keys))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
204
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
205 def table_lines_iterator(params, param_names):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
206 keys = ("value", "name", "extension", "hid",)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
207 for param_name in param_names:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
208 classes = ""
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
209 table_lines = []
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
210 subparam = params
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
211 while '.' in param_name:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
212 subkey, param_name = param_name.split('.', 1)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
213 subparam = subparam[subkey]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
214 for key in keys:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
215 param = extract_param_info(key, subparam[param_name])
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
216 table_lines.append(param)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
217 yield TABLE_LINE_LIST_TEMPLATE.format(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
218 classes=classes,
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
219 table_lines=(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
220 indent(COLUMN_TEMPLATE.format(param_name) + '\n')
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
221 + indent('\n'.join(map(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
222 TABLE_LINE_TEMPLATE.format,
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
223 table_lines
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
224 )))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
225 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
226 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
227
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
228 def extract_param_info(key, param):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
229 if key == "value":
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
230 return extract_param_value(param)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
231 if isinstance(param, dict) and key in param:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
232 return str(param[key])
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
233 if isinstance(param, list):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
234 return join_noempty(extract_param_info(key, p) for p in param)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
235 return ""
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
236
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
237 def extract_param_value(param):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
238 if isinstance(param, dict):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
239 if "__current_case__" in param:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
240 return join_dict_key_values(param, ignore=("__current_case__", ))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
241 for acceptable_value in ("file_data", "file_name"):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
242 if acceptable_value in param:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
243 return f"{acceptable_value}: {param[acceptable_value]}"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
244 if "values" in param:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
245 ids = []
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
246 for file_id in param["values"]:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
247 file_id = file_id["id"]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
248 if file_id in HISTORY_CACHE:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
249 file_info = HISTORY_CACHE[file_id]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
250 param["name"] = file_info["name"]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
251 param["hid"] = file_info["hid"]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
252 param["extension"] = file_info["extension"]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
253 ids.append(file_id)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
254 return join_noempty(ids)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
255 if isinstance(param, (str, int, float)):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
256 return str(param)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
257 if isinstance(param, (list, tuple)):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
258 return join_noempty(map(extract_param_value, param))
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
259 return str(param)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
260
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
261 def join_dict_key_values(dico, ignore=()):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
262 return join_noempty(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
263 f"{name}: {dico[name]}"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
264 for name in dico
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
265 if name not in ignore
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
266 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
267
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
268 if __name__ == "__main__":
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
269 import optparse
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
270 parser = optparse.OptionParser()
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
271 parser.add_option(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
272 "-j", "--jobs-attrs",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
273 dest="jobs_attrs",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
274 help="write report of FILE",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
275 metavar="FILE",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
276 default="jobs_attrs.txt"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
277 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
278 parser.add_option(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
279 "-d", "--dataset-attrs",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
280 dest="dataset_attrs",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
281 help="extract additional info from this file",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
282 metavar="FILE",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
283 default=None,
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
284 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
285 parser.add_option(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
286 "-o", "--output",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
287 dest="output",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
288 help="write report to FILE",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
289 metavar="FILE",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
290 default="out.html"
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
291 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
292 parser.add_option(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
293 "-v", "--version",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
294 action="store_true",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
295 help="Show this script's version and exits",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
296 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
297
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
298 (options, args) = parser.parse_args()
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
299
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
300 if options.version:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
301
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
302 import re
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
303
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
304 with open(os.path.join(sys.path[0], "README.md")) as readme:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
305 for line in readme.readlines():
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
306 if "**@VERSION**" in line:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
307 print(re.search(r"\d+\.\d+\.\d+", line)[0])
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
308 sys.exit(0)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
309
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
310 with open(options.jobs_attrs) as j:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
311 jobs_attrs = json.load(j)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
312
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
313 if options.dataset_attrs is not None:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
314 with open(options.dataset_attrs) as ds:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
315 dataset_attrs = json.load(ds)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
316 else:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
317 dataset_attrs = {}
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
318
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
319 jobs_attrs = [{
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
320 key: jobs_attr.get(key)
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
321 for key in (
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
322 "galaxy_version",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
323 "tool_id",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
324 "tool_version",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
325 "encoded_id",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
326 "params",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
327 "output_datasets",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
328 "exit_code",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
329 "output_dataset_mapping",
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
330 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
331 } for jobs_attr in jobs_attrs]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
332 if jobs_attrs and jobs_attrs[0].get("output_datasets"):
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
333 jobs_attrs = sorted(
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
334 jobs_attrs,
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
335 key=lambda x:x["output_datasets"][0]
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
336 )
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
337
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
338 with open(options.output, "w") as o:
426b0f85a311 " master branch Updating"
lain
parents:
diff changeset
339 o.write(convert_to_html(jobs_attrs, dataset_attrs=dataset_attrs))