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