annotate toolfactory/rgToolFactory2.py @ 48:5a7a5b06bce0 draft

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