84
|
1 # replace with shebang for biocontainer
|
48
|
2 # see https://github.com/fubar2/toolfactory
|
|
3 #
|
|
4 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
|
|
5 #
|
|
6 # all rights reserved
|
|
7 # Licensed under the LGPL
|
49
|
8 # suggestions for improvement and bug fixes welcome at
|
|
9 # https://github.com/fubar2/toolfactory
|
48
|
10 #
|
|
11 # July 2020: BCC was fun and I feel like rip van winkle after 5 years.
|
|
12 # Decided to
|
|
13 # 1. Fix the toolfactory so it works - done for simplest case
|
|
14 # 2. Fix planemo so the toolfactory function works
|
|
15 # 3. Rewrite bits using galaxyxml functions where that makes sense - done
|
|
16 #
|
|
17 # removed all the old complications including making the new tool use this same script
|
|
18 # galaxyxml now generates the tool xml https://github.com/hexylena/galaxyxml
|
|
19 # No support for automatic HTML file creation from arbitrary outputs
|
|
20 # essential problem is to create two command lines - one for the tool xml and a different
|
|
21 # one to run the executable with the supplied test data and settings
|
|
22 # Be simpler to write the tool, then run it with planemo and soak up the test outputs.
|
95
|
23 # well well. sh run_tests.sh --id rgtf2 --report_file tool_tests_tool_conf.html functional.test_toolbox
|
|
24 # does the needful. Use GALAXY_TEST_SAVE /foo to save outputs - only the tar.gz - not the rest sadly
|
|
25 # GALAXY_TEST_NO_CLEANUP GALAXY_TEST_TMP_DIR=wherever
|
99
|
26 # planemo test --engine docker_galaxy --test_data ./test-data/ --docker_extra_volume ./test-data rgToolFactory2.xml
|
48
|
27
|
|
28 import argparse
|
101
|
29 import copy
|
76
|
30 import datetime
|
103
|
31 import grp
|
76
|
32 import json
|
48
|
33 import logging
|
|
34 import os
|
|
35 import re
|
|
36 import shutil
|
|
37 import subprocess
|
|
38 import sys
|
|
39 import tarfile
|
|
40 import tempfile
|
|
41 import time
|
|
42
|
75
|
43
|
100
|
44 from bioblend import ConnectionError
|
63
|
45 from bioblend import toolshed
|
|
46
|
101
|
47 import docker
|
98
|
48
|
48
|
49 import galaxyxml.tool as gxt
|
|
50 import galaxyxml.tool.parameters as gxtp
|
|
51
|
|
52 import lxml
|
|
53
|
|
54 import yaml
|
|
55
|
|
56 myversion = "V2.1 July 2020"
|
|
57 verbose = True
|
|
58 debug = True
|
|
59 toolFactoryURL = "https://github.com/fubar2/toolfactory"
|
|
60 ourdelim = "~~~"
|
50
|
61 ALOT = 10000000 # srsly. command or test overrides use read() so just in case
|
49
|
62 STDIOXML = """<stdio>
|
|
63 <exit_code range="100:" level="debug" description="shite happens" />
|
|
64 </stdio>"""
|
48
|
65
|
|
66 # --input_files="$input_files~~~$CL~~~$input_formats~~~$input_label
|
|
67 # ~~~$input_help"
|
|
68 IPATHPOS = 0
|
|
69 ICLPOS = 1
|
|
70 IFMTPOS = 2
|
|
71 ILABPOS = 3
|
|
72 IHELPOS = 4
|
|
73 IOCLPOS = 5
|
|
74
|
108
|
75 # --output_files "$otab.history_name~~~$otab.history_format~~~$otab.history_CL~~~$otab.history_test"
|
48
|
76 ONAMEPOS = 0
|
|
77 OFMTPOS = 1
|
|
78 OCLPOS = 2
|
49
|
79 OTESTPOS = 3
|
|
80 OOCLPOS = 4
|
|
81
|
48
|
82
|
|
83 # --additional_parameters="$i.param_name~~~$i.param_value~~~
|
|
84 # $i.param_label~~~$i.param_help~~~$i.param_type~~~$i.CL~~~i$.param_CLoverride"
|
|
85 ANAMEPOS = 0
|
|
86 AVALPOS = 1
|
|
87 ALABPOS = 2
|
|
88 AHELPPOS = 3
|
|
89 ATYPEPOS = 4
|
|
90 ACLPOS = 5
|
|
91 AOVERPOS = 6
|
|
92 AOCLPOS = 7
|
|
93
|
|
94
|
|
95 foo = len(lxml.__version__)
|
|
96 # fug you, flake8. Say my name!
|
49
|
97 FAKEEXE = "~~~REMOVE~~~ME~~~"
|
|
98 # need this until a PR/version bump to fix galaxyxml prepending the exe even
|
|
99 # with override.
|
|
100
|
48
|
101
|
|
102 def timenow():
|
75
|
103 """return current time as a string"""
|
48
|
104 return time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(time.time()))
|
|
105
|
|
106
|
|
107 def quote_non_numeric(s):
|
|
108 """return a prequoted string for non-numerics
|
|
109 useful for perl and Rscript parameter passing?
|
|
110 """
|
|
111 try:
|
|
112 _ = float(s)
|
|
113 return s
|
|
114 except ValueError:
|
|
115 return '"%s"' % s
|
|
116
|
|
117
|
108
|
118 html_escape_table = {"&": "&", ">": ">", "<": "<", "$": r"\$","#":"#", "$":"$"}
|
|
119 cheetah_escape_table = {"$": "\$","#":"\#"}
|
48
|
120
|
|
121 def html_escape(text):
|
|
122 """Produce entities within text."""
|
|
123 return "".join(html_escape_table.get(c, c) for c in text)
|
|
124
|
108
|
125 def cheetah_escape(text):
|
|
126 """Produce entities within text."""
|
|
127 return "".join(cheetah_escape_table.get(c, c) for c in text)
|
|
128
|
48
|
129
|
|
130 def html_unescape(text):
|
|
131 """Revert entities within text. Multiple character targets so use replace"""
|
|
132 t = text.replace("&", "&")
|
|
133 t = t.replace(">", ">")
|
|
134 t = t.replace("<", "<")
|
|
135 t = t.replace("\\$", "$")
|
108
|
136 t = t.replace("$","$")
|
|
137 t = t.replace("#","#")
|
48
|
138 return t
|
|
139
|
|
140
|
|
141 def parse_citations(citations_text):
|
75
|
142 """"""
|
48
|
143 citations = [c for c in citations_text.split("**ENTRY**") if c.strip()]
|
|
144 citation_tuples = []
|
|
145 for citation in citations:
|
|
146 if citation.startswith("doi"):
|
|
147 citation_tuples.append(("doi", citation[len("doi") :].strip()))
|
|
148 else:
|
49
|
149 citation_tuples.append(("bibtex", citation[len("bibtex") :].strip()))
|
48
|
150 return citation_tuples
|
|
151
|
|
152
|
|
153 class ScriptRunner:
|
|
154 """Wrapper for an arbitrary script
|
|
155 uses galaxyxml
|
|
156
|
|
157 """
|
|
158
|
|
159 def __init__(self, args=None):
|
|
160 """
|
|
161 prepare command line cl for running the tool here
|
|
162 and prepare elements needed for galaxyxml tool generation
|
|
163 """
|
101
|
164 self.ourcwd = os.getcwd()
|
|
165 self.ourenv = copy.deepcopy(os.environ)
|
48
|
166 self.infiles = [x.split(ourdelim) for x in args.input_files]
|
|
167 self.outfiles = [x.split(ourdelim) for x in args.output_files]
|
|
168 self.addpar = [x.split(ourdelim) for x in args.additional_parameters]
|
|
169 self.args = args
|
|
170 self.cleanuppar()
|
|
171 self.lastclredirect = None
|
|
172 self.lastxclredirect = None
|
|
173 self.cl = []
|
|
174 self.xmlcl = []
|
|
175 self.is_positional = self.args.parampass == "positional"
|
63
|
176 if self.args.sysexe:
|
49
|
177 self.executeme = self.args.sysexe
|
63
|
178 else:
|
|
179 if self.args.packages:
|
|
180 self.executeme = self.args.packages.split(",")[0].split(":")[0]
|
|
181 else:
|
|
182 self.executeme = None
|
48
|
183 aCL = self.cl.append
|
49
|
184 aXCL = self.xmlcl.append
|
48
|
185 assert args.parampass in [
|
|
186 "0",
|
|
187 "argparse",
|
|
188 "positional",
|
49
|
189 ], 'args.parampass must be "0","positional" or "argparse"'
|
48
|
190 self.tool_name = re.sub("[^a-zA-Z0-9_]+", "", args.tool_name)
|
|
191 self.tool_id = self.tool_name
|
50
|
192 self.newtool = gxt.Tool(
|
101
|
193 self.tool_name,
|
48
|
194 self.tool_id,
|
|
195 self.args.tool_version,
|
|
196 self.args.tool_desc,
|
50
|
197 FAKEEXE,
|
48
|
198 )
|
76
|
199 self.newtarpath = "toolfactory_%s.tgz" % self.tool_name
|
98
|
200 self.tooloutdir = "./tfout"
|
|
201 self.repdir = "./TF_run_report_tempdir"
|
48
|
202 self.testdir = os.path.join(self.tooloutdir, "test-data")
|
|
203 if not os.path.exists(self.tooloutdir):
|
|
204 os.mkdir(self.tooloutdir)
|
|
205 if not os.path.exists(self.testdir):
|
|
206 os.mkdir(self.testdir) # make tests directory
|
|
207 if not os.path.exists(self.repdir):
|
|
208 os.mkdir(self.repdir)
|
|
209 self.tinputs = gxtp.Inputs()
|
|
210 self.toutputs = gxtp.Outputs()
|
|
211 self.testparam = []
|
49
|
212 if self.args.script_path:
|
|
213 self.prepScript()
|
|
214 if self.args.command_override:
|
|
215 scos = open(self.args.command_override, "r").readlines()
|
|
216 self.command_override = [x.rstrip() for x in scos]
|
|
217 else:
|
|
218 self.command_override = None
|
|
219 if self.args.test_override:
|
|
220 stos = open(self.args.test_override, "r").readlines()
|
|
221 self.test_override = [x.rstrip() for x in stos]
|
|
222 else:
|
|
223 self.test_override = None
|
50
|
224 if self.args.cl_prefix: # DIY CL start
|
49
|
225 clp = self.args.cl_prefix.split(" ")
|
|
226 for c in clp:
|
|
227 aCL(c)
|
|
228 aXCL(c)
|
|
229 else:
|
56
|
230 if self.args.script_path:
|
|
231 aCL(self.executeme)
|
|
232 aCL(self.sfile)
|
|
233 aXCL(self.executeme)
|
|
234 aXCL("$runme")
|
48
|
235 else:
|
56
|
236 aCL(self.executeme) # this little CL will just run
|
|
237 aXCL(self.executeme)
|
50
|
238 self.elog = os.path.join(self.repdir, "%s_error_log.txt" % self.tool_name)
|
|
239 self.tlog = os.path.join(self.repdir, "%s_runner_log.txt" % self.tool_name)
|
48
|
240
|
|
241 if self.args.parampass == "0":
|
|
242 self.clsimple()
|
|
243 else:
|
|
244 clsuffix = []
|
|
245 xclsuffix = []
|
|
246 for i, p in enumerate(self.infiles):
|
|
247 if p[IOCLPOS] == "STDIN":
|
|
248 appendme = [
|
|
249 p[IOCLPOS],
|
|
250 p[ICLPOS],
|
|
251 p[IPATHPOS],
|
|
252 "< %s" % p[IPATHPOS],
|
|
253 ]
|
|
254 xappendme = [
|
|
255 p[IOCLPOS],
|
|
256 p[ICLPOS],
|
|
257 p[IPATHPOS],
|
|
258 "< $%s" % p[ICLPOS],
|
|
259 ]
|
|
260 else:
|
|
261 appendme = [p[IOCLPOS], p[ICLPOS], p[IPATHPOS], ""]
|
|
262 xappendme = [p[IOCLPOS], p[ICLPOS], "$%s" % p[ICLPOS], ""]
|
|
263 clsuffix.append(appendme)
|
|
264 xclsuffix.append(xappendme)
|
|
265 for i, p in enumerate(self.outfiles):
|
|
266 if p[OOCLPOS] == "STDOUT":
|
|
267 self.lastclredirect = [">", p[ONAMEPOS]]
|
|
268 self.lastxclredirect = [">", "$%s" % p[OCLPOS]]
|
|
269 else:
|
|
270 clsuffix.append([p[OOCLPOS], p[OCLPOS], p[ONAMEPOS], ""])
|
49
|
271 xclsuffix.append([p[OOCLPOS], p[OCLPOS], "$%s" % p[ONAMEPOS], ""])
|
48
|
272 for p in self.addpar:
|
49
|
273 clsuffix.append([p[AOCLPOS], p[ACLPOS], p[AVALPOS], p[AOVERPOS]])
|
48
|
274 xclsuffix.append(
|
|
275 [p[AOCLPOS], p[ACLPOS], '"$%s"' % p[ANAMEPOS], p[AOVERPOS]]
|
|
276 )
|
|
277 clsuffix.sort()
|
|
278 xclsuffix.sort()
|
|
279 self.xclsuffix = xclsuffix
|
|
280 self.clsuffix = clsuffix
|
|
281 if self.args.parampass == "positional":
|
|
282 self.clpositional()
|
|
283 else:
|
|
284 self.clargparse()
|
|
285
|
|
286 def prepScript(self):
|
|
287 rx = open(self.args.script_path, "r").readlines()
|
|
288 rx = [x.rstrip() for x in rx]
|
|
289 rxcheck = [x.strip() for x in rx if x.strip() > ""]
|
|
290 assert len(rxcheck) > 0, "Supplied script is empty. Cannot run"
|
|
291 self.script = "\n".join(rx)
|
|
292 fhandle, self.sfile = tempfile.mkstemp(
|
49
|
293 prefix=self.tool_name, suffix="_%s" % (self.executeme)
|
48
|
294 )
|
|
295 tscript = open(self.sfile, "w")
|
|
296 tscript.write(self.script)
|
|
297 tscript.close()
|
108
|
298 self.indentedScript = " %s" % "\n".join([" %s" % cheetah_escape(x) for x in rx])
|
|
299 self.escapedScript = "%s" % "\n".join([" %s" % cheetah_escape(x) for x in rx])
|
49
|
300 art = "%s.%s" % (self.tool_name, self.executeme)
|
48
|
301 artifact = open(art, "wb")
|
108
|
302 artifact.write(bytes(self.escapedScript, "utf8"))
|
48
|
303 artifact.close()
|
|
304
|
|
305 def cleanuppar(self):
|
|
306 """ positional parameters are complicated by their numeric ordinal"""
|
|
307 for i, p in enumerate(self.infiles):
|
|
308 if self.args.parampass == "positional":
|
75
|
309 assert p[
|
|
310 ICLPOS
|
|
311 ].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (
|
|
312 p[ICLPOS],
|
|
313 p[ILABPOS],
|
48
|
314 )
|
|
315 p.append(p[ICLPOS])
|
|
316 if p[ICLPOS].isdigit() or self.args.parampass == "0":
|
|
317 scl = "input%d" % (i + 1)
|
|
318 p[ICLPOS] = scl
|
|
319 self.infiles[i] = p
|
|
320 for i, p in enumerate(
|
|
321 self.outfiles
|
|
322 ): # trying to automagically gather using extensions
|
|
323 if self.args.parampass == "positional" and p[OCLPOS] != "STDOUT":
|
75
|
324 assert p[
|
|
325 OCLPOS
|
|
326 ].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (
|
|
327 p[OCLPOS],
|
|
328 p[ONAMEPOS],
|
48
|
329 )
|
|
330 p.append(p[OCLPOS])
|
|
331 if p[OCLPOS].isdigit() or p[OCLPOS] == "STDOUT":
|
|
332 scl = p[ONAMEPOS]
|
|
333 p[OCLPOS] = scl
|
|
334 self.outfiles[i] = p
|
|
335 for i, p in enumerate(self.addpar):
|
|
336 if self.args.parampass == "positional":
|
75
|
337 assert p[
|
|
338 ACLPOS
|
|
339 ].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (
|
|
340 p[ACLPOS],
|
|
341 p[ANAMEPOS],
|
48
|
342 )
|
|
343 p.append(p[ACLPOS])
|
|
344 if p[ACLPOS].isdigit():
|
|
345 scl = "input%s" % p[ACLPOS]
|
|
346 p[ACLPOS] = scl
|
|
347 self.addpar[i] = p
|
|
348
|
|
349 def clsimple(self):
|
75
|
350 """no parameters - uses < and > for i/o"""
|
48
|
351 aCL = self.cl.append
|
|
352 aXCL = self.xmlcl.append
|
62
|
353
|
|
354 if len(self.infiles) > 0:
|
|
355 aCL("<")
|
|
356 aCL(self.infiles[0][IPATHPOS])
|
|
357 aXCL("<")
|
|
358 aXCL("$%s" % self.infiles[0][ICLPOS])
|
|
359 if len(self.outfiles) > 0:
|
|
360 aCL(">")
|
|
361 aCL(self.outfiles[0][OCLPOS])
|
|
362 aXCL(">")
|
|
363 aXCL("$%s" % self.outfiles[0][ONAMEPOS])
|
48
|
364
|
|
365 def clpositional(self):
|
|
366 # inputs in order then params
|
|
367 aCL = self.cl.append
|
|
368 for (o_v, k, v, koverride) in self.clsuffix:
|
|
369 if " " in v:
|
|
370 aCL("%s" % v)
|
|
371 else:
|
|
372 aCL(v)
|
|
373 aXCL = self.xmlcl.append
|
|
374 for (o_v, k, v, koverride) in self.xclsuffix:
|
|
375 aXCL(v)
|
|
376 if self.lastxclredirect:
|
|
377 aXCL(self.lastxclredirect[0])
|
|
378 aXCL(self.lastxclredirect[1])
|
|
379
|
|
380 def clargparse(self):
|
75
|
381 """argparse style"""
|
48
|
382 aCL = self.cl.append
|
|
383 aXCL = self.xmlcl.append
|
|
384 # inputs then params in argparse named form
|
|
385 for (o_v, k, v, koverride) in self.xclsuffix:
|
|
386 if koverride > "":
|
|
387 k = koverride
|
|
388 elif len(k.strip()) == 1:
|
|
389 k = "-%s" % k
|
|
390 else:
|
|
391 k = "--%s" % k
|
|
392 aXCL(k)
|
|
393 aXCL(v)
|
|
394 for (o_v, k, v, koverride) in self.clsuffix:
|
|
395 if koverride > "":
|
|
396 k = koverride
|
|
397 elif len(k.strip()) == 1:
|
|
398 k = "-%s" % k
|
|
399 else:
|
|
400 k = "--%s" % k
|
|
401 aCL(k)
|
|
402 aCL(v)
|
|
403
|
|
404 def getNdash(self, newname):
|
|
405 if self.is_positional:
|
|
406 ndash = 0
|
|
407 else:
|
|
408 ndash = 2
|
|
409 if len(newname) < 2:
|
|
410 ndash = 1
|
|
411 return ndash
|
|
412
|
|
413 def doXMLparam(self):
|
|
414 """flake8 made me do this..."""
|
|
415 for p in self.outfiles:
|
49
|
416 newname, newfmt, newcl, test, oldcl = p
|
108
|
417 test = test.strip()
|
48
|
418 ndash = self.getNdash(newcl)
|
|
419 aparm = gxtp.OutputData(newcl, format=newfmt, num_dashes=ndash)
|
|
420 aparm.positional = self.is_positional
|
|
421 if self.is_positional:
|
|
422 if oldcl == "STDOUT":
|
|
423 aparm.positional = 9999999
|
|
424 aparm.command_line_override = "> $%s" % newcl
|
|
425 else:
|
|
426 aparm.positional = int(oldcl)
|
|
427 aparm.command_line_override = "$%s" % newcl
|
|
428 self.toutputs.append(aparm)
|
49
|
429 ld = None
|
108
|
430 if test.strip() > "":
|
50
|
431 if test.startswith("diff"):
|
108
|
432 c = "diff"
|
|
433 ld = 0
|
50
|
434 if test.split(":")[1].isdigit:
|
|
435 ld = int(test.split(":")[1])
|
108
|
436 tp = gxtp.TestOutput(
|
|
437 name=newcl,
|
|
438 value="%s_sample" % newcl,
|
|
439 format=newfmt,
|
|
440 compare= c,
|
|
441 lines_diff=ld,
|
|
442 )
|
|
443 elif test.startswith("sim_size"):
|
|
444 c = "sim_size"
|
|
445 tn = test.split(":")[1].strip()
|
|
446 if tn > '':
|
|
447 if '.' in tn:
|
|
448 delta = None
|
|
449 delta_frac = min(1.0,float(tn))
|
|
450 else:
|
|
451 delta = int(tn)
|
|
452 delta_frac = None
|
|
453 tp = gxtp.TestOutput(
|
|
454 name=newcl,
|
|
455 value="%s_sample" % newcl,
|
|
456 format=newfmt,
|
|
457 compare= c,
|
|
458 delta = delta,
|
|
459 delta_frac = delta_frac
|
|
460 )
|
|
461 self.testparam.append(tp)
|
48
|
462 for p in self.infiles:
|
|
463 newname = p[ICLPOS]
|
|
464 newfmt = p[IFMTPOS]
|
|
465 ndash = self.getNdash(newname)
|
|
466 if not len(p[ILABPOS]) > 0:
|
|
467 alab = p[ICLPOS]
|
|
468 else:
|
|
469 alab = p[ILABPOS]
|
|
470 aninput = gxtp.DataParam(
|
|
471 newname,
|
|
472 optional=False,
|
|
473 label=alab,
|
|
474 help=p[IHELPOS],
|
|
475 format=newfmt,
|
|
476 multiple=False,
|
|
477 num_dashes=ndash,
|
|
478 )
|
|
479 aninput.positional = self.is_positional
|
|
480 self.tinputs.append(aninput)
|
|
481 tparm = gxtp.TestParam(name=newname, value="%s_sample" % newname)
|
|
482 self.testparam.append(tparm)
|
|
483 for p in self.addpar:
|
|
484 newname, newval, newlabel, newhelp, newtype, newcl, override, oldcl = p
|
|
485 if not len(newlabel) > 0:
|
|
486 newlabel = newname
|
|
487 ndash = self.getNdash(newname)
|
|
488 if newtype == "text":
|
|
489 aparm = gxtp.TextParam(
|
|
490 newname,
|
|
491 label=newlabel,
|
|
492 help=newhelp,
|
|
493 value=newval,
|
|
494 num_dashes=ndash,
|
|
495 )
|
|
496 elif newtype == "integer":
|
|
497 aparm = gxtp.IntegerParam(
|
|
498 newname,
|
|
499 label=newname,
|
|
500 help=newhelp,
|
|
501 value=newval,
|
|
502 num_dashes=ndash,
|
|
503 )
|
|
504 elif newtype == "float":
|
|
505 aparm = gxtp.FloatParam(
|
|
506 newname,
|
|
507 label=newname,
|
|
508 help=newhelp,
|
|
509 value=newval,
|
|
510 num_dashes=ndash,
|
|
511 )
|
|
512 else:
|
|
513 raise ValueError(
|
|
514 'Unrecognised parameter type "%s" for\
|
|
515 additional parameter %s in makeXML'
|
|
516 % (newtype, newname)
|
|
517 )
|
|
518 aparm.positional = self.is_positional
|
|
519 if self.is_positional:
|
63
|
520 aparm.positional = int(oldcl)
|
48
|
521 self.tinputs.append(aparm)
|
63
|
522 tparm = gxtp.TestParam(newname, value=newval)
|
48
|
523 self.testparam.append(tparm)
|
|
524
|
|
525 def doNoXMLparam(self):
|
49
|
526 """filter style package - stdin to stdout"""
|
62
|
527 if len(self.infiles) > 0:
|
|
528 alab = self.infiles[0][ILABPOS]
|
|
529 if len(alab) == 0:
|
|
530 alab = self.infiles[0][ICLPOS]
|
|
531 max1s = (
|
|
532 "Maximum one input if parampass is 0 but multiple input files supplied - %s"
|
|
533 % str(self.infiles)
|
|
534 )
|
|
535 assert len(self.infiles) == 1, max1s
|
|
536 newname = self.infiles[0][ICLPOS]
|
|
537 aninput = gxtp.DataParam(
|
|
538 newname,
|
|
539 optional=False,
|
|
540 label=alab,
|
|
541 help=self.infiles[0][IHELPOS],
|
|
542 format=self.infiles[0][IFMTPOS],
|
|
543 multiple=False,
|
|
544 num_dashes=0,
|
|
545 )
|
|
546 aninput.command_line_override = "< $%s" % newname
|
|
547 aninput.positional = self.is_positional
|
|
548 self.tinputs.append(aninput)
|
|
549 tp = gxtp.TestParam(name=newname, value="%s_sample" % newname)
|
|
550 self.testparam.append(tp)
|
63
|
551 if len(self.outfiles) > 0:
|
62
|
552 newname = self.outfiles[0][OCLPOS]
|
|
553 newfmt = self.outfiles[0][OFMTPOS]
|
|
554 anout = gxtp.OutputData(newname, format=newfmt, num_dashes=0)
|
|
555 anout.command_line_override = "> $%s" % newname
|
|
556 anout.positional = self.is_positional
|
|
557 self.toutputs.append(anout)
|
75
|
558 tp = gxtp.TestOutput(
|
|
559 name=newname, value="%s_sample" % newname, format=newfmt
|
|
560 )
|
62
|
561 self.testparam.append(tp)
|
48
|
562
|
|
563 def makeXML(self):
|
|
564 """
|
|
565 Create a Galaxy xml tool wrapper for the new script
|
|
566 Uses galaxyhtml
|
|
567 Hmmm. How to get the command line into correct order...
|
|
568 """
|
49
|
569 if self.command_override:
|
56
|
570 self.newtool.command_override = self.command_override # config file
|
48
|
571 else:
|
56
|
572 self.newtool.command_override = self.xmlcl
|
48
|
573 if self.args.help_text:
|
|
574 helptext = open(self.args.help_text, "r").readlines()
|
108
|
575 safertext = "\n".join([cheetah_escape(x) for x in helptext])
|
|
576 if self.args.script_path:
|
|
577 scr = self.script.split("\n")
|
|
578 scrp = [cheetah_escape(x) for x in scr]
|
|
579 scrpt = [' %s' % x for x in scrpt if x.strip() > ''] # indent
|
|
580 scrpt.insert(0,'------\n\nScript::\n')
|
50
|
581 if len(scrpt) > 300:
|
75
|
582 safertext = (
|
106
|
583 safertext
|
|
584 + scrpt[:100]
|
|
585 + [">500 lines - stuff deleted", "......"]
|
|
586 + scrpt[-100:]
|
75
|
587 )
|
50
|
588 else:
|
108
|
589 safertext = safertext + "\n".join(scrpt)
|
|
590 self.newtool.help = safertext
|
48
|
591 else:
|
50
|
592 self.newtool.help = (
|
48
|
593 "Please ask the tool author (%s) for help \
|
|
594 as none was supplied at tool generation\n"
|
|
595 % (self.args.user_email)
|
|
596 )
|
50
|
597 self.newtool.version_command = None # do not want
|
48
|
598 requirements = gxtp.Requirements()
|
49
|
599 if self.args.packages:
|
|
600 for d in self.args.packages.split(","):
|
|
601 if ":" in d:
|
|
602 packg, ver = d.split(":")
|
|
603 else:
|
|
604 packg = d
|
|
605 ver = ""
|
50
|
606 requirements.append(
|
|
607 gxtp.Requirement("package", packg.strip(), ver.strip())
|
|
608 )
|
|
609 self.newtool.requirements = requirements
|
48
|
610 if self.args.parampass == "0":
|
|
611 self.doNoXMLparam()
|
|
612 else:
|
|
613 self.doXMLparam()
|
50
|
614 self.newtool.outputs = self.toutputs
|
|
615 self.newtool.inputs = self.tinputs
|
|
616 if self.args.script_path:
|
48
|
617 configfiles = gxtp.Configfiles()
|
108
|
618 configfiles.append(gxtp.Configfile(name="runme", text=self.escapedScript))
|
50
|
619 self.newtool.configfiles = configfiles
|
48
|
620 tests = gxtp.Tests()
|
|
621 test_a = gxtp.Test()
|
|
622 for tp in self.testparam:
|
|
623 test_a.append(tp)
|
|
624 tests.append(test_a)
|
50
|
625 self.newtool.tests = tests
|
|
626 self.newtool.add_comment(
|
48
|
627 "Created by %s at %s using the Galaxy Tool Factory."
|
|
628 % (self.args.user_email, timenow())
|
|
629 )
|
50
|
630 self.newtool.add_comment("Source in git at: %s" % (toolFactoryURL))
|
|
631 self.newtool.add_comment(
|
108
|
632 "Cite: Creating re-usable tools from scripts doi:10.1093/bioinformatics/bts573"
|
48
|
633 )
|
50
|
634 exml0 = self.newtool.export()
|
49
|
635 exml = exml0.replace(FAKEEXE, "") # temporary work around until PR accepted
|
50
|
636 if (
|
|
637 self.test_override
|
|
638 ): # cannot do this inside galaxyxml as it expects lxml objects for tests
|
|
639 part1 = exml.split("<tests>")[0]
|
|
640 part2 = exml.split("</tests>")[1]
|
|
641 fixed = "%s\n%s\n%s" % (part1, self.test_override, part2)
|
49
|
642 exml = fixed
|
63
|
643 exml = exml.replace('range="1:"', 'range="1000:"')
|
49
|
644 xf = open("%s.xml" % self.tool_name, "w")
|
48
|
645 xf.write(exml)
|
|
646 xf.write("\n")
|
|
647 xf.close()
|
|
648 # ready for the tarball
|
|
649
|
|
650 def run(self):
|
|
651 """
|
50
|
652 generate test outputs by running a command line
|
56
|
653 won't work if command or test override in play - planemo is the
|
50
|
654 easiest way to generate test outputs for that case so is
|
|
655 automagically selected
|
48
|
656 """
|
|
657 scl = " ".join(self.cl)
|
|
658 err = None
|
|
659 if self.args.parampass != "0":
|
56
|
660 if os.path.exists(self.elog):
|
|
661 ste = open(self.elog, "a")
|
|
662 else:
|
|
663 ste = open(self.elog, "w")
|
48
|
664 if self.lastclredirect:
|
49
|
665 sto = open(self.lastclredirect[1], "wb") # is name of an output file
|
48
|
666 else:
|
56
|
667 if os.path.exists(self.tlog):
|
|
668 sto = open(self.tlog, "a")
|
|
669 else:
|
|
670 sto = open(self.tlog, "w")
|
48
|
671 sto.write(
|
75
|
672 "## Executing Toolfactory generated command line = %s\n" % scl
|
48
|
673 )
|
|
674 sto.flush()
|
106
|
675 subp = subprocess.run(
|
|
676 self.cl, env=self.ourenv, shell=False, stdout=sto, stderr=ste
|
|
677 )
|
48
|
678 sto.close()
|
|
679 ste.close()
|
98
|
680 retval = subp.returncode
|
49
|
681 else: # work around special case - stdin and write to stdout
|
62
|
682 if len(self.infiles) > 0:
|
|
683 sti = open(self.infiles[0][IPATHPOS], "rb")
|
|
684 else:
|
63
|
685 sti = sys.stdin
|
62
|
686 if len(self.outfiles) > 0:
|
|
687 sto = open(self.outfiles[0][ONAMEPOS], "wb")
|
|
688 else:
|
|
689 sto = sys.stdout
|
106
|
690 subp = subprocess.run(
|
|
691 self.cl, env=self.ourenv, shell=False, stdout=sto, stdin=sti
|
|
692 )
|
75
|
693 sto.write("## Executing Toolfactory generated command line = %s\n" % scl)
|
98
|
694 retval = subp.returncode
|
48
|
695 sto.close()
|
|
696 sti.close()
|
|
697 if os.path.isfile(self.tlog) and os.stat(self.tlog).st_size == 0:
|
|
698 os.unlink(self.tlog)
|
|
699 if os.path.isfile(self.elog) and os.stat(self.elog).st_size == 0:
|
|
700 os.unlink(self.elog)
|
|
701 if retval != 0 and err: # problem
|
|
702 sys.stderr.write(err)
|
|
703 logging.debug("run done")
|
|
704 return retval
|
|
705
|
101
|
706 def copy_to_container(self, src, dest, container):
|
106
|
707 """Recreate the src directory tree at dest - full path included"""
|
101
|
708 idir = os.getcwd()
|
|
709 workdir = os.path.dirname(src)
|
|
710 os.chdir(workdir)
|
|
711 _, tfname = tempfile.mkstemp(suffix=".tar")
|
106
|
712 tar = tarfile.open(tfname, mode="w")
|
101
|
713 srcb = os.path.basename(src)
|
|
714 tar.add(srcb)
|
|
715 tar.close()
|
106
|
716 data = open(tfname, "rb").read()
|
101
|
717 container.put_archive(dest, data)
|
|
718 os.unlink(tfname)
|
|
719 os.chdir(idir)
|
|
720
|
|
721 def copy_from_container(self, src, dest, container):
|
106
|
722 """recreate the src directory tree at dest using docker sdk"""
|
|
723 os.makedirs(dest, exist_ok=True)
|
101
|
724 _, tfname = tempfile.mkstemp(suffix=".tar")
|
106
|
725 tf = open(tfname, "wb")
|
101
|
726 bits, stat = container.get_archive(src)
|
|
727 for chunk in bits:
|
|
728 tf.write(chunk)
|
|
729 tf.close()
|
106
|
730 tar = tarfile.open(tfname, "r")
|
101
|
731 tar.extractall(dest)
|
|
732 tar.close()
|
|
733 os.unlink(tfname)
|
|
734
|
|
735 def planemo_biodocker_test(self):
|
|
736 """planemo currently leaks dependencies if used in the same container and gets unhappy after a
|
|
737 first successful run. https://github.com/galaxyproject/planemo/issues/1078#issuecomment-731476930
|
|
738
|
|
739 Docker biocontainer has planemo with caches filled to save repeated downloads
|
|
740
|
|
741
|
99
|
742 """
|
106
|
743
|
|
744 def prun(container, tout, cl, user="biodocker"):
|
|
745 rlog = container.exec_run(cl, user=user)
|
|
746 slogl = str(rlog).split("\\n")
|
|
747 slog = "\n".join(slogl)
|
103
|
748 tout.write(f"## got rlog {slog} from {cl}\n")
|
101
|
749 if os.path.exists(self.tlog):
|
|
750 tout = open(self.tlog, "a")
|
|
751 else:
|
|
752 tout = open(self.tlog, "w")
|
|
753 planemoimage = "quay.io/fubar2/planemo-biocontainer"
|
|
754 xreal = "%s.xml" % self.tool_name
|
|
755 repname = f"{self.tool_name}_planemo_test_report.html"
|
106
|
756 ptestrep_path = os.path.join(self.repdir, repname)
|
101
|
757 tool_name = self.tool_name
|
|
758 client = docker.from_env()
|
103
|
759 tvol = client.volumes.create()
|
|
760 tvolname = tvol.name
|
|
761 destdir = "/toolfactory/ptest"
|
106
|
762 imrep = os.path.join(destdir, repname)
|
105
|
763 # need to keep the container running so sleep a while - we stop and destroy it when we are done
|
106
|
764 container = client.containers.run(
|
|
765 planemoimage,
|
|
766 "sleep 30m",
|
|
767 detach=True,
|
|
768 user="biodocker",
|
|
769 volumes={f"{tvolname}": {"bind": "/toolfactory", "mode": "rw"}},
|
|
770 )
|
103
|
771 cl = f"mkdir -p {destdir}"
|
|
772 prun(container, tout, cl, user="root")
|
|
773 cl = f"rm -rf {destdir}/*"
|
|
774 prun(container, tout, cl, user="root")
|
106
|
775 ptestpath = os.path.join(destdir, "tfout", xreal)
|
|
776 self.copy_to_container(self.tooloutdir, destdir, container)
|
107
|
777 cl = "chown -R biodocker /toolfactory"
|
103
|
778 prun(container, tout, cl, user="root")
|
101
|
779 rlog = container.exec_run(f"ls -la {destdir}")
|
103
|
780 ptestcl = f"planemo test --update_test_data --no_cleanup --test_data {destdir}/tfout/test-data --galaxy_root /home/biodocker/galaxy-central {ptestpath}"
|
101
|
781 try:
|
|
782 rlog = container.exec_run(ptestcl)
|
|
783 except:
|
|
784 e = sys.exc_info()[0]
|
103
|
785 tout.write(f"#### error: {e} from {ptestcl}\n")
|
101
|
786 # fails - used to generate test outputs
|
103
|
787 cl = f"planemo test --test_output {imrep} --no_cleanup --test_data {destdir}/tfout/test-data --galaxy_root /home/biodocker/galaxy-central {ptestpath}"
|
101
|
788 try:
|
106
|
789 prun(container, tout, cl)
|
101
|
790 except:
|
|
791 pass
|
106
|
792 testouts = tempfile.mkdtemp(suffix=None, prefix="tftemp", dir=".")
|
|
793 self.copy_from_container(destdir, testouts, container)
|
|
794 src = os.path.join(testouts, "ptest")
|
103
|
795 if os.path.isdir(src):
|
106
|
796 shutil.copytree(src, ".", dirs_exist_ok=True)
|
103
|
797 src = repname
|
|
798 if os.path.isfile(repname):
|
106
|
799 shutil.copyfile(src, ptestrep_path)
|
103
|
800 else:
|
|
801 tout.write(f"No output from run to shutil.copytree in {src}\n")
|
101
|
802 tout.close()
|
|
803 container.stop()
|
|
804 container.remove()
|
103
|
805 tvol.remove()
|
106
|
806 # shutil.rmtree(testouts)
|
101
|
807
|
63
|
808 def shedLoad(self):
|
48
|
809 """
|
63
|
810 {'deleted': False,
|
|
811 'description': 'Tools for manipulating data',
|
|
812 'id': '175812cd7caaf439',
|
|
813 'model_class': 'Category',
|
|
814 'name': 'Text Manipulation',
|
|
815 'url': '/api/categories/175812cd7caaf439'}]
|
|
816
|
|
817
|
48
|
818 """
|
49
|
819 if os.path.exists(self.tlog):
|
63
|
820 sto = open(self.tlog, "a")
|
48
|
821 else:
|
63
|
822 sto = open(self.tlog, "w")
|
48
|
823
|
75
|
824 ts = toolshed.ToolShedInstance(
|
|
825 url=self.args.toolshed_url, key=self.args.toolshed_api_key, verify=False
|
|
826 )
|
63
|
827 repos = ts.repositories.get_repositories()
|
75
|
828 rnames = [x.get("name", "?") for x in repos]
|
|
829 rids = [x.get("id", "?") for x in repos]
|
98
|
830 tfcat = "ToolFactory generated tools"
|
101
|
831 if self.tool_name not in rnames:
|
63
|
832 tscat = ts.categories.get_categories()
|
98
|
833 cnames = [x.get("name", "?").strip() for x in tscat]
|
75
|
834 cids = [x.get("id", "?") for x in tscat]
|
63
|
835 catID = None
|
98
|
836 if tfcat.strip() in cnames:
|
|
837 ci = cnames.index(tfcat)
|
63
|
838 catID = cids[ci]
|
75
|
839 res = ts.repositories.create_repository(
|
|
840 name=self.args.tool_name,
|
|
841 synopsis="Synopsis:%s" % self.args.tool_desc,
|
|
842 description=self.args.tool_desc,
|
|
843 type="unrestricted",
|
|
844 remote_repository_url=self.args.toolshed_url,
|
|
845 homepage_url=None,
|
|
846 category_ids=catID,
|
|
847 )
|
|
848 tid = res.get("id", None)
|
49
|
849 else:
|
101
|
850 i = rnames.index(self.tool_name)
|
75
|
851 tid = rids[i]
|
100
|
852 try:
|
|
853 res = ts.repositories.update_repository(
|
106
|
854 id=tid, tar_ball_path=self.newtarpath, commit_message=None
|
|
855 )
|
100
|
856 sto.write(f"#####update res={res}\n")
|
|
857 except ConnectionError:
|
106
|
858 sto.write(
|
|
859 "Probably no change to repository - bioblend shed upload failed\n"
|
|
860 )
|
63
|
861 sto.close()
|
|
862
|
48
|
863 def eph_galaxy_load(self):
|
75
|
864 """load the new tool from the local toolshed after planemo uploads it"""
|
49
|
865 if os.path.exists(self.tlog):
|
50
|
866 tout = open(self.tlog, "a")
|
49
|
867 else:
|
50
|
868 tout = open(self.tlog, "w")
|
49
|
869 cll = [
|
|
870 "shed-tools",
|
|
871 "install",
|
|
872 "-g",
|
|
873 self.args.galaxy_url,
|
|
874 "--latest",
|
|
875 "-a",
|
|
876 self.args.galaxy_api_key,
|
|
877 "--name",
|
101
|
878 self.tool_name,
|
49
|
879 "--owner",
|
|
880 "fubar",
|
|
881 "--toolshed",
|
|
882 self.args.toolshed_url,
|
75
|
883 "--section_label",
|
63
|
884 "ToolFactory",
|
49
|
885 ]
|
63
|
886 tout.write("running\n%s\n" % " ".join(cll))
|
106
|
887 subp = subprocess.run(
|
|
888 cll, env=self.ourenv, cwd=self.ourcwd, shell=False, stderr=tout, stdout=tout
|
|
889 )
|
75
|
890 tout.write(
|
101
|
891 "installed %s - got retcode %d\n" % (self.tool_name, subp.returncode)
|
75
|
892 )
|
63
|
893 tout.close()
|
98
|
894 return subp.returncode
|
63
|
895
|
99
|
896 def planemo_shedLoad(self):
|
63
|
897 """
|
|
898 planemo shed_create --shed_target testtoolshed
|
|
899 planemo shed_init --name=<name>
|
|
900 --owner=<shed_username>
|
|
901 --description=<short description>
|
|
902 [--remote_repository_url=<URL to .shed.yml on github>]
|
|
903 [--homepage_url=<Homepage for tool.>]
|
|
904 [--long_description=<long description>]
|
|
905 [--category=<category name>]*
|
66
|
906
|
|
907
|
63
|
908 planemo shed_update --check_diff --shed_target testtoolshed
|
|
909 """
|
|
910 if os.path.exists(self.tlog):
|
|
911 tout = open(self.tlog, "a")
|
48
|
912 else:
|
63
|
913 tout = open(self.tlog, "w")
|
75
|
914 ts = toolshed.ToolShedInstance(
|
|
915 url=self.args.toolshed_url, key=self.args.toolshed_api_key, verify=False
|
|
916 )
|
63
|
917 repos = ts.repositories.get_repositories()
|
75
|
918 rnames = [x.get("name", "?") for x in repos]
|
|
919 rids = [x.get("id", "?") for x in repos]
|
106
|
920 # cat = "ToolFactory generated tools"
|
101
|
921 if self.tool_name not in rnames:
|
75
|
922 cll = [
|
|
923 "planemo",
|
|
924 "shed_create",
|
|
925 "--shed_target",
|
|
926 "local",
|
|
927 "--owner",
|
|
928 "fubar",
|
|
929 "--name",
|
101
|
930 self.tool_name,
|
75
|
931 "--shed_key",
|
|
932 self.args.toolshed_api_key,
|
|
933 ]
|
63
|
934 try:
|
98
|
935 subp = subprocess.run(
|
106
|
936 cll,
|
|
937 env=self.ourenv,
|
|
938 shell=False,
|
|
939 cwd=self.tooloutdir,
|
|
940 stdout=tout,
|
|
941 stderr=tout,
|
63
|
942 )
|
|
943 except:
|
|
944 pass
|
98
|
945 if subp.returncode != 0:
|
101
|
946 tout.write("Repository %s exists\n" % self.tool_name)
|
63
|
947 else:
|
101
|
948 tout.write("initiated %s\n" % self.tool_name)
|
63
|
949 cll = [
|
|
950 "planemo",
|
|
951 "shed_upload",
|
|
952 "--shed_target",
|
|
953 "local",
|
|
954 "--owner",
|
|
955 "fubar",
|
|
956 "--name",
|
101
|
957 self.tool_name,
|
63
|
958 "--shed_key",
|
|
959 self.args.toolshed_api_key,
|
|
960 "--tar",
|
|
961 self.newtarpath,
|
|
962 ]
|
106
|
963 subp = subprocess.run(
|
|
964 cll, env=self.ourenv, cwd=self.ourcwd, shell=False, stdout=tout, stderr=tout
|
|
965 )
|
|
966 tout.write("Ran %s got %d\n" % (" ".join(cll), subp.returncode))
|
49
|
967 tout.close()
|
98
|
968 return subp.returncode
|
48
|
969
|
76
|
970 def eph_test(self, genoutputs=True):
|
106
|
971 """problem getting jobid - ephemeris upload is the job before the one we want - but depends on how many inputs"""
|
75
|
972 if os.path.exists(self.tlog):
|
|
973 tout = open(self.tlog, "a")
|
|
974 else:
|
|
975 tout = open(self.tlog, "w")
|
|
976 cll = [
|
|
977 "shed-tools",
|
|
978 "test",
|
|
979 "-g",
|
|
980 self.args.galaxy_url,
|
|
981 "-a",
|
|
982 self.args.galaxy_api_key,
|
|
983 "--name",
|
101
|
984 self.tool_name,
|
75
|
985 "--owner",
|
|
986 "fubar",
|
|
987 ]
|
76
|
988 if genoutputs:
|
|
989 dummy, tfile = tempfile.mkstemp()
|
98
|
990 subp = subprocess.run(
|
106
|
991 cll,
|
|
992 env=self.ourenv,
|
|
993 cwd=self.ourcwd,
|
|
994 shell=False,
|
|
995 stderr=dummy,
|
|
996 stdout=dummy,
|
76
|
997 )
|
|
998
|
106
|
999 with open("tool_test_output.json", "rb") as f:
|
76
|
1000 s = json.loads(f.read())
|
106
|
1001 print("read %s" % s)
|
|
1002 cl = s["tests"][0]["data"]["job"]["command_line"].split()
|
|
1003 n = cl.index("--script_path")
|
|
1004 jobdir = cl[n + 1]
|
|
1005 jobdir = jobdir.replace('"', "")
|
|
1006 jobdir = jobdir.split("/configs")[0]
|
|
1007 print("jobdir=%s" % jobdir)
|
76
|
1008
|
106
|
1009 # "/home/ross/galthrow/database/jobs_directory/000/649/configs/tmptfxu51gs\"
|
|
1010 src = os.path.join(jobdir, "working", self.newtarpath)
|
76
|
1011 if os.path.exists(src):
|
|
1012 dest = os.path.join(self.testdir, self.newtarpath)
|
|
1013 shutil.copyfile(src, dest)
|
|
1014 else:
|
106
|
1015 tout.write(
|
|
1016 "No toolshed archive found after first ephemeris test - not a good sign"
|
|
1017 )
|
|
1018 ephouts = os.path.join(jobdir, "working", "tfout", "test-data")
|
76
|
1019 with os.scandir(ephouts) as outs:
|
|
1020 for entry in outs:
|
|
1021 if not entry.is_file():
|
|
1022 continue
|
|
1023 dest = os.path.join(self.tooloutdir, entry.name)
|
|
1024 src = os.path.join(ephouts, entry.name)
|
|
1025 shutil.copyfile(src, dest)
|
|
1026 else:
|
98
|
1027 subp = subprocess.run(
|
106
|
1028 cll,
|
|
1029 env=self.ourenv,
|
|
1030 cwd=self.ourcwd,
|
|
1031 shell=False,
|
|
1032 stderr=tout,
|
|
1033 stdout=tout,
|
|
1034 )
|
98
|
1035 tout.write("eph_test Ran %s got %d" % (" ".join(cll), subp.returncode))
|
75
|
1036 tout.close()
|
98
|
1037 return subp.returncode
|
63
|
1038
|
83
|
1039 def planemo_test_biocontainer(self, genoutputs=True):
|
|
1040 """planemo is a requirement so is available for testing but testing in a biocontainer
|
106
|
1041 requires some fiddling to use the hacked galaxy-central .venv
|
83
|
1042
|
106
|
1043 Planemo runs:
|
|
1044 python ./scripts/functional_tests.py -v --with-nosehtml --html-report-file
|
|
1045 /export/galaxy-central/database/job_working_directory/000/17/working/TF_run_report_tempdir/tacrev_planemo_test_report.html
|
|
1046 --with-xunit --xunit-file /tmp/tmpt90p7f9h/xunit.xml --with-structureddata
|
|
1047 --structured-data-file
|
|
1048 /export/galaxy-central/database/job_working_directory/000/17/working/tfout/tool_test_output.json functional.test_toolbox
|
83
|
1049
|
|
1050
|
106
|
1051 for the planemo-biocontainer,
|
|
1052 planemo test --conda_dependency_resolution --skip_venv --galaxy_root /galthrow/ rgToolFactory2.xml
|
83
|
1053
|
|
1054 """
|
|
1055 xreal = "%s.xml" % self.tool_name
|
106
|
1056 tool_test_path = os.path.join(
|
|
1057 self.repdir, f"{self.tool_name}_planemo_test_report.html"
|
|
1058 )
|
83
|
1059 if os.path.exists(self.tlog):
|
|
1060 tout = open(self.tlog, "a")
|
|
1061 else:
|
|
1062 tout = open(self.tlog, "w")
|
|
1063 if genoutputs:
|
|
1064 dummy, tfile = tempfile.mkstemp()
|
|
1065 cll = [
|
106
|
1066 ".",
|
|
1067 os.path.join(self.args.galaxy_root, ".venv", "bin", "activate"),
|
|
1068 "&&",
|
83
|
1069 "planemo",
|
|
1070 "test",
|
106
|
1071 "--test_data",
|
|
1072 self.testdir,
|
|
1073 "--test_output",
|
|
1074 tool_test_path,
|
83
|
1075 "--skip_venv",
|
|
1076 "--galaxy_root",
|
91
|
1077 self.args.galaxy_root,
|
83
|
1078 "--update_test_data",
|
98
|
1079 xreal,
|
83
|
1080 ]
|
98
|
1081 subp = subprocess.run(
|
83
|
1082 cll,
|
101
|
1083 env=self.ourenv,
|
83
|
1084 shell=False,
|
|
1085 cwd=self.tooloutdir,
|
|
1086 stderr=dummy,
|
|
1087 stdout=dummy,
|
|
1088 )
|
|
1089
|
|
1090 else:
|
|
1091 cll = [
|
106
|
1092 ".",
|
|
1093 os.path.join(self.args.galaxy_root, ".venv", "bin", "activate"),
|
|
1094 "&&",
|
83
|
1095 "planemo",
|
|
1096 "test",
|
106
|
1097 "--test_data",
|
|
1098 os.path.self.testdir,
|
|
1099 "--test_output",
|
|
1100 os.path.tool_test_path,
|
83
|
1101 "--skip_venv",
|
|
1102 "--galaxy_root",
|
91
|
1103 self.args.galaxy_root,
|
98
|
1104 xreal,
|
83
|
1105 ]
|
98
|
1106 subp = subprocess.run(
|
106
|
1107 cll,
|
|
1108 env=self.ourenv,
|
|
1109 shell=False,
|
|
1110 cwd=self.tooloutdir,
|
|
1111 stderr=tout,
|
|
1112 stdout=tout,
|
83
|
1113 )
|
|
1114 tout.close()
|
98
|
1115 return subp.returncode
|
83
|
1116
|
48
|
1117 def writeShedyml(self):
|
75
|
1118 """for planemo"""
|
49
|
1119 yuser = self.args.user_email.split("@")[0]
|
|
1120 yfname = os.path.join(self.tooloutdir, ".shed.yml")
|
|
1121 yamlf = open(yfname, "w")
|
|
1122 odict = {
|
|
1123 "name": self.tool_name,
|
|
1124 "owner": yuser,
|
|
1125 "type": "unrestricted",
|
|
1126 "description": self.args.tool_desc,
|
50
|
1127 "synopsis": self.args.tool_desc,
|
|
1128 "category": "TF Generated Tools",
|
49
|
1129 }
|
48
|
1130 yaml.dump(odict, yamlf, allow_unicode=True)
|
|
1131 yamlf.close()
|
|
1132
|
50
|
1133 def makeTool(self):
|
75
|
1134 """write xmls and input samples into place"""
|
50
|
1135 self.makeXML()
|
|
1136 if self.args.script_path:
|
|
1137 stname = os.path.join(self.tooloutdir, "%s" % (self.sfile))
|
|
1138 if not os.path.exists(stname):
|
|
1139 shutil.copyfile(self.sfile, stname)
|
|
1140 xreal = "%s.xml" % self.tool_name
|
|
1141 xout = os.path.join(self.tooloutdir, xreal)
|
|
1142 shutil.copyfile(xreal, xout)
|
|
1143 for p in self.infiles:
|
|
1144 pth = p[IPATHPOS]
|
|
1145 dest = os.path.join(self.testdir, "%s_sample" % p[ICLPOS])
|
|
1146 shutil.copyfile(pth, dest)
|
49
|
1147
|
50
|
1148 def makeToolTar(self):
|
75
|
1149 """move outputs into test-data and prepare the tarball"""
|
101
|
1150 excludeme = "_planemo_test_report.html"
|
75
|
1151
|
66
|
1152 def exclude_function(tarinfo):
|
75
|
1153 filename = tarinfo.name
|
106
|
1154 return None if filename.endswith(excludeme) else tarinfo
|
66
|
1155
|
50
|
1156 for p in self.outfiles:
|
96
|
1157 oname = p[ONAMEPOS]
|
99
|
1158 tdest = os.path.join(self.testdir, "%s_sample" % oname)
|
|
1159 if not os.path.isfile(tdest):
|
106
|
1160 src = os.path.join(self.testdir, oname)
|
99
|
1161 if os.path.isfile(src):
|
|
1162 shutil.copyfile(src, tdest)
|
|
1163 dest = os.path.join(self.repdir, "%s.sample" % (oname))
|
|
1164 shutil.copyfile(src, dest)
|
|
1165 else:
|
|
1166 print(
|
|
1167 "### problem - output file %s not found in testdir %s"
|
|
1168 % (tdest, self.testdir)
|
|
1169 )
|
50
|
1170 tf = tarfile.open(self.newtarpath, "w:gz")
|
66
|
1171 tf.add(name=self.tooloutdir, arcname=self.tool_name, filter=exclude_function)
|
50
|
1172 tf.close()
|
|
1173 shutil.copyfile(self.newtarpath, self.args.new_tool)
|
|
1174
|
|
1175 def moveRunOutputs(self):
|
75
|
1176 """need to move planemo or run outputs into toolfactory collection"""
|
50
|
1177 with os.scandir(self.tooloutdir) as outs:
|
|
1178 for entry in outs:
|
80
|
1179 if not entry.is_file():
|
|
1180 continue
|
|
1181 if "." in entry.name:
|
|
1182 nayme, ext = os.path.splitext(entry.name)
|
106
|
1183 if ext in [".yml", ".xml", ".json", ".yaml"]:
|
|
1184 ext = f"{ext}.txt"
|
80
|
1185 else:
|
|
1186 ext = ".txt"
|
|
1187 ofn = "%s%s" % (entry.name.replace(".", "_"), ext)
|
|
1188 dest = os.path.join(self.repdir, ofn)
|
|
1189 src = os.path.join(self.tooloutdir, entry.name)
|
|
1190 shutil.copyfile(src, dest)
|
|
1191 with os.scandir(self.testdir) as outs:
|
|
1192 for entry in outs:
|
106
|
1193 if (
|
|
1194 (not entry.is_file())
|
|
1195 or entry.name.endswith("_sample")
|
|
1196 or entry.name.endswith("_planemo_test_report.html")
|
|
1197 ):
|
50
|
1198 continue
|
|
1199 if "." in entry.name:
|
|
1200 nayme, ext = os.path.splitext(entry.name)
|
|
1201 else:
|
|
1202 ext = ".txt"
|
80
|
1203 newname = f"{entry.name}{ext}"
|
|
1204 dest = os.path.join(self.repdir, newname)
|
|
1205 src = os.path.join(self.testdir, entry.name)
|
50
|
1206 shutil.copyfile(src, dest)
|
|
1207
|
49
|
1208
|
48
|
1209 def main():
|
|
1210 """
|
|
1211 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
|
49
|
1212 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath"
|
|
1213 --tool_name "foo" --interpreter "Rscript"
|
48
|
1214 </command>
|
|
1215 """
|
|
1216 parser = argparse.ArgumentParser()
|
|
1217 a = parser.add_argument
|
49
|
1218 a("--script_path", default=None)
|
|
1219 a("--history_test", default=None)
|
|
1220 a("--cl_prefix", default=None)
|
|
1221 a("--sysexe", default=None)
|
|
1222 a("--packages", default=None)
|
76
|
1223 a("--tool_name", default="newtool")
|
72
|
1224 a("--tool_dir", default=None)
|
48
|
1225 a("--input_files", default=[], action="append")
|
|
1226 a("--output_files", default=[], action="append")
|
|
1227 a("--user_email", default="Unknown")
|
|
1228 a("--bad_user", default=None)
|
49
|
1229 a("--make_Tool", default="runonly")
|
48
|
1230 a("--help_text", default=None)
|
|
1231 a("--tool_desc", default=None)
|
|
1232 a("--tool_version", default=None)
|
|
1233 a("--citations", default=None)
|
49
|
1234 a("--command_override", default=None)
|
|
1235 a("--test_override", default=None)
|
48
|
1236 a("--additional_parameters", action="append", default=[])
|
|
1237 a("--edit_additional_parameters", action="store_true", default=False)
|
|
1238 a("--parampass", default="positional")
|
|
1239 a("--tfout", default="./tfout")
|
|
1240 a("--new_tool", default="new_tool")
|
49
|
1241 a("--galaxy_url", default="http://localhost:8080")
|
106
|
1242 a("--toolshed_url", default="http://localhost:9009")
|
76
|
1243 # make sure this is identical to tool_sheds_conf.xml localhost != 127.0.0.1 so validation fails
|
63
|
1244 a("--toolshed_api_key", default="fakekey")
|
50
|
1245 a("--galaxy_api_key", default="fakekey")
|
|
1246 a("--galaxy_root", default="/galaxy-central")
|
101
|
1247 a("--galaxy_venv", default="/galaxy_venv")
|
48
|
1248 args = parser.parse_args()
|
|
1249 assert not args.bad_user, (
|
|
1250 'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to "admin_users" in the Galaxy configuration file'
|
|
1251 % (args.bad_user, args.bad_user)
|
|
1252 )
|
49
|
1253 assert args.tool_name, "## Tool Factory expects a tool name - eg --tool_name=DESeq"
|
48
|
1254 assert (
|
49
|
1255 args.sysexe or args.packages
|
48
|
1256 ), "## Tool Factory wrapper expects an interpreter or an executable package"
|
49
|
1257 args.input_files = [x.replace('"', "").replace("'", "") for x in args.input_files]
|
48
|
1258 # remove quotes we need to deal with spaces in CL params
|
|
1259 for i, x in enumerate(args.additional_parameters):
|
49
|
1260 args.additional_parameters[i] = args.additional_parameters[i].replace('"', "")
|
48
|
1261 r = ScriptRunner(args)
|
49
|
1262 r.writeShedyml()
|
|
1263 r.makeTool()
|
66
|
1264 if args.make_Tool == "generate":
|
106
|
1265 retcode = r.run() # for testing toolfactory itself
|
66
|
1266 r.moveRunOutputs()
|
|
1267 r.makeToolTar()
|
|
1268 else:
|
106
|
1269 r.planemo_biodocker_test() # test to make outputs and then test
|
66
|
1270 r.moveRunOutputs()
|
|
1271 r.makeToolTar()
|
101
|
1272 if args.make_Tool == "gentestinstall":
|
|
1273 r.shedLoad()
|
|
1274 r.eph_galaxy_load()
|
63
|
1275
|
48
|
1276
|
|
1277 if __name__ == "__main__":
|
|
1278 main()
|