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