annotate toolfactory_docker/rgToolFactory2.py @ 4:b4985006fb05 draft

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