annotate toolfactory/rgToolFactory2.py @ 1:0778fb523693 draft

Uploaded
author fubar
date Fri, 11 Dec 2020 02:52:53 +0000
parents 83f8bb78781e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1 # replace with shebang for biocontainer
83f8bb78781e Uploaded
fubar
parents:
diff changeset
2 # see https://github.com/fubar2/toolfactory
83f8bb78781e Uploaded
fubar
parents:
diff changeset
3 #
83f8bb78781e Uploaded
fubar
parents:
diff changeset
4 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
83f8bb78781e Uploaded
fubar
parents:
diff changeset
5 #
83f8bb78781e Uploaded
fubar
parents:
diff changeset
6 # all rights reserved
83f8bb78781e Uploaded
fubar
parents:
diff changeset
7 # Licensed under the LGPL
83f8bb78781e Uploaded
fubar
parents:
diff changeset
8 # suggestions for improvement and bug fixes welcome at
83f8bb78781e Uploaded
fubar
parents:
diff changeset
9 # https://github.com/fubar2/toolfactory
83f8bb78781e Uploaded
fubar
parents:
diff changeset
10 #
83f8bb78781e Uploaded
fubar
parents:
diff changeset
11 # July 2020: BCC was fun and I feel like rip van winkle after 5 years.
83f8bb78781e Uploaded
fubar
parents:
diff changeset
12 # Decided to
83f8bb78781e Uploaded
fubar
parents:
diff changeset
13 # 1. Fix the toolfactory so it works - done for simplest case
83f8bb78781e Uploaded
fubar
parents:
diff changeset
14 # 2. Fix planemo so the toolfactory function works
83f8bb78781e Uploaded
fubar
parents:
diff changeset
15 # 3. Rewrite bits using galaxyxml functions where that makes sense - done
83f8bb78781e Uploaded
fubar
parents:
diff changeset
16 #
83f8bb78781e Uploaded
fubar
parents:
diff changeset
17 # uses planemo in a biodocker sort of image as a requirement
83f8bb78781e Uploaded
fubar
parents:
diff changeset
18 # otherwise planemo seems to leak dependencies back into the
83f8bb78781e Uploaded
fubar
parents:
diff changeset
19 # calling venv. Hilarity ensues.
83f8bb78781e Uploaded
fubar
parents:
diff changeset
20
83f8bb78781e Uploaded
fubar
parents:
diff changeset
21
83f8bb78781e Uploaded
fubar
parents:
diff changeset
22
83f8bb78781e Uploaded
fubar
parents:
diff changeset
23 import argparse
83f8bb78781e Uploaded
fubar
parents:
diff changeset
24 import copy
83f8bb78781e Uploaded
fubar
parents:
diff changeset
25 import datetime
83f8bb78781e Uploaded
fubar
parents:
diff changeset
26 import grp
83f8bb78781e Uploaded
fubar
parents:
diff changeset
27 import json
83f8bb78781e Uploaded
fubar
parents:
diff changeset
28 import logging
83f8bb78781e Uploaded
fubar
parents:
diff changeset
29 import os
83f8bb78781e Uploaded
fubar
parents:
diff changeset
30 import re
83f8bb78781e Uploaded
fubar
parents:
diff changeset
31 import shutil
83f8bb78781e Uploaded
fubar
parents:
diff changeset
32 import subprocess
83f8bb78781e Uploaded
fubar
parents:
diff changeset
33 import sys
83f8bb78781e Uploaded
fubar
parents:
diff changeset
34 import tarfile
83f8bb78781e Uploaded
fubar
parents:
diff changeset
35 import tempfile
83f8bb78781e Uploaded
fubar
parents:
diff changeset
36 import time
83f8bb78781e Uploaded
fubar
parents:
diff changeset
37
83f8bb78781e Uploaded
fubar
parents:
diff changeset
38
83f8bb78781e Uploaded
fubar
parents:
diff changeset
39 from bioblend import ConnectionError
83f8bb78781e Uploaded
fubar
parents:
diff changeset
40 from bioblend import toolshed
83f8bb78781e Uploaded
fubar
parents:
diff changeset
41
83f8bb78781e Uploaded
fubar
parents:
diff changeset
42 import docker
83f8bb78781e Uploaded
fubar
parents:
diff changeset
43
83f8bb78781e Uploaded
fubar
parents:
diff changeset
44 import galaxyxml.tool as gxt
83f8bb78781e Uploaded
fubar
parents:
diff changeset
45 import galaxyxml.tool.parameters as gxtp
83f8bb78781e Uploaded
fubar
parents:
diff changeset
46
83f8bb78781e Uploaded
fubar
parents:
diff changeset
47 import lxml
83f8bb78781e Uploaded
fubar
parents:
diff changeset
48
83f8bb78781e Uploaded
fubar
parents:
diff changeset
49 import yaml
83f8bb78781e Uploaded
fubar
parents:
diff changeset
50
83f8bb78781e Uploaded
fubar
parents:
diff changeset
51 myversion = "V2.1 July 2020"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
52 verbose = True
83f8bb78781e Uploaded
fubar
parents:
diff changeset
53 debug = True
83f8bb78781e Uploaded
fubar
parents:
diff changeset
54 toolFactoryURL = "https://github.com/fubar2/toolfactory"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
55 ourdelim = "~~~"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
56
83f8bb78781e Uploaded
fubar
parents:
diff changeset
57 # --input_files="$intab.input_files~~~$intab.input_CL~~~$intab.input_formats\
83f8bb78781e Uploaded
fubar
parents:
diff changeset
58 #~~~$intab.input_label~~~$intab.input_help"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
59 IPATHPOS = 0
83f8bb78781e Uploaded
fubar
parents:
diff changeset
60 ICLPOS = 1
83f8bb78781e Uploaded
fubar
parents:
diff changeset
61 IFMTPOS = 2
83f8bb78781e Uploaded
fubar
parents:
diff changeset
62 ILABPOS = 3
83f8bb78781e Uploaded
fubar
parents:
diff changeset
63 IHELPOS = 4
83f8bb78781e Uploaded
fubar
parents:
diff changeset
64 IOCLPOS = 5
83f8bb78781e Uploaded
fubar
parents:
diff changeset
65
83f8bb78781e Uploaded
fubar
parents:
diff changeset
66 # --output_files "$otab.history_name~~~$otab.history_format~~~$otab.history_CL~~~$otab.history_test"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
67 ONAMEPOS = 0
83f8bb78781e Uploaded
fubar
parents:
diff changeset
68 OFMTPOS = 1
83f8bb78781e Uploaded
fubar
parents:
diff changeset
69 OCLPOS = 2
83f8bb78781e Uploaded
fubar
parents:
diff changeset
70 OTESTPOS = 3
83f8bb78781e Uploaded
fubar
parents:
diff changeset
71 OOCLPOS = 4
83f8bb78781e Uploaded
fubar
parents:
diff changeset
72
83f8bb78781e Uploaded
fubar
parents:
diff changeset
73
83f8bb78781e Uploaded
fubar
parents:
diff changeset
74 # --additional_parameters="$i.param_name~~~$i.param_value~~~
83f8bb78781e Uploaded
fubar
parents:
diff changeset
75 # $i.param_label~~~$i.param_help~~~$i.param_type~~~$i.CL~~~i$.param_CLoverride"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
76 ANAMEPOS = 0
83f8bb78781e Uploaded
fubar
parents:
diff changeset
77 AVALPOS = 1
83f8bb78781e Uploaded
fubar
parents:
diff changeset
78 ALABPOS = 2
83f8bb78781e Uploaded
fubar
parents:
diff changeset
79 AHELPPOS = 3
83f8bb78781e Uploaded
fubar
parents:
diff changeset
80 ATYPEPOS = 4
83f8bb78781e Uploaded
fubar
parents:
diff changeset
81 ACLPOS = 5
83f8bb78781e Uploaded
fubar
parents:
diff changeset
82 AOVERPOS = 6
83f8bb78781e Uploaded
fubar
parents:
diff changeset
83 AOCLPOS = 7
83f8bb78781e Uploaded
fubar
parents:
diff changeset
84
83f8bb78781e Uploaded
fubar
parents:
diff changeset
85
83f8bb78781e Uploaded
fubar
parents:
diff changeset
86 foo = len(lxml.__version__)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
87 # fug you, flake8. Say my name!
83f8bb78781e Uploaded
fubar
parents:
diff changeset
88 FAKEEXE = "~~~REMOVE~~~ME~~~"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
89 # need this until a PR/version bump to fix galaxyxml prepending the exe even
83f8bb78781e Uploaded
fubar
parents:
diff changeset
90 # with override.
83f8bb78781e Uploaded
fubar
parents:
diff changeset
91
83f8bb78781e Uploaded
fubar
parents:
diff changeset
92
83f8bb78781e Uploaded
fubar
parents:
diff changeset
93 def timenow():
83f8bb78781e Uploaded
fubar
parents:
diff changeset
94 """return current time as a string"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
95 return time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(time.time()))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
96
83f8bb78781e Uploaded
fubar
parents:
diff changeset
97
83f8bb78781e Uploaded
fubar
parents:
diff changeset
98 def quote_non_numeric(s):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
99 """return a prequoted string for non-numerics
83f8bb78781e Uploaded
fubar
parents:
diff changeset
100 useful for perl and Rscript parameter passing?
83f8bb78781e Uploaded
fubar
parents:
diff changeset
101 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
102 try:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
103 _ = float(s)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
104 return s
83f8bb78781e Uploaded
fubar
parents:
diff changeset
105 except ValueError:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
106 return '"%s"' % s
83f8bb78781e Uploaded
fubar
parents:
diff changeset
107
83f8bb78781e Uploaded
fubar
parents:
diff changeset
108
83f8bb78781e Uploaded
fubar
parents:
diff changeset
109 html_escape_table = {"&": "&amp;", ">": "&gt;", "<": "&lt;", "$": r"\$","#":"&#35;", "$":"&#36;"}
83f8bb78781e Uploaded
fubar
parents:
diff changeset
110 cheetah_escape_table = {"$": "\$","#":"\#"}
83f8bb78781e Uploaded
fubar
parents:
diff changeset
111
83f8bb78781e Uploaded
fubar
parents:
diff changeset
112 def html_escape(text):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
113 """Produce entities within text."""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
114 return "".join([html_escape_table.get(c, c) for c in text])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
115
83f8bb78781e Uploaded
fubar
parents:
diff changeset
116 def cheetah_escape(text):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
117 """Produce entities within text."""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
118 return "".join([cheetah_escape_table.get(c, c) for c in text])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
119
83f8bb78781e Uploaded
fubar
parents:
diff changeset
120
83f8bb78781e Uploaded
fubar
parents:
diff changeset
121 def html_unescape(text):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
122 """Revert entities within text. Multiple character targets so use replace"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
123 t = text.replace("&amp;", "&")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
124 t = t.replace("&gt;", ">")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
125 t = t.replace("&lt;", "<")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
126 t = t.replace("\\$", "$")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
127 t = t.replace("&#36;","$")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
128 t = t.replace("&#35;","#")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
129 return t
83f8bb78781e Uploaded
fubar
parents:
diff changeset
130
83f8bb78781e Uploaded
fubar
parents:
diff changeset
131
83f8bb78781e Uploaded
fubar
parents:
diff changeset
132 def parse_citations(citations_text):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
133 """"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
134 citations = [c for c in citations_text.split("**ENTRY**") if c.strip()]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
135 citation_tuples = []
83f8bb78781e Uploaded
fubar
parents:
diff changeset
136 for citation in citations:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
137 if citation.startswith("doi"):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
138 citation_tuples.append(("doi", citation[len("doi") :].strip()))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
139 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
140 citation_tuples.append(("bibtex", citation[len("bibtex") :].strip()))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
141 return citation_tuples
83f8bb78781e Uploaded
fubar
parents:
diff changeset
142
83f8bb78781e Uploaded
fubar
parents:
diff changeset
143
83f8bb78781e Uploaded
fubar
parents:
diff changeset
144 class ScriptRunner:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
145 """Wrapper for an arbitrary script
83f8bb78781e Uploaded
fubar
parents:
diff changeset
146 uses galaxyxml
83f8bb78781e Uploaded
fubar
parents:
diff changeset
147
83f8bb78781e Uploaded
fubar
parents:
diff changeset
148 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
149
83f8bb78781e Uploaded
fubar
parents:
diff changeset
150 def __init__(self, args=None):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
151 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
152 prepare command line cl for running the tool here
83f8bb78781e Uploaded
fubar
parents:
diff changeset
153 and prepare elements needed for galaxyxml tool generation
83f8bb78781e Uploaded
fubar
parents:
diff changeset
154 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
155 self.ourcwd = os.getcwd()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
156 self.ourenv = copy.deepcopy(os.environ)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
157 self.infiles = [x.split(ourdelim) for x in args.input_files]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
158 self.outfiles = [x.split(ourdelim) for x in args.output_files]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
159 self.addpar = [x.split(ourdelim) for x in args.additional_parameters]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
160 self.args = args
83f8bb78781e Uploaded
fubar
parents:
diff changeset
161 self.cleanuppar()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
162 self.lastclredirect = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
163 self.lastxclredirect = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
164 self.cl = []
83f8bb78781e Uploaded
fubar
parents:
diff changeset
165 self.xmlcl = []
83f8bb78781e Uploaded
fubar
parents:
diff changeset
166 self.is_positional = self.args.parampass == "positional"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
167 if self.args.sysexe:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
168 self.executeme = self.args.sysexe
83f8bb78781e Uploaded
fubar
parents:
diff changeset
169 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
170 if self.args.packages:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
171 self.executeme = self.args.packages.split(",")[0].split(":")[0]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
172 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
173 self.executeme = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
174 aCL = self.cl.append
83f8bb78781e Uploaded
fubar
parents:
diff changeset
175 aXCL = self.xmlcl.append
83f8bb78781e Uploaded
fubar
parents:
diff changeset
176 assert args.parampass in [
83f8bb78781e Uploaded
fubar
parents:
diff changeset
177 "0",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
178 "argparse",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
179 "positional",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
180 ], 'args.parampass must be "0","positional" or "argparse"'
83f8bb78781e Uploaded
fubar
parents:
diff changeset
181 self.tool_name = re.sub("[^a-zA-Z0-9_]+", "", args.tool_name)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
182 self.tool_id = self.tool_name
83f8bb78781e Uploaded
fubar
parents:
diff changeset
183 self.newtool = gxt.Tool(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
184 self.tool_name,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
185 self.tool_id,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
186 self.args.tool_version,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
187 self.args.tool_desc,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
188 FAKEEXE,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
189 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
190 self.newtarpath = "toolfactory_%s.tgz" % self.tool_name
83f8bb78781e Uploaded
fubar
parents:
diff changeset
191 self.tooloutdir = "./tfout"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
192 self.repdir = "./TF_run_report_tempdir"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
193 self.testdir = os.path.join(self.tooloutdir, "test-data")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
194 if not os.path.exists(self.tooloutdir):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
195 os.mkdir(self.tooloutdir)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
196 if not os.path.exists(self.testdir):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
197 os.mkdir(self.testdir)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
198 if not os.path.exists(self.repdir):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
199 os.mkdir(self.repdir)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
200 self.tinputs = gxtp.Inputs()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
201 self.toutputs = gxtp.Outputs()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
202 self.testparam = []
83f8bb78781e Uploaded
fubar
parents:
diff changeset
203 if self.args.script_path:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
204 self.prepScript()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
205 if self.args.command_override:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
206 scos = open(self.args.command_override, "r").readlines()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
207 self.command_override = [x.rstrip() for x in scos]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
208 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
209 self.command_override = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
210 if self.args.test_override:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
211 stos = open(self.args.test_override, "r").readlines()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
212 self.test_override = [x.rstrip() for x in stos]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
213 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
214 self.test_override = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
215 if self.args.cl_prefix: # DIY CL start
83f8bb78781e Uploaded
fubar
parents:
diff changeset
216 clp = self.args.cl_prefix.split(" ")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
217 for c in clp:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
218 aCL(c)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
219 aXCL(c)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
220 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
221 if self.args.script_path:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
222 aCL(self.executeme)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
223 aCL(self.sfile)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
224 aXCL(self.executeme)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
225 aXCL("$runme")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
226 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
227 aCL(self.executeme)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
228 aXCL(self.executeme)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
229 self.elog = os.path.join(self.repdir, "%s_error_log.txt" % self.tool_name)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
230 self.tlog = os.path.join(self.repdir, "%s_runner_log.txt" % self.tool_name)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
231
83f8bb78781e Uploaded
fubar
parents:
diff changeset
232 if self.args.parampass == "0":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
233 self.clsimple()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
234 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
235 clsuffix = []
83f8bb78781e Uploaded
fubar
parents:
diff changeset
236 xclsuffix = []
83f8bb78781e Uploaded
fubar
parents:
diff changeset
237 for i, p in enumerate(self.infiles):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
238 if p[IOCLPOS] == "STDIN":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
239 appendme = [
83f8bb78781e Uploaded
fubar
parents:
diff changeset
240 p[IOCLPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
241 p[ICLPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
242 p[IPATHPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
243 "< %s" % p[IPATHPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
244 ]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
245 xappendme = [
83f8bb78781e Uploaded
fubar
parents:
diff changeset
246 p[IOCLPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
247 p[ICLPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
248 p[IPATHPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
249 "< $%s" % p[ICLPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
250 ]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
251 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
252 appendme = [p[IOCLPOS], p[ICLPOS], p[IPATHPOS], ""]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
253 xappendme = [p[IOCLPOS], p[ICLPOS], "$%s" % p[ICLPOS], ""]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
254 clsuffix.append(appendme)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
255 xclsuffix.append(xappendme)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
256 for i, p in enumerate(self.outfiles):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
257 if p[OOCLPOS] == "STDOUT":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
258 self.lastclredirect = [">", p[ONAMEPOS]]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
259 self.lastxclredirect = [">", "$%s" % p[OCLPOS]]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
260 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
261 clsuffix.append([p[ONAMEPOS], p[ONAMEPOS], p[ONAMEPOS], ""])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
262 xclsuffix.append([p[ONAMEPOS], p[ONAMEPOS], "$%s" % p[ONAMEPOS], ""])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
263 for p in self.addpar:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
264 clsuffix.append([p[AOCLPOS], p[ACLPOS], p[AVALPOS], p[AOVERPOS]])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
265 xclsuffix.append(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
266 [p[AOCLPOS], p[ACLPOS], '"$%s"' % p[ANAMEPOS], p[AOVERPOS]]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
267 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
268 clsuffix.sort()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
269 xclsuffix.sort()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
270 self.xclsuffix = xclsuffix
83f8bb78781e Uploaded
fubar
parents:
diff changeset
271 self.clsuffix = clsuffix
83f8bb78781e Uploaded
fubar
parents:
diff changeset
272 if self.args.parampass == "positional":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
273 self.clpositional()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
274 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
275 self.clargparse()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
276
83f8bb78781e Uploaded
fubar
parents:
diff changeset
277 def prepScript(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
278 rx = open(self.args.script_path, "r").readlines()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
279 rx = [x.rstrip() for x in rx]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
280 rxcheck = [x.strip() for x in rx if x.strip() > ""]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
281 assert len(rxcheck) > 0, "Supplied script is empty. Cannot run"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
282 self.script = "\n".join(rx)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
283 fhandle, self.sfile = tempfile.mkstemp(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
284 prefix=self.tool_name, suffix="_%s" % (self.executeme)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
285 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
286 tscript = open(self.sfile, "w")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
287 tscript.write(self.script)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
288 tscript.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
289 self.escapedScript = [cheetah_escape(x) for x in rx]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
290 self.spacedScript = [f" {x}" for x in rx if x.strip() > ""]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
291 art = "%s.%s" % (self.tool_name, self.executeme)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
292 artifact = open(art, "wb")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
293 artifact.write(bytes('\n'.join(self.escapedScript),'utf8'))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
294 artifact.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
295
83f8bb78781e Uploaded
fubar
parents:
diff changeset
296 def cleanuppar(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
297 """ positional parameters are complicated by their numeric ordinal"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
298 for i, p in enumerate(self.infiles):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
299 infp = copy.copy(p)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
300 if self.args.parampass == "positional":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
301 assert infp[
83f8bb78781e Uploaded
fubar
parents:
diff changeset
302 ICLPOS
83f8bb78781e Uploaded
fubar
parents:
diff changeset
303 ].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (
83f8bb78781e Uploaded
fubar
parents:
diff changeset
304 infp[ICLPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
305 infp[ILABPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
306 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
307 icl = infp[ICLPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
308 infp.append(icl)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
309 if infp[ICLPOS].isdigit() or self.args.parampass == "0":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
310 scl = "input%d" % (i + 1)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
311 infp[ICLPOS] = scl
83f8bb78781e Uploaded
fubar
parents:
diff changeset
312 self.infiles[i] = infp
83f8bb78781e Uploaded
fubar
parents:
diff changeset
313 for i, p in enumerate(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
314 self.outfiles
83f8bb78781e Uploaded
fubar
parents:
diff changeset
315 ):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
316 if self.args.parampass == "positional" and p[OCLPOS].upper() != "STDOUT":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
317 assert p[
83f8bb78781e Uploaded
fubar
parents:
diff changeset
318 OCLPOS
83f8bb78781e Uploaded
fubar
parents:
diff changeset
319 ].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (
83f8bb78781e Uploaded
fubar
parents:
diff changeset
320 p[OCLPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
321 p[ONAMEPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
322 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
323 p.append(p[OCLPOS]) # keep copy
83f8bb78781e Uploaded
fubar
parents:
diff changeset
324 if p[OOCLPOS].isdigit() or p[OOCLPOS].upper() == "STDOUT":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
325 scl = p[ONAMEPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
326 p[OCLPOS] = scl
83f8bb78781e Uploaded
fubar
parents:
diff changeset
327 self.outfiles[i] = p
83f8bb78781e Uploaded
fubar
parents:
diff changeset
328 for i, p in enumerate(self.addpar):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
329 if self.args.parampass == "positional":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
330 assert p[
83f8bb78781e Uploaded
fubar
parents:
diff changeset
331 ACLPOS
83f8bb78781e Uploaded
fubar
parents:
diff changeset
332 ].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (
83f8bb78781e Uploaded
fubar
parents:
diff changeset
333 p[ACLPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
334 p[ANAMEPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
335 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
336 p.append(p[ACLPOS])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
337 if p[ACLPOS].isdigit():
83f8bb78781e Uploaded
fubar
parents:
diff changeset
338 scl = "input%s" % p[ACLPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
339 p[ACLPOS] = scl
83f8bb78781e Uploaded
fubar
parents:
diff changeset
340 self.addpar[i] = p
83f8bb78781e Uploaded
fubar
parents:
diff changeset
341
83f8bb78781e Uploaded
fubar
parents:
diff changeset
342 def clsimple(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
343 """no parameters - uses < and > for i/o"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
344 aCL = self.cl.append
83f8bb78781e Uploaded
fubar
parents:
diff changeset
345 aXCL = self.xmlcl.append
83f8bb78781e Uploaded
fubar
parents:
diff changeset
346
83f8bb78781e Uploaded
fubar
parents:
diff changeset
347 if len(self.infiles) > 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
348 aCL("<")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
349 aCL(self.infiles[0][IPATHPOS])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
350 aXCL("<")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
351 aXCL("$%s" % self.infiles[0][ICLPOS])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
352 if len(self.outfiles) > 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
353 aCL(">")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
354 aCL(self.outfiles[0][OCLPOS])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
355 aXCL(">")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
356 aXCL("$%s" % self.outfiles[0][ONAMEPOS])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
357
83f8bb78781e Uploaded
fubar
parents:
diff changeset
358 def clpositional(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
359 # inputs in order then params
83f8bb78781e Uploaded
fubar
parents:
diff changeset
360 aCL = self.cl.append
83f8bb78781e Uploaded
fubar
parents:
diff changeset
361 for (o_v, k, v, koverride) in self.clsuffix:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
362 if " " in v:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
363 aCL("%s" % v)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
364 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
365 aCL(v)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
366 aXCL = self.xmlcl.append
83f8bb78781e Uploaded
fubar
parents:
diff changeset
367 for (o_v, k, v, koverride) in self.xclsuffix:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
368 aXCL(v)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
369 if self.lastxclredirect:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
370 aXCL(self.lastxclredirect[0])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
371 aXCL(self.lastxclredirect[1])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
372
83f8bb78781e Uploaded
fubar
parents:
diff changeset
373
83f8bb78781e Uploaded
fubar
parents:
diff changeset
374 def clargparse(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
375 """argparse style"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
376 aCL = self.cl.append
83f8bb78781e Uploaded
fubar
parents:
diff changeset
377 aXCL = self.xmlcl.append
83f8bb78781e Uploaded
fubar
parents:
diff changeset
378 # inputs then params in argparse named form
83f8bb78781e Uploaded
fubar
parents:
diff changeset
379
83f8bb78781e Uploaded
fubar
parents:
diff changeset
380 for (o_v, k, v, koverride) in self.xclsuffix:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
381 if koverride > "":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
382 k = koverride
83f8bb78781e Uploaded
fubar
parents:
diff changeset
383 elif len(k.strip()) == 1:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
384 k = "-%s" % k
83f8bb78781e Uploaded
fubar
parents:
diff changeset
385 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
386 k = "--%s" % k
83f8bb78781e Uploaded
fubar
parents:
diff changeset
387 aXCL(k)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
388 aXCL(v)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
389 for (o_v, k, v, koverride) in self.clsuffix:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
390 if koverride > "":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
391 k = koverride
83f8bb78781e Uploaded
fubar
parents:
diff changeset
392 elif len(k.strip()) == 1:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
393 k = "-%s" % k
83f8bb78781e Uploaded
fubar
parents:
diff changeset
394 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
395 k = "--%s" % k
83f8bb78781e Uploaded
fubar
parents:
diff changeset
396 aCL(k)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
397 aCL(v)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
398
83f8bb78781e Uploaded
fubar
parents:
diff changeset
399
83f8bb78781e Uploaded
fubar
parents:
diff changeset
400 def getNdash(self, newname):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
401 if self.is_positional:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
402 ndash = 0
83f8bb78781e Uploaded
fubar
parents:
diff changeset
403 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
404 ndash = 2
83f8bb78781e Uploaded
fubar
parents:
diff changeset
405 if len(newname) < 2:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
406 ndash = 1
83f8bb78781e Uploaded
fubar
parents:
diff changeset
407 return ndash
83f8bb78781e Uploaded
fubar
parents:
diff changeset
408
83f8bb78781e Uploaded
fubar
parents:
diff changeset
409 def doXMLparam(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
410 """flake8 made me do this..."""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
411 for p in self.outfiles: # --output_files "$otab.history_name~~~$otab.history_format~~~$otab.history_CL~~~$otab.history_test"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
412 newname, newfmt, newcl, test, oldcl = p
83f8bb78781e Uploaded
fubar
parents:
diff changeset
413 test = test.strip()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
414 ndash = self.getNdash(newcl)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
415 aparm = gxtp.OutputData(name=newname, format=newfmt, num_dashes=ndash, label=newcl)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
416 aparm.positional = self.is_positional
83f8bb78781e Uploaded
fubar
parents:
diff changeset
417 if self.is_positional:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
418 if oldcl.upper() == "STDOUT":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
419 aparm.positional = 9999999
83f8bb78781e Uploaded
fubar
parents:
diff changeset
420 aparm.command_line_override = "> $%s" % newname
83f8bb78781e Uploaded
fubar
parents:
diff changeset
421 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
422 aparm.positional = int(oldcl)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
423 aparm.command_line_override = "$%s" % newname
83f8bb78781e Uploaded
fubar
parents:
diff changeset
424 self.toutputs.append(aparm)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
425 ld = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
426 if test.strip() > "":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
427 if test.startswith("diff"):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
428 c = "diff"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
429 ld = 0
83f8bb78781e Uploaded
fubar
parents:
diff changeset
430 if test.split(":")[1].isdigit:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
431 ld = int(test.split(":")[1])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
432 tp = gxtp.TestOutput(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
433 name=newcl,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
434 value="%s_sample" % newcl,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
435 format=newfmt,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
436 compare= c,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
437 lines_diff=ld,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
438 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
439 elif test.startswith("sim_size"):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
440 c = "sim_size"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
441 tn = test.split(":")[1].strip()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
442 if tn > '':
83f8bb78781e Uploaded
fubar
parents:
diff changeset
443 if '.' in tn:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
444 delta = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
445 delta_frac = min(1.0,float(tn))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
446 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
447 delta = int(tn)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
448 delta_frac = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
449 tp = gxtp.TestOutput(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
450 name=newcl,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
451 value="%s_sample" % newcl,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
452 format=newfmt,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
453 compare= c,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
454 delta = delta,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
455 delta_frac = delta_frac
83f8bb78781e Uploaded
fubar
parents:
diff changeset
456 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
457 self.testparam.append(tp)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
458 for p in self.infiles:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
459 newname = p[ICLPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
460 newfmt = p[IFMTPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
461 ndash = self.getNdash(newname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
462 if not len(p[ILABPOS]) > 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
463 alab = p[ICLPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
464 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
465 alab = p[ILABPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
466 aninput = gxtp.DataParam(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
467 newname,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
468 optional=False,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
469 label=alab,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
470 help=p[IHELPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
471 format=newfmt,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
472 multiple=False,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
473 num_dashes=ndash,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
474 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
475 aninput.positional = self.is_positional
83f8bb78781e Uploaded
fubar
parents:
diff changeset
476 self.tinputs.append(aninput)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
477 tparm = gxtp.TestParam(name=newname, value="%s_sample" % newname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
478 self.testparam.append(tparm)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
479 for p in self.addpar:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
480 newname, newval, newlabel, newhelp, newtype, newcl, override, oldcl = p
83f8bb78781e Uploaded
fubar
parents:
diff changeset
481 if not len(newlabel) > 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
482 newlabel = newname
83f8bb78781e Uploaded
fubar
parents:
diff changeset
483 ndash = self.getNdash(newname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
484 if newtype == "text":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
485 aparm = gxtp.TextParam(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
486 newname,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
487 label=newlabel,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
488 help=newhelp,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
489 value=newval,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
490 num_dashes=ndash,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
491 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
492 elif newtype == "integer":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
493 aparm = gxtp.IntegerParam(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
494 newname,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
495 label=newname,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
496 help=newhelp,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
497 value=newval,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
498 num_dashes=ndash,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
499 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
500 elif newtype == "float":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
501 aparm = gxtp.FloatParam(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
502 newname,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
503 label=newname,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
504 help=newhelp,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
505 value=newval,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
506 num_dashes=ndash,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
507 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
508 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
509 raise ValueError(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
510 'Unrecognised parameter type "%s" for\
83f8bb78781e Uploaded
fubar
parents:
diff changeset
511 additional parameter %s in makeXML'
83f8bb78781e Uploaded
fubar
parents:
diff changeset
512 % (newtype, newname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
513 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
514 aparm.positional = self.is_positional
83f8bb78781e Uploaded
fubar
parents:
diff changeset
515 if self.is_positional:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
516 aparm.positional = int(oldcl)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
517 self.tinputs.append(aparm)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
518 tparm = gxtp.TestParam(newname, value=newval)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
519 self.testparam.append(tparm)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
520
83f8bb78781e Uploaded
fubar
parents:
diff changeset
521 def doNoXMLparam(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
522 """filter style package - stdin to stdout"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
523 if len(self.infiles) > 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
524 alab = self.infiles[0][ILABPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
525 if len(alab) == 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
526 alab = self.infiles[0][ICLPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
527 max1s = (
83f8bb78781e Uploaded
fubar
parents:
diff changeset
528 "Maximum one input if parampass is 0 but multiple input files supplied - %s"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
529 % str(self.infiles)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
530 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
531 assert len(self.infiles) == 1, max1s
83f8bb78781e Uploaded
fubar
parents:
diff changeset
532 newname = self.infiles[0][ICLPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
533 aninput = gxtp.DataParam(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
534 newname,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
535 optional=False,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
536 label=alab,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
537 help=self.infiles[0][IHELPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
538 format=self.infiles[0][IFMTPOS],
83f8bb78781e Uploaded
fubar
parents:
diff changeset
539 multiple=False,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
540 num_dashes=0,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
541 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
542 aninput.command_line_override = "< $%s" % newname
83f8bb78781e Uploaded
fubar
parents:
diff changeset
543 aninput.positional = self.is_positional
83f8bb78781e Uploaded
fubar
parents:
diff changeset
544 self.tinputs.append(aninput)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
545 tp = gxtp.TestParam(name=newname, value="%s_sample" % newname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
546 self.testparam.append(tp)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
547 if len(self.outfiles) > 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
548 newname = self.outfiles[0][OCLPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
549 newfmt = self.outfiles[0][OFMTPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
550 anout = gxtp.OutputData(newname, format=newfmt, num_dashes=0)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
551 anout.command_line_override = "> $%s" % newname
83f8bb78781e Uploaded
fubar
parents:
diff changeset
552 anout.positional = self.is_positional
83f8bb78781e Uploaded
fubar
parents:
diff changeset
553 self.toutputs.append(anout)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
554 tp = gxtp.TestOutput(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
555 name=newname, value="%s_sample" % newname, format=newfmt
83f8bb78781e Uploaded
fubar
parents:
diff changeset
556 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
557 self.testparam.append(tp)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
558
83f8bb78781e Uploaded
fubar
parents:
diff changeset
559 def makeXML(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
560 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
561 Create a Galaxy xml tool wrapper for the new script
83f8bb78781e Uploaded
fubar
parents:
diff changeset
562 Uses galaxyhtml
83f8bb78781e Uploaded
fubar
parents:
diff changeset
563 Hmmm. How to get the command line into correct order...
83f8bb78781e Uploaded
fubar
parents:
diff changeset
564 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
565 if self.command_override:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
566 self.newtool.command_override = self.command_override # config file
83f8bb78781e Uploaded
fubar
parents:
diff changeset
567 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
568 self.newtool.command_override = self.xmlcl
83f8bb78781e Uploaded
fubar
parents:
diff changeset
569 if self.args.help_text:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
570 helptext = open(self.args.help_text, "r").readlines()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
571 safertext = "\n".join([cheetah_escape(x) for x in helptext])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
572 if self.args.script_path:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
573 scr = [x for x in self.spacedScript if x.strip() > ""]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
574 scr.insert(0,'\n------\n\n\nScript::\n')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
575 if len(scr) > 300:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
576 scr = (
83f8bb78781e Uploaded
fubar
parents:
diff changeset
577 scr[:100]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
578 + [" >300 lines - stuff deleted", " ......"]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
579 + scr[-100:]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
580 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
581 scr.append('\n')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
582 safertext = safertext + "\n".join(scr)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
583 self.newtool.help = safertext
83f8bb78781e Uploaded
fubar
parents:
diff changeset
584 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
585 self.newtool.help = (
83f8bb78781e Uploaded
fubar
parents:
diff changeset
586 "Please ask the tool author (%s) for help \
83f8bb78781e Uploaded
fubar
parents:
diff changeset
587 as none was supplied at tool generation\n"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
588 % (self.args.user_email)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
589 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
590 self.newtool.version_command = None # do not want
83f8bb78781e Uploaded
fubar
parents:
diff changeset
591 requirements = gxtp.Requirements()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
592 if self.args.packages:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
593 for d in self.args.packages.split(","):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
594 ver = ''
83f8bb78781e Uploaded
fubar
parents:
diff changeset
595 d = d.replace('==',':')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
596 d = d.replace('=',':')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
597 if ":" in d:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
598 packg, ver = d.split(":")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
599 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
600 packg = d
83f8bb78781e Uploaded
fubar
parents:
diff changeset
601 requirements.append(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
602 gxtp.Requirement("package", packg.strip(), ver.strip())
83f8bb78781e Uploaded
fubar
parents:
diff changeset
603 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
604 self.newtool.requirements = requirements
83f8bb78781e Uploaded
fubar
parents:
diff changeset
605 if self.args.parampass == "0":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
606 self.doNoXMLparam()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
607 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
608 self.doXMLparam()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
609 self.newtool.outputs = self.toutputs
83f8bb78781e Uploaded
fubar
parents:
diff changeset
610 self.newtool.inputs = self.tinputs
83f8bb78781e Uploaded
fubar
parents:
diff changeset
611 if self.args.script_path:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
612 configfiles = gxtp.Configfiles()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
613 configfiles.append(gxtp.Configfile(name="runme", text="\n".join(self.escapedScript)))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
614 self.newtool.configfiles = configfiles
83f8bb78781e Uploaded
fubar
parents:
diff changeset
615 tests = gxtp.Tests()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
616 test_a = gxtp.Test()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
617 for tp in self.testparam:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
618 test_a.append(tp)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
619 tests.append(test_a)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
620 self.newtool.tests = tests
83f8bb78781e Uploaded
fubar
parents:
diff changeset
621 self.newtool.add_comment(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
622 "Created by %s at %s using the Galaxy Tool Factory."
83f8bb78781e Uploaded
fubar
parents:
diff changeset
623 % (self.args.user_email, timenow())
83f8bb78781e Uploaded
fubar
parents:
diff changeset
624 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
625 self.newtool.add_comment("Source in git at: %s" % (toolFactoryURL))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
626 self.newtool.add_comment(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
627 "Cite: Creating re-usable tools from scripts doi:10.1093/bioinformatics/bts573"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
628 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
629 exml0 = self.newtool.export()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
630 exml = exml0.replace(FAKEEXE, "") # temporary work around until PR accepted
83f8bb78781e Uploaded
fubar
parents:
diff changeset
631 if (
83f8bb78781e Uploaded
fubar
parents:
diff changeset
632 self.test_override
83f8bb78781e Uploaded
fubar
parents:
diff changeset
633 ): # cannot do this inside galaxyxml as it expects lxml objects for tests
83f8bb78781e Uploaded
fubar
parents:
diff changeset
634 part1 = exml.split("<tests>")[0]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
635 part2 = exml.split("</tests>")[1]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
636 fixed = "%s\n%s\n%s" % (part1, self.test_override, part2)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
637 exml = fixed
83f8bb78781e Uploaded
fubar
parents:
diff changeset
638 #exml = exml.replace('range="1:"', 'range="1000:"')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
639 xf = open("%s.xml" % self.tool_name, "w")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
640 xf.write(exml)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
641 xf.write("\n")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
642 xf.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
643 # ready for the tarball
83f8bb78781e Uploaded
fubar
parents:
diff changeset
644
83f8bb78781e Uploaded
fubar
parents:
diff changeset
645 def run(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
646 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
647 generate test outputs by running a command line
83f8bb78781e Uploaded
fubar
parents:
diff changeset
648 won't work if command or test override in play - planemo is the
83f8bb78781e Uploaded
fubar
parents:
diff changeset
649 easiest way to generate test outputs for that case so is
83f8bb78781e Uploaded
fubar
parents:
diff changeset
650 automagically selected
83f8bb78781e Uploaded
fubar
parents:
diff changeset
651 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
652 scl = " ".join(self.cl)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
653 err = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
654 if self.args.parampass != "0":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
655 if os.path.exists(self.elog):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
656 ste = open(self.elog, "a")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
657 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
658 ste = open(self.elog, "w")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
659 if self.lastclredirect:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
660 sto = open(self.lastclredirect[1], "wb") # is name of an output file
83f8bb78781e Uploaded
fubar
parents:
diff changeset
661 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
662 if os.path.exists(self.tlog):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
663 sto = open(self.tlog, "a")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
664 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
665 sto = open(self.tlog, "w")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
666 sto.write(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
667 "## Executing Toolfactory generated command line = %s\n" % scl
83f8bb78781e Uploaded
fubar
parents:
diff changeset
668 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
669 sto.flush()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
670 subp = subprocess.run(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
671 self.cl, env=self.ourenv, shell=False, stdout=sto, stderr=ste
83f8bb78781e Uploaded
fubar
parents:
diff changeset
672 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
673 sto.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
674 ste.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
675 retval = subp.returncode
83f8bb78781e Uploaded
fubar
parents:
diff changeset
676 else: # work around special case - stdin and write to stdout
83f8bb78781e Uploaded
fubar
parents:
diff changeset
677 if len(self.infiles) > 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
678 sti = open(self.infiles[0][IPATHPOS], "rb")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
679 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
680 sti = sys.stdin
83f8bb78781e Uploaded
fubar
parents:
diff changeset
681 if len(self.outfiles) > 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
682 sto = open(self.outfiles[0][ONAMEPOS], "wb")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
683 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
684 sto = sys.stdout
83f8bb78781e Uploaded
fubar
parents:
diff changeset
685 subp = subprocess.run(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
686 self.cl, env=self.ourenv, shell=False, stdout=sto, stdin=sti
83f8bb78781e Uploaded
fubar
parents:
diff changeset
687 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
688 sto.write("## Executing Toolfactory generated command line = %s\n" % scl)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
689 retval = subp.returncode
83f8bb78781e Uploaded
fubar
parents:
diff changeset
690 sto.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
691 sti.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
692 if os.path.isfile(self.tlog) and os.stat(self.tlog).st_size == 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
693 os.unlink(self.tlog)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
694 if os.path.isfile(self.elog) and os.stat(self.elog).st_size == 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
695 os.unlink(self.elog)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
696 if retval != 0 and err: # problem
83f8bb78781e Uploaded
fubar
parents:
diff changeset
697 sys.stderr.write(err)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
698 logging.debug("run done")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
699 return retval
83f8bb78781e Uploaded
fubar
parents:
diff changeset
700
83f8bb78781e Uploaded
fubar
parents:
diff changeset
701 def copy_to_container(self, src, dest, container):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
702 """Recreate the src directory tree at dest - full path included"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
703 idir = os.getcwd()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
704 workdir = os.path.dirname(src)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
705 os.chdir(workdir)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
706 _, tfname = tempfile.mkstemp(suffix=".tar")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
707 tar = tarfile.open(tfname, mode="w")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
708 srcb = os.path.basename(src)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
709 tar.add(srcb)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
710 tar.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
711 data = open(tfname, "rb").read()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
712 container.put_archive(dest, data)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
713 os.unlink(tfname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
714 os.chdir(idir)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
715
83f8bb78781e Uploaded
fubar
parents:
diff changeset
716 def copy_from_container(self, src, dest, container):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
717 """recreate the src directory tree at dest using docker sdk"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
718 os.makedirs(dest, exist_ok=True)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
719 _, tfname = tempfile.mkstemp(suffix=".tar")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
720 tf = open(tfname, "wb")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
721 bits, stat = container.get_archive(src)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
722 for chunk in bits:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
723 tf.write(chunk)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
724 tf.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
725 tar = tarfile.open(tfname, "r")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
726 tar.extractall(dest)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
727 tar.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
728 os.unlink(tfname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
729
83f8bb78781e Uploaded
fubar
parents:
diff changeset
730 def planemo_biodocker_test(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
731 """planemo currently leaks dependencies if used in the same container and gets unhappy after a
83f8bb78781e Uploaded
fubar
parents:
diff changeset
732 first successful run. https://github.com/galaxyproject/planemo/issues/1078#issuecomment-731476930
83f8bb78781e Uploaded
fubar
parents:
diff changeset
733
83f8bb78781e Uploaded
fubar
parents:
diff changeset
734 Docker biocontainer has planemo with caches filled to save repeated downloads
83f8bb78781e Uploaded
fubar
parents:
diff changeset
735
83f8bb78781e Uploaded
fubar
parents:
diff changeset
736
83f8bb78781e Uploaded
fubar
parents:
diff changeset
737 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
738
83f8bb78781e Uploaded
fubar
parents:
diff changeset
739 def prun(container, tout, cl, user="biodocker"):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
740 rlog = container.exec_run(cl, user=user)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
741 slogl = str(rlog).split("\\n")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
742 slog = "\n".join(slogl)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
743 tout.write(f"## got rlog {slog} from {cl}\n")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
744
83f8bb78781e Uploaded
fubar
parents:
diff changeset
745 if os.path.exists(self.tlog):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
746 tout = open(self.tlog, "a")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
747 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
748 tout = open(self.tlog, "w")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
749 planemoimage = "quay.io/fubar2/planemo-biocontainer"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
750 xreal = "%s.xml" % self.tool_name
83f8bb78781e Uploaded
fubar
parents:
diff changeset
751 repname = f"{self.tool_name}_planemo_test_report.html"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
752 ptestrep_path = os.path.join(self.repdir, repname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
753 tool_name = self.tool_name
83f8bb78781e Uploaded
fubar
parents:
diff changeset
754 client = docker.from_env()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
755 tvol = client.volumes.create()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
756 tvolname = tvol.name
83f8bb78781e Uploaded
fubar
parents:
diff changeset
757 destdir = "/toolfactory/ptest"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
758 imrep = os.path.join(destdir, repname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
759 # need to keep the container running so keep it open with sleep
83f8bb78781e Uploaded
fubar
parents:
diff changeset
760 # will stop and destroy it when we are done
83f8bb78781e Uploaded
fubar
parents:
diff changeset
761 container = client.containers.run(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
762 planemoimage,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
763 "sleep 120m",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
764 detach=True,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
765 user="biodocker",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
766 volumes={f"{tvolname}": {"bind": "/toolfactory", "mode": "rw"}},
83f8bb78781e Uploaded
fubar
parents:
diff changeset
767 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
768 cl = f"mkdir -p {destdir}"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
769 prun(container, tout, cl, user="root")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
770 # that's how hard it is to get root on a biodocker container :(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
771 cl = f"rm -rf {destdir}/*"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
772 prun(container, tout, cl, user="root")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
773 ptestpath = os.path.join(destdir, "tfout", xreal)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
774 self.copy_to_container(self.tooloutdir, destdir, container)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
775 cl = "chown -R biodocker /toolfactory"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
776 prun(container, tout, cl, user="root")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
777 rlog = container.exec_run(f"ls -la {destdir}")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
778 ptestcl = f"planemo test --update_test_data --no_cleanup --test_data {destdir}/tfout/test-data --galaxy_root /home/biodocker/galaxy-central {ptestpath}"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
779 try:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
780 rlog = container.exec_run(ptestcl)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
781 # fails because test outputs missing but updates the test-data directory
83f8bb78781e Uploaded
fubar
parents:
diff changeset
782 except:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
783 e = sys.exc_info()[0]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
784 tout.write(f"#### error: {e} from {ptestcl}\n")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
785 cl = f"planemo test --test_output {imrep} --no_cleanup --test_data {destdir}/tfout/test-data --galaxy_root /home/biodocker/galaxy-central {ptestpath}"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
786 try:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
787 prun(container, tout, cl)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
788 except:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
789 e = sys.exc_info()[0]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
790 tout.write(f"#### error: {e} from {ptestcl}\n")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
791 testouts = tempfile.mkdtemp(suffix=None, prefix="tftemp", dir=".")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
792 self.copy_from_container(destdir, testouts, container)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
793 src = os.path.join(testouts, "ptest")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
794 if os.path.isdir(src):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
795 shutil.copytree(src, ".", dirs_exist_ok=True)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
796 src = repname
83f8bb78781e Uploaded
fubar
parents:
diff changeset
797 if os.path.isfile(repname):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
798 shutil.copyfile(src, ptestrep_path)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
799 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
800 tout.write(f"No output from run to shutil.copytree in {src}\n")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
801 tout.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
802 container.stop()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
803 container.remove()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
804 tvol.remove()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
805 shutil.rmtree(testouts) # leave for debugging
83f8bb78781e Uploaded
fubar
parents:
diff changeset
806
83f8bb78781e Uploaded
fubar
parents:
diff changeset
807 def shedLoad(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
808 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
809 use bioblend to create new repository
83f8bb78781e Uploaded
fubar
parents:
diff changeset
810 or update existing
83f8bb78781e Uploaded
fubar
parents:
diff changeset
811
83f8bb78781e Uploaded
fubar
parents:
diff changeset
812 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
813 if os.path.exists(self.tlog):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
814 sto = open(self.tlog, "a")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
815 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
816 sto = open(self.tlog, "w")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
817
83f8bb78781e Uploaded
fubar
parents:
diff changeset
818 ts = toolshed.ToolShedInstance(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
819 url=self.args.toolshed_url, key=self.args.toolshed_api_key, verify=False
83f8bb78781e Uploaded
fubar
parents:
diff changeset
820 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
821 repos = ts.repositories.get_repositories()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
822 rnames = [x.get("name", "?") for x in repos]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
823 rids = [x.get("id", "?") for x in repos]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
824 tfcat = "ToolFactory generated tools"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
825 if self.tool_name not in rnames:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
826 tscat = ts.categories.get_categories()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
827 cnames = [x.get("name", "?").strip() for x in tscat]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
828 cids = [x.get("id", "?") for x in tscat]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
829 catID = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
830 if tfcat.strip() in cnames:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
831 ci = cnames.index(tfcat)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
832 catID = cids[ci]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
833 res = ts.repositories.create_repository(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
834 name=self.args.tool_name,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
835 synopsis="Synopsis:%s" % self.args.tool_desc,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
836 description=self.args.tool_desc,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
837 type="unrestricted",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
838 remote_repository_url=self.args.toolshed_url,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
839 homepage_url=None,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
840 category_ids=catID,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
841 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
842 tid = res.get("id", None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
843 sto.write(f"#create_repository {self.args.tool_name} tid={tid} res={res}\n")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
844 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
845 i = rnames.index(self.tool_name)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
846 tid = rids[i]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
847 try:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
848 res = ts.repositories.update_repository(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
849 id=tid, tar_ball_path=self.newtarpath, commit_message=None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
850 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
851 sto.write(f"#update res id {id} ={res}\n")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
852 except ConnectionError:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
853 sto.write(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
854 "####### Is the toolshed running and the API key correct? Bioblend shed upload failed\n"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
855 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
856 sto.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
857
83f8bb78781e Uploaded
fubar
parents:
diff changeset
858 def eph_galaxy_load(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
859 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
860 use ephemeris to load the new tool from the local toolshed after planemo uploads it
83f8bb78781e Uploaded
fubar
parents:
diff changeset
861 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
862 if os.path.exists(self.tlog):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
863 tout = open(self.tlog, "a")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
864 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
865 tout = open(self.tlog, "w")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
866 cll = [
83f8bb78781e Uploaded
fubar
parents:
diff changeset
867 "shed-tools",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
868 "install",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
869 "-g",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
870 self.args.galaxy_url,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
871 "--latest",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
872 "-a",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
873 self.args.galaxy_api_key,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
874 "--name",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
875 self.tool_name,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
876 "--owner",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
877 "fubar",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
878 "--toolshed",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
879 self.args.toolshed_url,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
880 "--section_label",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
881 "ToolFactory",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
882 ]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
883 tout.write("running\n%s\n" % " ".join(cll))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
884 subp = subprocess.run(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
885 cll, env=self.ourenv, cwd=self.ourcwd, shell=False, stderr=tout, stdout=tout
83f8bb78781e Uploaded
fubar
parents:
diff changeset
886 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
887 tout.write(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
888 "installed %s - got retcode %d\n" % (self.tool_name, subp.returncode)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
889 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
890 tout.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
891 return subp.returncode
83f8bb78781e Uploaded
fubar
parents:
diff changeset
892
83f8bb78781e Uploaded
fubar
parents:
diff changeset
893
83f8bb78781e Uploaded
fubar
parents:
diff changeset
894
83f8bb78781e Uploaded
fubar
parents:
diff changeset
895 def writeShedyml(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
896 """for planemo"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
897 yuser = self.args.user_email.split("@")[0]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
898 yfname = os.path.join(self.tooloutdir, ".shed.yml")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
899 yamlf = open(yfname, "w")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
900 odict = {
83f8bb78781e Uploaded
fubar
parents:
diff changeset
901 "name": self.tool_name,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
902 "owner": yuser,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
903 "type": "unrestricted",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
904 "description": self.args.tool_desc,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
905 "synopsis": self.args.tool_desc,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
906 "category": "TF Generated Tools",
83f8bb78781e Uploaded
fubar
parents:
diff changeset
907 }
83f8bb78781e Uploaded
fubar
parents:
diff changeset
908 yaml.dump(odict, yamlf, allow_unicode=True)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
909 yamlf.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
910
83f8bb78781e Uploaded
fubar
parents:
diff changeset
911 def makeTool(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
912 """write xmls and input samples into place"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
913 self.makeXML()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
914 if self.args.script_path:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
915 stname = os.path.join(self.tooloutdir, "%s" % (self.sfile))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
916 if not os.path.exists(stname):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
917 shutil.copyfile(self.sfile, stname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
918 xreal = "%s.xml" % self.tool_name
83f8bb78781e Uploaded
fubar
parents:
diff changeset
919 xout = os.path.join(self.tooloutdir, xreal)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
920 shutil.copyfile(xreal, xout)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
921 for p in self.infiles:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
922 pth = p[IPATHPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
923 dest = os.path.join(self.testdir, "%s_sample" % p[ICLPOS])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
924 shutil.copyfile(pth, dest)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
925
83f8bb78781e Uploaded
fubar
parents:
diff changeset
926 def makeToolTar(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
927 """move outputs into test-data and prepare the tarball"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
928 excludeme = "_planemo_test_report.html"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
929
83f8bb78781e Uploaded
fubar
parents:
diff changeset
930 def exclude_function(tarinfo):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
931 filename = tarinfo.name
83f8bb78781e Uploaded
fubar
parents:
diff changeset
932 return None if filename.endswith(excludeme) else tarinfo
83f8bb78781e Uploaded
fubar
parents:
diff changeset
933
83f8bb78781e Uploaded
fubar
parents:
diff changeset
934 if os.path.exists(self.tlog):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
935 tout = open(self.tlog, "a")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
936 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
937 tout = open(self.tlog, "w")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
938 for p in self.outfiles:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
939 oname = p[ONAMEPOS]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
940 tdest = os.path.join(self.testdir, "%s_sample" % oname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
941 if not os.path.isfile(tdest):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
942 src = os.path.join(self.testdir, oname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
943 if os.path.isfile(src):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
944 shutil.copyfile(src, tdest)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
945 dest = os.path.join(self.repdir, "%s.sample" % (oname))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
946 shutil.copyfile(src, dest)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
947 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
948 tout.write(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
949 "###Output file %s not found in testdir %s. This is normal during the first Planemo run that generates test outputs"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
950 % (tdest, self.testdir)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
951 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
952 tf = tarfile.open(self.newtarpath, "w:gz")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
953 tf.add(name=self.tooloutdir, arcname=self.tool_name, filter=exclude_function)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
954 tf.close()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
955 shutil.copyfile(self.newtarpath, self.args.new_tool)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
956
83f8bb78781e Uploaded
fubar
parents:
diff changeset
957 def moveRunOutputs(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
958 """need to move planemo or run outputs into toolfactory collection"""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
959 with os.scandir(self.tooloutdir) as outs:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
960 for entry in outs:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
961 if not entry.is_file():
83f8bb78781e Uploaded
fubar
parents:
diff changeset
962 continue
83f8bb78781e Uploaded
fubar
parents:
diff changeset
963 if "." in entry.name:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
964 nayme, ext = os.path.splitext(entry.name)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
965 if ext in [".yml", ".xml", ".json", ".yaml"]:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
966 ext = f"{ext}.txt"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
967 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
968 ext = ".txt"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
969 ofn = "%s%s" % (entry.name.replace(".", "_"), ext)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
970 dest = os.path.join(self.repdir, ofn)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
971 src = os.path.join(self.tooloutdir, entry.name)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
972 shutil.copyfile(src, dest)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
973 with os.scandir(self.testdir) as outs:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
974 for entry in outs:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
975 if (
83f8bb78781e Uploaded
fubar
parents:
diff changeset
976 (not entry.is_file())
83f8bb78781e Uploaded
fubar
parents:
diff changeset
977 or entry.name.endswith("_sample")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
978 or entry.name.endswith("_planemo_test_report.html")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
979 ):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
980 continue
83f8bb78781e Uploaded
fubar
parents:
diff changeset
981 if "." in entry.name:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
982 nayme, ext = os.path.splitext(entry.name)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
983 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
984 ext = ".txt"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
985 newname = f"{entry.name}{ext}"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
986 dest = os.path.join(self.repdir, newname)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
987 src = os.path.join(self.testdir, entry.name)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
988 shutil.copyfile(src, dest)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
989
83f8bb78781e Uploaded
fubar
parents:
diff changeset
990
83f8bb78781e Uploaded
fubar
parents:
diff changeset
991 def main():
83f8bb78781e Uploaded
fubar
parents:
diff changeset
992 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
993 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml
83f8bb78781e Uploaded
fubar
parents:
diff changeset
994
83f8bb78781e Uploaded
fubar
parents:
diff changeset
995 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
996 parser = argparse.ArgumentParser()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
997 a = parser.add_argument
83f8bb78781e Uploaded
fubar
parents:
diff changeset
998 a("--script_path", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
999 a("--history_test", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1000 a("--cl_prefix", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1001 a("--sysexe", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1002 a("--packages", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1003 a("--tool_name", default="newtool")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1004 a("--tool_dir", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1005 a("--input_files", default=[], action="append")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1006 a("--output_files", default=[], action="append")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1007 a("--user_email", default="Unknown")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1008 a("--bad_user", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1009 a("--make_Tool", default="runonly")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1010 a("--help_text", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1011 a("--tool_desc", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1012 a("--tool_version", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1013 a("--citations", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1014 a("--command_override", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1015 a("--test_override", default=None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1016 a("--additional_parameters", action="append", default=[])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1017 a("--edit_additional_parameters", action="store_true", default=False)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1018 a("--parampass", default="positional")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1019 a("--tfout", default="./tfout")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1020 a("--new_tool", default="new_tool")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1021 a("--galaxy_url", default="http://localhost:8080")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1022 a("--toolshed_url", default="http://localhost:9009")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1023 # make sure this is identical to tool_sheds_conf.xml localhost != 127.0.0.1 so validation fails
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1024 a("--toolshed_api_key", default="fakekey")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1025 a("--galaxy_api_key", default="fakekey")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1026 a("--galaxy_root", default="/galaxy-central")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1027 a("--galaxy_venv", default="/galaxy_venv")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1028 args = parser.parse_args()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1029 assert not args.bad_user, (
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1030 'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to "admin_users" in the galaxy.yml Galaxy configuration file'
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1031 % (args.bad_user, args.bad_user)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1032 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1033 assert args.tool_name, "## Tool Factory expects a tool name - eg --tool_name=DESeq"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1034 assert (
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1035 args.sysexe or args.packages
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1036 ), "## Tool Factory wrapper expects an interpreter or an executable package"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1037 args.input_files = [x.replace('"', "").replace("'", "") for x in args.input_files]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1038 # remove quotes we need to deal with spaces in CL params
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1039 for i, x in enumerate(args.additional_parameters):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1040 args.additional_parameters[i] = args.additional_parameters[i].replace('"', "")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1041 r = ScriptRunner(args)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1042 r.writeShedyml()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1043 r.makeTool()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1044 if args.make_Tool == "generate":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1045 retcode = r.run() # for testing toolfactory itself
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1046 r.moveRunOutputs()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1047 r.makeToolTar()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1048 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1049 r.planemo_biodocker_test() # test to make outputs and then test
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1050 r.moveRunOutputs()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1051 r.makeToolTar()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1052 if args.make_Tool == "gentestinstall":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1053 r.shedLoad()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1054 r.eph_galaxy_load()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1055
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1056
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1057 if __name__ == "__main__":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1058 main()