annotate toolfactory/ToolFactory_tester.xml @ 6:efefe43f23c8 draft default tip

Uploaded
author fubar
date Fri, 30 Apr 2021 02:10:32 +0000
parents 2a46da701dde
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
1 <tool name="toolfactory_tester" id="toolfactory_tester" version="1">
2a46da701dde Uploaded
fubar
parents:
diff changeset
2 <!--Source in git at: https://github.com/fubar2/toolfactory-->
2a46da701dde Uploaded
fubar
parents:
diff changeset
3 <!--Created by admin@galaxy.org at 23/04/2021 10:25:58 using the Galaxy Tool Factory.-->
2a46da701dde Uploaded
fubar
parents:
diff changeset
4 <description>Test an untested tool and update it</description>
2a46da701dde Uploaded
fubar
parents:
diff changeset
5 <requirements>
2a46da701dde Uploaded
fubar
parents:
diff changeset
6 <requirement type="package" version="2.30.2">git</requirement>
2a46da701dde Uploaded
fubar
parents:
diff changeset
7 <requirement type="package" version="0.74.3">planemo</requirement> </requirements>
2a46da701dde Uploaded
fubar
parents:
diff changeset
8 <stdio>
2a46da701dde Uploaded
fubar
parents:
diff changeset
9 <exit_code range="1:" level="fatal"/>
2a46da701dde Uploaded
fubar
parents:
diff changeset
10 </stdio>
2a46da701dde Uploaded
fubar
parents:
diff changeset
11 <version_command><![CDATA[echo "1"]]></version_command>
2a46da701dde Uploaded
fubar
parents:
diff changeset
12 <command><![CDATA[
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
13 mkdir 'TF_run_report';
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
14 python
2a46da701dde Uploaded
fubar
parents:
diff changeset
15 $runme
2a46da701dde Uploaded
fubar
parents:
diff changeset
16 --in_tool_archive
2a46da701dde Uploaded
fubar
parents:
diff changeset
17 $in_tool_archive
2a46da701dde Uploaded
fubar
parents:
diff changeset
18 --new_tested_tool_archive
2a46da701dde Uploaded
fubar
parents:
diff changeset
19 $new_tested_tool_archive
2a46da701dde Uploaded
fubar
parents:
diff changeset
20 --galaxy_root
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
21 "$__root_dir__"
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
22 >
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
23 "TF_run_report/${in_tool_archive.name}_test_log.txt"
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
24 ]]></command>
2a46da701dde Uploaded
fubar
parents:
diff changeset
25 <configfiles>
2a46da701dde Uploaded
fubar
parents:
diff changeset
26 <configfile name="runme"><![CDATA[#raw
2a46da701dde Uploaded
fubar
parents:
diff changeset
27
2a46da701dde Uploaded
fubar
parents:
diff changeset
28 # see https://github.com/fubar2/toolfactory
2a46da701dde Uploaded
fubar
parents:
diff changeset
29 #
2a46da701dde Uploaded
fubar
parents:
diff changeset
30 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
2a46da701dde Uploaded
fubar
parents:
diff changeset
31 #
2a46da701dde Uploaded
fubar
parents:
diff changeset
32 # all rights reserved
2a46da701dde Uploaded
fubar
parents:
diff changeset
33 # Licensed under the LGPL
2a46da701dde Uploaded
fubar
parents:
diff changeset
34 # suggestions for improvement and bug fixes welcome at
2a46da701dde Uploaded
fubar
parents:
diff changeset
35 # https://github.com/fubar2/toolfactory
2a46da701dde Uploaded
fubar
parents:
diff changeset
36 #
2a46da701dde Uploaded
fubar
parents:
diff changeset
37 # July 2020: BCC was fun and I feel like rip van winkle after 5 years.
2a46da701dde Uploaded
fubar
parents:
diff changeset
38 # Decided to
2a46da701dde Uploaded
fubar
parents:
diff changeset
39 # 1. Fix the toolfactory so it works - done for simplest case
2a46da701dde Uploaded
fubar
parents:
diff changeset
40 # 2. Fix planemo so the toolfactory function works
2a46da701dde Uploaded
fubar
parents:
diff changeset
41 # 3. Rewrite bits using galaxyxml functions where that makes sense - done
2a46da701dde Uploaded
fubar
parents:
diff changeset
42
2a46da701dde Uploaded
fubar
parents:
diff changeset
43 import argparse
2a46da701dde Uploaded
fubar
parents:
diff changeset
44 import copy
2a46da701dde Uploaded
fubar
parents:
diff changeset
45 import os
2a46da701dde Uploaded
fubar
parents:
diff changeset
46 import subprocess
2a46da701dde Uploaded
fubar
parents:
diff changeset
47 import shutil
2a46da701dde Uploaded
fubar
parents:
diff changeset
48 import sys
2a46da701dde Uploaded
fubar
parents:
diff changeset
49 import tarfile
2a46da701dde Uploaded
fubar
parents:
diff changeset
50 import tempfile
2a46da701dde Uploaded
fubar
parents:
diff changeset
51 import time
2a46da701dde Uploaded
fubar
parents:
diff changeset
52 import xml.etree.ElementTree as ET
2a46da701dde Uploaded
fubar
parents:
diff changeset
53
2a46da701dde Uploaded
fubar
parents:
diff changeset
54
2a46da701dde Uploaded
fubar
parents:
diff changeset
55 myversion = "V2.2 April 2021"
2a46da701dde Uploaded
fubar
parents:
diff changeset
56 verbose = True
2a46da701dde Uploaded
fubar
parents:
diff changeset
57 debug = True
2a46da701dde Uploaded
fubar
parents:
diff changeset
58 toolFactoryURL = "https://github.com/fubar2/toolfactory"
2a46da701dde Uploaded
fubar
parents:
diff changeset
59
2a46da701dde Uploaded
fubar
parents:
diff changeset
60 def timenow():
2a46da701dde Uploaded
fubar
parents:
diff changeset
61 """return current time as a string"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
62 return time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(time.time()))
2a46da701dde Uploaded
fubar
parents:
diff changeset
63
2a46da701dde Uploaded
fubar
parents:
diff changeset
64 class ToolTester():
2a46da701dde Uploaded
fubar
parents:
diff changeset
65 # uncompress passed tar, run planemo and rebuild a new tarball with tests
2a46da701dde Uploaded
fubar
parents:
diff changeset
66
2a46da701dde Uploaded
fubar
parents:
diff changeset
67 def __init__(self, args=None, in_tool_archive='/galaxy-central/tools/newtool/newtool_toolshed.gz', new_tool_archive=None):
2a46da701dde Uploaded
fubar
parents:
diff changeset
68 self.args = args
2a46da701dde Uploaded
fubar
parents:
diff changeset
69 self.new_tool_archive = new_tool_archive
2a46da701dde Uploaded
fubar
parents:
diff changeset
70 assert tarfile.is_tarfile(in_tool_archive)
2a46da701dde Uploaded
fubar
parents:
diff changeset
71 # this is not going to go well with arbitrary names.
2a46da701dde Uploaded
fubar
parents:
diff changeset
72 tff = tarfile.open(in_tool_archive, "r:*")
2a46da701dde Uploaded
fubar
parents:
diff changeset
73 flist = tff.getnames()
2a46da701dde Uploaded
fubar
parents:
diff changeset
74 ourdir = os.path.commonpath(flist) # eg pyrevpos
2a46da701dde Uploaded
fubar
parents:
diff changeset
75 self.tool_name = ourdir
2a46da701dde Uploaded
fubar
parents:
diff changeset
76 ourxmls = [x for x in flist if x.lower().endswith('.xml') and os.path.split(x)[0] == ourdir]
2a46da701dde Uploaded
fubar
parents:
diff changeset
77 # planemo_test/planemo_test.xml
2a46da701dde Uploaded
fubar
parents:
diff changeset
78 assert len(ourxmls) > 0
2a46da701dde Uploaded
fubar
parents:
diff changeset
79 self.ourxmls = ourxmls # [os.path.join(tool_path,x) for x in ourxmls]
2a46da701dde Uploaded
fubar
parents:
diff changeset
80 res = tff.extractall()
2a46da701dde Uploaded
fubar
parents:
diff changeset
81 self.update_tests(ourdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
82 tff.close()
2a46da701dde Uploaded
fubar
parents:
diff changeset
83 self.tooloutdir = "./tfout"
2a46da701dde Uploaded
fubar
parents:
diff changeset
84 self.repdir = "./TF_run_report"
2a46da701dde Uploaded
fubar
parents:
diff changeset
85 self.testdir = os.path.join(self.tooloutdir, "test-data")
2a46da701dde Uploaded
fubar
parents:
diff changeset
86 if not os.path.exists(self.tooloutdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
87 os.mkdir(self.tooloutdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
88 if not os.path.exists(self.testdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
89 os.mkdir(self.testdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
90 if not os.path.exists(self.repdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
91 os.mkdir(self.repdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
92 self.makeTool()
2a46da701dde Uploaded
fubar
parents:
diff changeset
93 self.moveRunOutputs()
2a46da701dde Uploaded
fubar
parents:
diff changeset
94 self.makeToolTar()
2a46da701dde Uploaded
fubar
parents:
diff changeset
95
2a46da701dde Uploaded
fubar
parents:
diff changeset
96 def call_planemo(self,xmlpath,ourdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
97 penv = os.environ
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
98 penv['HOME'] = os.path.join(self.args.galaxy_root,'planemo')
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
99 newpath = f"{penv['HOME']}:{penv['PATH']}"
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
100 penv['PATH'] = newpath
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
101 penv["GALAXY_VIRTUAL_ENV"] = os.path.join(self.args.galaxy_root,'.venv')
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
102 penv["PIP_CACHE_DIR"] = os.path.join(self.args.galaxy_root,'pipcache')
2a46da701dde Uploaded
fubar
parents:
diff changeset
103 toolfile = os.path.split(xmlpath)[1]
2a46da701dde Uploaded
fubar
parents:
diff changeset
104 tool_name = self.tool_name
2a46da701dde Uploaded
fubar
parents:
diff changeset
105 tool_test_output = f"{tool_name}_planemo_test_report.html"
2a46da701dde Uploaded
fubar
parents:
diff changeset
106 cll = [
2a46da701dde Uploaded
fubar
parents:
diff changeset
107 "planemo",
2a46da701dde Uploaded
fubar
parents:
diff changeset
108 "test",
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
109 "--no_cleanup",
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
110 "--test_data",
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
111 os.path.abspath(ourdir),
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
112 "--test_output",
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
113 os.path.abspath(self.tool_test_output),
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
114 "--galaxy_root",
2a46da701dde Uploaded
fubar
parents:
diff changeset
115 self.args.galaxy_root,
2a46da701dde Uploaded
fubar
parents:
diff changeset
116 "--update_test_data",
2a46da701dde Uploaded
fubar
parents:
diff changeset
117 os.path.abspath(xmlpath),
2a46da701dde Uploaded
fubar
parents:
diff changeset
118 ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
119 print(cll)
2a46da701dde Uploaded
fubar
parents:
diff changeset
120 p = subprocess.run(
2a46da701dde Uploaded
fubar
parents:
diff changeset
121 cll,
2a46da701dde Uploaded
fubar
parents:
diff changeset
122 #capture_output=True,
2a46da701dde Uploaded
fubar
parents:
diff changeset
123 encoding='utf8',
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
124 cwd = os.path.abspath(self.tool_name),
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
125 env = penv,
2a46da701dde Uploaded
fubar
parents:
diff changeset
126 shell=False,
2a46da701dde Uploaded
fubar
parents:
diff changeset
127 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
128 return p
2a46da701dde Uploaded
fubar
parents:
diff changeset
129
2a46da701dde Uploaded
fubar
parents:
diff changeset
130 def makeTool(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
131 """write xmls and input samples into place"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
132 for xreal in self.ourxmls:
2a46da701dde Uploaded
fubar
parents:
diff changeset
133 x = os.path.split(xreal)[1]
2a46da701dde Uploaded
fubar
parents:
diff changeset
134 xout = os.path.join(self.tooloutdir,x)
2a46da701dde Uploaded
fubar
parents:
diff changeset
135 shutil.copyfile(xreal, xout)
2a46da701dde Uploaded
fubar
parents:
diff changeset
136
2a46da701dde Uploaded
fubar
parents:
diff changeset
137 def makeToolTar(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
138 """move outputs into test-data and prepare the tarball"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
139 excludeme = "_planemo_test_report.html"
2a46da701dde Uploaded
fubar
parents:
diff changeset
140
2a46da701dde Uploaded
fubar
parents:
diff changeset
141 def exclude_function(tarinfo):
2a46da701dde Uploaded
fubar
parents:
diff changeset
142 filename = tarinfo.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
143 return None if filename.endswith(excludeme) else tarinfo
2a46da701dde Uploaded
fubar
parents:
diff changeset
144
2a46da701dde Uploaded
fubar
parents:
diff changeset
145 newtar = 'new_%s_toolshed.gz' % self.tool_name
2a46da701dde Uploaded
fubar
parents:
diff changeset
146 ttf = tarfile.open(newtar, "w:gz")
2a46da701dde Uploaded
fubar
parents:
diff changeset
147 ttf.add(name=self.tool_name,
2a46da701dde Uploaded
fubar
parents:
diff changeset
148 arcname=self.tool_name,
2a46da701dde Uploaded
fubar
parents:
diff changeset
149 filter=exclude_function)
2a46da701dde Uploaded
fubar
parents:
diff changeset
150 ttf.close()
2a46da701dde Uploaded
fubar
parents:
diff changeset
151 shutil.copyfile(newtar, self.new_tool_archive)
2a46da701dde Uploaded
fubar
parents:
diff changeset
152
2a46da701dde Uploaded
fubar
parents:
diff changeset
153 def moveRunOutputs(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
154 """need to move planemo or run outputs into toolfactory collection"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
155 with os.scandir(self.tooloutdir) as outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
156 for entry in outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
157 if not entry.is_file():
2a46da701dde Uploaded
fubar
parents:
diff changeset
158 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
159 if "." in entry.name:
2a46da701dde Uploaded
fubar
parents:
diff changeset
160 _, ext = os.path.splitext(entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
161 if ext in [".tgz", ".json"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
162 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
163 if ext in [".yml", ".xml", ".yaml"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
164 newname = f"{entry.name.replace('.','_')}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
165 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
166 newname = entry.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
167 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
168 newname = f"{entry.name}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
169 dest = os.path.join(self.repdir, newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
170 src = os.path.join(self.tooloutdir, entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
171 shutil.copyfile(src, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
172 with os.scandir('.') as outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
173 for entry in outs:
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
174 if not entry.is_file() or entry.name == "conda_activate.log":
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
175 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
176 if "." in entry.name:
2a46da701dde Uploaded
fubar
parents:
diff changeset
177 _, ext = os.path.splitext(entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
178 if ext in [".yml", ".xml", ".yaml"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
179 newname = f"{entry.name.replace('.','_')}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
180 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
181 newname = entry.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
182 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
183 newname = f"{entry.name}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
184 dest = os.path.join(self.repdir, newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
185 src =entry.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
186 shutil.copyfile(src, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
187 if True or self.args.include_tests:
2a46da701dde Uploaded
fubar
parents:
diff changeset
188 with os.scandir(self.testdir) as outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
189 for entry in outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
190 if (not entry.is_file()) or entry.name.endswith(
2a46da701dde Uploaded
fubar
parents:
diff changeset
191 "_planemo_test_report.html"
2a46da701dde Uploaded
fubar
parents:
diff changeset
192 ):
2a46da701dde Uploaded
fubar
parents:
diff changeset
193 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
194 if "." in entry.name:
2a46da701dde Uploaded
fubar
parents:
diff changeset
195 _, ext = os.path.splitext(entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
196 if ext in [".tgz", ".json"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
197 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
198 if ext in [".yml", ".xml", ".yaml"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
199 newname = f"{entry.name.replace('.','_')}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
200 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
201 newname = entry.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
202 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
203 newname = f"{entry.name}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
204 dest = os.path.join(self.repdir, newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
205 src = os.path.join(self.testdir, entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
206 shutil.copyfile(src, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
207
2a46da701dde Uploaded
fubar
parents:
diff changeset
208
2a46da701dde Uploaded
fubar
parents:
diff changeset
209 def update_tests(self,ourdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
210 for xmlf in self.ourxmls:
2a46da701dde Uploaded
fubar
parents:
diff changeset
211 capture = self.call_planemo(xmlf,ourdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
212
2a46da701dde Uploaded
fubar
parents:
diff changeset
213 def main():
2a46da701dde Uploaded
fubar
parents:
diff changeset
214 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
215 This is a Galaxy wrapper.
2a46da701dde Uploaded
fubar
parents:
diff changeset
216 It expects to be called by a special purpose tool.xml
2a46da701dde Uploaded
fubar
parents:
diff changeset
217
2a46da701dde Uploaded
fubar
parents:
diff changeset
218 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
219 parser = argparse.ArgumentParser()
2a46da701dde Uploaded
fubar
parents:
diff changeset
220 a = parser.add_argument
2a46da701dde Uploaded
fubar
parents:
diff changeset
221 a("--in_tool_archive", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
222 a("--new_tested_tool_archive", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
223 a("--galaxy_root", default="/home/ross/gal21/")
2a46da701dde Uploaded
fubar
parents:
diff changeset
224 args = parser.parse_args()
2a46da701dde Uploaded
fubar
parents:
diff changeset
225 print('Hello from',os.getcwd())
2a46da701dde Uploaded
fubar
parents:
diff changeset
226 tt = ToolTester(args=args, in_tool_archive=args.in_tool_archive, new_tool_archive=args.new_tested_tool_archive)
2a46da701dde Uploaded
fubar
parents:
diff changeset
227
2a46da701dde Uploaded
fubar
parents:
diff changeset
228 if __name__ == "__main__":
2a46da701dde Uploaded
fubar
parents:
diff changeset
229 main()
2a46da701dde Uploaded
fubar
parents:
diff changeset
230
2a46da701dde Uploaded
fubar
parents:
diff changeset
231
2a46da701dde Uploaded
fubar
parents:
diff changeset
232 #end raw]]></configfile>
2a46da701dde Uploaded
fubar
parents:
diff changeset
233 </configfiles>
2a46da701dde Uploaded
fubar
parents:
diff changeset
234 <inputs>
2a46da701dde Uploaded
fubar
parents:
diff changeset
235 <param name="new_tool_name" value="" type="hidden"/>
2a46da701dde Uploaded
fubar
parents:
diff changeset
236 <param name="in_tool_archive" type="data" optional="false" label="Select a no_test tarfile to test and update for a toolshed" help="" format="toolshed.gz" multiple="false"/>
2a46da701dde Uploaded
fubar
parents:
diff changeset
237 </inputs>
2a46da701dde Uploaded
fubar
parents:
diff changeset
238 <outputs>
2a46da701dde Uploaded
fubar
parents:
diff changeset
239 <data name="new_tested_tool_archive" format="toolshed.gz" label="${in_tool_archive.name.split('_')[0]}_tested_toolshed.gz" hidden="false"/>
2a46da701dde Uploaded
fubar
parents:
diff changeset
240 <collection name="TF_run_report" type="list" label="${in_tool_archive.name} test Run reports">
2a46da701dde Uploaded
fubar
parents:
diff changeset
241 <discover_datasets pattern="__name_and_ext__" directory="TF_run_report" visible="false"/>
2a46da701dde Uploaded
fubar
parents:
diff changeset
242 </collection>
2a46da701dde Uploaded
fubar
parents:
diff changeset
243 </outputs>
2a46da701dde Uploaded
fubar
parents:
diff changeset
244 <tests>
2a46da701dde Uploaded
fubar
parents:
diff changeset
245 <test>
2a46da701dde Uploaded
fubar
parents:
diff changeset
246 <output name="new_tested_tool_archive" value="new_tested_tool_archive_sample" compare="sim_size" delta_frac="0.5"/>
2a46da701dde Uploaded
fubar
parents:
diff changeset
247 <param name="in_tool_archive" value="in_tool_archive_sample"/>
2a46da701dde Uploaded
fubar
parents:
diff changeset
248 <output_collection name="TF_run_report"/>
2a46da701dde Uploaded
fubar
parents:
diff changeset
249 </test>
2a46da701dde Uploaded
fubar
parents:
diff changeset
250 </tests>
2a46da701dde Uploaded
fubar
parents:
diff changeset
251 <help><![CDATA[
2a46da701dde Uploaded
fubar
parents:
diff changeset
252
2a46da701dde Uploaded
fubar
parents:
diff changeset
253 **What it Does**
2a46da701dde Uploaded
fubar
parents:
diff changeset
254
2a46da701dde Uploaded
fubar
parents:
diff changeset
255 ------
2a46da701dde Uploaded
fubar
parents:
diff changeset
256
2a46da701dde Uploaded
fubar
parents:
diff changeset
257 Script::
2a46da701dde Uploaded
fubar
parents:
diff changeset
258
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
259 # see https://github.com/fubar2/toolfactory
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
260 #
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
261 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
262 #
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
263 # all rights reserved
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
264 # Licensed under the LGPL
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
265 # suggestions for improvement and bug fixes welcome at
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
266 # https://github.com/fubar2/toolfactory
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
267
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
268 import argparse
2a46da701dde Uploaded
fubar
parents:
diff changeset
269 import copy
2a46da701dde Uploaded
fubar
parents:
diff changeset
270 import os
2a46da701dde Uploaded
fubar
parents:
diff changeset
271 import subprocess
2a46da701dde Uploaded
fubar
parents:
diff changeset
272 import shutil
2a46da701dde Uploaded
fubar
parents:
diff changeset
273 import sys
2a46da701dde Uploaded
fubar
parents:
diff changeset
274 import tarfile
2a46da701dde Uploaded
fubar
parents:
diff changeset
275 import tempfile
2a46da701dde Uploaded
fubar
parents:
diff changeset
276 import time
2a46da701dde Uploaded
fubar
parents:
diff changeset
277 import xml.etree.ElementTree as ET
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
278
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
279
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
280 myversion = "V2.2 April 2021"
2a46da701dde Uploaded
fubar
parents:
diff changeset
281 verbose = True
2a46da701dde Uploaded
fubar
parents:
diff changeset
282 debug = True
2a46da701dde Uploaded
fubar
parents:
diff changeset
283 toolFactoryURL = "https://github.com/fubar2/toolfactory"
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
284
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
285 def timenow():
2a46da701dde Uploaded
fubar
parents:
diff changeset
286 """return current time as a string"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
287 return time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(time.time()))
2a46da701dde Uploaded
fubar
parents:
diff changeset
288
2a46da701dde Uploaded
fubar
parents:
diff changeset
289 class ToolTester():
2a46da701dde Uploaded
fubar
parents:
diff changeset
290 # uncompress passed tar, run planemo and rebuild a new tarball with tests
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
291
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
292 def __init__(self, args=None, in_tool_archive='/galaxy-central/tools/newtool/newtool_toolshed.gz', new_tool_archive=None):
2a46da701dde Uploaded
fubar
parents:
diff changeset
293 self.args = args
2a46da701dde Uploaded
fubar
parents:
diff changeset
294 self.new_tool_archive = new_tool_archive
2a46da701dde Uploaded
fubar
parents:
diff changeset
295 assert tarfile.is_tarfile(in_tool_archive)
2a46da701dde Uploaded
fubar
parents:
diff changeset
296 # this is not going to go well with arbitrary names.
2a46da701dde Uploaded
fubar
parents:
diff changeset
297 tff = tarfile.open(in_tool_archive, "r:*")
2a46da701dde Uploaded
fubar
parents:
diff changeset
298 flist = tff.getnames()
2a46da701dde Uploaded
fubar
parents:
diff changeset
299 ourdir = os.path.commonpath(flist) # eg pyrevpos
2a46da701dde Uploaded
fubar
parents:
diff changeset
300 self.tool_name = ourdir
2a46da701dde Uploaded
fubar
parents:
diff changeset
301 ourxmls = [x for x in flist if x.lower().endswith('.xml') and os.path.split(x)[0] == ourdir]
2a46da701dde Uploaded
fubar
parents:
diff changeset
302 assert len(ourxmls) > 0
2a46da701dde Uploaded
fubar
parents:
diff changeset
303 self.ourxmls = ourxmls # [os.path.join(tool_path,x) for x in ourxmls]
2a46da701dde Uploaded
fubar
parents:
diff changeset
304 res = tff.extractall()
2a46da701dde Uploaded
fubar
parents:
diff changeset
305 self.update_tests(ourdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
306 tff.close()
2a46da701dde Uploaded
fubar
parents:
diff changeset
307 self.tooloutdir = "./tfout"
2a46da701dde Uploaded
fubar
parents:
diff changeset
308 self.repdir = "./TF_run_report"
2a46da701dde Uploaded
fubar
parents:
diff changeset
309 self.testdir = os.path.join(self.tooloutdir, "test-data")
2a46da701dde Uploaded
fubar
parents:
diff changeset
310 if not os.path.exists(self.tooloutdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
311 os.mkdir(self.tooloutdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
312 if not os.path.exists(self.testdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
313 os.mkdir(self.testdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
314 if not os.path.exists(self.repdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
315 os.mkdir(self.repdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
316 self.makeTool()
2a46da701dde Uploaded
fubar
parents:
diff changeset
317 self.moveRunOutputs()
2a46da701dde Uploaded
fubar
parents:
diff changeset
318 self.makeToolTar()
2a46da701dde Uploaded
fubar
parents:
diff changeset
319
2a46da701dde Uploaded
fubar
parents:
diff changeset
320 def call_planemo(self,xmlpath,ourdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
321 penv = os.environ
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
322 penv["PIP_CACHE_DIR"] = os.path.join(self.args.galaxy_root,'pipcache')
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
323 toolfile = os.path.split(xmlpath)[1]
2a46da701dde Uploaded
fubar
parents:
diff changeset
324 tool_name = self.tool_name
2a46da701dde Uploaded
fubar
parents:
diff changeset
325 tool_test_output = f"{tool_name}_planemo_test_report.html"
2a46da701dde Uploaded
fubar
parents:
diff changeset
326 cll = [
2a46da701dde Uploaded
fubar
parents:
diff changeset
327 "planemo",
2a46da701dde Uploaded
fubar
parents:
diff changeset
328 "test",
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
329 "--biocontainers",
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
330 "--test_output",
2a46da701dde Uploaded
fubar
parents:
diff changeset
331 os.path.abspath(tool_test_output),
2a46da701dde Uploaded
fubar
parents:
diff changeset
332 "--galaxy_root",
2a46da701dde Uploaded
fubar
parents:
diff changeset
333 self.args.galaxy_root,
2a46da701dde Uploaded
fubar
parents:
diff changeset
334 "--update_test_data",
2a46da701dde Uploaded
fubar
parents:
diff changeset
335 os.path.abspath(xmlpath),
2a46da701dde Uploaded
fubar
parents:
diff changeset
336 ]
2a46da701dde Uploaded
fubar
parents:
diff changeset
337 print(cll)
2a46da701dde Uploaded
fubar
parents:
diff changeset
338 p = subprocess.run(
2a46da701dde Uploaded
fubar
parents:
diff changeset
339 cll,
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
340 #capture_output=True,
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
341 encoding='utf8',
2a46da701dde Uploaded
fubar
parents:
diff changeset
342 env = penv,
2a46da701dde Uploaded
fubar
parents:
diff changeset
343 shell=False,
2a46da701dde Uploaded
fubar
parents:
diff changeset
344 )
2a46da701dde Uploaded
fubar
parents:
diff changeset
345 return p
2a46da701dde Uploaded
fubar
parents:
diff changeset
346
2a46da701dde Uploaded
fubar
parents:
diff changeset
347 def makeTool(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
348 """write xmls and input samples into place"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
349 for xreal in self.ourxmls:
2a46da701dde Uploaded
fubar
parents:
diff changeset
350 x = os.path.split(xreal)[1]
2a46da701dde Uploaded
fubar
parents:
diff changeset
351 xout = os.path.join(self.tooloutdir,x)
2a46da701dde Uploaded
fubar
parents:
diff changeset
352 shutil.copyfile(xreal, xout)
2a46da701dde Uploaded
fubar
parents:
diff changeset
353
2a46da701dde Uploaded
fubar
parents:
diff changeset
354 def makeToolTar(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
355 """move outputs into test-data and prepare the tarball"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
356 excludeme = "_planemo_test_report.html"
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
357
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
358 def exclude_function(tarinfo):
2a46da701dde Uploaded
fubar
parents:
diff changeset
359 filename = tarinfo.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
360 return None if filename.endswith(excludeme) else tarinfo
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
361
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
362 newtar = 'new_%s_toolshed.gz' % self.tool_name
2a46da701dde Uploaded
fubar
parents:
diff changeset
363 ttf = tarfile.open(newtar, "w:gz")
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
364 ttf.add(name=self.tool_name,
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
365 arcname=self.tool_name,
2a46da701dde Uploaded
fubar
parents:
diff changeset
366 filter=exclude_function)
2a46da701dde Uploaded
fubar
parents:
diff changeset
367 ttf.close()
2a46da701dde Uploaded
fubar
parents:
diff changeset
368 shutil.copyfile(newtar, self.new_tool_archive)
2a46da701dde Uploaded
fubar
parents:
diff changeset
369
2a46da701dde Uploaded
fubar
parents:
diff changeset
370 def moveRunOutputs(self):
2a46da701dde Uploaded
fubar
parents:
diff changeset
371 """need to move planemo or run outputs into toolfactory collection"""
2a46da701dde Uploaded
fubar
parents:
diff changeset
372 with os.scandir(self.tooloutdir) as outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
373 for entry in outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
374 if not entry.is_file():
2a46da701dde Uploaded
fubar
parents:
diff changeset
375 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
376 if "." in entry.name:
2a46da701dde Uploaded
fubar
parents:
diff changeset
377 _, ext = os.path.splitext(entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
378 if ext in [".tgz", ".json"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
379 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
380 if ext in [".yml", ".xml", ".yaml"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
381 newname = f"{entry.name.replace('.','_')}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
382 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
383 newname = entry.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
384 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
385 newname = f"{entry.name}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
386 dest = os.path.join(self.repdir, newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
387 src = os.path.join(self.tooloutdir, entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
388 shutil.copyfile(src, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
389 with os.scandir('.') as outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
390 for entry in outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
391 if not entry.is_file():
2a46da701dde Uploaded
fubar
parents:
diff changeset
392 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
393 if "." in entry.name:
2a46da701dde Uploaded
fubar
parents:
diff changeset
394 _, ext = os.path.splitext(entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
395 if ext in [".yml", ".xml", ".yaml"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
396 newname = f"{entry.name.replace('.','_')}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
397 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
398 newname = entry.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
399 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
400 newname = f"{entry.name}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
401 dest = os.path.join(self.repdir, newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
402 src =entry.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
403 shutil.copyfile(src, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
404 if True or self.args.include_tests:
2a46da701dde Uploaded
fubar
parents:
diff changeset
405 with os.scandir(self.testdir) as outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
406 for entry in outs:
2a46da701dde Uploaded
fubar
parents:
diff changeset
407 if (not entry.is_file()) or entry.name.endswith(
2a46da701dde Uploaded
fubar
parents:
diff changeset
408 "_planemo_test_report.html"
2a46da701dde Uploaded
fubar
parents:
diff changeset
409 ):
2a46da701dde Uploaded
fubar
parents:
diff changeset
410 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
411 if "." in entry.name:
2a46da701dde Uploaded
fubar
parents:
diff changeset
412 _, ext = os.path.splitext(entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
413 if ext in [".tgz", ".json"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
414 continue
2a46da701dde Uploaded
fubar
parents:
diff changeset
415 if ext in [".yml", ".xml", ".yaml"]:
2a46da701dde Uploaded
fubar
parents:
diff changeset
416 newname = f"{entry.name.replace('.','_')}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
417 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
418 newname = entry.name
2a46da701dde Uploaded
fubar
parents:
diff changeset
419 else:
2a46da701dde Uploaded
fubar
parents:
diff changeset
420 newname = f"{entry.name}.txt"
2a46da701dde Uploaded
fubar
parents:
diff changeset
421 dest = os.path.join(self.repdir, newname)
2a46da701dde Uploaded
fubar
parents:
diff changeset
422 src = os.path.join(self.testdir, entry.name)
2a46da701dde Uploaded
fubar
parents:
diff changeset
423 shutil.copyfile(src, dest)
2a46da701dde Uploaded
fubar
parents:
diff changeset
424
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
425
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
426 def update_tests(self,ourdir):
2a46da701dde Uploaded
fubar
parents:
diff changeset
427 for xmlf in self.ourxmls:
2a46da701dde Uploaded
fubar
parents:
diff changeset
428 capture = self.call_planemo(xmlf,ourdir)
2a46da701dde Uploaded
fubar
parents:
diff changeset
429
2a46da701dde Uploaded
fubar
parents:
diff changeset
430 def main():
2a46da701dde Uploaded
fubar
parents:
diff changeset
431 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
432 This is a Galaxy wrapper.
2a46da701dde Uploaded
fubar
parents:
diff changeset
433 It expects to be called by a special purpose tool.xml
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
434
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
435 """
2a46da701dde Uploaded
fubar
parents:
diff changeset
436 parser = argparse.ArgumentParser()
2a46da701dde Uploaded
fubar
parents:
diff changeset
437 a = parser.add_argument
2a46da701dde Uploaded
fubar
parents:
diff changeset
438 a("--in_tool_archive", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
439 a("--new_tested_tool_archive", default=None)
2a46da701dde Uploaded
fubar
parents:
diff changeset
440 a("--galaxy_root", default="/home/ross/gal21/")
2a46da701dde Uploaded
fubar
parents:
diff changeset
441 args = parser.parse_args()
2a46da701dde Uploaded
fubar
parents:
diff changeset
442 print('Hello from',os.getcwd())
2a46da701dde Uploaded
fubar
parents:
diff changeset
443 tt = ToolTester(args=args, in_tool_archive=args.in_tool_archive, new_tool_archive=args.new_tested_tool_archive)
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
444
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
445 if __name__ == "__main__":
2a46da701dde Uploaded
fubar
parents:
diff changeset
446 main()
2a46da701dde Uploaded
fubar
parents:
diff changeset
447
6
efefe43f23c8 Uploaded
fubar
parents: 4
diff changeset
448
4
2a46da701dde Uploaded
fubar
parents:
diff changeset
449 ]]></help>
2a46da701dde Uploaded
fubar
parents:
diff changeset
450 <citations>
2a46da701dde Uploaded
fubar
parents:
diff changeset
451 <citation type="doi">10.1093/bioinformatics/bts573</citation>
2a46da701dde Uploaded
fubar
parents:
diff changeset
452 </citations>
2a46da701dde Uploaded
fubar
parents:
diff changeset
453 </tool>
2a46da701dde Uploaded
fubar
parents:
diff changeset
454