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