annotate toolfactory/rgToolFactory2.py @ 46:f8700bed1e17 draft

Uploaded
author fubar
date Tue, 18 Aug 2020 08:25:43 -0400
parents e7e9732ebed6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
1 #!/usr/bin/env python
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
2 # rgToolFactory.py
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
3 # see https://github.com/fubar2/toolfactory
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
4 #
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
5 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
6 #
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
7 # all rights reserved
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
8 # Licensed under the LGPL
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
9 # suggestions for improvement and bug fixes welcome at https://github.com/fubar2/toolfactory
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
10 #
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
11 # July 2020: BCC was fun and I feel like rip van winkle after 5 years.
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
12 # Decided to
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
13 # 1. Fix the toolfactory so it works - done for simplest case
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
14 # 2. Fix planemo so the toolfactory function works
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
15 # 3. Rewrite bits using galaxyxml functions where that makes sense - done
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
16 #
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
17 # removed all the old complications including making the new tool use this same script
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
18 # galaxyxml now generates the tool xml https://github.com/hexylena/galaxyxml
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
19 # No support for automatic HTML file creation from arbitrary outputs
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
20 # essential problem is to create two command lines - one for the tool xml and a different
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
21 # one to run the executable with the supplied test data and settings
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
22 # Be simpler to write the tool, then run it with planemo and soak up the test outputs.
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
23
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
24
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
25
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
26 import argparse
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
27 import logging
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
28 import os
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
29 import re
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
30 import shutil
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
31 import subprocess
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
32 import sys
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
33 import tarfile
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
34 import tempfile
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
35 import time
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
36
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
37 import galaxyxml.tool as gxt
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
38 import galaxyxml.tool.parameters as gxtp
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
39
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
40 import lxml
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
41
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
42 myversion = "V2.1 July 2020"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
43 verbose = True
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
44 debug = True
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
45 toolFactoryURL = "https://github.com/fubar2/toolfactory"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
46 ourdelim = "~~~"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
47
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
48 # --input_files="$input_files~~~$CL~~~$input_formats~~~$input_label
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
49 # ~~~$input_help"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
50 IPATHPOS = 0
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
51 ICLPOS = 1
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
52 IFMTPOS = 2
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
53 ILABPOS = 3
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
54 IHELPOS = 4
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
55 IOCLPOS = 5
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
56
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
57 # --output_files "$otab.history_name~~~$otab.history_format~~~$otab.CL
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
58 ONAMEPOS = 0
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
59 OFMTPOS = 1
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
60 OCLPOS = 2
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
61 OOCLPOS = 3
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
62
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
63 # --additional_parameters="$i.param_name~~~$i.param_value~~~
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
64 # $i.param_label~~~$i.param_help~~~$i.param_type~~~$i.CL~~~i$.param_CLoverride"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
65 ANAMEPOS = 0
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
66 AVALPOS = 1
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
67 ALABPOS = 2
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
68 AHELPPOS = 3
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
69 ATYPEPOS = 4
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
70 ACLPOS = 5
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
71 AOVERPOS = 6
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
72 AOCLPOS = 7
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
73
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
74
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
75 foo = len(lxml.__version__)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
76 # fug you, flake8. Say my name!
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
77
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
78 def timenow():
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
79 """return current time as a string
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
80 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
81 return time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(time.time()))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
82
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
83
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
84 def quote_non_numeric(s):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
85 """return a prequoted string for non-numerics
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
86 useful for perl and Rscript parameter passing?
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
87 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
88 try:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
89 _ = float(s)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
90 return s
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
91 except ValueError:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
92 return '"%s"' % s
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
93
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
94
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
95 html_escape_table = {"&": "&amp;", ">": "&gt;", "<": "&lt;", "$": r"\$"}
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
96
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
97
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
98 def html_escape(text):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
99 """Produce entities within text."""
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
100 return "".join(html_escape_table.get(c, c) for c in text)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
101
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
102
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
103 def html_unescape(text):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
104 """Revert entities within text. Multiple character targets so use replace"""
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
105 t = text.replace("&amp;", "&")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
106 t = t.replace("&gt;", ">")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
107 t = t.replace("&lt;", "<")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
108 t = t.replace("\\$", "$")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
109 return t
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
110
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
111
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
112 def parse_citations(citations_text):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
113 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
114 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
115 citations = [c for c in citations_text.split("**ENTRY**") if c.strip()]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
116 citation_tuples = []
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
117 for citation in citations:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
118 if citation.startswith("doi"):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
119 citation_tuples.append(("doi", citation[len("doi") :].strip()))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
120 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
121 citation_tuples.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
122 ("bibtex", citation[len("bibtex") :].strip())
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
123 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
124 return citation_tuples
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
125
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
126
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
127 class ScriptRunner:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
128 """Wrapper for an arbitrary script
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
129 uses galaxyxml
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
130
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
131 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
132
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
133 def __init__(self, args=None):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
134 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
135 prepare command line cl for running the tool here
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
136 and prepare elements needed for galaxyxml tool generation
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
137 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
138
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
139 self.infiles = [x.split(ourdelim) for x in args.input_files]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
140 self.outfiles = [x.split(ourdelim) for x in args.output_files]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
141 self.addpar = [x.split(ourdelim) for x in args.additional_parameters]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
142 self.args = args
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
143 self.cleanuppar()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
144 self.lastclredirect = None
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
145 self.lastxclredirect = None
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
146 self.cl = []
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
147 self.xmlcl = []
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
148 self.is_positional = self.args.parampass == "positional"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
149 aCL = self.cl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
150 assert args.parampass in [
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
151 "0",
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
152 "argparse",
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
153 "positional",
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
154 ], 'Parameter passing in args.parampass must be "0","positional" or "argparse"'
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
155 self.tool_name = re.sub("[^a-zA-Z0-9_]+", "", args.tool_name)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
156 self.tool_id = self.tool_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
157 if self.args.interpreter_name:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
158 exe = "$runMe"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
159 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
160 exe = self.args.exe_package
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
161 assert (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
162 exe is not None
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
163 ), "No interpeter or executable passed in - nothing to run so cannot build"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
164 self.tool = gxt.Tool(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
165 self.args.tool_name,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
166 self.tool_id,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
167 self.args.tool_version,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
168 self.args.tool_desc,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
169 exe,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
170 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
171 self.tinputs = gxtp.Inputs()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
172 self.toutputs = gxtp.Outputs()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
173 self.testparam = []
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
174 if (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
175 self.args.runmode == "Executable" or self.args.runmode == "system"
46
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
176 ):
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
177 if len(self.args.cl_override) > 0:
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
178 for x in self.args.cl_override.split(' '):
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
179 aCL(x)
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
180 else:
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
181 aCL(self.args.exe_package) # this little CL will just run
43
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
182 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
183 self.prepScript()
46
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
184 aCL(self.args.interpreter_name)
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
185 aCL(self.sfile)
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
186
43
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
187 self.elog = "%s_error_log.txt" % self.tool_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
188 self.tlog = "%s_runner_log.txt" % self.tool_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
189
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
190 if self.args.parampass == "0":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
191 self.clsimple()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
192 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
193 clsuffix = []
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
194 xclsuffix = []
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
195 for i, p in enumerate(self.infiles):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
196 if p[IOCLPOS] == "STDIN":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
197 appendme = [
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
198 p[IOCLPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
199 p[ICLPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
200 p[IPATHPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
201 "< %s" % p[IPATHPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
202 ]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
203 xappendme = [
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
204 p[IOCLPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
205 p[ICLPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
206 p[IPATHPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
207 "< $%s" % p[ICLPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
208 ]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
209 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
210 appendme = [p[IOCLPOS], p[ICLPOS], p[IPATHPOS], ""]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
211 xappendme = [p[IOCLPOS], p[ICLPOS], "$%s" % p[ICLPOS], ""]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
212 clsuffix.append(appendme)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
213 xclsuffix.append(xappendme)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
214 # print('##infile i=%d, appendme=%s' % (i,appendme))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
215 for i, p in enumerate(self.outfiles):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
216 if p[OOCLPOS] == "STDOUT":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
217 self.lastclredirect = [">", p[ONAMEPOS]]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
218 self.lastxclredirect = [">", "$%s" % p[OCLPOS]]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
219 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
220 clsuffix.append([p[OOCLPOS], p[OCLPOS], p[ONAMEPOS], ""])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
221 xclsuffix.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
222 [p[OOCLPOS], p[OCLPOS], "$%s" % p[ONAMEPOS], ""]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
223 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
224 for p in self.addpar:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
225 clsuffix.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
226 [p[AOCLPOS], p[ACLPOS], p[AVALPOS], p[AOVERPOS]]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
227 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
228 xclsuffix.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
229 [p[AOCLPOS], p[ACLPOS], '"$%s"' % p[ANAMEPOS], p[AOVERPOS]]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
230 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
231 clsuffix.sort()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
232 xclsuffix.sort()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
233 self.xclsuffix = xclsuffix
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
234 self.clsuffix = clsuffix
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
235 if self.args.parampass == "positional":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
236 self.clpositional()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
237 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
238 self.clargparse()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
239
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
240 def prepScript(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
241 rx = open(self.args.script_path, "r").readlines()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
242 rx = [x.rstrip() for x in rx]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
243 rxcheck = [x.strip() for x in rx if x.strip() > ""]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
244 assert len(rxcheck) > 0, "Supplied script is empty. Cannot run"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
245 self.script = "\n".join(rx)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
246 fhandle, self.sfile = tempfile.mkstemp(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
247 prefix=self.tool_name, suffix="_%s" % (self.args.interpreter_name)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
248 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
249 tscript = open(self.sfile, "w")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
250 tscript.write(self.script)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
251 tscript.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
252 self.indentedScript = " %s" % "\n".join(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
253 [" %s" % html_escape(x) for x in rx]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
254 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
255 self.escapedScript = "%s" % "\n".join(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
256 [" %s" % html_escape(x) for x in rx]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
257 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
258 art = "%s.%s" % (self.tool_name, self.args.interpreter_name)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
259 artifact = open(art, "wb")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
260 if self.args.interpreter_name == "python":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
261 artifact.write(bytes("#!/usr/bin/env python\n", "utf8"))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
262 artifact.write(bytes(self.script, "utf8"))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
263 artifact.close()
46
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
264
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
265
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
266
43
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
267 def cleanuppar(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
268 """ positional parameters are complicated by their numeric ordinal"""
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
269 for i, p in enumerate(self.infiles):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
270 if self.args.parampass == "positional":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
271 assert p[ICLPOS].isdigit(), (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
272 "Positional parameters must be ordinal integers - got %s for %s"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
273 % (p[ICLPOS], p[ILABPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
274 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
275 p.append(p[ICLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
276 if p[ICLPOS].isdigit() or self.args.parampass == "0":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
277 scl = "input%d" % (i + 1)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
278 p[ICLPOS] = scl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
279 self.infiles[i] = p
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
280 for i, p in enumerate(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
281 self.outfiles
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
282 ): # trying to automagically gather using extensions
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
283 if self.args.parampass == "positional" and p[OCLPOS] != "STDOUT":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
284 assert p[OCLPOS].isdigit(), (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
285 "Positional parameters must be ordinal integers - got %s for %s"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
286 % (p[OCLPOS], p[ONAMEPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
287 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
288 p.append(p[OCLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
289 if p[OCLPOS].isdigit() or p[OCLPOS] == "STDOUT":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
290 scl = p[ONAMEPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
291 p[OCLPOS] = scl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
292 self.outfiles[i] = p
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
293 for i, p in enumerate(self.addpar):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
294 if self.args.parampass == "positional":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
295 assert p[ACLPOS].isdigit(), (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
296 "Positional parameters must be ordinal integers - got %s for %s"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
297 % (p[ACLPOS], p[ANAMEPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
298 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
299 p.append(p[ACLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
300 if p[ACLPOS].isdigit():
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
301 scl = "input%s" % p[ACLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
302 p[ACLPOS] = scl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
303 self.addpar[i] = p
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
304
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
305 def clsimple(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
306 """ no parameters - uses < and > for i/o
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
307 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
308 aCL = self.cl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
309 aCL("<")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
310 aCL(self.infiles[0][IPATHPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
311 aCL(">")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
312 aCL(self.outfiles[0][OCLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
313 aXCL = self.xmlcl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
314 aXCL("<")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
315 aXCL("$%s" % self.infiles[0][ICLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
316 aXCL(">")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
317 aXCL("$%s" % self.outfiles[0][ONAMEPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
318
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
319 def clpositional(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
320 # inputs in order then params
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
321 aCL = self.cl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
322 for (o_v, k, v, koverride) in self.clsuffix:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
323 if " " in v:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
324 aCL("%s" % v)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
325 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
326 aCL(v)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
327 aXCL = self.xmlcl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
328 for (o_v, k, v, koverride) in self.xclsuffix:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
329 aXCL(v)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
330 if self.lastxclredirect:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
331 aXCL(self.lastxclredirect[0])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
332 aXCL(self.lastxclredirect[1])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
333
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
334 def clargparse(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
335 """ argparse style
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
336 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
337 aCL = self.cl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
338 aXCL = self.xmlcl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
339 # inputs then params in argparse named form
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
340 for (o_v, k, v, koverride) in self.xclsuffix:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
341 if koverride > "":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
342 k = koverride
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
343 elif len(k.strip()) == 1:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
344 k = "-%s" % k
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
345 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
346 k = "--%s" % k
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
347 aXCL(k)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
348 aXCL(v)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
349 for (o_v, k, v, koverride) in self.clsuffix:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
350 if koverride > "":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
351 k = koverride
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
352 elif len(k.strip()) == 1:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
353 k = "-%s" % k
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
354 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
355 k = "--%s" % k
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
356 aCL(k)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
357 aCL(v)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
358
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
359 def getNdash(self, newname):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
360 if self.is_positional:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
361 ndash = 0
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
362 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
363 ndash = 2
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
364 if len(newname) < 2:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
365 ndash = 1
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
366 return ndash
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
367
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
368 def doXMLparam(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
369 """flake8 made me do this..."""
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
370 for p in self.outfiles:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
371 newname, newfmt, newcl, oldcl = p
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
372 ndash = self.getNdash(newcl)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
373 aparm = gxtp.OutputData(newcl, format=newfmt, num_dashes=ndash)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
374 aparm.positional = self.is_positional
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
375 if self.is_positional:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
376 if oldcl == "STDOUT":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
377 aparm.positional = 9999999
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
378 aparm.command_line_override = "> $%s" % newcl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
379 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
380 aparm.positional = int(oldcl)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
381 aparm.command_line_override = "$%s" % newcl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
382 self.toutputs.append(aparm)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
383 tp = gxtp.TestOutput(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
384 name=newcl, value="%s_sample" % newcl, format=newfmt
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
385 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
386 self.testparam.append(tp)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
387 for p in self.infiles:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
388 newname = p[ICLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
389 newfmt = p[IFMTPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
390 ndash = self.getNdash(newname)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
391 if not len(p[ILABPOS]) > 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
392 alab = p[ICLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
393 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
394 alab = p[ILABPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
395 aninput = gxtp.DataParam(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
396 newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
397 optional=False,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
398 label=alab,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
399 help=p[IHELPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
400 format=newfmt,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
401 multiple=False,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
402 num_dashes=ndash,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
403 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
404 aninput.positional = self.is_positional
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
405 self.tinputs.append(aninput)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
406 tparm = gxtp.TestParam(name=newname, value="%s_sample" % newname)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
407 self.testparam.append(tparm)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
408 for p in self.addpar:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
409 newname, newval, newlabel, newhelp, newtype, newcl, override, oldcl = p
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
410 if not len(newlabel) > 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
411 newlabel = newname
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
412 ndash = self.getNdash(newname)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
413 if newtype == "text":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
414 aparm = gxtp.TextParam(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
415 newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
416 label=newlabel,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
417 help=newhelp,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
418 value=newval,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
419 num_dashes=ndash,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
420 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
421 elif newtype == "integer":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
422 aparm = gxtp.IntegerParam(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
423 newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
424 label=newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
425 help=newhelp,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
426 value=newval,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
427 num_dashes=ndash,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
428 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
429 elif newtype == "float":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
430 aparm = gxtp.FloatParam(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
431 newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
432 label=newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
433 help=newhelp,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
434 value=newval,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
435 num_dashes=ndash,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
436 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
437 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
438 raise ValueError(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
439 'Unrecognised parameter type "%s" for\
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
440 additional parameter %s in makeXML'
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
441 % (newtype, newname)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
442 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
443 aparm.positional = self.is_positional
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
444 if self.is_positional:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
445 aninput.positional = int(oldcl)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
446 self.tinputs.append(aparm)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
447 self.tparm = gxtp.TestParam(newname, value=newval)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
448 self.testparam.append(tparm)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
449
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
450 def doNoXMLparam(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
451 alab = self.infiles[0][ILABPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
452 if len(alab) == 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
453 alab = self.infiles[0][ICLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
454 max1s = (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
455 "Maximum one input if parampass is 0 - more than one input files supplied - %s"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
456 % str(self.infiles)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
457 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
458 assert len(self.infiles) == 1, max1s
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
459 newname = self.infiles[0][ICLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
460 aninput = gxtp.DataParam(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
461 newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
462 optional=False,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
463 label=alab,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
464 help=self.infiles[0][IHELPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
465 format=self.infiles[0][IFMTPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
466 multiple=False,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
467 num_dashes=0,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
468 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
469 aninput.command_line_override = "< $%s" % newname
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
470 aninput.positional = self.is_positional
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
471 self.tinputs.append(aninput)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
472 tp = gxtp.TestParam(name=newname, value="%s_sample" % newname)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
473 self.testparam.append(tp)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
474 newname = self.outfiles[0][OCLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
475 newfmt = self.outfiles[0][OFMTPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
476 anout = gxtp.OutputData(newname, format=newfmt, num_dashes=0)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
477 anout.command_line_override = "> $%s" % newname
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
478 anout.positional = self.is_positional
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
479 self.toutputs.append(anout)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
480 tp = gxtp.TestOutput(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
481 name=newname, value="%s_sample" % newname, format=newfmt
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
482 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
483 self.testparam.append(tp)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
484
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
485 def makeXML(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
486 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
487 Create a Galaxy xml tool wrapper for the new script
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
488 Uses galaxyhtml
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
489 Hmmm. How to get the command line into correct order...
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
490 """
46
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
491 if self.args.cl_override:
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
492 self.tool.command_line_override = self.args.cl_override.split(' ') + self.xmlcl
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
493 else:
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
494 self.tool.command_line_override = self.xmlcl
43
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
495 if self.args.interpreter_name:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
496 self.tool.interpreter = self.args.interpreter_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
497 if self.args.help_text:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
498 helptext = open(self.args.help_text, "r").readlines()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
499 helptext = [html_escape(x) for x in helptext]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
500 self.tool.help = "".join([x for x in helptext])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
501 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
502 self.tool.help = (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
503 "Please ask the tool author (%s) for help \
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
504 as none was supplied at tool generation\n"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
505 % (self.args.user_email)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
506 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
507 self.tool.version_command = None # do not want
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
508 requirements = gxtp.Requirements()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
509
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
510 if self.args.interpreter_name:
46
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
511 if self.args.dependencies:
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
512 for d in self.args.dependencies.split(','):
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
513 requirements.append(
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
514 gxtp.Requirement(
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
515 "package", d, ""
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
516 )
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
517 )
43
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
518 if self.args.interpreter_name == "python":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
519 requirements.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
520 gxtp.Requirement(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
521 "package", "python", self.args.interpreter_version
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
522 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
523 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
524 elif self.args.interpreter_name not in ["bash", "sh"]:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
525 requirements.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
526 gxtp.Requirement(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
527 "package",
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
528 self.args.interpreter_name,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
529 self.args.interpreter_version,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
530 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
531 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
532 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
533 if self.args.exe_package and self.args.parampass != "system":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
534 requirements.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
535 gxtp.Requirement(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
536 "package",
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
537 self.args.exe_package,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
538 self.args.exe_package_version,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
539 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
540 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
541 self.tool.requirements = requirements
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
542 if self.args.parampass == "0":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
543 self.doNoXMLparam()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
544 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
545 self.doXMLparam()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
546 self.tool.outputs = self.toutputs
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
547 self.tool.inputs = self.tinputs
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
548 if self.args.runmode not in ["Executable", "system"]:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
549 configfiles = gxtp.Configfiles()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
550 configfiles.append(gxtp.Configfile(name="runMe", text=self.script))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
551 self.tool.configfiles = configfiles
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
552 tests = gxtp.Tests()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
553 test_a = gxtp.Test()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
554 for tp in self.testparam:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
555 test_a.append(tp)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
556 tests.append(test_a)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
557 self.tool.tests = tests
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
558 self.tool.add_comment(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
559 "Created by %s at %s using the Galaxy Tool Factory."
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
560 % (self.args.user_email, timenow())
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
561 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
562 self.tool.add_comment("Source in git at: %s" % (toolFactoryURL))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
563 self.tool.add_comment(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
564 "Cite: Creating re-usable tools from scripts doi: \
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
565 10.1093/bioinformatics/bts573"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
566 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
567 exml = self.tool.export()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
568 xf = open('%s.xml' % self.tool_name, "w")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
569 xf.write(exml)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
570 xf.write("\n")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
571 xf.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
572 # ready for the tarball
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
573
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
574 def makeTooltar(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
575 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
576 a tool is a gz tarball with eg
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
577 /toolname/tool.xml /toolname/tool.py /toolname/test-data/test1_in.foo ...
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
578 NOTE names for test inputs and outputs are munged here so must
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
579 correspond to actual input and output names used on the generated cl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
580 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
581 retval = self.run()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
582 if retval:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
583 sys.stderr.write(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
584 "## Run failed. Cannot build yet. Please fix and retry"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
585 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
586 sys.exit(1)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
587 tdir = "tfout"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
588 if not os.path.exists(tdir):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
589 os.mkdir(tdir)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
590 self.makeXML()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
591 testdir = os.path.join(tdir, "test-data")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
592 if not os.path.exists(testdir):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
593 os.mkdir(testdir) # make tests directory
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
594 for p in self.infiles:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
595 pth = p[IPATHPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
596 dest = os.path.join(testdir, "%s_sample" % p[ICLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
597 shutil.copyfile(pth, dest)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
598 for p in self.outfiles:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
599 pth = p[OCLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
600 if p[OOCLPOS] == "STDOUT" or self.args.parampass == "0":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
601 pth = p[ONAMEPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
602 dest = os.path.join(testdir, "%s_sample" % p[ONAMEPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
603 shutil.copyfile(pth, dest)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
604 dest = os.path.join(tdir, p[ONAMEPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
605 shutil.copyfile(pth, dest)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
606 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
607 pth = p[OCLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
608 dest = os.path.join(testdir, "%s_sample" % p[OCLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
609 shutil.copyfile(pth, dest)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
610 dest = os.path.join(tdir, p[OCLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
611 shutil.copyfile(pth, dest)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
612
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
613 if os.path.exists(self.tlog) and os.stat(self.tlog).st_size > 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
614 shutil.copyfile(self.tlog, os.path.join(testdir, "test1_log_outfiletxt"))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
615 if self.args.runmode not in ["Executable", "system"]:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
616 stname = os.path.join(tdir, "%s" % (self.sfile))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
617 if not os.path.exists(stname):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
618 shutil.copyfile(self.sfile, stname)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
619 xreal = '%s.xml' % self.tool_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
620 xout = os.path.join(tdir,xreal)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
621 shutil.copyfile(xreal, xout)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
622 tarpath = "toolfactory_%s.tgz" % self.tool_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
623 tf = tarfile.open(tarpath, "w:gz")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
624 tf.add(name=tdir, arcname=self.tool_name)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
625 tf.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
626 shutil.copyfile(tarpath, self.args.new_tool)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
627 shutil.copyfile(xreal,"tool_xml.txt")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
628 repdir = "TF_run_report_tempdir"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
629 if not os.path.exists(repdir):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
630 os.mkdir(repdir)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
631 repoutnames = [x[OCLPOS] for x in self.outfiles]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
632 with os.scandir('.') as outs:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
633 for entry in outs:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
634 if entry.name.endswith('.tgz') or not entry.is_file():
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
635 continue
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
636 if entry.name in repoutnames:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
637 shutil.copyfile(entry.name,os.path.join(repdir,entry.name))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
638 elif entry.name == "%s.xml" % self.tool_name:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
639 shutil.copyfile(entry.name,os.path.join(repdir,"new_tool_xml"))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
640 return retval
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
641
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
642 def run(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
643 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
644 Some devteam tools have this defensive stderr read so I'm keeping with the faith
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
645 Feel free to update.
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
646 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
647 s = "run cl=%s" % str(self.cl)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
648
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
649 logging.debug(s)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
650 scl = " ".join(self.cl)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
651 err = None
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
652 if self.args.parampass != "0":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
653 ste = open(self.elog, "wb")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
654 if self.lastclredirect:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
655 sto = open(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
656 self.lastclredirect[1], "wb"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
657 ) # is name of an output file
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
658 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
659 sto = open(self.tlog, "wb")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
660 sto.write(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
661 bytes(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
662 "## Executing Toolfactory generated command line = %s\n"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
663 % scl,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
664 "utf8",
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
665 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
666 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
667 sto.flush()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
668 p = subprocess.run(self.cl, shell=False, stdout=sto, stderr=ste)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
669 sto.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
670 ste.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
671 tmp_stderr = open(self.elog, "rb")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
672 err = ""
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
673 buffsize = 1048576
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
674 try:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
675 while True:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
676 err += str(tmp_stderr.read(buffsize))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
677 if not err or len(err) % buffsize != 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
678 break
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
679 except OverflowError:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
680 pass
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
681 tmp_stderr.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
682 retval = p.returncode
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
683 else: # work around special case of simple scripts that take stdin and write to stdout
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
684 sti = open(self.infiles[0][IPATHPOS], "rb")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
685 sto = open(self.outfiles[0][ONAMEPOS], "wb")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
686 # must use shell to redirect
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
687 p = subprocess.run(self.cl, shell=False, stdout=sto, stdin=sti)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
688 retval = p.returncode
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
689 sto.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
690 sti.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
691 if os.path.isfile(self.tlog) and os.stat(self.tlog).st_size == 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
692 os.unlink(self.tlog)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
693 if os.path.isfile(self.elog) and os.stat(self.elog).st_size == 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
694 os.unlink(self.elog)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
695 if p.returncode != 0 and err: # problem
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
696 sys.stderr.write(err)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
697 logging.debug("run done")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
698 return retval
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
699
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
700
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
701 def main():
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
702 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
703 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
704 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
705 </command>
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
706 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
707 parser = argparse.ArgumentParser()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
708 a = parser.add_argument
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
709 a("--script_path", default="")
46
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
710 a("--dependencies", default="")
f8700bed1e17 Uploaded
fubar
parents: 43
diff changeset
711 a("--cl_override", default="")
43
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
712 a("--tool_name", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
713 a("--interpreter_name", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
714 a("--interpreter_version", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
715 a("--exe_package", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
716 a("--exe_package_version", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
717 a("--input_files", default=[], action="append")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
718 a("--output_files", default=[], action="append")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
719 a("--user_email", default="Unknown")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
720 a("--bad_user", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
721 a("--make_Tool", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
722 a("--help_text", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
723 a("--tool_desc", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
724 a("--tool_version", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
725 a("--citations", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
726 a("--additional_parameters", action="append", default=[])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
727 a("--edit_additional_parameters", action="store_true", default=False)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
728 a("--parampass", default="positional")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
729 a("--tfout", default="./tfout")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
730 a("--new_tool", default="new_tool")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
731 a("--runmode", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
732 args = parser.parse_args()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
733 assert not args.bad_user, (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
734 'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to "admin_users" in the Galaxy configuration file'
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
735 % (args.bad_user, args.bad_user)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
736 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
737 assert (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
738 args.tool_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
739 ), "## Tool Factory expects a tool name - eg --tool_name=DESeq"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
740 assert (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
741 args.interpreter_name or args.exe_package
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
742 ), "## Tool Factory wrapper expects an interpreter or an executable package"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
743 assert args.exe_package or (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
744 len(args.script_path) > 0 and os.path.isfile(args.script_path)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
745 ), "## Tool Factory wrapper expects a script path - eg --script_path=foo.R if no executable"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
746 args.input_files = [
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
747 x.replace('"', "").replace("'", "") for x in args.input_files
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
748 ]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
749 # remove quotes we need to deal with spaces in CL params
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
750 for i, x in enumerate(args.additional_parameters):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
751 args.additional_parameters[i] = args.additional_parameters[i].replace(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
752 '"', ""
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
753 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
754 r = ScriptRunner(args)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
755 if args.make_Tool:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
756 retcode = r.makeTooltar()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
757 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
758 retcode = r.run()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
759 if retcode:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
760 sys.exit(retcode) # indicate failure to job runner
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
761
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
762
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
763 if __name__ == "__main__":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
764 main()