annotate toolfactory/rgToolFactory2.py @ 38:a30536c100bf draft

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