annotate toolfactory/rgToolFactory2.py @ 36:ce2b1f8ea68d draft

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