annotate toolfactory/ToolFactory.py @ 5:e2c8c2fa192d draft

Uploaded
author fubar
date Tue, 27 Apr 2021 23:33:49 +0000
parents 2a46da701dde
children efefe43f23c8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
1
2a46da701dde Uploaded
fubar
parents:
diff changeset
2 # see https://github.com/fubar2/toolfactory
2a46da701dde Uploaded
fubar
parents:
diff changeset
3 #
2a46da701dde Uploaded
fubar
parents:
diff changeset
4 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
2a46da701dde Uploaded
fubar
parents:
diff changeset
5 #
2a46da701dde Uploaded
fubar
parents:
diff changeset
6 # all rights reserved
2a46da701dde Uploaded
fubar
parents:
diff changeset
7 # Licensed under the LGPL
2a46da701dde Uploaded
fubar
parents:
diff changeset
8 # suggestions for improvement and bug fixes welcome at
2a46da701dde Uploaded
fubar
parents:
diff changeset
9 # https://github.com/fubar2/toolfactory
2a46da701dde Uploaded
fubar
parents:
diff changeset
10 #
2a46da701dde Uploaded
fubar
parents:
diff changeset
11 # April 2021: Refactored into two tools - generate and test/install
2a46da701dde Uploaded
fubar
parents:
diff changeset
12 # as part of GTN tutorial development and biocontainer adoption
2a46da701dde Uploaded
fubar
parents:
diff changeset
13 # The tester runs planemo on a non-tested archive, creates the test outputs
2a46da701dde Uploaded
fubar
parents:
diff changeset
14 # and returns a new proper tool with test.
2a46da701dde Uploaded
fubar
parents:
diff changeset
15 # The tester was generated from the ToolFactory_tester.py script
2a46da701dde Uploaded
fubar
parents:
diff changeset
16
2a46da701dde Uploaded
fubar
parents:
diff changeset
17
2a46da701dde Uploaded
fubar
parents:
diff changeset
18 import argparse
2a46da701dde Uploaded
fubar
parents:
diff changeset
19 import copy
2a46da701dde Uploaded
fubar
parents:
diff changeset
20 import json
2a46da701dde Uploaded
fubar
parents:
diff changeset
21 import logging
2a46da701dde Uploaded
fubar
parents:
diff changeset
22 import os
2a46da701dde Uploaded
fubar
parents:
diff changeset
23 import re
2a46da701dde Uploaded
fubar
parents:
diff changeset
24 import shlex
2a46da701dde Uploaded
fubar
parents:
diff changeset
25 import shutil
2a46da701dde Uploaded
fubar
parents:
diff changeset
26 import subprocess
2a46da701dde Uploaded
fubar
parents:
diff changeset
27 import sys
2a46da701dde Uploaded
fubar
parents:
diff changeset
28 import tarfile
2a46da701dde Uploaded
fubar
parents:
diff changeset
29 import tempfile
2a46da701dde Uploaded
fubar
parents:
diff changeset
30 import time
2a46da701dde Uploaded
fubar
parents:
diff changeset
31
2a46da701dde Uploaded
fubar
parents:
diff changeset
32 from bioblend import ConnectionError
2a46da701dde Uploaded
fubar
parents:
diff changeset
33 from bioblend import galaxy
2a46da701dde Uploaded
fubar
parents:
diff changeset
34 from bioblend import toolshed
2a46da701dde Uploaded
fubar
parents:
diff changeset
35
2a46da701dde Uploaded
fubar
parents:
diff changeset
36 import galaxyxml.tool as gxt
2a46da701dde Uploaded
fubar
parents:
diff changeset
37 import galaxyxml.tool.parameters as gxtp
2a46da701dde Uploaded
fubar
parents:
diff changeset
38
2a46da701dde Uploaded
fubar
parents:
diff changeset
39 import lxml.etree as ET
2a46da701dde Uploaded
fubar
parents:
diff changeset
40
2a46da701dde Uploaded
fubar
parents:
diff changeset
41 import yaml
2a46da701dde Uploaded
fubar
parents:
diff changeset
42
2a46da701dde Uploaded
fubar
parents:
diff changeset
43 myversion = "V2.3 April 2021"
2a46da701dde Uploaded
fubar
parents:
diff changeset
44 verbose = True
2a46da701dde Uploaded
fubar
parents:
diff changeset
45 debug = True
2a46da701dde Uploaded
fubar
parents:
diff changeset
46 toolFactoryURL = "https://github.com/fubar2/toolfactory"
2a46da701dde Uploaded
fubar
parents:
diff changeset
47 FAKEEXE = "~~~REMOVE~~~ME~~~"
2a46da701dde Uploaded
fubar
parents:
diff changeset
48 # need this until a PR/version bump to fix galaxyxml prepending the exe even
2a46da701dde Uploaded
fubar
parents:
diff changeset
49 # with override.
2a46da701dde Uploaded
fubar
parents:
diff changeset
50
2a46da701dde Uploaded
fubar
parents:
diff changeset
51
2a46da701dde Uploaded
fubar
parents:
diff changeset
52 def timenow():
2a46da701dde Uploaded
fubar
parents:
diff changeset
53 """return current time as a string"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
54 return time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(time.time()))
2a46da701dde Uploaded
fubar
parents:
diff changeset
55
2a46da701dde Uploaded
fubar
parents:
diff changeset
56 cheetah_escape_table = {"$": "\\$", "#": "\\#"}
2a46da701dde Uploaded
fubar
parents:
diff changeset
57
2a46da701dde Uploaded
fubar
parents:
diff changeset
58 def cheetah_escape(text):
2a46da701dde Uploaded
fubar
parents:
diff changeset
59 """Produce entities within text."""
2a46da701dde Uploaded
fubar
parents:
diff changeset
60 return "".join([cheetah_escape_table.get(c, c) for c in text])
2a46da701dde Uploaded
fubar
parents:
diff changeset
61
2a46da701dde Uploaded
fubar
parents:
diff changeset
62 def parse_citations(citations_text):
2a46da701dde Uploaded
fubar
parents:
diff changeset
63 """"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
64 citations = [c for c in citations_text.split("**ENTRY**") if c.strip()]
2a46da701dde Uploaded
fubar
parents:
diff changeset
65 citation_tuples = []
2a46da701dde Uploaded
fubar
parents:
diff changeset
66 for citation in citations:
2a46da701dde Uploaded
fubar
parents:
diff changeset
67 if citation.startswith("doi"):
2a46da701dde Uploaded
fubar
parents:
diff changeset
68 citation_tuples.append(("doi", citation[len("doi") :].strip()))
2a46da701dde Uploaded
fubar
parents:
diff changeset
69 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
70 citation_tuples.append(("bibtex", citation[len("bibtex") :].strip()))
2a46da701dde Uploaded
fubar
parents:
diff changeset
71 return citation_tuples
2a46da701dde Uploaded
fubar
parents:
diff changeset
72
2a46da701dde Uploaded
fubar
parents:
diff changeset
73 class ToolConfUpdater():
2a46da701dde Uploaded
fubar
parents:
diff changeset
74 # update config/tool_conf.xml with a new tool unpacked in /tools
2a46da701dde Uploaded
fubar
parents:
diff changeset
75 # requires highly insecure docker settings - like write to tool_conf.xml and to tools !
2a46da701dde Uploaded
fubar
parents:
diff changeset
76 # if in a container possibly not so courageous.
2a46da701dde Uploaded
fubar
parents:
diff changeset
77 # Fine on your own laptop but security red flag for most production instances
2a46da701dde Uploaded
fubar
parents:
diff changeset
78
5
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
79 def __init__(self, args, tool_conf_path, new_tool_archive_path, new_tool_name, tool_dir):
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
80 self.args = args
5
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
81 self.tool_conf_path = os.path.join(args.galaxy_root,tool_conf_path)
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
82 self.tool_dir = os.path.join(args.galaxy_root, tool_dir)
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
83 self.our_name = 'ToolFactory'
2a46da701dde Uploaded
fubar
parents:
diff changeset
84 tff = tarfile.open(new_tool_archive_path, "r:*")
2a46da701dde Uploaded
fubar
parents:
diff changeset
85 flist = tff.getnames()
2a46da701dde Uploaded
fubar
parents:
diff changeset
86 ourdir = os.path.commonpath(flist) # eg pyrevpos
2a46da701dde Uploaded
fubar
parents:
diff changeset
87 self.tool_id = ourdir # they are the same for TF tools
2a46da701dde Uploaded
fubar
parents:
diff changeset
88 ourxml = [x for x in flist if x.lower().endswith('.xml')]
5
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
89 res = tff.extractall()
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
90 tff.close()
5
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
91 self.run_rsync(ourdir, self.tool_dir)
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
92 self.update_toolconf(ourdir,ourxml)
2a46da701dde Uploaded
fubar
parents:
diff changeset
93
5
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
94 def run_rsync(self, srcf, dstf):
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
95 src = os.path.abspath(srcf)
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
96 dst = os.path.abspath(dstf)
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
97 if os.path.isdir(src):
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
98 cll = ['rsync', '-vr', src, dst]
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
99 else:
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
100 cll = ['rsync', '-v', src, dst]
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
101 p = subprocess.run(
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
102 cll,
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
103 capture_output=False,
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
104 encoding='utf8',
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
105 shell=False,
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
106 )
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
107
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
108 def install_deps(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
109 gi = galaxy.GalaxyInstance(url=self.args.galaxy_url, key=self.args.galaxy_api_key)
2a46da701dde Uploaded
fubar
parents:
diff changeset
110 x = gi.tools.install_dependencies(self.tool_id)
2a46da701dde Uploaded
fubar
parents:
diff changeset
111 print(f"Called install_dependencies on {self.tool_id} - got {x}")
2a46da701dde Uploaded
fubar
parents:
diff changeset
112
2a46da701dde Uploaded
fubar
parents:
diff changeset
113 def update_toolconf(self,ourdir,ourxml): # path is relative to tools
2a46da701dde Uploaded
fubar
parents:
diff changeset
114 updated = False
5
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
115 localconf = './local_tool_conf.xml'
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
116 self.run_rsync(self.tool_conf_path,localconf)
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
117 tree = ET.parse(localconf)
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
118 root = tree.getroot()
2a46da701dde Uploaded
fubar
parents:
diff changeset
119 hasTF = False
2a46da701dde Uploaded
fubar
parents:
diff changeset
120 TFsection = None
2a46da701dde Uploaded
fubar
parents:
diff changeset
121 for e in root.findall('section'):
2a46da701dde Uploaded
fubar
parents:
diff changeset
122 if e.attrib['name'] == self.our_name:
2a46da701dde Uploaded
fubar
parents:
diff changeset
123 hasTF = True
2a46da701dde Uploaded
fubar
parents:
diff changeset
124 TFsection = e
2a46da701dde Uploaded
fubar
parents:
diff changeset
125 if not hasTF:
2a46da701dde Uploaded
fubar
parents:
diff changeset
126 TFsection = ET.Element('section')
2a46da701dde Uploaded
fubar
parents:
diff changeset
127 root.insert(0,TFsection) # at the top!
2a46da701dde Uploaded
fubar
parents:
diff changeset
128 our_tools = TFsection.findall('tool')
2a46da701dde Uploaded
fubar
parents:
diff changeset
129 conf_tools = [x.attrib['file'] for x in our_tools]
2a46da701dde Uploaded
fubar
parents:
diff changeset
130 for xml in ourxml: # may be > 1
2a46da701dde Uploaded
fubar
parents:
diff changeset
131 if not xml in conf_tools: # new
2a46da701dde Uploaded
fubar
parents:
diff changeset
132 updated = True
2a46da701dde Uploaded
fubar
parents:
diff changeset
133 ET.SubElement(TFsection, 'tool', {'file':xml})
2a46da701dde Uploaded
fubar
parents:
diff changeset
134 ET.indent(tree)
5
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
135 newconf = f"{self.tool_id}_conf"
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
136 tree.write(newconf, pretty_print=True)
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
137 self.run_rsync(newconf,self.tool_conf_path)
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
138 if False and self.args.packages and self.args.packages > '':
2a46da701dde Uploaded
fubar
parents:
diff changeset
139 self.install_deps()
2a46da701dde Uploaded
fubar
parents:
diff changeset
140
2a46da701dde Uploaded
fubar
parents:
diff changeset
141 class ScriptRunner:
2a46da701dde Uploaded
fubar
parents:
diff changeset
142 """Wrapper for an arbitrary script
2a46da701dde Uploaded
fubar
parents:
diff changeset
143 uses galaxyxml
2a46da701dde Uploaded
fubar
parents:
diff changeset
144
2a46da701dde Uploaded
fubar
parents:
diff changeset
145 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
146
2a46da701dde Uploaded
fubar
parents:
diff changeset
147 def __init__(self, args=None): # noqa
2a46da701dde Uploaded
fubar
parents:
diff changeset
148 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
149 prepare command line cl for running the tool here
2a46da701dde Uploaded
fubar
parents:
diff changeset
150 and prepare elements needed for galaxyxml tool generation
2a46da701dde Uploaded
fubar
parents:
diff changeset
151 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
152 self.ourcwd = os.getcwd()
2a46da701dde Uploaded
fubar
parents:
diff changeset
153 self.collections = []
2a46da701dde Uploaded
fubar
parents:
diff changeset
154 if len(args.collection) > 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
155 try:
2a46da701dde Uploaded
fubar
parents:
diff changeset
156 self.collections = [
2a46da701dde Uploaded
fubar
parents:
diff changeset
157 json.loads(x) for x in args.collection if len(x.strip()) > 1
2a46da701dde Uploaded
fubar
parents:
diff changeset
158 ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
159 except Exception:
2a46da701dde Uploaded
fubar
parents:
diff changeset
160 print(
2a46da701dde Uploaded
fubar
parents:
diff changeset
161 f"--collections parameter {str(args.collection)} is malformed - should be a dictionary"
2a46da701dde Uploaded
fubar
parents:
diff changeset
162 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
163 try:
2a46da701dde Uploaded
fubar
parents:
diff changeset
164 self.infiles = [
2a46da701dde Uploaded
fubar
parents:
diff changeset
165 json.loads(x) for x in args.input_files if len(x.strip()) > 1
2a46da701dde Uploaded
fubar
parents:
diff changeset
166 ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
167 except Exception:
2a46da701dde Uploaded
fubar
parents:
diff changeset
168 print(
2a46da701dde Uploaded
fubar
parents:
diff changeset
169 f"--input_files parameter {str(args.input_files)} is malformed - should be a dictionary"
2a46da701dde Uploaded
fubar
parents:
diff changeset
170 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
171 try:
2a46da701dde Uploaded
fubar
parents:
diff changeset
172 self.outfiles = [
2a46da701dde Uploaded
fubar
parents:
diff changeset
173 json.loads(x) for x in args.output_files if len(x.strip()) > 1
2a46da701dde Uploaded
fubar
parents:
diff changeset
174 ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
175 except Exception:
2a46da701dde Uploaded
fubar
parents:
diff changeset
176 print(
2a46da701dde Uploaded
fubar
parents:
diff changeset
177 f"--output_files parameter {args.output_files} is malformed - should be a dictionary"
2a46da701dde Uploaded
fubar
parents:
diff changeset
178 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
179 try:
2a46da701dde Uploaded
fubar
parents:
diff changeset
180 self.addpar = [
2a46da701dde Uploaded
fubar
parents:
diff changeset
181 json.loads(x) for x in args.additional_parameters if len(x.strip()) > 1
2a46da701dde Uploaded
fubar
parents:
diff changeset
182 ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
183 except Exception:
2a46da701dde Uploaded
fubar
parents:
diff changeset
184 print(
2a46da701dde Uploaded
fubar
parents:
diff changeset
185 f"--additional_parameters {args.additional_parameters} is malformed - should be a dictionary"
2a46da701dde Uploaded
fubar
parents:
diff changeset
186 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
187 try:
2a46da701dde Uploaded
fubar
parents:
diff changeset
188 self.selpar = [
2a46da701dde Uploaded
fubar
parents:
diff changeset
189 json.loads(x) for x in args.selecttext_parameters if len(x.strip()) > 1
2a46da701dde Uploaded
fubar
parents:
diff changeset
190 ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
191 except Exception:
2a46da701dde Uploaded
fubar
parents:
diff changeset
192 print(
2a46da701dde Uploaded
fubar
parents:
diff changeset
193 f"--selecttext_parameters {args.selecttext_parameters} is malformed - should be a dictionary"
2a46da701dde Uploaded
fubar
parents:
diff changeset
194 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
195 self.args = args
2a46da701dde Uploaded
fubar
parents:
diff changeset
196 self.cleanuppar()
2a46da701dde Uploaded
fubar
parents:
diff changeset
197 self.lastclredirect = None
2a46da701dde Uploaded
fubar
parents:
diff changeset
198 self.lastxclredirect = None
2a46da701dde Uploaded
fubar
parents:
diff changeset
199 self.cl = []
2a46da701dde Uploaded
fubar
parents:
diff changeset
200 self.xmlcl = []
2a46da701dde Uploaded
fubar
parents:
diff changeset
201 self.is_positional = self.args.parampass == "positional"
2a46da701dde Uploaded
fubar
parents:
diff changeset
202 if self.args.sysexe:
2a46da701dde Uploaded
fubar
parents:
diff changeset
203 if ' ' in self.args.sysexe:
2a46da701dde Uploaded
fubar
parents:
diff changeset
204 self.executeme = self.args.sysexe.split(' ')
2a46da701dde Uploaded
fubar
parents:
diff changeset
205 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
206 self.executeme = [self.args.sysexe, ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
207 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
208 if self.args.packages:
2a46da701dde Uploaded
fubar
parents:
diff changeset
209 self.executeme = [self.args.packages.split(",")[0].split(":")[0].strip(), ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
210 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
211 self.executeme = None
2a46da701dde Uploaded
fubar
parents:
diff changeset
212 aCL = self.cl.append
2a46da701dde Uploaded
fubar
parents:
diff changeset
213 aXCL = self.xmlcl.append
2a46da701dde Uploaded
fubar
parents:
diff changeset
214 assert args.parampass in [
2a46da701dde Uploaded
fubar
parents:
diff changeset
215 "0",
2a46da701dde Uploaded
fubar
parents:
diff changeset
216 "argparse",
2a46da701dde Uploaded
fubar
parents:
diff changeset
217 "positional",
2a46da701dde Uploaded
fubar
parents:
diff changeset
218 ], 'args.parampass must be "0","positional" or "argparse"'
2a46da701dde Uploaded
fubar
parents:
diff changeset
219 self.tool_name = re.sub("[^a-zA-Z0-9_]+", "", args.tool_name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
220 self.tool_id = self.tool_name
2a46da701dde Uploaded
fubar
parents:
diff changeset
221 self.newtool = gxt.Tool(
2a46da701dde Uploaded
fubar
parents:
diff changeset
222 self.tool_name,
2a46da701dde Uploaded
fubar
parents:
diff changeset
223 self.tool_id,
2a46da701dde Uploaded
fubar
parents:
diff changeset
224 self.args.tool_version,
2a46da701dde Uploaded
fubar
parents:
diff changeset
225 self.args.tool_desc,
2a46da701dde Uploaded
fubar
parents:
diff changeset
226 FAKEEXE,
2a46da701dde Uploaded
fubar
parents:
diff changeset
227 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
228 self.newtarpath = "%s_toolshed.gz" % self.tool_name
2a46da701dde Uploaded
fubar
parents:
diff changeset
229 self.tooloutdir = "./tfout"
2a46da701dde Uploaded
fubar
parents:
diff changeset
230 self.repdir = "./TF_run_report"
2a46da701dde Uploaded
fubar
parents:
diff changeset
231 self.testdir = os.path.join(self.tooloutdir, "test-data")
2a46da701dde Uploaded
fubar
parents:
diff changeset
232 if not os.path.exists(self.tooloutdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
233 os.mkdir(self.tooloutdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
234 if not os.path.exists(self.testdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
235 os.mkdir(self.testdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
236 if not os.path.exists(self.repdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
237 os.mkdir(self.repdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
238 self.tinputs = gxtp.Inputs()
2a46da701dde Uploaded
fubar
parents:
diff changeset
239 self.toutputs = gxtp.Outputs()
2a46da701dde Uploaded
fubar
parents:
diff changeset
240 self.testparam = []
2a46da701dde Uploaded
fubar
parents:
diff changeset
241 if self.args.script_path:
2a46da701dde Uploaded
fubar
parents:
diff changeset
242 self.prepScript()
2a46da701dde Uploaded
fubar
parents:
diff changeset
243 if self.args.command_override:
2a46da701dde Uploaded
fubar
parents:
diff changeset
244 scos = open(self.args.command_override, "r").readlines()
2a46da701dde Uploaded
fubar
parents:
diff changeset
245 self.command_override = [x.rstrip() for x in scos]
2a46da701dde Uploaded
fubar
parents:
diff changeset
246 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
247 self.command_override = None
2a46da701dde Uploaded
fubar
parents:
diff changeset
248 if self.args.test_override:
2a46da701dde Uploaded
fubar
parents:
diff changeset
249 stos = open(self.args.test_override, "r").readlines()
2a46da701dde Uploaded
fubar
parents:
diff changeset
250 self.test_override = [x.rstrip() for x in stos]
2a46da701dde Uploaded
fubar
parents:
diff changeset
251 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
252 self.test_override = None
2a46da701dde Uploaded
fubar
parents:
diff changeset
253 if self.args.script_path:
2a46da701dde Uploaded
fubar
parents:
diff changeset
254 for ex in self.executeme:
2a46da701dde Uploaded
fubar
parents:
diff changeset
255 aCL(ex)
2a46da701dde Uploaded
fubar
parents:
diff changeset
256 aXCL(ex)
2a46da701dde Uploaded
fubar
parents:
diff changeset
257 aCL(self.sfile)
2a46da701dde Uploaded
fubar
parents:
diff changeset
258 aXCL("$runme")
2a46da701dde Uploaded
fubar
parents:
diff changeset
259 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
260 for ex in self.executeme:
2a46da701dde Uploaded
fubar
parents:
diff changeset
261 aCL(ex)
2a46da701dde Uploaded
fubar
parents:
diff changeset
262 aXCL(ex)
2a46da701dde Uploaded
fubar
parents:
diff changeset
263
2a46da701dde Uploaded
fubar
parents:
diff changeset
264 if self.args.parampass == "0":
2a46da701dde Uploaded
fubar
parents:
diff changeset
265 self.clsimple()
2a46da701dde Uploaded
fubar
parents:
diff changeset
266 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
267 if self.args.parampass == "positional":
2a46da701dde Uploaded
fubar
parents:
diff changeset
268 self.prepclpos()
2a46da701dde Uploaded
fubar
parents:
diff changeset
269 self.clpositional()
2a46da701dde Uploaded
fubar
parents:
diff changeset
270 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
271 self.prepargp()
2a46da701dde Uploaded
fubar
parents:
diff changeset
272 self.clargparse()
2a46da701dde Uploaded
fubar
parents:
diff changeset
273
2a46da701dde Uploaded
fubar
parents:
diff changeset
274 def clsimple(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
275 """no parameters or repeats - uses < and > for i/o"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
276 aCL = self.cl.append
2a46da701dde Uploaded
fubar
parents:
diff changeset
277 aXCL = self.xmlcl.append
2a46da701dde Uploaded
fubar
parents:
diff changeset
278 if len(self.infiles) > 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
279 aCL("<")
2a46da701dde Uploaded
fubar
parents:
diff changeset
280 aCL(self.infiles[0]["infilename"])
2a46da701dde Uploaded
fubar
parents:
diff changeset
281 aXCL("<")
2a46da701dde Uploaded
fubar
parents:
diff changeset
282 aXCL("$%s" % self.infiles[0]["infilename"])
2a46da701dde Uploaded
fubar
parents:
diff changeset
283 if len(self.outfiles) > 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
284 aCL(">")
2a46da701dde Uploaded
fubar
parents:
diff changeset
285 aCL(self.outfiles[0]["name"])
2a46da701dde Uploaded
fubar
parents:
diff changeset
286 aXCL(">")
2a46da701dde Uploaded
fubar
parents:
diff changeset
287 aXCL("$%s" % self.outfiles[0]["name"])
2a46da701dde Uploaded
fubar
parents:
diff changeset
288 if self.args.cl_user_suffix: # DIY CL end
2a46da701dde Uploaded
fubar
parents:
diff changeset
289 clp = shlex.split(self.args.cl_user_suffix)
2a46da701dde Uploaded
fubar
parents:
diff changeset
290 for c in clp:
2a46da701dde Uploaded
fubar
parents:
diff changeset
291 aCL(c)
2a46da701dde Uploaded
fubar
parents:
diff changeset
292 aXCL(c)
2a46da701dde Uploaded
fubar
parents:
diff changeset
293
2a46da701dde Uploaded
fubar
parents:
diff changeset
294 def prepargp(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
295 clsuffix = []
2a46da701dde Uploaded
fubar
parents:
diff changeset
296 xclsuffix = []
2a46da701dde Uploaded
fubar
parents:
diff changeset
297 for i, p in enumerate(self.infiles):
2a46da701dde Uploaded
fubar
parents:
diff changeset
298 nam = p["infilename"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
299 if p["origCL"].strip().upper() == "STDIN":
2a46da701dde Uploaded
fubar
parents:
diff changeset
300 appendme = [
2a46da701dde Uploaded
fubar
parents:
diff changeset
301 nam,
2a46da701dde Uploaded
fubar
parents:
diff changeset
302 nam,
2a46da701dde Uploaded
fubar
parents:
diff changeset
303 "< %s" % nam,
2a46da701dde Uploaded
fubar
parents:
diff changeset
304 ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
305 xappendme = [
2a46da701dde Uploaded
fubar
parents:
diff changeset
306 nam,
2a46da701dde Uploaded
fubar
parents:
diff changeset
307 nam,
2a46da701dde Uploaded
fubar
parents:
diff changeset
308 "< $%s" % nam,
2a46da701dde Uploaded
fubar
parents:
diff changeset
309 ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
310 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
311 rep = p["repeat"] == "1"
2a46da701dde Uploaded
fubar
parents:
diff changeset
312 over = ""
2a46da701dde Uploaded
fubar
parents:
diff changeset
313 if rep:
2a46da701dde Uploaded
fubar
parents:
diff changeset
314 over = f'#for $rep in $R_{nam}:\n--{nam} "$rep.{nam}"\n#end for'
2a46da701dde Uploaded
fubar
parents:
diff changeset
315 appendme = [p["CL"], p["CL"], ""]
2a46da701dde Uploaded
fubar
parents:
diff changeset
316 xappendme = [p["CL"], "$%s" % p["CL"], over]
2a46da701dde Uploaded
fubar
parents:
diff changeset
317 clsuffix.append(appendme)
2a46da701dde Uploaded
fubar
parents:
diff changeset
318 xclsuffix.append(xappendme)
2a46da701dde Uploaded
fubar
parents:
diff changeset
319 for i, p in enumerate(self.outfiles):
2a46da701dde Uploaded
fubar
parents:
diff changeset
320 if p["origCL"].strip().upper() == "STDOUT":
2a46da701dde Uploaded
fubar
parents:
diff changeset
321 self.lastclredirect = [">", p["name"]]
2a46da701dde Uploaded
fubar
parents:
diff changeset
322 self.lastxclredirect = [">", "$%s" % p["name"]]
2a46da701dde Uploaded
fubar
parents:
diff changeset
323 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
324 clsuffix.append([p["name"], p["name"], ""])
2a46da701dde Uploaded
fubar
parents:
diff changeset
325 xclsuffix.append([p["name"], "$%s" % p["name"], ""])
2a46da701dde Uploaded
fubar
parents:
diff changeset
326 for p in self.addpar:
2a46da701dde Uploaded
fubar
parents:
diff changeset
327 nam = p["name"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
328 rep = p["repeat"] == "1"
2a46da701dde Uploaded
fubar
parents:
diff changeset
329 if rep:
2a46da701dde Uploaded
fubar
parents:
diff changeset
330 over = f'#for $rep in $R_{nam}:\n--{nam} "$rep.{nam}"\n#end for'
2a46da701dde Uploaded
fubar
parents:
diff changeset
331 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
332 over = p["override"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
333 clsuffix.append([p["CL"], nam, over])
2a46da701dde Uploaded
fubar
parents:
diff changeset
334 xclsuffix.append([p["CL"], '"$%s"' % nam, over])
2a46da701dde Uploaded
fubar
parents:
diff changeset
335 for p in self.selpar:
2a46da701dde Uploaded
fubar
parents:
diff changeset
336 clsuffix.append([p["CL"], p["name"], p["override"]])
2a46da701dde Uploaded
fubar
parents:
diff changeset
337 xclsuffix.append([p["CL"], '"$%s"' % p["name"], p["override"]])
2a46da701dde Uploaded
fubar
parents:
diff changeset
338 self.xclsuffix = xclsuffix
2a46da701dde Uploaded
fubar
parents:
diff changeset
339 self.clsuffix = clsuffix
2a46da701dde Uploaded
fubar
parents:
diff changeset
340
2a46da701dde Uploaded
fubar
parents:
diff changeset
341 def prepclpos(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
342 clsuffix = []
2a46da701dde Uploaded
fubar
parents:
diff changeset
343 xclsuffix = []
2a46da701dde Uploaded
fubar
parents:
diff changeset
344 for i, p in enumerate(self.infiles):
2a46da701dde Uploaded
fubar
parents:
diff changeset
345 if p["origCL"].strip().upper() == "STDIN":
2a46da701dde Uploaded
fubar
parents:
diff changeset
346 appendme = [
2a46da701dde Uploaded
fubar
parents:
diff changeset
347 "999",
2a46da701dde Uploaded
fubar
parents:
diff changeset
348 p["infilename"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
349 "< $%s" % p["infilename"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
350 ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
351 xappendme = [
2a46da701dde Uploaded
fubar
parents:
diff changeset
352 "999",
2a46da701dde Uploaded
fubar
parents:
diff changeset
353 p["infilename"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
354 "< $%s" % p["infilename"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
355 ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
356 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
357 appendme = [p["CL"], p["infilename"], ""]
2a46da701dde Uploaded
fubar
parents:
diff changeset
358 xappendme = [p["CL"], "$%s" % p["infilename"], ""]
2a46da701dde Uploaded
fubar
parents:
diff changeset
359 clsuffix.append(appendme)
2a46da701dde Uploaded
fubar
parents:
diff changeset
360 xclsuffix.append(xappendme)
2a46da701dde Uploaded
fubar
parents:
diff changeset
361 for i, p in enumerate(self.outfiles):
2a46da701dde Uploaded
fubar
parents:
diff changeset
362 if p["origCL"].strip().upper() == "STDOUT":
2a46da701dde Uploaded
fubar
parents:
diff changeset
363 self.lastclredirect = [">", p["name"]]
2a46da701dde Uploaded
fubar
parents:
diff changeset
364 self.lastxclredirect = [">", "$%s" % p["name"]]
2a46da701dde Uploaded
fubar
parents:
diff changeset
365 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
366 clsuffix.append([p["CL"], p["name"], ""])
2a46da701dde Uploaded
fubar
parents:
diff changeset
367 xclsuffix.append([p["CL"], "$%s" % p["name"], ""])
2a46da701dde Uploaded
fubar
parents:
diff changeset
368 for p in self.addpar:
2a46da701dde Uploaded
fubar
parents:
diff changeset
369 nam = p["name"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
370 rep = p["repeat"] == "1" # repeats make NO sense
2a46da701dde Uploaded
fubar
parents:
diff changeset
371 if rep:
2a46da701dde Uploaded
fubar
parents:
diff changeset
372 print(f'### warning. Repeats for {nam} ignored - not permitted in positional parameter command lines!')
2a46da701dde Uploaded
fubar
parents:
diff changeset
373 over = p["override"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
374 clsuffix.append([p["CL"], nam, over])
2a46da701dde Uploaded
fubar
parents:
diff changeset
375 xclsuffix.append([p["CL"], '"$%s"' % nam, over])
2a46da701dde Uploaded
fubar
parents:
diff changeset
376 for p in self.selpar:
2a46da701dde Uploaded
fubar
parents:
diff changeset
377 clsuffix.append([p["CL"], p["name"], p["override"]])
2a46da701dde Uploaded
fubar
parents:
diff changeset
378 xclsuffix.append([p["CL"], '"$%s"' % p["name"], p["override"]])
2a46da701dde Uploaded
fubar
parents:
diff changeset
379 clsuffix.sort()
2a46da701dde Uploaded
fubar
parents:
diff changeset
380 xclsuffix.sort()
2a46da701dde Uploaded
fubar
parents:
diff changeset
381 self.xclsuffix = xclsuffix
2a46da701dde Uploaded
fubar
parents:
diff changeset
382 self.clsuffix = clsuffix
2a46da701dde Uploaded
fubar
parents:
diff changeset
383
2a46da701dde Uploaded
fubar
parents:
diff changeset
384 def prepScript(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
385 rx = open(self.args.script_path, "r").readlines()
2a46da701dde Uploaded
fubar
parents:
diff changeset
386 rx = [x.rstrip() for x in rx]
2a46da701dde Uploaded
fubar
parents:
diff changeset
387 rxcheck = [x.strip() for x in rx if x.strip() > ""]
2a46da701dde Uploaded
fubar
parents:
diff changeset
388 assert len(rxcheck) > 0, "Supplied script is empty. Cannot run"
2a46da701dde Uploaded
fubar
parents:
diff changeset
389 self.script = "\n".join(rx)
2a46da701dde Uploaded
fubar
parents:
diff changeset
390 fhandle, self.sfile = tempfile.mkstemp(
2a46da701dde Uploaded
fubar
parents:
diff changeset
391 prefix=self.tool_name, suffix="_%s" % (self.executeme[0])
2a46da701dde Uploaded
fubar
parents:
diff changeset
392 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
393 tscript = open(self.sfile, "w")
2a46da701dde Uploaded
fubar
parents:
diff changeset
394 tscript.write(self.script)
2a46da701dde Uploaded
fubar
parents:
diff changeset
395 tscript.close()
2a46da701dde Uploaded
fubar
parents:
diff changeset
396 self.spacedScript = [f" {x}" for x in rx if x.strip() > ""]
2a46da701dde Uploaded
fubar
parents:
diff changeset
397 rx.insert(0,'#raw')
2a46da701dde Uploaded
fubar
parents:
diff changeset
398 rx.append('#end raw')
2a46da701dde Uploaded
fubar
parents:
diff changeset
399 self.escapedScript = rx
2a46da701dde Uploaded
fubar
parents:
diff changeset
400 art = "%s.%s" % (self.tool_name, self.executeme[0])
2a46da701dde Uploaded
fubar
parents:
diff changeset
401 artifact = open(art, "wb")
2a46da701dde Uploaded
fubar
parents:
diff changeset
402 artifact.write(bytes(self.script, "utf8"))
2a46da701dde Uploaded
fubar
parents:
diff changeset
403 artifact.close()
2a46da701dde Uploaded
fubar
parents:
diff changeset
404
2a46da701dde Uploaded
fubar
parents:
diff changeset
405 def cleanuppar(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
406 """ positional parameters are complicated by their numeric ordinal"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
407 if self.args.parampass == "positional":
2a46da701dde Uploaded
fubar
parents:
diff changeset
408 for i, p in enumerate(self.infiles):
2a46da701dde Uploaded
fubar
parents:
diff changeset
409 assert (
2a46da701dde Uploaded
fubar
parents:
diff changeset
410 p["CL"].isdigit() or p["CL"].strip().upper() == "STDIN"
2a46da701dde Uploaded
fubar
parents:
diff changeset
411 ), "Positional parameters must be ordinal integers - got %s for %s" % (
2a46da701dde Uploaded
fubar
parents:
diff changeset
412 p["CL"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
413 p["label"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
414 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
415 for i, p in enumerate(self.outfiles):
2a46da701dde Uploaded
fubar
parents:
diff changeset
416 assert (
2a46da701dde Uploaded
fubar
parents:
diff changeset
417 p["CL"].isdigit() or p["CL"].strip().upper() == "STDOUT"
2a46da701dde Uploaded
fubar
parents:
diff changeset
418 ), "Positional parameters must be ordinal integers - got %s for %s" % (
2a46da701dde Uploaded
fubar
parents:
diff changeset
419 p["CL"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
420 p["name"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
421 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
422 for i, p in enumerate(self.addpar):
2a46da701dde Uploaded
fubar
parents:
diff changeset
423 assert p[
2a46da701dde Uploaded
fubar
parents:
diff changeset
424 "CL"
2a46da701dde Uploaded
fubar
parents:
diff changeset
425 ].isdigit(), "Positional parameters must be ordinal integers - got %s for %s" % (
2a46da701dde Uploaded
fubar
parents:
diff changeset
426 p["CL"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
427 p["name"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
428 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
429 for i, p in enumerate(self.infiles):
2a46da701dde Uploaded
fubar
parents:
diff changeset
430 infp = copy.copy(p)
2a46da701dde Uploaded
fubar
parents:
diff changeset
431 infp["origCL"] = infp["CL"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
432 if self.args.parampass in ["positional", "0"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
433 infp["infilename"] = infp["label"].replace(" ", "_")
2a46da701dde Uploaded
fubar
parents:
diff changeset
434 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
435 infp["infilename"] = infp["CL"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
436 self.infiles[i] = infp
2a46da701dde Uploaded
fubar
parents:
diff changeset
437 for i, p in enumerate(self.outfiles):
2a46da701dde Uploaded
fubar
parents:
diff changeset
438 p["origCL"] = p["CL"] # keep copy
2a46da701dde Uploaded
fubar
parents:
diff changeset
439 self.outfiles[i] = p
2a46da701dde Uploaded
fubar
parents:
diff changeset
440 for i, p in enumerate(self.addpar):
2a46da701dde Uploaded
fubar
parents:
diff changeset
441 p["origCL"] = p["CL"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
442 self.addpar[i] = p
2a46da701dde Uploaded
fubar
parents:
diff changeset
443
2a46da701dde Uploaded
fubar
parents:
diff changeset
444 def clpositional(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
445 # inputs in order then params
2a46da701dde Uploaded
fubar
parents:
diff changeset
446 aCL = self.cl.append
2a46da701dde Uploaded
fubar
parents:
diff changeset
447 for (k, v, koverride) in self.clsuffix:
2a46da701dde Uploaded
fubar
parents:
diff changeset
448 if " " in v:
2a46da701dde Uploaded
fubar
parents:
diff changeset
449 aCL("%s" % v)
2a46da701dde Uploaded
fubar
parents:
diff changeset
450 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
451 aCL(v)
2a46da701dde Uploaded
fubar
parents:
diff changeset
452 aXCL = self.xmlcl.append
2a46da701dde Uploaded
fubar
parents:
diff changeset
453 for (k, v, koverride) in self.xclsuffix:
2a46da701dde Uploaded
fubar
parents:
diff changeset
454 aXCL(v)
2a46da701dde Uploaded
fubar
parents:
diff changeset
455 if self.lastxclredirect:
2a46da701dde Uploaded
fubar
parents:
diff changeset
456 aXCL(self.lastxclredirect[0])
2a46da701dde Uploaded
fubar
parents:
diff changeset
457 aXCL(self.lastxclredirect[1])
2a46da701dde Uploaded
fubar
parents:
diff changeset
458 if self.args.cl_user_suffix: # DIY CL end
2a46da701dde Uploaded
fubar
parents:
diff changeset
459 clp = shlex.split(self.args.cl_user_suffix)
2a46da701dde Uploaded
fubar
parents:
diff changeset
460 for c in clp:
2a46da701dde Uploaded
fubar
parents:
diff changeset
461 aCL(c)
2a46da701dde Uploaded
fubar
parents:
diff changeset
462 aXCL(c)
2a46da701dde Uploaded
fubar
parents:
diff changeset
463
2a46da701dde Uploaded
fubar
parents:
diff changeset
464
2a46da701dde Uploaded
fubar
parents:
diff changeset
465 def clargparse(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
466 """argparse style"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
467 aCL = self.cl.append
2a46da701dde Uploaded
fubar
parents:
diff changeset
468 aXCL = self.xmlcl.append
2a46da701dde Uploaded
fubar
parents:
diff changeset
469 # inputs then params in argparse named form
2a46da701dde Uploaded
fubar
parents:
diff changeset
470
2a46da701dde Uploaded
fubar
parents:
diff changeset
471 for (k, v, koverride) in self.xclsuffix:
2a46da701dde Uploaded
fubar
parents:
diff changeset
472 if koverride > "":
2a46da701dde Uploaded
fubar
parents:
diff changeset
473 k = koverride
2a46da701dde Uploaded
fubar
parents:
diff changeset
474 aXCL(k)
2a46da701dde Uploaded
fubar
parents:
diff changeset
475 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
476 if len(k.strip()) == 1:
2a46da701dde Uploaded
fubar
parents:
diff changeset
477 k = "-%s" % k
2a46da701dde Uploaded
fubar
parents:
diff changeset
478 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
479 k = "--%s" % k
2a46da701dde Uploaded
fubar
parents:
diff changeset
480 aXCL(k)
2a46da701dde Uploaded
fubar
parents:
diff changeset
481 aXCL(v)
2a46da701dde Uploaded
fubar
parents:
diff changeset
482 for (k, v, koverride) in self.clsuffix:
2a46da701dde Uploaded
fubar
parents:
diff changeset
483 if koverride > "":
2a46da701dde Uploaded
fubar
parents:
diff changeset
484 k = koverride
2a46da701dde Uploaded
fubar
parents:
diff changeset
485 elif len(k.strip()) == 1:
2a46da701dde Uploaded
fubar
parents:
diff changeset
486 k = "-%s" % k
2a46da701dde Uploaded
fubar
parents:
diff changeset
487 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
488 k = "--%s" % k
2a46da701dde Uploaded
fubar
parents:
diff changeset
489 aCL(k)
2a46da701dde Uploaded
fubar
parents:
diff changeset
490 aCL(v)
2a46da701dde Uploaded
fubar
parents:
diff changeset
491 if self.lastxclredirect:
2a46da701dde Uploaded
fubar
parents:
diff changeset
492 aXCL(self.lastxclredirect[0])
2a46da701dde Uploaded
fubar
parents:
diff changeset
493 aXCL(self.lastxclredirect[1])
2a46da701dde Uploaded
fubar
parents:
diff changeset
494 if self.args.cl_user_suffix: # DIY CL end
2a46da701dde Uploaded
fubar
parents:
diff changeset
495 clp = shlex.split(self.args.cl_user_suffix)
2a46da701dde Uploaded
fubar
parents:
diff changeset
496 for c in clp:
2a46da701dde Uploaded
fubar
parents:
diff changeset
497 aCL(c)
2a46da701dde Uploaded
fubar
parents:
diff changeset
498 aXCL(c)
2a46da701dde Uploaded
fubar
parents:
diff changeset
499
2a46da701dde Uploaded
fubar
parents:
diff changeset
500 def getNdash(self, newname):
2a46da701dde Uploaded
fubar
parents:
diff changeset
501 if self.is_positional:
2a46da701dde Uploaded
fubar
parents:
diff changeset
502 ndash = 0
2a46da701dde Uploaded
fubar
parents:
diff changeset
503 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
504 ndash = 2
2a46da701dde Uploaded
fubar
parents:
diff changeset
505 if len(newname) < 2:
2a46da701dde Uploaded
fubar
parents:
diff changeset
506 ndash = 1
2a46da701dde Uploaded
fubar
parents:
diff changeset
507 return ndash
2a46da701dde Uploaded
fubar
parents:
diff changeset
508
2a46da701dde Uploaded
fubar
parents:
diff changeset
509 def doXMLparam(self): # noqa
2a46da701dde Uploaded
fubar
parents:
diff changeset
510 """Add all needed elements to tool"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
511 for p in self.outfiles:
2a46da701dde Uploaded
fubar
parents:
diff changeset
512 newname = p["name"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
513 newfmt = p["format"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
514 newcl = p["CL"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
515 test = p["test"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
516 oldcl = p["origCL"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
517 test = test.strip()
2a46da701dde Uploaded
fubar
parents:
diff changeset
518 ndash = self.getNdash(newcl)
2a46da701dde Uploaded
fubar
parents:
diff changeset
519 aparm = gxtp.OutputData(
2a46da701dde Uploaded
fubar
parents:
diff changeset
520 name=newname, format=newfmt, num_dashes=ndash, label=newname
2a46da701dde Uploaded
fubar
parents:
diff changeset
521 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
522 aparm.positional = self.is_positional
2a46da701dde Uploaded
fubar
parents:
diff changeset
523 if self.is_positional:
2a46da701dde Uploaded
fubar
parents:
diff changeset
524 if oldcl.upper() == "STDOUT":
2a46da701dde Uploaded
fubar
parents:
diff changeset
525 aparm.positional = 9999999
2a46da701dde Uploaded
fubar
parents:
diff changeset
526 aparm.command_line_override = "> $%s" % newname
2a46da701dde Uploaded
fubar
parents:
diff changeset
527 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
528 aparm.positional = int(oldcl)
2a46da701dde Uploaded
fubar
parents:
diff changeset
529 aparm.command_line_override = "$%s" % newname
2a46da701dde Uploaded
fubar
parents:
diff changeset
530 self.toutputs.append(aparm)
2a46da701dde Uploaded
fubar
parents:
diff changeset
531 ld = None
2a46da701dde Uploaded
fubar
parents:
diff changeset
532 if test.strip() > "":
2a46da701dde Uploaded
fubar
parents:
diff changeset
533 if test.startswith("diff"):
2a46da701dde Uploaded
fubar
parents:
diff changeset
534 c = "diff"
2a46da701dde Uploaded
fubar
parents:
diff changeset
535 ld = 0
2a46da701dde Uploaded
fubar
parents:
diff changeset
536 if test.split(":")[1].isdigit:
2a46da701dde Uploaded
fubar
parents:
diff changeset
537 ld = int(test.split(":")[1])
2a46da701dde Uploaded
fubar
parents:
diff changeset
538 tp = gxtp.TestOutput(
2a46da701dde Uploaded
fubar
parents:
diff changeset
539 name=newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
540 value="%s_sample" % newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
541 compare=c,
2a46da701dde Uploaded
fubar
parents:
diff changeset
542 lines_diff=ld,
2a46da701dde Uploaded
fubar
parents:
diff changeset
543 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
544 elif test.startswith("sim_size"):
2a46da701dde Uploaded
fubar
parents:
diff changeset
545 c = "sim_size"
2a46da701dde Uploaded
fubar
parents:
diff changeset
546 tn = test.split(":")[1].strip()
2a46da701dde Uploaded
fubar
parents:
diff changeset
547 if tn > "":
2a46da701dde Uploaded
fubar
parents:
diff changeset
548 if "." in tn:
2a46da701dde Uploaded
fubar
parents:
diff changeset
549 delta = None
2a46da701dde Uploaded
fubar
parents:
diff changeset
550 delta_frac = min(1.0, float(tn))
2a46da701dde Uploaded
fubar
parents:
diff changeset
551 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
552 delta = int(tn)
2a46da701dde Uploaded
fubar
parents:
diff changeset
553 delta_frac = None
2a46da701dde Uploaded
fubar
parents:
diff changeset
554 tp = gxtp.TestOutput(
2a46da701dde Uploaded
fubar
parents:
diff changeset
555 name=newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
556 value="%s_sample" % newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
557 compare=c,
2a46da701dde Uploaded
fubar
parents:
diff changeset
558 delta=delta,
2a46da701dde Uploaded
fubar
parents:
diff changeset
559 delta_frac=delta_frac,
2a46da701dde Uploaded
fubar
parents:
diff changeset
560 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
561 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
562 c = test
2a46da701dde Uploaded
fubar
parents:
diff changeset
563 tp = gxtp.TestOutput(
2a46da701dde Uploaded
fubar
parents:
diff changeset
564 name=newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
565 value="%s_sample" % newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
566 compare=c,
2a46da701dde Uploaded
fubar
parents:
diff changeset
567 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
568 self.testparam.append(tp)
2a46da701dde Uploaded
fubar
parents:
diff changeset
569 for p in self.infiles:
2a46da701dde Uploaded
fubar
parents:
diff changeset
570 newname = p["infilename"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
571 newfmt = p["format"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
572 ndash = self.getNdash(newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
573 reps = p.get("repeat", "0") == "1"
2a46da701dde Uploaded
fubar
parents:
diff changeset
574 if not len(p["label"]) > 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
575 alab = p["CL"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
576 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
577 alab = p["label"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
578 aninput = gxtp.DataParam(
2a46da701dde Uploaded
fubar
parents:
diff changeset
579 newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
580 optional=False,
2a46da701dde Uploaded
fubar
parents:
diff changeset
581 label=alab,
2a46da701dde Uploaded
fubar
parents:
diff changeset
582 help=p["help"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
583 format=newfmt,
2a46da701dde Uploaded
fubar
parents:
diff changeset
584 multiple=False,
2a46da701dde Uploaded
fubar
parents:
diff changeset
585 num_dashes=ndash,
2a46da701dde Uploaded
fubar
parents:
diff changeset
586 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
587 aninput.positional = self.is_positional
2a46da701dde Uploaded
fubar
parents:
diff changeset
588 if self.is_positional:
2a46da701dde Uploaded
fubar
parents:
diff changeset
589 if p["origCL"].upper() == "STDIN":
2a46da701dde Uploaded
fubar
parents:
diff changeset
590 aninput.positional = 9999998
2a46da701dde Uploaded
fubar
parents:
diff changeset
591 aninput.command_line_override = "> $%s" % newname
2a46da701dde Uploaded
fubar
parents:
diff changeset
592 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
593 aninput.positional = int(p["origCL"])
2a46da701dde Uploaded
fubar
parents:
diff changeset
594 aninput.command_line_override = "$%s" % newname
2a46da701dde Uploaded
fubar
parents:
diff changeset
595 if reps:
2a46da701dde Uploaded
fubar
parents:
diff changeset
596 repe = gxtp.Repeat(name=f"R_{newname}", title=f"Add as many {alab} as needed")
2a46da701dde Uploaded
fubar
parents:
diff changeset
597 repe.append(aninput)
2a46da701dde Uploaded
fubar
parents:
diff changeset
598 self.tinputs.append(repe)
2a46da701dde Uploaded
fubar
parents:
diff changeset
599 tparm = gxtp.TestRepeat(name=f"R_{newname}")
2a46da701dde Uploaded
fubar
parents:
diff changeset
600 tparm2 = gxtp.TestParam(newname, value="%s_sample" % newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
601 tparm.append(tparm2)
2a46da701dde Uploaded
fubar
parents:
diff changeset
602 self.testparam.append(tparm)
2a46da701dde Uploaded
fubar
parents:
diff changeset
603 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
604 self.tinputs.append(aninput)
2a46da701dde Uploaded
fubar
parents:
diff changeset
605 tparm = gxtp.TestParam(newname, value="%s_sample" % newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
606 self.testparam.append(tparm)
2a46da701dde Uploaded
fubar
parents:
diff changeset
607 for p in self.addpar:
2a46da701dde Uploaded
fubar
parents:
diff changeset
608 newname = p["name"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
609 newval = p["value"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
610 newlabel = p["label"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
611 newhelp = p["help"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
612 newtype = p["type"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
613 newcl = p["CL"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
614 oldcl = p["origCL"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
615 reps = p["repeat"] == "1"
2a46da701dde Uploaded
fubar
parents:
diff changeset
616 if not len(newlabel) > 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
617 newlabel = newname
2a46da701dde Uploaded
fubar
parents:
diff changeset
618 ndash = self.getNdash(newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
619 if newtype == "text":
2a46da701dde Uploaded
fubar
parents:
diff changeset
620 aparm = gxtp.TextParam(
2a46da701dde Uploaded
fubar
parents:
diff changeset
621 newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
622 label=newlabel,
2a46da701dde Uploaded
fubar
parents:
diff changeset
623 help=newhelp,
2a46da701dde Uploaded
fubar
parents:
diff changeset
624 value=newval,
2a46da701dde Uploaded
fubar
parents:
diff changeset
625 num_dashes=ndash,
2a46da701dde Uploaded
fubar
parents:
diff changeset
626 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
627 elif newtype == "integer":
2a46da701dde Uploaded
fubar
parents:
diff changeset
628 aparm = gxtp.IntegerParam(
2a46da701dde Uploaded
fubar
parents:
diff changeset
629 newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
630 label=newlabel,
2a46da701dde Uploaded
fubar
parents:
diff changeset
631 help=newhelp,
2a46da701dde Uploaded
fubar
parents:
diff changeset
632 value=newval,
2a46da701dde Uploaded
fubar
parents:
diff changeset
633 num_dashes=ndash,
2a46da701dde Uploaded
fubar
parents:
diff changeset
634 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
635 elif newtype == "float":
2a46da701dde Uploaded
fubar
parents:
diff changeset
636 aparm = gxtp.FloatParam(
2a46da701dde Uploaded
fubar
parents:
diff changeset
637 newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
638 label=newlabel,
2a46da701dde Uploaded
fubar
parents:
diff changeset
639 help=newhelp,
2a46da701dde Uploaded
fubar
parents:
diff changeset
640 value=newval,
2a46da701dde Uploaded
fubar
parents:
diff changeset
641 num_dashes=ndash,
2a46da701dde Uploaded
fubar
parents:
diff changeset
642 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
643 elif newtype == "boolean":
2a46da701dde Uploaded
fubar
parents:
diff changeset
644 aparm = gxtp.BooleanParam(
2a46da701dde Uploaded
fubar
parents:
diff changeset
645 newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
646 label=newlabel,
2a46da701dde Uploaded
fubar
parents:
diff changeset
647 help=newhelp,
2a46da701dde Uploaded
fubar
parents:
diff changeset
648 value=newval,
2a46da701dde Uploaded
fubar
parents:
diff changeset
649 num_dashes=ndash,
2a46da701dde Uploaded
fubar
parents:
diff changeset
650 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
651 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
652 raise ValueError(
2a46da701dde Uploaded
fubar
parents:
diff changeset
653 'Unrecognised parameter type "%s" for\
2a46da701dde Uploaded
fubar
parents:
diff changeset
654 additional parameter %s in makeXML'
2a46da701dde Uploaded
fubar
parents:
diff changeset
655 % (newtype, newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
656 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
657 aparm.positional = self.is_positional
2a46da701dde Uploaded
fubar
parents:
diff changeset
658 if self.is_positional:
2a46da701dde Uploaded
fubar
parents:
diff changeset
659 aparm.positional = int(oldcl)
2a46da701dde Uploaded
fubar
parents:
diff changeset
660 if reps:
2a46da701dde Uploaded
fubar
parents:
diff changeset
661 repe = gxtp.Repeat(name=f"R_{newname}", title=f"Add as many {newlabel} as needed")
2a46da701dde Uploaded
fubar
parents:
diff changeset
662 repe.append(aparm)
2a46da701dde Uploaded
fubar
parents:
diff changeset
663 self.tinputs.append(repe)
2a46da701dde Uploaded
fubar
parents:
diff changeset
664 tparm = gxtp.TestRepeat(name=f"R_{newname}")
2a46da701dde Uploaded
fubar
parents:
diff changeset
665 tparm2 = gxtp.TestParam(newname, value=newval)
2a46da701dde Uploaded
fubar
parents:
diff changeset
666 tparm.append(tparm2)
2a46da701dde Uploaded
fubar
parents:
diff changeset
667 self.testparam.append(tparm)
2a46da701dde Uploaded
fubar
parents:
diff changeset
668 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
669 self.tinputs.append(aparm)
2a46da701dde Uploaded
fubar
parents:
diff changeset
670 tparm = gxtp.TestParam(newname, value=newval)
2a46da701dde Uploaded
fubar
parents:
diff changeset
671 self.testparam.append(tparm)
2a46da701dde Uploaded
fubar
parents:
diff changeset
672 for p in self.selpar:
2a46da701dde Uploaded
fubar
parents:
diff changeset
673 newname = p["name"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
674 newval = p["value"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
675 newlabel = p["label"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
676 newhelp = p["help"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
677 newtype = p["type"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
678 newcl = p["CL"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
679 if not len(newlabel) > 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
680 newlabel = newname
2a46da701dde Uploaded
fubar
parents:
diff changeset
681 ndash = self.getNdash(newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
682 if newtype == "selecttext":
2a46da701dde Uploaded
fubar
parents:
diff changeset
683 newtext = p["texts"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
684 aparm = gxtp.SelectParam(
2a46da701dde Uploaded
fubar
parents:
diff changeset
685 newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
686 label=newlabel,
2a46da701dde Uploaded
fubar
parents:
diff changeset
687 help=newhelp,
2a46da701dde Uploaded
fubar
parents:
diff changeset
688 num_dashes=ndash,
2a46da701dde Uploaded
fubar
parents:
diff changeset
689 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
690 for i in range(len(newval)):
2a46da701dde Uploaded
fubar
parents:
diff changeset
691 anopt = gxtp.SelectOption(
2a46da701dde Uploaded
fubar
parents:
diff changeset
692 value=newval[i],
2a46da701dde Uploaded
fubar
parents:
diff changeset
693 text=newtext[i],
2a46da701dde Uploaded
fubar
parents:
diff changeset
694 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
695 aparm.append(anopt)
2a46da701dde Uploaded
fubar
parents:
diff changeset
696 aparm.positional = self.is_positional
2a46da701dde Uploaded
fubar
parents:
diff changeset
697 if self.is_positional:
2a46da701dde Uploaded
fubar
parents:
diff changeset
698 aparm.positional = int(newcl)
2a46da701dde Uploaded
fubar
parents:
diff changeset
699 self.tinputs.append(aparm)
2a46da701dde Uploaded
fubar
parents:
diff changeset
700 tparm = gxtp.TestParam(newname, value=newval)
2a46da701dde Uploaded
fubar
parents:
diff changeset
701 self.testparam.append(tparm)
2a46da701dde Uploaded
fubar
parents:
diff changeset
702 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
703 raise ValueError(
2a46da701dde Uploaded
fubar
parents:
diff changeset
704 'Unrecognised parameter type "%s" for\
2a46da701dde Uploaded
fubar
parents:
diff changeset
705 selecttext parameter %s in makeXML'
2a46da701dde Uploaded
fubar
parents:
diff changeset
706 % (newtype, newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
707 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
708 for p in self.collections:
2a46da701dde Uploaded
fubar
parents:
diff changeset
709 newkind = p["kind"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
710 newname = p["name"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
711 newlabel = p["label"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
712 newdisc = p["discover"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
713 collect = gxtp.OutputCollection(newname, label=newlabel, type=newkind)
2a46da701dde Uploaded
fubar
parents:
diff changeset
714 disc = gxtp.DiscoverDatasets(
2a46da701dde Uploaded
fubar
parents:
diff changeset
715 pattern=newdisc, directory=f"{newname}", visible="false"
2a46da701dde Uploaded
fubar
parents:
diff changeset
716 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
717 collect.append(disc)
2a46da701dde Uploaded
fubar
parents:
diff changeset
718 self.toutputs.append(collect)
2a46da701dde Uploaded
fubar
parents:
diff changeset
719 try:
2a46da701dde Uploaded
fubar
parents:
diff changeset
720 tparm = gxtp.TestOutputCollection(newname) # broken until PR merged.
2a46da701dde Uploaded
fubar
parents:
diff changeset
721 self.testparam.append(tparm)
2a46da701dde Uploaded
fubar
parents:
diff changeset
722 except Exception:
2a46da701dde Uploaded
fubar
parents:
diff changeset
723 print("#### WARNING: Galaxyxml version does not have the PR merged yet - tests for collections must be over-ridden until then!")
2a46da701dde Uploaded
fubar
parents:
diff changeset
724
2a46da701dde Uploaded
fubar
parents:
diff changeset
725 def doNoXMLparam(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
726 """filter style package - stdin to stdout"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
727 if len(self.infiles) > 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
728 alab = self.infiles[0]["label"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
729 if len(alab) == 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
730 alab = self.infiles[0]["infilename"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
731 max1s = (
2a46da701dde Uploaded
fubar
parents:
diff changeset
732 "Maximum one input if parampass is 0 but multiple input files supplied - %s"
2a46da701dde Uploaded
fubar
parents:
diff changeset
733 % str(self.infiles)
2a46da701dde Uploaded
fubar
parents:
diff changeset
734 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
735 assert len(self.infiles) == 1, max1s
2a46da701dde Uploaded
fubar
parents:
diff changeset
736 newname = self.infiles[0]["infilename"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
737 aninput = gxtp.DataParam(
2a46da701dde Uploaded
fubar
parents:
diff changeset
738 newname,
2a46da701dde Uploaded
fubar
parents:
diff changeset
739 optional=False,
2a46da701dde Uploaded
fubar
parents:
diff changeset
740 label=alab,
2a46da701dde Uploaded
fubar
parents:
diff changeset
741 help=self.infiles[0]["help"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
742 format=self.infiles[0]["format"],
2a46da701dde Uploaded
fubar
parents:
diff changeset
743 multiple=False,
2a46da701dde Uploaded
fubar
parents:
diff changeset
744 num_dashes=0,
2a46da701dde Uploaded
fubar
parents:
diff changeset
745 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
746 aninput.command_line_override = "< $%s" % newname
2a46da701dde Uploaded
fubar
parents:
diff changeset
747 aninput.positional = True
2a46da701dde Uploaded
fubar
parents:
diff changeset
748 self.tinputs.append(aninput)
2a46da701dde Uploaded
fubar
parents:
diff changeset
749 tp = gxtp.TestParam(name=newname, value="%s_sample" % newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
750 self.testparam.append(tp)
2a46da701dde Uploaded
fubar
parents:
diff changeset
751 if len(self.outfiles) > 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
752 newname = self.outfiles[0]["name"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
753 newfmt = self.outfiles[0]["format"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
754 anout = gxtp.OutputData(newname, format=newfmt, num_dashes=0)
2a46da701dde Uploaded
fubar
parents:
diff changeset
755 anout.command_line_override = "> $%s" % newname
2a46da701dde Uploaded
fubar
parents:
diff changeset
756 anout.positional = self.is_positional
2a46da701dde Uploaded
fubar
parents:
diff changeset
757 self.toutputs.append(anout)
2a46da701dde Uploaded
fubar
parents:
diff changeset
758 tp = gxtp.TestOutput(name=newname, value="%s_sample" % newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
759 self.testparam.append(tp)
2a46da701dde Uploaded
fubar
parents:
diff changeset
760
2a46da701dde Uploaded
fubar
parents:
diff changeset
761 def makeXML(self): # noqa
2a46da701dde Uploaded
fubar
parents:
diff changeset
762 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
763 Create a Galaxy xml tool wrapper for the new script
2a46da701dde Uploaded
fubar
parents:
diff changeset
764 Uses galaxyhtml
2a46da701dde Uploaded
fubar
parents:
diff changeset
765 Hmmm. How to get the command line into correct order...
2a46da701dde Uploaded
fubar
parents:
diff changeset
766 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
767 if self.command_override:
2a46da701dde Uploaded
fubar
parents:
diff changeset
768 self.newtool.command_override = self.command_override # config file
2a46da701dde Uploaded
fubar
parents:
diff changeset
769 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
770 self.newtool.command_override = self.xmlcl
2a46da701dde Uploaded
fubar
parents:
diff changeset
771 cite = gxtp.Citations()
2a46da701dde Uploaded
fubar
parents:
diff changeset
772 acite = gxtp.Citation(type="doi", value="10.1093/bioinformatics/bts573")
2a46da701dde Uploaded
fubar
parents:
diff changeset
773 cite.append(acite)
2a46da701dde Uploaded
fubar
parents:
diff changeset
774 self.newtool.citations = cite
2a46da701dde Uploaded
fubar
parents:
diff changeset
775 safertext = ""
2a46da701dde Uploaded
fubar
parents:
diff changeset
776 if self.args.help_text:
2a46da701dde Uploaded
fubar
parents:
diff changeset
777 helptext = open(self.args.help_text, "r").readlines()
2a46da701dde Uploaded
fubar
parents:
diff changeset
778 safertext = "\n".join([cheetah_escape(x) for x in helptext])
2a46da701dde Uploaded
fubar
parents:
diff changeset
779 if len(safertext.strip()) == 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
780 safertext = (
2a46da701dde Uploaded
fubar
parents:
diff changeset
781 "Ask the tool author (%s) to rebuild with help text please\n"
2a46da701dde Uploaded
fubar
parents:
diff changeset
782 % (self.args.user_email)
2a46da701dde Uploaded
fubar
parents:
diff changeset
783 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
784 if self.args.script_path:
2a46da701dde Uploaded
fubar
parents:
diff changeset
785 if len(safertext) > 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
786 safertext = safertext + "\n\n------\n" # transition allowed!
2a46da701dde Uploaded
fubar
parents:
diff changeset
787 scr = [x for x in self.spacedScript if x.strip() > ""]
2a46da701dde Uploaded
fubar
parents:
diff changeset
788 scr.insert(0, "\n\nScript::\n")
2a46da701dde Uploaded
fubar
parents:
diff changeset
789 if len(scr) > 300:
2a46da701dde Uploaded
fubar
parents:
diff changeset
790 scr = (
2a46da701dde Uploaded
fubar
parents:
diff changeset
791 scr[:100]
2a46da701dde Uploaded
fubar
parents:
diff changeset
792 + [" >300 lines - stuff deleted", " ......"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
793 + scr[-100:]
2a46da701dde Uploaded
fubar
parents:
diff changeset
794 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
795 scr.append("\n")
2a46da701dde Uploaded
fubar
parents:
diff changeset
796 safertext = safertext + "\n".join(scr)
2a46da701dde Uploaded
fubar
parents:
diff changeset
797 self.newtool.help = safertext
2a46da701dde Uploaded
fubar
parents:
diff changeset
798 self.newtool.version_command = f'echo "{self.args.tool_version}"'
2a46da701dde Uploaded
fubar
parents:
diff changeset
799 std = gxtp.Stdios()
2a46da701dde Uploaded
fubar
parents:
diff changeset
800 std1 = gxtp.Stdio()
2a46da701dde Uploaded
fubar
parents:
diff changeset
801 std.append(std1)
2a46da701dde Uploaded
fubar
parents:
diff changeset
802 self.newtool.stdios = std
2a46da701dde Uploaded
fubar
parents:
diff changeset
803 requirements = gxtp.Requirements()
2a46da701dde Uploaded
fubar
parents:
diff changeset
804 if self.args.packages:
2a46da701dde Uploaded
fubar
parents:
diff changeset
805 try:
2a46da701dde Uploaded
fubar
parents:
diff changeset
806 for d in self.args.packages.split(","):
2a46da701dde Uploaded
fubar
parents:
diff changeset
807 ver = ""
2a46da701dde Uploaded
fubar
parents:
diff changeset
808 d = d.replace("==", ":")
2a46da701dde Uploaded
fubar
parents:
diff changeset
809 d = d.replace("=", ":")
2a46da701dde Uploaded
fubar
parents:
diff changeset
810 if ":" in d:
2a46da701dde Uploaded
fubar
parents:
diff changeset
811 packg, ver = d.split(":")
2a46da701dde Uploaded
fubar
parents:
diff changeset
812 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
813 packg = d
2a46da701dde Uploaded
fubar
parents:
diff changeset
814 requirements.append(
2a46da701dde Uploaded
fubar
parents:
diff changeset
815 gxtp.Requirement("package", packg.strip(), ver.strip())
2a46da701dde Uploaded
fubar
parents:
diff changeset
816 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
817 except Exception:
2a46da701dde Uploaded
fubar
parents:
diff changeset
818 print('### malformed packages string supplied - cannot parse =',self.args.packages)
2a46da701dde Uploaded
fubar
parents:
diff changeset
819 sys.exit(2)
2a46da701dde Uploaded
fubar
parents:
diff changeset
820 self.newtool.requirements = requirements
2a46da701dde Uploaded
fubar
parents:
diff changeset
821 if self.args.parampass == "0":
2a46da701dde Uploaded
fubar
parents:
diff changeset
822 self.doNoXMLparam()
2a46da701dde Uploaded
fubar
parents:
diff changeset
823 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
824 self.doXMLparam()
2a46da701dde Uploaded
fubar
parents:
diff changeset
825 self.newtool.outputs = self.toutputs
2a46da701dde Uploaded
fubar
parents:
diff changeset
826 self.newtool.inputs = self.tinputs
2a46da701dde Uploaded
fubar
parents:
diff changeset
827 if self.args.script_path:
2a46da701dde Uploaded
fubar
parents:
diff changeset
828 configfiles = gxtp.Configfiles()
2a46da701dde Uploaded
fubar
parents:
diff changeset
829 configfiles.append(
2a46da701dde Uploaded
fubar
parents:
diff changeset
830 gxtp.Configfile(name="runme", text="\n".join(self.escapedScript))
2a46da701dde Uploaded
fubar
parents:
diff changeset
831 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
832 self.newtool.configfiles = configfiles
2a46da701dde Uploaded
fubar
parents:
diff changeset
833 tests = gxtp.Tests()
2a46da701dde Uploaded
fubar
parents:
diff changeset
834 test_a = gxtp.Test()
2a46da701dde Uploaded
fubar
parents:
diff changeset
835 for tp in self.testparam:
2a46da701dde Uploaded
fubar
parents:
diff changeset
836 test_a.append(tp)
2a46da701dde Uploaded
fubar
parents:
diff changeset
837 tests.append(test_a)
2a46da701dde Uploaded
fubar
parents:
diff changeset
838 self.newtool.tests = tests
2a46da701dde Uploaded
fubar
parents:
diff changeset
839 self.newtool.add_comment(
2a46da701dde Uploaded
fubar
parents:
diff changeset
840 "Created by %s at %s using the Galaxy Tool Factory."
2a46da701dde Uploaded
fubar
parents:
diff changeset
841 % (self.args.user_email, timenow())
2a46da701dde Uploaded
fubar
parents:
diff changeset
842 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
843 self.newtool.add_comment("Source in git at: %s" % (toolFactoryURL))
2a46da701dde Uploaded
fubar
parents:
diff changeset
844 exml0 = self.newtool.export()
2a46da701dde Uploaded
fubar
parents:
diff changeset
845 exml = exml0.replace(FAKEEXE, "") # temporary work around until PR accepted
2a46da701dde Uploaded
fubar
parents:
diff changeset
846 if (
2a46da701dde Uploaded
fubar
parents:
diff changeset
847 self.test_override
2a46da701dde Uploaded
fubar
parents:
diff changeset
848 ): # cannot do this inside galaxyxml as it expects lxml objects for tests
2a46da701dde Uploaded
fubar
parents:
diff changeset
849 part1 = exml.split("<tests>")[0]
2a46da701dde Uploaded
fubar
parents:
diff changeset
850 part2 = exml.split("</tests>")[1]
2a46da701dde Uploaded
fubar
parents:
diff changeset
851 fixed = "%s\n%s\n%s" % (part1, "\n".join(self.test_override), part2)
2a46da701dde Uploaded
fubar
parents:
diff changeset
852 exml = fixed
2a46da701dde Uploaded
fubar
parents:
diff changeset
853 # exml = exml.replace('range="1:"', 'range="1000:"')
2a46da701dde Uploaded
fubar
parents:
diff changeset
854 xf = open("%s.xml" % self.tool_name, "w")
2a46da701dde Uploaded
fubar
parents:
diff changeset
855 xf.write(exml)
2a46da701dde Uploaded
fubar
parents:
diff changeset
856 xf.write("\n")
2a46da701dde Uploaded
fubar
parents:
diff changeset
857 xf.close()
2a46da701dde Uploaded
fubar
parents:
diff changeset
858 # ready for the tarball
2a46da701dde Uploaded
fubar
parents:
diff changeset
859
2a46da701dde Uploaded
fubar
parents:
diff changeset
860 def run(self): #noqa
2a46da701dde Uploaded
fubar
parents:
diff changeset
861 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
862 generate test outputs by running a command line
2a46da701dde Uploaded
fubar
parents:
diff changeset
863 won't work if command or test override in play - planemo is the
2a46da701dde Uploaded
fubar
parents:
diff changeset
864 easiest way to generate test outputs for that case so is
2a46da701dde Uploaded
fubar
parents:
diff changeset
865 automagically selected
2a46da701dde Uploaded
fubar
parents:
diff changeset
866 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
867 scl = " ".join(self.cl)
2a46da701dde Uploaded
fubar
parents:
diff changeset
868 err = None
2a46da701dde Uploaded
fubar
parents:
diff changeset
869 logname = f"{self.tool_name}_runner_log"
2a46da701dde Uploaded
fubar
parents:
diff changeset
870 if self.args.parampass != "0":
2a46da701dde Uploaded
fubar
parents:
diff changeset
871 if self.lastclredirect:
2a46da701dde Uploaded
fubar
parents:
diff changeset
872 logf = open(self.lastclredirect[1], "wb") # is name of an output file
2a46da701dde Uploaded
fubar
parents:
diff changeset
873 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
874 logf = open(logname,'w')
2a46da701dde Uploaded
fubar
parents:
diff changeset
875 logf.write("No dependencies so sending CL = '%s' to the fast direct runner instead of planemo to generate tests" % scl)
2a46da701dde Uploaded
fubar
parents:
diff changeset
876 subp = subprocess.run(
2a46da701dde Uploaded
fubar
parents:
diff changeset
877 self.cl, shell=False, stdout=logf, stderr=logf
2a46da701dde Uploaded
fubar
parents:
diff changeset
878 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
879 logf.close()
2a46da701dde Uploaded
fubar
parents:
diff changeset
880 retval = subp.returncode
2a46da701dde Uploaded
fubar
parents:
diff changeset
881 else: # work around special case - stdin and write to stdout
2a46da701dde Uploaded
fubar
parents:
diff changeset
882 if len(self.infiles) > 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
883 sti = open(self.infiles[0]["name"], "rb")
2a46da701dde Uploaded
fubar
parents:
diff changeset
884 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
885 sti = sys.stdin
2a46da701dde Uploaded
fubar
parents:
diff changeset
886 if len(self.outfiles) > 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
887 sto = open(self.outfiles[0]["name"], "wb")
2a46da701dde Uploaded
fubar
parents:
diff changeset
888 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
889 sto = sys.stdout
2a46da701dde Uploaded
fubar
parents:
diff changeset
890 subp = subprocess.run(
2a46da701dde Uploaded
fubar
parents:
diff changeset
891 self.cl, shell=False, stdout=sto, stdin=sti
2a46da701dde Uploaded
fubar
parents:
diff changeset
892 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
893 retval = subp.returncode
2a46da701dde Uploaded
fubar
parents:
diff changeset
894 sto.close()
2a46da701dde Uploaded
fubar
parents:
diff changeset
895 sti.close()
2a46da701dde Uploaded
fubar
parents:
diff changeset
896 if retval != 0 and err: # problem
2a46da701dde Uploaded
fubar
parents:
diff changeset
897 sys.stderr.write(err)
2a46da701dde Uploaded
fubar
parents:
diff changeset
898 for p in self.outfiles:
2a46da701dde Uploaded
fubar
parents:
diff changeset
899 oname = p["name"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
900 tdest = os.path.join(self.testdir, "%s_sample" % oname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
901 if not os.path.isfile(tdest):
2a46da701dde Uploaded
fubar
parents:
diff changeset
902 if os.path.isfile(oname):
2a46da701dde Uploaded
fubar
parents:
diff changeset
903 shutil.copyfile(oname, tdest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
904 dest = os.path.join(self.repdir, "%s.sample.%s" % (oname,p['format']))
2a46da701dde Uploaded
fubar
parents:
diff changeset
905 shutil.copyfile(oname, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
906 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
907 if report_fail:
2a46da701dde Uploaded
fubar
parents:
diff changeset
908 tout.write(
2a46da701dde Uploaded
fubar
parents:
diff changeset
909 "###Tool may have failed - output file %s not found in testdir after planemo run %s."
2a46da701dde Uploaded
fubar
parents:
diff changeset
910 % (oname, self.testdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
911 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
912 for p in self.infiles:
2a46da701dde Uploaded
fubar
parents:
diff changeset
913 pth = p["name"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
914 dest = os.path.join(self.testdir, "%s_sample" % p["infilename"])
2a46da701dde Uploaded
fubar
parents:
diff changeset
915 shutil.copyfile(pth, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
916 dest = os.path.join(self.repdir, "%s_sample.%s" % (p["infilename"],p["format"]))
2a46da701dde Uploaded
fubar
parents:
diff changeset
917 shutil.copyfile(pth, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
918 with os.scandir('.') as outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
919 for entry in outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
920 newname = entry.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
921 if not entry.is_file() or entry.name.endswith('_sample'):
2a46da701dde Uploaded
fubar
parents:
diff changeset
922 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
923 if not (entry.name.endswith('.html') or entry.name.endswith('.gz') or entry.name.endswith(".tgz")):
2a46da701dde Uploaded
fubar
parents:
diff changeset
924 fname, ext = os.path.splitext(entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
925 if len(ext) > 1:
2a46da701dde Uploaded
fubar
parents:
diff changeset
926 newname = f"{fname}_{ext[1:]}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
927 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
928 newname = f"{fname}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
929 dest = os.path.join(self.repdir, newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
930 src = entry.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
931 shutil.copyfile(src, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
932 return retval
2a46da701dde Uploaded
fubar
parents:
diff changeset
933
2a46da701dde Uploaded
fubar
parents:
diff changeset
934 def writeShedyml(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
935 """for planemo"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
936 yuser = self.args.user_email.split("@")[0]
2a46da701dde Uploaded
fubar
parents:
diff changeset
937 yfname = os.path.join(self.tooloutdir, ".shed.yml")
2a46da701dde Uploaded
fubar
parents:
diff changeset
938 yamlf = open(yfname, "w")
2a46da701dde Uploaded
fubar
parents:
diff changeset
939 odict = {
2a46da701dde Uploaded
fubar
parents:
diff changeset
940 "name": self.tool_name,
2a46da701dde Uploaded
fubar
parents:
diff changeset
941 "owner": yuser,
2a46da701dde Uploaded
fubar
parents:
diff changeset
942 "type": "unrestricted",
2a46da701dde Uploaded
fubar
parents:
diff changeset
943 "description": self.args.tool_desc,
2a46da701dde Uploaded
fubar
parents:
diff changeset
944 "synopsis": self.args.tool_desc,
2a46da701dde Uploaded
fubar
parents:
diff changeset
945 "category": "TF Generated Tools",
2a46da701dde Uploaded
fubar
parents:
diff changeset
946 }
2a46da701dde Uploaded
fubar
parents:
diff changeset
947 yaml.dump(odict, yamlf, allow_unicode=True)
2a46da701dde Uploaded
fubar
parents:
diff changeset
948 yamlf.close()
2a46da701dde Uploaded
fubar
parents:
diff changeset
949
2a46da701dde Uploaded
fubar
parents:
diff changeset
950 def makeTool(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
951 """write xmls and input samples into place"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
952 if self.args.parampass == 0:
2a46da701dde Uploaded
fubar
parents:
diff changeset
953 self.doNoXMLparam()
2a46da701dde Uploaded
fubar
parents:
diff changeset
954 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
955 self.makeXML()
2a46da701dde Uploaded
fubar
parents:
diff changeset
956 if self.args.script_path:
2a46da701dde Uploaded
fubar
parents:
diff changeset
957 stname = os.path.join(self.tooloutdir, self.sfile)
2a46da701dde Uploaded
fubar
parents:
diff changeset
958 if not os.path.exists(stname):
2a46da701dde Uploaded
fubar
parents:
diff changeset
959 shutil.copyfile(self.sfile, stname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
960 xreal = "%s.xml" % self.tool_name
2a46da701dde Uploaded
fubar
parents:
diff changeset
961 xout = os.path.join(self.tooloutdir, xreal)
2a46da701dde Uploaded
fubar
parents:
diff changeset
962 shutil.copyfile(xreal, xout)
2a46da701dde Uploaded
fubar
parents:
diff changeset
963 for p in self.infiles:
2a46da701dde Uploaded
fubar
parents:
diff changeset
964 pth = p["name"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
965 dest = os.path.join(self.testdir, "%s_sample" % p["infilename"])
2a46da701dde Uploaded
fubar
parents:
diff changeset
966 shutil.copyfile(pth, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
967 dest = os.path.join(self.repdir, "%s_sample.%s" % (p["infilename"],p["format"]))
2a46da701dde Uploaded
fubar
parents:
diff changeset
968 shutil.copyfile(pth, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
969
2a46da701dde Uploaded
fubar
parents:
diff changeset
970 def makeToolTar(self, report_fail=False):
2a46da701dde Uploaded
fubar
parents:
diff changeset
971 """move outputs into test-data and prepare the tarball"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
972 excludeme = "_planemo_test_report.html"
2a46da701dde Uploaded
fubar
parents:
diff changeset
973
2a46da701dde Uploaded
fubar
parents:
diff changeset
974 def exclude_function(tarinfo):
2a46da701dde Uploaded
fubar
parents:
diff changeset
975 filename = tarinfo.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
976 return None if filename.endswith(excludeme) else tarinfo
2a46da701dde Uploaded
fubar
parents:
diff changeset
977
2a46da701dde Uploaded
fubar
parents:
diff changeset
978 for p in self.outfiles:
2a46da701dde Uploaded
fubar
parents:
diff changeset
979 oname = p["name"]
2a46da701dde Uploaded
fubar
parents:
diff changeset
980 tdest = os.path.join(self.testdir, "%s_sample" % oname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
981 src = os.path.join(self.testdir, oname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
982 if not os.path.isfile(tdest):
2a46da701dde Uploaded
fubar
parents:
diff changeset
983 if os.path.isfile(src):
2a46da701dde Uploaded
fubar
parents:
diff changeset
984 shutil.copyfile(src, tdest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
985 dest = os.path.join(self.repdir, "%s.sample" % (oname))
2a46da701dde Uploaded
fubar
parents:
diff changeset
986 shutil.copyfile(src, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
987 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
988 if report_fail:
2a46da701dde Uploaded
fubar
parents:
diff changeset
989 print(
2a46da701dde Uploaded
fubar
parents:
diff changeset
990 "###Tool may have failed - output file %s not found in testdir after planemo run %s."
2a46da701dde Uploaded
fubar
parents:
diff changeset
991 % (tdest, self.testdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
992 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
993 tf = tarfile.open(self.newtarpath, "w:gz")
2a46da701dde Uploaded
fubar
parents:
diff changeset
994 tf.add(
2a46da701dde Uploaded
fubar
parents:
diff changeset
995 name=self.tooloutdir,
2a46da701dde Uploaded
fubar
parents:
diff changeset
996 arcname=self.tool_name,
2a46da701dde Uploaded
fubar
parents:
diff changeset
997 filter=exclude_function,
2a46da701dde Uploaded
fubar
parents:
diff changeset
998 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
999 tf.close()
2a46da701dde Uploaded
fubar
parents:
diff changeset
1000 shutil.copyfile(self.newtarpath, self.args.new_tool)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1001
2a46da701dde Uploaded
fubar
parents:
diff changeset
1002 def moveRunOutputs(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
1003 """need to move planemo or run outputs into toolfactory collection"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
1004 with os.scandir(self.tooloutdir) as outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1005 for entry in outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1006 if not entry.is_file():
2a46da701dde Uploaded
fubar
parents:
diff changeset
1007 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
1008 if not entry.name.endswith('.html'):
2a46da701dde Uploaded
fubar
parents:
diff changeset
1009 _, ext = os.path.splitext(entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1010 newname = f"{entry.name.replace('.','_')}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
1011 dest = os.path.join(self.repdir, newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1012 src = os.path.join(self.tooloutdir, entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1013 shutil.copyfile(src, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1014 if self.args.include_tests:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1015 with os.scandir(self.testdir) as outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1016 for entry in outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1017 if (not entry.is_file()) or entry.name.endswith(
2a46da701dde Uploaded
fubar
parents:
diff changeset
1018 "_planemo_test_report.html"
2a46da701dde Uploaded
fubar
parents:
diff changeset
1019 ):
2a46da701dde Uploaded
fubar
parents:
diff changeset
1020 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
1021 if "." in entry.name:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1022 _, ext = os.path.splitext(entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1023 if ext in [".tgz", ".json"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1024 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
1025 if ext in [".yml", ".xml", ".yaml"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1026 newname = f"{entry.name.replace('.','_')}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
1027 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1028 newname = entry.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
1029 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1030 newname = f"{entry.name}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
1031 dest = os.path.join(self.repdir, newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1032 src = os.path.join(self.testdir, entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1033 shutil.copyfile(src, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1034
2a46da701dde Uploaded
fubar
parents:
diff changeset
1035
2a46da701dde Uploaded
fubar
parents:
diff changeset
1036 def main():
2a46da701dde Uploaded
fubar
parents:
diff changeset
1037 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
1038 This is a Galaxy wrapper.
2a46da701dde Uploaded
fubar
parents:
diff changeset
1039 It expects to be called by a special purpose tool.xml
2a46da701dde Uploaded
fubar
parents:
diff changeset
1040
2a46da701dde Uploaded
fubar
parents:
diff changeset
1041 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
1042 parser = argparse.ArgumentParser()
2a46da701dde Uploaded
fubar
parents:
diff changeset
1043 a = parser.add_argument
2a46da701dde Uploaded
fubar
parents:
diff changeset
1044 a("--script_path", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1045 a("--history_test", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1046 a("--cl_user_suffix", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1047 a("--sysexe", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1048 a("--packages", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1049 a("--tool_name", default="newtool")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1050 a("--tool_dir", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1051 a("--input_files", default=[], action="append")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1052 a("--output_files", default=[], action="append")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1053 a("--user_email", default="Unknown")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1054 a("--bad_user", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1055 a("--help_text", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1056 a("--tool_desc", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1057 a("--tool_version", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1058 a("--citations", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1059 a("--command_override", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1060 a("--test_override", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1061 a("--additional_parameters", action="append", default=[])
2a46da701dde Uploaded
fubar
parents:
diff changeset
1062 a("--selecttext_parameters", action="append", default=[])
2a46da701dde Uploaded
fubar
parents:
diff changeset
1063 a("--edit_additional_parameters", action="store_true", default=False)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1064 a("--parampass", default="positional")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1065 a("--tfout", default="./tfout")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1066 a("--new_tool", default="new_tool")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1067 a("--galaxy_root", default="/galaxy-central")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1068 a("--galaxy_venv", default="/galaxy_venv")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1069 a("--collection", action="append", default=[])
2a46da701dde Uploaded
fubar
parents:
diff changeset
1070 a("--include_tests", default=False, action="store_true")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1071 a("--install", default=False, action="store_true")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1072 a("--run_test", default=False, action="store_true")
5
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
1073 a("--local_tools", default='tools') # relative to $__root_dir__
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
1074 a("--tool_conf_path", default='config/tool_conf.xml') # relative to $__root_dir__
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
1075 a("--galaxy_url", default="http://localhost:8080")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1076 a("--toolshed_url", default="http://localhost:9009")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1077 # make sure this is identical to tool_sheds_conf.xml
2a46da701dde Uploaded
fubar
parents:
diff changeset
1078 # localhost != 127.0.0.1 so validation fails
2a46da701dde Uploaded
fubar
parents:
diff changeset
1079 a("--toolshed_api_key", default="fakekey")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1080 a("--galaxy_api_key", default="8993d65865e6d6d1773c2c34a1cc207d")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1081 args = parser.parse_args()
2a46da701dde Uploaded
fubar
parents:
diff changeset
1082 assert not args.bad_user, (
2a46da701dde Uploaded
fubar
parents:
diff changeset
1083 'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy \
2a46da701dde Uploaded
fubar
parents:
diff changeset
1084 admin adds %s to "admin_users" in the galaxy.yml Galaxy configuration file'
2a46da701dde Uploaded
fubar
parents:
diff changeset
1085 % (args.bad_user, args.bad_user)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1086 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
1087 assert args.tool_name, "## Tool Factory expects a tool name - eg --tool_name=DESeq"
2a46da701dde Uploaded
fubar
parents:
diff changeset
1088 assert (
2a46da701dde Uploaded
fubar
parents:
diff changeset
1089 args.sysexe or args.packages
2a46da701dde Uploaded
fubar
parents:
diff changeset
1090 ), "## Tool Factory wrapper expects an interpreter \
2a46da701dde Uploaded
fubar
parents:
diff changeset
1091 or an executable package in --sysexe or --packages"
2a46da701dde Uploaded
fubar
parents:
diff changeset
1092 print('Hello from',os.getcwd())
2a46da701dde Uploaded
fubar
parents:
diff changeset
1093 r = ScriptRunner(args)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1094 r.writeShedyml()
2a46da701dde Uploaded
fubar
parents:
diff changeset
1095 r.makeTool()
2a46da701dde Uploaded
fubar
parents:
diff changeset
1096 r.makeToolTar()
2a46da701dde Uploaded
fubar
parents:
diff changeset
1097 if args.run_test:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1098 if not args.packages or args.packages.strip() == "bash":
2a46da701dde Uploaded
fubar
parents:
diff changeset
1099 r.run()
2a46da701dde Uploaded
fubar
parents:
diff changeset
1100 r.makeToolTar()
2a46da701dde Uploaded
fubar
parents:
diff changeset
1101 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1102 tt = ToolTester(report_dir=r.repdir, in_tool_archive=r.newtarpath, new_tool_archive=r.args.new_tool, galaxy_root=args.galaxy_root, include_tests=False)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1103 if args.install:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1104 #try:
5
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
1105 tcu = ToolConfUpdater(args=args, tool_dir=args.local_tools,
e2c8c2fa192d Uploaded
fubar
parents: 4
diff changeset
1106 new_tool_archive_path=r.newtarpath, tool_conf_path=args.tool_conf_path,
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
1107 new_tool_name=r.tool_name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
1108 #except Exception:
2a46da701dde Uploaded
fubar
parents:
diff changeset
1109 # print("### Unable to install the new tool. Are you sure you have all the required special settings?")
2a46da701dde Uploaded
fubar
parents:
diff changeset
1110
2a46da701dde Uploaded
fubar
parents:
diff changeset
1111 if __name__ == "__main__":
2a46da701dde Uploaded
fubar
parents:
diff changeset
1112 main()
2a46da701dde Uploaded
fubar
parents:
diff changeset
1113