annotate toolfactory/ToolFactory_tester.py @ 0:f288fab71d8b draft default tip

Uploaded
author fubar2
date Mon, 26 Apr 2021 04:18:54 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
1 # see https://github.com/fubar2/toolfactory
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
2 #
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
3 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
4 #
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
5 # all rights reserved
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
6 # Licensed under the LGPL
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
7 # suggestions for improvement and bug fixes welcome at
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
8 # https://github.com/fubar2/toolfactory
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
9 #
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
10 # July 2020: BCC was fun and I feel like rip van winkle after 5 years.
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
11 # Decided to
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
12 # 1. Fix the toolfactory so it works - done for simplest case
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
13 # 2. Fix planemo so the toolfactory function works
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
14 # 3. Rewrite bits using galaxyxml functions where that makes sense - done
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
15
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
16 import argparse
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
17 import copy
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
18 import os
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
19 import subprocess
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
20 import shutil
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
21 import sys
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
22 import tarfile
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
23 import tempfile
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
24 import time
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
25
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
26 myversion = "V2.2 April 2021"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
27 verbose = True
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
28 debug = True
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
29 toolFactoryURL = "https://github.com/fubar2/toolfactory"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
30
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
31 def timenow():
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
32 """return current time as a string"""
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
33 return time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(time.time()))
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
34
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
35 class ToolTester():
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
36 # requires highly insecure docker settings - like write to tool_conf.xml and to tools !
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
37 # if in a container possibly not so courageous.
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
38 # Fine on your own laptop but security red flag for most production instances
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
39 # uncompress passed tar, run planemo and rebuild a new tarball with tests
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
40
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
41 def __init__(self, args=None, in_tool_archive='/galaxy-central/tools/newtool/newtool_toolshed.gz', new_tool_archive=None):
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
42 self.args = args
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
43 self.new_tool_archive = new_tool_archive
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
44 assert tarfile.is_tarfile(in_tool_archive)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
45 # this is not going to go well with arbitrary names. TODO introspect tool xml!
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
46 self.tooloutdir = "./tfout"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
47 self.repdir = "./TF_run_report"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
48 self.testdir = os.path.join(self.tooloutdir, "test-data")
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
49 if not os.path.exists(self.tooloutdir):
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
50 os.mkdir(self.tooloutdir)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
51 if not os.path.exists(self.testdir):
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
52 os.mkdir(self.testdir)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
53 if not os.path.exists(self.repdir):
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
54 os.mkdir(self.repdir)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
55 tff = tarfile.open(in_tool_archive, "r:*")
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
56 flist = tff.getnames()
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
57 ourdir = os.path.commonpath(flist) # eg pyrevpos
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
58 self.tool_name = ourdir
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
59 ourxmls = [x for x in flist if x.lower().endswith('.xml') and os.path.split(x)[0] == ourdir]
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
60 assert len(ourxmls) > 0
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
61 self.ourxmls = ourxmls # [os.path.join(tool_path,x) for x in ourxmls]
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
62 res = tff.extractall()
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
63 tff.close()
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
64 self.update_tests(ourdir)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
65 self.makeTool()
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
66 self.moveRunOutputs()
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
67 self.makeToolTar()
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
68
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
69 def call_planemo(self,xmlpath,ourdir):
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
70 penv = os.environ
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
71 penv['HOME'] = '/home/ross/galaxy-release_21.01'
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
72 toolfile = os.path.split(xmlpath)[1]
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
73 tool_name = self.tool_name
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
74 tool_test_output = f"{tool_name}_planemo_test_report.html"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
75 cll = [
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
76 "planemo",
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
77 "test",
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
78 "--test_output",
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
79 os.path.abspath(tool_test_output),
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
80 "--galaxy_root",
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
81 self.args.galaxy_root,
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
82 "--update_test_data",
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
83 os.path.abspath(xmlpath),
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
84 ]
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
85 print(cll)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
86 p = subprocess.run(
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
87 cll,
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
88 capture_output=True,
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
89 encoding='utf8',
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
90 env = penv,
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
91 shell=False,
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
92 )
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
93 return p
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
94
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
95 def makeTool(self):
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
96 """write xmls and input samples into place"""
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
97 for xreal in self.ourxmls:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
98 x = os.path.split(xreal)[1]
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
99 xout = os.path.join(self.tooloutdir,x)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
100 shutil.copyfile(xreal, xout)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
101 # for p in self.infiles:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
102 # pth = p["name"]
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
103 # dest = os.path.join(self.testdir, "%s_sample" % p["infilename"])
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
104 # shutil.copyfile(pth, dest)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
105 # dest = os.path.join(self.repdir, "%s_sample" % p["infilename"])
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
106 # shutil.copyfile(pth, dest)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
107
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
108 def makeToolTar(self):
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
109 """move outputs into test-data and prepare the tarball"""
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
110 excludeme = "_planemo_test_report.html"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
111
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
112 def exclude_function(tarinfo):
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
113 filename = tarinfo.name
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
114 return None if filename.endswith(excludeme) else tarinfo
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
115
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
116 newtar = 'new_%s_toolshed.gz' % self.tool_name
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
117 ttf = tarfile.open(newtar, "w:gz")
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
118 ttf.add(name=self.tooloutdir,
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
119 arcname=self.tool_name,
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
120 filter=exclude_function)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
121 ttf.close()
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
122 shutil.copyfile(newtar, self.new_tool_archive)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
123
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
124 def moveRunOutputs(self):
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
125 """need to move planemo or run outputs into toolfactory collection"""
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
126 with os.scandir(self.tooloutdir) as outs:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
127 for entry in outs:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
128 if not entry.is_file():
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
129 continue
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
130 if "." in entry.name:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
131 _, ext = os.path.splitext(entry.name)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
132 if ext in [".tgz", ".json"]:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
133 continue
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
134 if ext in [".yml", ".xml", ".yaml"]:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
135 newname = f"{entry.name.replace('.','_')}.txt"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
136 else:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
137 newname = entry.name
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
138 else:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
139 newname = f"{entry.name}.txt"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
140 dest = os.path.join(self.repdir, newname)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
141 src = os.path.join(self.tooloutdir, entry.name)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
142 shutil.copyfile(src, dest)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
143 with os.scandir('.') as outs:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
144 for entry in outs:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
145 if not entry.is_file():
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
146 continue
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
147 if "." in entry.name:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
148 _, ext = os.path.splitext(entry.name)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
149 if ext in [".yml", ".xml", ".yaml"]:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
150 newname = f"{entry.name.replace('.','_')}.txt"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
151 else:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
152 newname = entry.name
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
153 else:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
154 newname = f"{entry.name}.txt"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
155 dest = os.path.join(self.repdir, newname)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
156 src =entry.name
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
157 shutil.copyfile(src, dest)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
158 if True or self.args.include_tests:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
159 with os.scandir(self.testdir) as outs:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
160 for entry in outs:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
161 if (not entry.is_file()) or entry.name.endswith(
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
162 "_planemo_test_report.html"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
163 ):
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
164 continue
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
165 if "." in entry.name:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
166 _, ext = os.path.splitext(entry.name)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
167 if ext in [".tgz", ".json"]:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
168 continue
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
169 if ext in [".yml", ".xml", ".yaml"]:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
170 newname = f"{entry.name.replace('.','_')}.txt"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
171 else:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
172 newname = entry.name
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
173 else:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
174 newname = f"{entry.name}.txt"
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
175 dest = os.path.join(self.repdir, newname)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
176 src = os.path.join(self.testdir, entry.name)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
177 shutil.copyfile(src, dest)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
178
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
179
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
180 def update_tests(self,ourdir):
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
181 for xmlf in self.ourxmls:
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
182 capture = self.call_planemo(xmlf,ourdir)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
183 #sys.stderr.write('%s, stdout=%s, stderr=%s' % (xmlf, capture.stdout, capture.stdout))
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
184 print('%s, stdout=%s, stderr=%s' % (capture.stdout, capture.stdout,xmlf))
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
185
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
186 def main():
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
187 """
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
188 This is a Galaxy wrapper.
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
189 It expects to be called by a special purpose tool.xml
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
190
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
191 """
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
192 parser = argparse.ArgumentParser()
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
193 a = parser.add_argument
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
194 a("--in_tool_archive", default=None)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
195 a("--new_tested_tool_archive", default=None)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
196 a("--galaxy_root", default="/home/ross/galaxy-release_21.01/")
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
197 args = parser.parse_args()
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
198 print('Hello from',os.getcwd())
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
199 tt = ToolTester(args=args, in_tool_archive=args.in_tool_archive, new_tool_archive=args.new_tested_tool_archive)
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
200
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
201 if __name__ == "__main__":
f288fab71d8b Uploaded
fubar2
parents:
diff changeset
202 main()