annotate fubar-galaxytoolfactory-1bf2d54452a6/rgToolFactory.py @ 4:b8edcaf27335 draft

Functional tests now built - not sure if they're correctly auto-installed yet
author fubar
date Sun, 10 Jun 2012 19:28:12 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
1 # rgToolFactory.py
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
2 # see https://bitbucket.org/fubar/galaxytoolfactory/wiki/Home
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
3 #
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
4 # copyright ross lazarus (ross stop lazarus at gmail stop com) May 2012
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
5 #
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
6 # all rights reserved
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
7 # Licensed under the LGPL
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
8 # suggestions for improvement and bug fixes welcome at https://bitbucket.org/fubar/galaxytoolfactory/wiki/Home
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
9
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
10 # This is a Galaxy tool factory for simple scripts in python, R or whatever ails ye.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
11 # It also serves as the wrapper for the new tool.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
12 #
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
13 # you paste and run your script
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
14 # Only works for simple scripts that read one input from the history.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
15 # Optionally can write one new history dataset,
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
16 # and optionally collect any number of outputs into links on an autogenerated HTML page.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
17
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
18 # DO NOT install on a public or important site - please.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
19
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
20 # installed generated tools are fine if the script is safe.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
21 # They just run normally and their user cannot do anything unusually insecure
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
22 # but please, practice safe toolshed.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
23 # Read the fucking code before you install any tool
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
24 # especially this one
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
25
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
26 # After you get the script working on some test data, you can
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
27 # optionally generate a toolshed compatible gzip file
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
28 # containing your script safely wrapped as an ordinary Galaxy script in your local toolshed for
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
29 # safe and largely automated installation in a production Galaxy.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
30
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
31 # If you opt for an HTML output, you get all the script outputs arranged
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
32 # as a single Html history item - all output files are linked, thumbnails for all the pdfs.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
33 # Ugly but really inexpensive.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
34 #
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
35 # Patches appreciated please.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
36 #
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
37 #
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
38 # long route to June 2012 product
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
39 # Behold the awesome power of Galaxy and the toolshed with the tool factory binds to bind them
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
40 # derived from an integrated script model
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
41 # called rgBaseScriptWrapper.py
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
42 # Note to the unwary:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
43 # This tool allows arbitrary scripting on your Galaxy as the Galaxy user
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
44 # There is nothing stopping a malicious user doing whatever they choose
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
45 # Extremely dangerous!!
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
46 # Totally insecure. So, trusted users only
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
47 #
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
48 # preferred model is a developer using their throw away workstation instance - ie a private site.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
49 # no real risk. The universe_wsgi.ini admin_users string is checked - only admin users are permitted to run this tool.
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
50 #
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
51
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
52 import sys
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
53 import shutil
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
54 import subprocess
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
55 import os
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
56 import time
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
57 import tempfile
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
58 import optparse
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
59 import tarfile
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
60 import re
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
61 import shutil
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
62
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
63 progname = os.path.split(sys.argv[0])[1]
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
64 myversion = 'V000.2 June 2012'
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
65 verbose = False
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
66 debug = False
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
67
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
68
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
69 def timenow():
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
70 """return current time as a string
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
71 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
72 return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
73
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
74
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
75 class ScriptRunner:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
76 """class is a wrapper for an arbitrary script
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
77 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
78
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
79 def __init__(self,opts=None):
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
80 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
81 cleanup inputs, setup some outputs
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
82
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
83 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
84 if opts.output_dir: # simplify for the tool tarball
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
85 os.chdir(opts.output_dir)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
86 self.thumbformat = 'jpg'
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
87 self.opts = opts
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
88 self.toolname = re.sub('[^a-zA-Z0-9_]+', '', opts.tool_name) # a sanitizer now does this but..
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
89 self.toolid = self.toolname
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
90 s = open(self.opts.script_path,'r').read()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
91 self.script = s
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
92 self.myname = sys.argv[0] # get our name because we write ourselves out as a tool later
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
93 self.pyfile = self.myname # crude but efficient - the cruft won't hurt much
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
94 self.xmlfile = '%s.xml' % self.toolname
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
95 self.sfile = '%s.%s' % (self.toolname,opts.interpreter)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
96 if opts.output_dir: # may not want these complexities
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
97 self.tlog = os.path.join(opts.output_dir,"%s_runner.log" % self.toolname)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
98 artifactpath = os.path.join(opts.output_dir,'%s_run.script' % self.toolname)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
99 artifact = open(artifactpath,'w')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
100 artifact.write(self.script)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
101 artifact.write('\n')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
102 artifact.close()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
103 if opts.make_Tool: # need this code and the user script for the tarball
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
104 localscript = open(self.sfile,'w')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
105 localscript.write(self.script)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
106 localscript.close()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
107 self.cl = []
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
108 self.html = []
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
109 a = self.cl.append
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
110 a(opts.interpreter)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
111 a('-') # use stdin
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
112 a(opts.input_tab)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
113 a(opts.output_tab)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
114 self.outFormats = 'tabular' # TODO make this an option at tool generation time
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
115 self.inputFormats = 'tabular' # TODO make this an option at tool generation time
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
116 self.test1Input = '%s_test1_input.xls' % self.toolname
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
117 self.test1Output = '%s_test1_output.xls' % self.toolname
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
118 self.test1HTML = '%s_test1_output.html' % self.toolname
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
119
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
120 def makeXML(self):
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
121 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
122 Create a Galaxy xml tool wrapper for the new script as a string to write out
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
123 fixme - use templating or something less fugly than this example of what we produce
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
124
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
125 <tool id="reverse" name="reverse" version="0.01">
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
126 <description>a tabular file</description>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
127 <command interpreter="python">
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
128 reverse.py --script_path "$runMe" --interpreter "python"
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
129 --tool_name "reverse" --input_tab "$input1" --output_tab "$tab_file"
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
130 </command>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
131 <inputs>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
132 <param name="input1" type="data" format="tabular" label="Select a suitable input file from your history"/><param name="job_name" type="text" label="Supply a name for the outputs to remind you what they contain" value="reverse"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
133
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
134 </inputs>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
135 <outputs>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
136 <data format="tabular" name="tab_file" label="${job_name}"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
137
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
138 </outputs>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
139 <help>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
140
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
141 **What it Does**
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
142
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
143 Reverse the columns in a tabular file
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
144
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
145 </help>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
146 <configfiles>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
147 <configfile name="runMe">
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
148
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
149 # reverse order of columns in a tabular file
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
150 import sys
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
151 inp = sys.argv[1]
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
152 outp = sys.argv[2]
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
153 i = open(inp,'r')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
154 o = open(outp,'w')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
155 for row in i:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
156 rs = row.rstrip().split('\t')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
157 rs.reverse()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
158 o.write('\t'.join(rs))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
159 o.write('\n')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
160 i.close()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
161 o.close()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
162
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
163
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
164 </configfile>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
165 </configfiles>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
166 </tool>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
167
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
168 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
169 newXML="""<tool id="%(toolid)s" name="%(toolname)s" version="0.01">
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
170 %(tooldesc)s
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
171 %(command)s
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
172 <inputs>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
173 %(inputs)s
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
174 </inputs>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
175 <outputs>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
176 %(outputs)s
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
177 </outputs>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
178 <help>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
179 %(help)s
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
180 </help>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
181 <configfiles>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
182 <configfile name="runMe">
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
183 %(script)s
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
184 </configfile>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
185 </configfiles>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
186 %(tooltests)s
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
187 </tool>""" # needs a dict with toolname, toolid, interpreter, scriptname, command, inputs as a multi line string ready to write, outputs ditto, help ditto
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
188
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
189 newCommand="""<command interpreter="python">
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
190 %(toolname)s.py --script_path "$runMe" --interpreter "%(interpreter)s"
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
191 --tool_name "%(toolname)s" %(command_inputs)s %(command_outputs)s
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
192 </command>""" # may NOT be an input or htmlout
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
193 tooltestsTabOnly = """<tests><test>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
194 <param name="input1" value="%s" ftype="tabular"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
195 <param name="job_name" value="test1"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
196 <param name="runMe" value="$runMe"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
197 <output name="tab_file" file="%s" ftype="tabular"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
198 </test></tests>"""
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
199 tooltestsHTMLOnly = """<tests><test>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
200 <param name="input1" value="%s" ftype="tabular"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
201 <param name="job_name" value="test1"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
202 <param name="runMe" value="$runMe"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
203 <output name="html_file" file="%s" ftype="html" lines_diff="5"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
204 </test></tests>"""
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
205 tooltestsBoth = """<tests><test>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
206 <param name="input1" value="%s" ftype="tabular"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
207 <param name="job_name" value="test1"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
208 <param name="runMe" value="$runMe"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
209 <output name="tab_file" file="%s" ftype="tabular" />
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
210 <output name="html_file" file="%s" ftype="html" lines_diff="10"/>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
211 </test></tests>"""
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
212 xdict = {}
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
213 if self.opts.make_HTML and self.opts.output_tab <> 'None':
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
214 xdict['tooltests'] = tooltestsBoth % (self.test1Input,self.test1Output,self.test1HTML)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
215 elif self.opts.make_HTML:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
216 xdict['tooltests'] = tooltestsHTMLOnly % (self.test1Input,self.test1HTML)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
217 else:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
218 xdict['tooltests'] = tooltestsTabOnly % (self.test1Input,self.test1Output)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
219 xdict['script'] = self.script # configfile is least painful way to embed script to avoid external dependencies
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
220 if self.opts.help_text:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
221 xdict['help'] = open(self.opts.help_text,'r').read()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
222 else:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
223 xdict['help'] = 'Please ask the tool author for help as none was supplied at tool generation'
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
224 if self.opts.tool_desc:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
225 xdict['tooldesc'] = '<description>%s</description>' % self.opts.tool_desc
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
226 else:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
227 xdict['tooldesc'] = ''
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
228 xdict['command_outputs'] = ''
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
229 xdict['outputs'] = ''
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
230 if self.opts.input_tab <> 'None':
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
231 xdict['command_inputs'] = '--input_tab "$input1"'
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
232 xdict['inputs'] = '<param name="input1" type="data" format="%s" label="Select a suitable input file from your history"/>' % self.inputFormats
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
233 else:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
234 xdict['command_inputs'] = '' # assume no input - eg a random data generator
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
235 xdict['inputs'] = ''
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
236 xdict['inputs'] += '<param name="job_name" type="text" label="Supply a name for the outputs to remind you what they contain" value="%s"/>\n' % self.toolname
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
237 xdict['toolname'] = self.toolname
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
238 xdict['toolid'] = self.toolid
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
239 xdict['interpreter'] = self.opts.interpreter
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
240 xdict['scriptname'] = self.sfile
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
241 if self.opts.make_HTML:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
242 xdict['command_outputs'] += '--output_dir "$html_file.files_path" --output_html "$html_file" --make_HTML "yes"'
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
243 xdict['outputs'] += '<data format="html" name="html_file" label="${job_name}.html"/>\n'
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
244 if self.opts.output_tab <> 'None':
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
245 xdict['command_outputs'] += '--output_tab "$tab_file"'
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
246 xdict['outputs'] += '<data format="%s" name="tab_file" label="${job_name}"/>\n' % self.outFormats
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
247 xdict['command'] = newCommand % xdict
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
248 xmls = newXML % xdict
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
249 xf = open(self.xmlfile,'w')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
250 xf.write(xmls)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
251 xf.write('\n')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
252 xf.close()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
253 # ready for the tarball
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
254
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
255
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
256 def makeTooltar(self):
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
257 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
258 a tool is a gz tarball with eg
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
259 /toolname/tool.xml /toolname/tool.py /toolname/test-data/test1_in.foo ...
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
260 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
261 retval = self.run()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
262 if retval:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
263 print >> sys.stderr,'## Run failed. Cannot build yet. Please fix and retry'
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
264 sys.exit(1)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
265 self.makeXML()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
266 tdir = self.toolname
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
267 os.mkdir(tdir)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
268 if self.opts.input_tab <> 'None': # no reproducible test otherwise? TODO: maybe..
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
269 testdir = os.path.join(tdir,'test-data')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
270 os.mkdir(testdir) # make tests directory
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
271 shutil.copyfile(self.opts.input_tab,os.path.join(testdir,self.test1Input))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
272 if self.opts.output_tab <> 'None':
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
273 shutil.copyfile(self.opts.output_tab,os.path.join(testdir,self.test1Output))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
274 if self.opts.make_HTML:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
275 shutil.copyfile(self.opts.output_html,os.path.join(testdir,self.test1HTML))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
276 if self.opts.output_dir:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
277 shutil.copyfile(self.tlog,os.path.join(testdir,'test1_out.log'))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
278 op = '%s.py' % self.toolname # new name
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
279 outpiname = os.path.join(tdir,op) # path for the tool tarball
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
280 pyin = os.path.basename(self.pyfile) # our name - we rewrite ourselves (TM)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
281 notes = ['# %s - a self annotated version of %s generated by running %s\n' % (op,pyin,pyin),]
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
282 notes.append('# to make a new Galaxy tool called %s\n' % self.toolname)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
283 notes.append('# User %s at %s\n' % (self.opts.user_email,timenow()))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
284 pi = open(self.pyfile,'r').readlines() # our code becomes new tool wrapper (!) - first Galaxy worm
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
285 notes += pi
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
286 outpi = open(outpiname,'w')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
287 outpi.write(''.join(notes))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
288 outpi.write('\n')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
289 outpi.close()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
290 print >> sys.stdout, 'wrote %s' % outpiname
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
291 shutil.copyfile(self.sfile,os.path.join(tdir,self.sfile))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
292 shutil.copyfile(self.xmlfile,os.path.join(tdir,self.xmlfile))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
293 tarpath = "%s.gz" % self.toolname
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
294 tar = tarfile.open(tarpath, "w:gz")
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
295 tar.add(tdir,arcname=self.toolname)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
296 tar.close()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
297 shutil.copyfile(tarpath,self.opts.new_tool)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
298 shutil.rmtree(tdir)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
299 ## TODO: replace with optional direct upload to local toolshed?
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
300 return retval
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
301
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
302 def compressPDF(self,inpdf=None,thumbformat='png'):
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
303 """need absolute path to pdf
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
304 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
305 assert os.path.isfile(inpdf), "## Input %s supplied to %s compressPDF not found" % (inpdf,self.myName)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
306 hf,hlog = tempfile.mkstemp(suffix="%s.log" % self.toolname)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
307 sto = open(hlog,'w')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
308 outpdf = '%s_compressed' % inpdf
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
309 cl = ["gs", "-sDEVICE=pdfwrite", "-dNOPAUSE", "-dBATCH", "-sOutputFile=%s" % outpdf,inpdf]
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
310 x = subprocess.Popen(cl,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
311 retval1 = x.wait()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
312 if retval1 == 0:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
313 os.unlink(inpdf)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
314 shutil.move(outpdf,inpdf)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
315 outpng = '%s.%s' % (os.path.splitext(inpdf)[0],thumbformat)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
316 cl2 = ['convert', inpdf, outpng]
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
317 x = subprocess.Popen(cl2,stdout=sto,stderr=sto,cwd=self.opts.output_dir)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
318 retval2 = x.wait()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
319 sto.close()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
320 retval = retval1 or retval2
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
321 return retval
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
322
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
323
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
324 def getfSize(self,fpath,outpath):
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
325 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
326 format a nice file size string
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
327 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
328 size = ''
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
329 fp = os.path.join(outpath,fpath)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
330 if os.path.isfile(fp):
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
331 n = float(os.path.getsize(fp))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
332 if n > 2**20:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
333 size = ' (%1.1f MB)' % (n/2**20)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
334 elif n > 2**10:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
335 size = ' (%1.1f KB)' % (n/2**10)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
336 elif n > 0:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
337 size = ' (%d B)' % (int(n))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
338 return size
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
339
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
340 def makeHtml(self):
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
341 """ Create an HTML file content to list all the artefacts found in the output_dir
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
342 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
343
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
344 galhtmlprefix = """<?xml version="1.0" encoding="utf-8" ?>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
345 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
346 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
347 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
348 <meta name="generator" content="Galaxy %s tool output - see http://g2.trac.bx.psu.edu/" />
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
349 <title></title>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
350 <link rel="stylesheet" href="/static/style/base.css" type="text/css" />
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
351 </head>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
352 <body>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
353 <div class="document">
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
354 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
355 galhtmlattr = """<hr/><b><a href="https://bitbucket.org/fubar/galaxytoolfactory/overview">Galaxy Tool Factory Script Wrapper</a> tool output %s run at %s</b><br/>"""
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
356 galhtmlpostfix = """</div></body></html>\n"""
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
357
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
358 flist = os.listdir(self.opts.output_dir)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
359 flist = [x for x in flist if x <> 'Rplots.pdf']
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
360 flist.sort()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
361 html = [galhtmlprefix % progname,]
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
362 html.append('<h2>Galaxy %s outputs run at %s</h2><br/>\n' % (self.toolname,timenow()))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
363 fhtml = []
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
364 if len(flist) > 0:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
365 html.append('<table cellpadding="3" cellspacing="3">\n')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
366 for fname in flist:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
367 dname,e = os.path.splitext(fname)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
368 sfsize = self.getfSize(fname,self.opts.output_dir)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
369 if e.lower() == '.pdf' : # compress and make a thumbnail
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
370 thumb = '%s.%s' % (dname,self.thumbformat)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
371 pdff = os.path.join(self.opts.output_dir,fname)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
372 retval = self.compressPDF(inpdf=pdff,thumbformat=self.thumbformat)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
373 if retval == 0:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
374 s= '<tr><td><a href="%s"><img src="%s" title="Click to download a PDF of %s" hspace="10" width="600"></a></td></tr>\n' % (fname,thumb,fname)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
375 html.append(s)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
376 fhtml.append('<li><a href="%s">%s %s</a></li>' % (fname,fname,sfsize))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
377 else:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
378 fhtml.append('<li><a href="%s">%s %s</a></li>' % (fname,fname,sfsize))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
379 html.append('</table>\n')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
380 if len(fhtml) > 0:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
381 fhtml.insert(0,'<ul>')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
382 fhtml.append('</ul><br/>')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
383 html += fhtml # add all non-pdf files to the end of the display
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
384 else:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
385 html.append('<h2>### Error - %s returned no files - please confirm that parameters are sane</h1>' % self.opts.interpreter)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
386 html.append('<h3>%s log follows below</h3><hr/><pre><br/>\n' % self.opts.interpreter)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
387 rlog = open(self.tlog,'r').readlines()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
388 html += rlog
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
389 html.append('<br/>%s CL = %s<br/>\n' % (self.toolname,' '.join(sys.argv)))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
390 html.append('</pre>\n')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
391 html.append(galhtmlattr % (progname,timenow()))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
392 html.append(galhtmlpostfix)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
393 htmlf = file(self.opts.output_html,'w')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
394 htmlf.write('\n'.join(html))
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
395 htmlf.write('\n')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
396 htmlf.close()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
397 self.html = html
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
398
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
399
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
400 def run(self):
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
401 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
402 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
403 if self.opts.output_dir:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
404 sto = open(self.tlog,'w')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
405 p = subprocess.Popen(' '.join(self.cl),shell=True,stdout=sto,stderr=sto,stdin=subprocess.PIPE,cwd=self.opts.output_dir)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
406 else:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
407 p = subprocess.Popen(' '.join(self.cl),shell=True,stdin=subprocess.PIPE)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
408 p.stdin.write(self.script)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
409 p.stdin.close()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
410 retval = p.wait()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
411 if self.opts.output_dir:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
412 sto.close()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
413 if self.opts.make_HTML:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
414 self.makeHtml()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
415 return retval
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
416
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
417
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
418 def main():
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
419 u = """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
420 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
421 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript"
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
422 </command>
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
423 """
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
424 op = optparse.OptionParser()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
425 a = op.add_option
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
426 a('--script_path',default=None)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
427 a('--tool_name',default=None)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
428 a('--interpreter',default=None)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
429 a('--output_dir',default=None)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
430 a('--output_html',default=None)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
431 a('--input_tab',default="None")
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
432 a('--output_tab',default="None")
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
433 a('--user_email',default='Unknown')
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
434 a('--bad_user',default=None)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
435 a('--make_Tool',default=None)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
436 a('--make_HTML',default=None)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
437 a('--help_text',default=None)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
438 a('--tool_desc',default=None)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
439 a('--new_tool',default=None)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
440 opts, args = op.parse_args()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
441 assert not opts.bad_user,'UNAUTHORISED: %s is NOT authorized to use this tool until Galaxy admin adds %s to admin_users in universe_wsgi.ini' % (opts.bad_user,opts.bad_user)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
442 assert opts.tool_name,'## Tool Factory expects a tool name - eg --tool_name=DESeq'
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
443 assert opts.interpreter,'## Tool Factory wrapper expects an interpreter - eg --interpreter=Rscript'
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
444 assert os.path.isfile(opts.script_path),'## Tool Factory wrapper expects a script path - eg --script_path=foo.R'
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
445 if opts.output_dir:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
446 try:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
447 os.makedirs(opts.output_dir)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
448 except:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
449 pass
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
450 r = ScriptRunner(opts)
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
451 if opts.make_Tool:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
452 retcode = r.makeTooltar()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
453 else:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
454 retcode = r.run()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
455 if retcode:
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
456 sys.exit(retcode) # indicate failure to job runner
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
457
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
458
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
459 if __name__ == "__main__":
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
460 main()
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
461
b8edcaf27335 Functional tests now built - not sure if they're correctly auto-installed yet
fubar
parents:
diff changeset
462