annotate toolfactory/rgToolFactory2.py @ 43:e7e9732ebed6 draft

Uploaded
author fubar
date Sun, 16 Aug 2020 08:51:14 -0400
parents
children f8700bed1e17
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"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
176 ): # binary - no need
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
177 aCL(self.args.exe_package) # this little CL will just run
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
178 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
179 self.prepScript()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
180 self.elog = "%s_error_log.txt" % self.tool_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
181 self.tlog = "%s_runner_log.txt" % self.tool_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
182
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
183 if self.args.parampass == "0":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
184 self.clsimple()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
185 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
186 clsuffix = []
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
187 xclsuffix = []
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
188 for i, p in enumerate(self.infiles):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
189 if p[IOCLPOS] == "STDIN":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
190 appendme = [
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
191 p[IOCLPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
192 p[ICLPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
193 p[IPATHPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
194 "< %s" % p[IPATHPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
195 ]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
196 xappendme = [
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
197 p[IOCLPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
198 p[ICLPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
199 p[IPATHPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
200 "< $%s" % p[ICLPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
201 ]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
202 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
203 appendme = [p[IOCLPOS], p[ICLPOS], p[IPATHPOS], ""]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
204 xappendme = [p[IOCLPOS], p[ICLPOS], "$%s" % p[ICLPOS], ""]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
205 clsuffix.append(appendme)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
206 xclsuffix.append(xappendme)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
207 # print('##infile i=%d, appendme=%s' % (i,appendme))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
208 for i, p in enumerate(self.outfiles):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
209 if p[OOCLPOS] == "STDOUT":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
210 self.lastclredirect = [">", p[ONAMEPOS]]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
211 self.lastxclredirect = [">", "$%s" % p[OCLPOS]]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
212 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
213 clsuffix.append([p[OOCLPOS], p[OCLPOS], p[ONAMEPOS], ""])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
214 xclsuffix.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
215 [p[OOCLPOS], p[OCLPOS], "$%s" % p[ONAMEPOS], ""]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
216 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
217 for p in self.addpar:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
218 clsuffix.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
219 [p[AOCLPOS], p[ACLPOS], p[AVALPOS], p[AOVERPOS]]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
220 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
221 xclsuffix.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
222 [p[AOCLPOS], p[ACLPOS], '"$%s"' % p[ANAMEPOS], p[AOVERPOS]]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
223 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
224 clsuffix.sort()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
225 xclsuffix.sort()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
226 self.xclsuffix = xclsuffix
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
227 self.clsuffix = clsuffix
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
228 if self.args.parampass == "positional":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
229 self.clpositional()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
230 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
231 self.clargparse()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
232
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
233 def prepScript(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
234 aCL = self.cl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
235 rx = open(self.args.script_path, "r").readlines()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
236 rx = [x.rstrip() for x in rx]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
237 rxcheck = [x.strip() for x in rx if x.strip() > ""]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
238 assert len(rxcheck) > 0, "Supplied script is empty. Cannot run"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
239 self.script = "\n".join(rx)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
240 fhandle, self.sfile = tempfile.mkstemp(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
241 prefix=self.tool_name, suffix="_%s" % (self.args.interpreter_name)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
242 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
243 tscript = open(self.sfile, "w")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
244 tscript.write(self.script)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
245 tscript.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
246 self.indentedScript = " %s" % "\n".join(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
247 [" %s" % html_escape(x) for x in rx]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
248 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
249 self.escapedScript = "%s" % "\n".join(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
250 [" %s" % html_escape(x) for x in rx]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
251 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
252 art = "%s.%s" % (self.tool_name, self.args.interpreter_name)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
253 artifact = open(art, "wb")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
254 if self.args.interpreter_name == "python":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
255 artifact.write(bytes("#!/usr/bin/env python\n", "utf8"))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
256 artifact.write(bytes(self.script, "utf8"))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
257 artifact.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
258 aCL(self.args.interpreter_name)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
259 aCL(self.sfile)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
260
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
261 def cleanuppar(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
262 """ positional parameters are complicated by their numeric ordinal"""
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
263 for i, p in enumerate(self.infiles):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
264 if self.args.parampass == "positional":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
265 assert p[ICLPOS].isdigit(), (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
266 "Positional parameters must be ordinal integers - got %s for %s"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
267 % (p[ICLPOS], p[ILABPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
268 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
269 p.append(p[ICLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
270 if p[ICLPOS].isdigit() or self.args.parampass == "0":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
271 scl = "input%d" % (i + 1)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
272 p[ICLPOS] = scl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
273 self.infiles[i] = p
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
274 for i, p in enumerate(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
275 self.outfiles
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
276 ): # trying to automagically gather using extensions
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
277 if self.args.parampass == "positional" and p[OCLPOS] != "STDOUT":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
278 assert p[OCLPOS].isdigit(), (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
279 "Positional parameters must be ordinal integers - got %s for %s"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
280 % (p[OCLPOS], p[ONAMEPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
281 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
282 p.append(p[OCLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
283 if p[OCLPOS].isdigit() or p[OCLPOS] == "STDOUT":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
284 scl = p[ONAMEPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
285 p[OCLPOS] = scl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
286 self.outfiles[i] = p
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
287 for i, p in enumerate(self.addpar):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
288 if self.args.parampass == "positional":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
289 assert p[ACLPOS].isdigit(), (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
290 "Positional parameters must be ordinal integers - got %s for %s"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
291 % (p[ACLPOS], p[ANAMEPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
292 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
293 p.append(p[ACLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
294 if p[ACLPOS].isdigit():
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
295 scl = "input%s" % p[ACLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
296 p[ACLPOS] = scl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
297 self.addpar[i] = p
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
298
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
299 def clsimple(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
300 """ no parameters - uses < and > for i/o
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
301 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
302 aCL = self.cl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
303 aCL("<")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
304 aCL(self.infiles[0][IPATHPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
305 aCL(">")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
306 aCL(self.outfiles[0][OCLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
307 aXCL = self.xmlcl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
308 aXCL("<")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
309 aXCL("$%s" % self.infiles[0][ICLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
310 aXCL(">")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
311 aXCL("$%s" % self.outfiles[0][ONAMEPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
312
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
313 def clpositional(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
314 # inputs in order then params
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
315 aCL = self.cl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
316 for (o_v, k, v, koverride) in self.clsuffix:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
317 if " " in v:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
318 aCL("%s" % v)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
319 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
320 aCL(v)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
321 aXCL = self.xmlcl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
322 for (o_v, k, v, koverride) in self.xclsuffix:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
323 aXCL(v)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
324 if self.lastxclredirect:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
325 aXCL(self.lastxclredirect[0])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
326 aXCL(self.lastxclredirect[1])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
327
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
328 def clargparse(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
329 """ argparse style
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
330 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
331 aCL = self.cl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
332 aXCL = self.xmlcl.append
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
333 # inputs then params in argparse named form
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
334 for (o_v, k, v, koverride) in self.xclsuffix:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
335 if koverride > "":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
336 k = koverride
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
337 elif len(k.strip()) == 1:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
338 k = "-%s" % k
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
339 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
340 k = "--%s" % k
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
341 aXCL(k)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
342 aXCL(v)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
343 for (o_v, k, v, koverride) in self.clsuffix:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
344 if koverride > "":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
345 k = koverride
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
346 elif len(k.strip()) == 1:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
347 k = "-%s" % k
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
348 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
349 k = "--%s" % k
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
350 aCL(k)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
351 aCL(v)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
352
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
353 def getNdash(self, newname):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
354 if self.is_positional:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
355 ndash = 0
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
356 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
357 ndash = 2
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
358 if len(newname) < 2:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
359 ndash = 1
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
360 return ndash
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
361
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
362 def doXMLparam(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
363 """flake8 made me do this..."""
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
364 for p in self.outfiles:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
365 newname, newfmt, newcl, oldcl = p
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
366 ndash = self.getNdash(newcl)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
367 aparm = gxtp.OutputData(newcl, format=newfmt, num_dashes=ndash)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
368 aparm.positional = self.is_positional
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
369 if self.is_positional:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
370 if oldcl == "STDOUT":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
371 aparm.positional = 9999999
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
372 aparm.command_line_override = "> $%s" % newcl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
373 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
374 aparm.positional = int(oldcl)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
375 aparm.command_line_override = "$%s" % newcl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
376 self.toutputs.append(aparm)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
377 tp = gxtp.TestOutput(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
378 name=newcl, value="%s_sample" % newcl, format=newfmt
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
379 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
380 self.testparam.append(tp)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
381 for p in self.infiles:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
382 newname = p[ICLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
383 newfmt = p[IFMTPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
384 ndash = self.getNdash(newname)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
385 if not len(p[ILABPOS]) > 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
386 alab = p[ICLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
387 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
388 alab = p[ILABPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
389 aninput = gxtp.DataParam(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
390 newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
391 optional=False,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
392 label=alab,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
393 help=p[IHELPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
394 format=newfmt,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
395 multiple=False,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
396 num_dashes=ndash,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
397 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
398 aninput.positional = self.is_positional
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
399 self.tinputs.append(aninput)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
400 tparm = gxtp.TestParam(name=newname, value="%s_sample" % newname)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
401 self.testparam.append(tparm)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
402 for p in self.addpar:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
403 newname, newval, newlabel, newhelp, newtype, newcl, override, oldcl = p
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
404 if not len(newlabel) > 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
405 newlabel = newname
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
406 ndash = self.getNdash(newname)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
407 if newtype == "text":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
408 aparm = gxtp.TextParam(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
409 newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
410 label=newlabel,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
411 help=newhelp,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
412 value=newval,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
413 num_dashes=ndash,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
414 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
415 elif newtype == "integer":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
416 aparm = gxtp.IntegerParam(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
417 newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
418 label=newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
419 help=newhelp,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
420 value=newval,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
421 num_dashes=ndash,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
422 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
423 elif newtype == "float":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
424 aparm = gxtp.FloatParam(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
425 newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
426 label=newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
427 help=newhelp,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
428 value=newval,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
429 num_dashes=ndash,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
430 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
431 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
432 raise ValueError(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
433 'Unrecognised parameter type "%s" for\
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
434 additional parameter %s in makeXML'
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
435 % (newtype, newname)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
436 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
437 aparm.positional = self.is_positional
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
438 if self.is_positional:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
439 aninput.positional = int(oldcl)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
440 self.tinputs.append(aparm)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
441 self.tparm = gxtp.TestParam(newname, value=newval)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
442 self.testparam.append(tparm)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
443
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
444 def doNoXMLparam(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
445 alab = self.infiles[0][ILABPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
446 if len(alab) == 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
447 alab = self.infiles[0][ICLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
448 max1s = (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
449 "Maximum one input if parampass is 0 - more than one input files supplied - %s"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
450 % str(self.infiles)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
451 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
452 assert len(self.infiles) == 1, max1s
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
453 newname = self.infiles[0][ICLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
454 aninput = gxtp.DataParam(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
455 newname,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
456 optional=False,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
457 label=alab,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
458 help=self.infiles[0][IHELPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
459 format=self.infiles[0][IFMTPOS],
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
460 multiple=False,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
461 num_dashes=0,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
462 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
463 aninput.command_line_override = "< $%s" % newname
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
464 aninput.positional = self.is_positional
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
465 self.tinputs.append(aninput)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
466 tp = gxtp.TestParam(name=newname, value="%s_sample" % newname)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
467 self.testparam.append(tp)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
468 newname = self.outfiles[0][OCLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
469 newfmt = self.outfiles[0][OFMTPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
470 anout = gxtp.OutputData(newname, format=newfmt, num_dashes=0)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
471 anout.command_line_override = "> $%s" % newname
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
472 anout.positional = self.is_positional
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
473 self.toutputs.append(anout)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
474 tp = gxtp.TestOutput(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
475 name=newname, value="%s_sample" % newname, format=newfmt
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
476 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
477 self.testparam.append(tp)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
478
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
479 def makeXML(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
480 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
481 Create a Galaxy xml tool wrapper for the new script
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
482 Uses galaxyhtml
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
483 Hmmm. How to get the command line into correct order...
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
484 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
485 self.tool.command_line_override = self.xmlcl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
486 if self.args.interpreter_name:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
487 self.tool.interpreter = self.args.interpreter_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
488 if self.args.help_text:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
489 helptext = open(self.args.help_text, "r").readlines()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
490 helptext = [html_escape(x) for x in helptext]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
491 self.tool.help = "".join([x for x in helptext])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
492 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
493 self.tool.help = (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
494 "Please ask the tool author (%s) for help \
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
495 as none was supplied at tool generation\n"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
496 % (self.args.user_email)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
497 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
498 self.tool.version_command = None # do not want
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
499 requirements = gxtp.Requirements()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
500
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
501 if self.args.interpreter_name:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
502 if self.args.interpreter_name == "python":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
503 requirements.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
504 gxtp.Requirement(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
505 "package", "python", self.args.interpreter_version
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
506 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
507 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
508 elif self.args.interpreter_name not in ["bash", "sh"]:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
509 requirements.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
510 gxtp.Requirement(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
511 "package",
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
512 self.args.interpreter_name,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
513 self.args.interpreter_version,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
514 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
515 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
516 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
517 if self.args.exe_package and self.args.parampass != "system":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
518 requirements.append(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
519 gxtp.Requirement(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
520 "package",
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
521 self.args.exe_package,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
522 self.args.exe_package_version,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
523 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
524 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
525 self.tool.requirements = requirements
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
526 if self.args.parampass == "0":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
527 self.doNoXMLparam()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
528 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
529 self.doXMLparam()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
530 self.tool.outputs = self.toutputs
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
531 self.tool.inputs = self.tinputs
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
532 if self.args.runmode not in ["Executable", "system"]:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
533 configfiles = gxtp.Configfiles()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
534 configfiles.append(gxtp.Configfile(name="runMe", text=self.script))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
535 self.tool.configfiles = configfiles
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
536 tests = gxtp.Tests()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
537 test_a = gxtp.Test()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
538 for tp in self.testparam:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
539 test_a.append(tp)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
540 tests.append(test_a)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
541 self.tool.tests = tests
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
542 self.tool.add_comment(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
543 "Created by %s at %s using the Galaxy Tool Factory."
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
544 % (self.args.user_email, timenow())
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
545 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
546 self.tool.add_comment("Source in git at: %s" % (toolFactoryURL))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
547 self.tool.add_comment(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
548 "Cite: Creating re-usable tools from scripts doi: \
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
549 10.1093/bioinformatics/bts573"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
550 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
551 exml = self.tool.export()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
552 xf = open('%s.xml' % self.tool_name, "w")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
553 xf.write(exml)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
554 xf.write("\n")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
555 xf.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
556 # ready for the tarball
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
557
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
558 def makeTooltar(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
559 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
560 a tool is a gz tarball with eg
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
561 /toolname/tool.xml /toolname/tool.py /toolname/test-data/test1_in.foo ...
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
562 NOTE names for test inputs and outputs are munged here so must
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
563 correspond to actual input and output names used on the generated cl
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
564 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
565 retval = self.run()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
566 if retval:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
567 sys.stderr.write(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
568 "## Run failed. Cannot build yet. Please fix and retry"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
569 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
570 sys.exit(1)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
571 tdir = "tfout"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
572 if not os.path.exists(tdir):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
573 os.mkdir(tdir)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
574 self.makeXML()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
575 testdir = os.path.join(tdir, "test-data")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
576 if not os.path.exists(testdir):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
577 os.mkdir(testdir) # make tests directory
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
578 for p in self.infiles:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
579 pth = p[IPATHPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
580 dest = os.path.join(testdir, "%s_sample" % p[ICLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
581 shutil.copyfile(pth, dest)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
582 for p in self.outfiles:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
583 pth = p[OCLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
584 if p[OOCLPOS] == "STDOUT" or self.args.parampass == "0":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
585 pth = p[ONAMEPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
586 dest = os.path.join(testdir, "%s_sample" % p[ONAMEPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
587 shutil.copyfile(pth, dest)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
588 dest = os.path.join(tdir, p[ONAMEPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
589 shutil.copyfile(pth, dest)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
590 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
591 pth = p[OCLPOS]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
592 dest = os.path.join(testdir, "%s_sample" % p[OCLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
593 shutil.copyfile(pth, dest)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
594 dest = os.path.join(tdir, p[OCLPOS])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
595 shutil.copyfile(pth, dest)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
596
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
597 if os.path.exists(self.tlog) and os.stat(self.tlog).st_size > 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
598 shutil.copyfile(self.tlog, os.path.join(testdir, "test1_log_outfiletxt"))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
599 if self.args.runmode not in ["Executable", "system"]:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
600 stname = os.path.join(tdir, "%s" % (self.sfile))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
601 if not os.path.exists(stname):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
602 shutil.copyfile(self.sfile, stname)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
603 xreal = '%s.xml' % self.tool_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
604 xout = os.path.join(tdir,xreal)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
605 shutil.copyfile(xreal, xout)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
606 tarpath = "toolfactory_%s.tgz" % self.tool_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
607 tf = tarfile.open(tarpath, "w:gz")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
608 tf.add(name=tdir, arcname=self.tool_name)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
609 tf.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
610 shutil.copyfile(tarpath, self.args.new_tool)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
611 shutil.copyfile(xreal,"tool_xml.txt")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
612 repdir = "TF_run_report_tempdir"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
613 if not os.path.exists(repdir):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
614 os.mkdir(repdir)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
615 repoutnames = [x[OCLPOS] for x in self.outfiles]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
616 with os.scandir('.') as outs:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
617 for entry in outs:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
618 if entry.name.endswith('.tgz') or not entry.is_file():
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
619 continue
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
620 if entry.name in repoutnames:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
621 shutil.copyfile(entry.name,os.path.join(repdir,entry.name))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
622 elif entry.name == "%s.xml" % self.tool_name:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
623 shutil.copyfile(entry.name,os.path.join(repdir,"new_tool_xml"))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
624 return retval
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
625
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
626 def run(self):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
627 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
628 Some devteam tools have this defensive stderr read so I'm keeping with the faith
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
629 Feel free to update.
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
630 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
631 s = "run cl=%s" % str(self.cl)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
632
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
633 logging.debug(s)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
634 scl = " ".join(self.cl)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
635 err = None
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
636 if self.args.parampass != "0":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
637 ste = open(self.elog, "wb")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
638 if self.lastclredirect:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
639 sto = open(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
640 self.lastclredirect[1], "wb"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
641 ) # is name of an output file
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
642 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
643 sto = open(self.tlog, "wb")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
644 sto.write(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
645 bytes(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
646 "## Executing Toolfactory generated command line = %s\n"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
647 % scl,
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
648 "utf8",
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
649 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
650 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
651 sto.flush()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
652 p = subprocess.run(self.cl, shell=False, stdout=sto, stderr=ste)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
653 sto.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
654 ste.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
655 tmp_stderr = open(self.elog, "rb")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
656 err = ""
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
657 buffsize = 1048576
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
658 try:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
659 while True:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
660 err += str(tmp_stderr.read(buffsize))
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
661 if not err or len(err) % buffsize != 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
662 break
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
663 except OverflowError:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
664 pass
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
665 tmp_stderr.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
666 retval = p.returncode
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
667 else: # work around special case of simple scripts that take stdin and write to stdout
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
668 sti = open(self.infiles[0][IPATHPOS], "rb")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
669 sto = open(self.outfiles[0][ONAMEPOS], "wb")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
670 # must use shell to redirect
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
671 p = subprocess.run(self.cl, shell=False, stdout=sto, stdin=sti)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
672 retval = p.returncode
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
673 sto.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
674 sti.close()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
675 if os.path.isfile(self.tlog) and os.stat(self.tlog).st_size == 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
676 os.unlink(self.tlog)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
677 if os.path.isfile(self.elog) and os.stat(self.elog).st_size == 0:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
678 os.unlink(self.elog)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
679 if p.returncode != 0 and err: # problem
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
680 sys.stderr.write(err)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
681 logging.debug("run done")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
682 return retval
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
683
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
684
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
685 def main():
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
686 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
687 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
688 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
689 </command>
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
690 """
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
691 parser = argparse.ArgumentParser()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
692 a = parser.add_argument
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
693 a("--script_path", default="")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
694 a("--tool_name", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
695 a("--interpreter_name", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
696 a("--interpreter_version", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
697 a("--exe_package", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
698 a("--exe_package_version", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
699 a("--input_files", default=[], action="append")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
700 a("--output_files", default=[], action="append")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
701 a("--user_email", default="Unknown")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
702 a("--bad_user", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
703 a("--make_Tool", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
704 a("--help_text", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
705 a("--tool_desc", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
706 a("--tool_version", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
707 a("--citations", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
708 a("--additional_parameters", action="append", default=[])
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
709 a("--edit_additional_parameters", action="store_true", default=False)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
710 a("--parampass", default="positional")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
711 a("--tfout", default="./tfout")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
712 a("--new_tool", default="new_tool")
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
713 a("--runmode", default=None)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
714 args = parser.parse_args()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
715 assert not args.bad_user, (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
716 '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
717 % (args.bad_user, args.bad_user)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
718 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
719 assert (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
720 args.tool_name
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
721 ), "## Tool Factory expects a tool name - eg --tool_name=DESeq"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
722 assert (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
723 args.interpreter_name or args.exe_package
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
724 ), "## Tool Factory wrapper expects an interpreter or an executable package"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
725 assert args.exe_package or (
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
726 len(args.script_path) > 0 and os.path.isfile(args.script_path)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
727 ), "## Tool Factory wrapper expects a script path - eg --script_path=foo.R if no executable"
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
728 args.input_files = [
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
729 x.replace('"', "").replace("'", "") for x in args.input_files
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
730 ]
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
731 # remove quotes we need to deal with spaces in CL params
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
732 for i, x in enumerate(args.additional_parameters):
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
733 args.additional_parameters[i] = args.additional_parameters[i].replace(
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
734 '"', ""
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
735 )
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
736 r = ScriptRunner(args)
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
737 if args.make_Tool:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
738 retcode = r.makeTooltar()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
739 else:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
740 retcode = r.run()
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
741 if retcode:
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
742 sys.exit(retcode) # indicate failure to job runner
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
743
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
744
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
745 if __name__ == "__main__":
e7e9732ebed6 Uploaded
fubar
parents:
diff changeset
746 main()