Mercurial > repos > mvdbeek > docker_toolfactory_alpha
comparison DockerToolFactory.py @ 0:7e0392d4531c
Initial Commit.
| author | m.vandenbeek@gmail.com |
|---|---|
| date | Tue, 02 Dec 2014 15:14:12 +0100 |
| parents | |
| children | 5d70248d1e01 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:7e0392d4531c |
|---|---|
| 1 # DockerToolFactory.py | |
| 2 # see https://bitbucket.org/mvdbeek/DockerToolFactory | |
| 3 | |
| 4 import sys | |
| 5 import shutil | |
| 6 import subprocess | |
| 7 import os | |
| 8 import time | |
| 9 import tempfile | |
| 10 import argparse | |
| 11 import tarfile | |
| 12 import re | |
| 13 import shutil | |
| 14 import math | |
| 15 import fileinput | |
| 16 from os.path import abspath | |
| 17 | |
| 18 progname = os.path.split(sys.argv[0])[1] | |
| 19 myversion = 'V001.1 March 2014' | |
| 20 verbose = False | |
| 21 debug = False | |
| 22 toolFactoryURL = 'https://bitbucket.org/fubar/galaxytoolfactory' | |
| 23 | |
| 24 # if we do html we need these dependencies specified in a tool_dependencies.xml file and referred to in the generated | |
| 25 # tool xml | |
| 26 toolhtmldepskel = """<?xml version="1.0"?> | |
| 27 <tool_dependency> | |
| 28 <package name="ghostscript" version="9.10"> | |
| 29 <repository name="package_ghostscript_9_10" owner="devteam" prior_installation_required="True" /> | |
| 30 </package> | |
| 31 <package name="graphicsmagick" version="1.3.18"> | |
| 32 <repository name="package_graphicsmagick_1_3" owner="iuc" prior_installation_required="True" /> | |
| 33 </package> | |
| 34 <readme> | |
| 35 %s | |
| 36 </readme> | |
| 37 </tool_dependency> | |
| 38 """ | |
| 39 | |
| 40 protorequirements = """<requirements> | |
| 41 <requirement type="package" version="9.10">ghostscript</requirement> | |
| 42 <requirement type="package" version="1.3.18">graphicsmagick</requirement> | |
| 43 <container type="docker">toolfactory/custombuild:%s</container> | |
| 44 </requirements>""" | |
| 45 | |
| 46 def timenow(): | |
| 47 """return current time as a string | |
| 48 """ | |
| 49 return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time())) | |
| 50 | |
| 51 html_escape_table = { | |
| 52 "&": "&", | |
| 53 ">": ">", | |
| 54 "<": "<", | |
| 55 "$": "\$" | |
| 56 } | |
| 57 | |
| 58 def html_escape(text): | |
| 59 """Produce entities within text.""" | |
| 60 return "".join(html_escape_table.get(c,c) for c in text) | |
| 61 | |
| 62 def cmd_exists(cmd): | |
| 63 return subprocess.call("type " + cmd, shell=True, | |
| 64 stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0 | |
| 65 | |
| 66 def edit_dockerfile(dockerfile): | |
| 67 '''we have to change the userid of galaxy inside the container to the id with which the tool is run, | |
| 68 otherwise we have a mismatch in the file permissions inside the container''' | |
| 69 uid=os.getuid() | |
| 70 for line in fileinput.FileInput(dockerfile, inplace=1): | |
| 71 sys.stdout.write(line.replace("RUN adduser galaxy\n", "RUN adduser galaxy -u {0}\n".format(uid))) | |
| 72 | |
| 73 def build_docker(dockerfile, docker_client, image_tag='base'): | |
| 74 '''Given the path to a dockerfile, and a docker_client, build the image, if it does not | |
| 75 exist yet.''' | |
| 76 image_id='toolfactory/custombuild:'+image_tag | |
| 77 existing_images=", ".join(["".join(d['RepoTags']) for d in docker_client.images()]) | |
| 78 if image_id in existing_images: | |
| 79 print 'docker container exists, skipping build' | |
| 80 return image_id | |
| 81 print "Building Docker image, using Dockerfile:{0}".format(dockerfile) | |
| 82 build_process=docker_client.build(fileobj=open(dockerfile, 'r'), image_tag=image_id) | |
| 83 print "succesfully dispatched docker build process, building now" | |
| 84 build_log=[line for line in build_process] #will block until image is built. | |
| 85 return image_id | |
| 86 | |
| 87 def construct_bind(host_path, container_path=False, binds=None, ro=True): | |
| 88 #TODO remove container_path if it's alwyas going to be the same as host_path | |
| 89 '''build or extend binds dictionary with container path. binds is used | |
| 90 to mount all files using the docker-py client.''' | |
| 91 if not binds: | |
| 92 binds={} | |
| 93 if isinstance(host_path, list): | |
| 94 for k,v in enumerate(host_path): | |
| 95 if not container_path: | |
| 96 container_path=host_path[k] | |
| 97 binds[host_path[k]]={'bind':container_path, 'ro':ro} | |
| 98 container_path=False #could be more elegant | |
| 99 return binds | |
| 100 else: | |
| 101 if not container_path: | |
| 102 container_path=host_path | |
| 103 binds[host_path]={'bind':container_path, 'ro':ro} | |
| 104 return binds | |
| 105 | |
| 106 def switch_to_docker(opts): | |
| 107 import docker #need local import, as container does not have docker-py | |
| 108 docker_client=docker.Client() | |
| 109 toolfactory_path=abspath(sys.argv[0]) | |
| 110 dockerfile=os.path.dirname(toolfactory_path)+'/Dockerfile' | |
| 111 edit_dockerfile(dockerfile) | |
| 112 image_id=build_docker(dockerfile, docker_client) | |
| 113 binds=construct_bind(host_path=opts.script_path, ro=False) | |
| 114 binds=construct_bind(binds=binds, host_path=abspath(opts.output_dir), ro=False) | |
| 115 if len(opts.input_tab)>0: | |
| 116 binds=construct_bind(binds=binds, host_path=opts.input_tab, ro=True) | |
| 117 if not opts.output_tab == 'None': | |
| 118 binds=construct_bind(binds=binds, host_path=opts.output_tab, ro=False) | |
| 119 if opts.make_HTML: | |
| 120 binds=construct_bind(binds=binds, host_path=opts.output_html, ro=False) | |
| 121 if opts.make_Tool: | |
| 122 binds=construct_bind(binds=binds, host_path=opts.new_tool, ro=False) | |
| 123 binds=construct_bind(binds=binds, host_path=opts.help_text, ro=True) | |
| 124 binds=construct_bind(binds=binds, host_path=toolfactory_path) | |
| 125 volumes=binds.keys() | |
| 126 sys.argv=[abspath(opts.output_dir) if sys.argv[i-1]=='--output_dir' else arg for i,arg in enumerate(sys.argv)] ##inject absolute path of working_dir | |
| 127 cmd=['python', '-u']+sys.argv+['--dockerized', '1'] | |
| 128 container=docker_client.create_container( | |
| 129 image=image_id, | |
| 130 user='galaxy', | |
| 131 volumes=volumes, | |
| 132 command=cmd | |
| 133 ) | |
| 134 docker_client.start(container=container[u'Id'], binds=binds) | |
| 135 docker_client.wait(container=container[u'Id']) | |
| 136 logs=docker_client.logs(container=container[u'Id']) | |
| 137 print "".join([log for log in logs]) | |
| 138 | |
| 139 class ScriptRunner: | |
| 140 """class is a wrapper for an arbitrary script | |
| 141 """ | |
| 142 | |
| 143 def __init__(self,opts=None,treatbashSpecial=True, image_tag='base'): | |
| 144 """ | |
| 145 cleanup inputs, setup some outputs | |
| 146 | |
| 147 """ | |
| 148 self.opts = opts | |
| 149 self.useGM = cmd_exists('gm') | |
| 150 self.useIM = cmd_exists('convert') | |
| 151 self.useGS = cmd_exists('gs') | |
| 152 self.temp_warned = False # we want only one warning if $TMP not set | |
| 153 self.treatbashSpecial = treatbashSpecial | |
| 154 self.image_tag = image_tag | |
| 155 os.chdir(abspath(opts.output_dir)) | |
| 156 self.thumbformat = 'png' | |
| 157 self.toolname = re.sub('[^a-zA-Z0-9_]+', '', opts.tool_name) # a sanitizer now does this but.. | |
| 158 self.toolid = self.toolname | |
| 159 self.myname = sys.argv[0] # get our name because we write ourselves out as a tool later | |
| 160 self.pyfile = self.myname # crude but efficient - the cruft won't hurt much | |
| 161 self.xmlfile = '%s.xml' % self.toolname | |
| 162 s = open(self.opts.script_path,'r').readlines() | |
| 163 s = [x.rstrip() for x in s] # remove pesky dos line endings if needed | |
| 164 self.script = '\n'.join(s) | |
| 165 fhandle,self.sfile = tempfile.mkstemp(prefix=self.toolname,suffix=".%s" % (opts.interpreter)) | |
| 166 tscript = open(self.sfile,'w') # use self.sfile as script source for Popen | |
| 167 tscript.write(self.script) | |
| 168 tscript.close() | |
| 169 self.indentedScript = '\n'.join([' %s' % html_escape(x) for x in s]) # for restructured text in help | |
| 170 self.escapedScript = '\n'.join([html_escape(x) for x in s]) | |
| 171 self.elog = os.path.join(self.opts.output_dir,"%s_error.log" % self.toolname) | |
| 172 if opts.output_dir: # may not want these complexities | |
| 173 self.tlog = os.path.join(self.opts.output_dir,"%s_runner.log" % self.toolname) | |
| 174 art = '%s.%s' % (self.toolname,opts.interpreter) | |
| 175 artpath = os.path.join(self.opts.output_dir,art) # need full path | |
| 176 artifact = open(artpath,'w') # use self.sfile as script source for Popen | |
| 177 artifact.write(self.script) | |
| 178 artifact.close() | |
| 179 self.cl = [] | |
| 180 self.html = [] | |
| 181 a = self.cl.append | |
| 182 a(opts.interpreter) | |
| 183 if self.treatbashSpecial and opts.interpreter in ['bash','sh']: | |
| 184 a(self.sfile) | |
| 185 else: | |
| 186 a('-') # stdin | |
| 187 for input in opts.input_tab: | |
| 188 a(input) | |
| 189 if opts.output_tab == 'None': #If tool generates only HTML, set output name to toolname | |
| 190 a(str(self.toolname)+'.out') | |
| 191 a(opts.output_tab) | |
| 192 for param in opts.additional_parameters: | |
| 193 param, value=param.split(',') | |
| 194 a('--'+param) | |
| 195 a(value) | |
| 196 #print self.cl | |
| 197 self.outFormats = opts.output_format | |
| 198 self.inputFormats = [formats for formats in opts.input_formats] | |
| 199 self.test1Input = '%s_test1_input.xls' % self.toolname | |
| 200 self.test1Output = '%s_test1_output.xls' % self.toolname | |
| 201 self.test1HTML = '%s_test1_output.html' % self.toolname | |
| 202 | |
| 203 def makeXML(self): | |
| 204 """ | |
| 205 Create a Galaxy xml tool wrapper for the new script as a string to write out | |
| 206 fixme - use templating or something less fugly than this example of what we produce | |
| 207 | |
| 208 <tool id="reverse" name="reverse" version="0.01"> | |
| 209 <description>a tabular file</description> | |
| 210 <command interpreter="python"> | |
| 211 reverse.py --script_path "$runMe" --interpreter "python" | |
| 212 --tool_name "reverse" --input_tab "$input1" --output_tab "$tab_file" | |
| 213 </command> | |
| 214 <inputs> | |
| 215 <param name="input1" type="data" format="tabular" label="Select a suitable input file from your history"/> | |
| 216 | |
| 217 </inputs> | |
| 218 <outputs> | |
| 219 <data format=opts.output_format name="tab_file"/> | |
| 220 | |
| 221 </outputs> | |
| 222 <help> | |
| 223 | |
| 224 **What it Does** | |
| 225 | |
| 226 Reverse the columns in a tabular file | |
| 227 | |
| 228 </help> | |
| 229 <configfiles> | |
| 230 <configfile name="runMe"> | |
| 231 | |
| 232 # reverse order of columns in a tabular file | |
| 233 import sys | |
| 234 inp = sys.argv[1] | |
| 235 outp = sys.argv[2] | |
| 236 i = open(inp,'r') | |
| 237 o = open(outp,'w') | |
| 238 for row in i: | |
| 239 rs = row.rstrip().split('\t') | |
| 240 rs.reverse() | |
| 241 o.write('\t'.join(rs)) | |
| 242 o.write('\n') | |
| 243 i.close() | |
| 244 o.close() | |
| 245 | |
| 246 | |
| 247 </configfile> | |
| 248 </configfiles> | |
| 249 </tool> | |
| 250 | |
| 251 """ | |
| 252 newXML="""<tool id="%(toolid)s" name="%(toolname)s" version="%(tool_version)s"> | |
| 253 %(tooldesc)s | |
| 254 %(requirements)s | |
| 255 <command interpreter="python"> | |
| 256 %(command)s | |
| 257 </command> | |
| 258 <inputs> | |
| 259 %(inputs)s | |
| 260 </inputs> | |
| 261 <outputs> | |
| 262 %(outputs)s | |
| 263 </outputs> | |
| 264 <configfiles> | |
| 265 <configfile name="runMe"> | |
| 266 %(script)s | |
| 267 </configfile> | |
| 268 </configfiles> | |
| 269 | |
| 270 %(tooltests)s | |
| 271 | |
| 272 <help> | |
| 273 | |
| 274 %(help)s | |
| 275 | |
| 276 </help> | |
| 277 </tool>""" # needs a dict with toolname, toolid, interpreter, scriptname, command, inputs as a multi line string ready to write, outputs ditto, help ditto | |
| 278 | |
| 279 newCommand=""" | |
| 280 %(toolname)s.py --script_path "$runMe" --interpreter "%(interpreter)s" | |
| 281 --tool_name "%(toolname)s" %(command_inputs)s %(command_outputs)s """ | |
| 282 # may NOT be an input or htmlout - appended later | |
| 283 tooltestsTabOnly = """ | |
| 284 <tests> | |
| 285 <test> | |
| 286 <param name="input1" value="%(test1Input)s" ftype="tabular"/> | |
| 287 <param name="runMe" value="$runMe"/> | |
| 288 <output name="tab_file" file="%(test1Output)s" ftype="tabular"/> | |
| 289 </test> | |
| 290 </tests> | |
| 291 """ | |
| 292 tooltestsHTMLOnly = """ | |
| 293 <tests> | |
| 294 <test> | |
| 295 <param name="input1" value="%(test1Input)s" ftype="tabular"/> | |
| 296 <param name="runMe" value="$runMe"/> | |
| 297 <output name="html_file" file="%(test1HTML)s" ftype="html" lines_diff="5"/> | |
| 298 </test> | |
| 299 </tests> | |
| 300 """ | |
| 301 tooltestsBoth = """<tests> | |
| 302 <test> | |
| 303 <param name="input1" value="%(test1Input)s" ftype="tabular"/> | |
| 304 <param name="runMe" value="$runMe"/> | |
| 305 <output name="tab_file" file="%(test1Output)s" ftype="tabular" /> | |
| 306 <output name="html_file" file="%(test1HTML)s" ftype="html" lines_diff="10"/> | |
| 307 </test> | |
| 308 </tests> | |
| 309 """ | |
| 310 xdict = {} | |
| 311 #xdict['requirements'] = '' | |
| 312 #if self.opts.make_HTML: | |
| 313 xdict['requirements'] = protorequirements % self.image_tag | |
| 314 xdict['tool_version'] = self.opts.tool_version | |
| 315 xdict['test1Input'] = self.test1Input | |
| 316 xdict['test1HTML'] = self.test1HTML | |
| 317 xdict['test1Output'] = self.test1Output | |
| 318 if self.opts.make_HTML and self.opts.output_tab <> 'None': | |
| 319 xdict['tooltests'] = tooltestsBoth % xdict | |
| 320 elif self.opts.make_HTML: | |
| 321 xdict['tooltests'] = tooltestsHTMLOnly % xdict | |
| 322 else: | |
| 323 xdict['tooltests'] = tooltestsTabOnly % xdict | |
| 324 xdict['script'] = self.escapedScript | |
| 325 # configfile is least painful way to embed script to avoid external dependencies | |
| 326 # but requires escaping of <, > and $ to avoid Mako parsing | |
| 327 if self.opts.help_text: | |
| 328 helptext = open(self.opts.help_text,'r').readlines() | |
| 329 helptext = [html_escape(x) for x in helptext] # must html escape here too - thanks to Marius van den Beek | |
| 330 xdict['help'] = ''.join([x for x in helptext]) | |
| 331 else: | |
| 332 xdict['help'] = 'Please ask the tool author (%s) for help as none was supplied at tool generation\n' % (self.opts.user_email) | |
| 333 coda = ['**Script**','Pressing execute will run the following code over your input file and generate some outputs in your history::'] | |
| 334 coda.append('\n') | |
| 335 coda.append(self.indentedScript) | |
| 336 coda.append('\n**Attribution**\nThis Galaxy tool was created by %s at %s\nusing the Galaxy Tool Factory.\n' % (self.opts.user_email,timenow())) | |
| 337 coda.append('See %s for details of that project' % (toolFactoryURL)) | |
| 338 coda.append('Please cite: Creating re-usable tools from scripts: The Galaxy Tool Factory. Ross Lazarus; Antony Kaspi; Mark Ziemann; The Galaxy Team. ') | |
| 339 coda.append('Bioinformatics 2012; doi: 10.1093/bioinformatics/bts573\n') | |
| 340 xdict['help'] = '%s\n%s' % (xdict['help'],'\n'.join(coda)) | |
| 341 if self.opts.tool_desc: | |
| 342 xdict['tooldesc'] = '<description>%s</description>' % self.opts.tool_desc | |
| 343 else: | |
| 344 xdict['tooldesc'] = '' | |
| 345 xdict['command_outputs'] = '' | |
| 346 xdict['outputs'] = '' | |
| 347 if self.opts.input_tab <> 'None': | |
| 348 xdict['command_inputs'] = '--input_tab' | |
| 349 xdict['inputs']='' | |
| 350 for i,input in enumerate(self.inputFormats): | |
| 351 xdict['inputs' ]+='<param name="input{0}" type="data" format="{1}" label="Select a suitable input file from your history"/> \n'.format(i+1, input) | |
| 352 xdict['command_inputs'] += ' $input{0}'.format(i+1) | |
| 353 else: | |
| 354 xdict['command_inputs'] = '' # assume no input - eg a random data generator | |
| 355 xdict['inputs'] = '' | |
| 356 # I find setting the job name not very logical. can be changed in workflows anyway. 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 | |
| 357 xdict['toolname'] = self.toolname | |
| 358 xdict['toolid'] = self.toolid | |
| 359 xdict['interpreter'] = self.opts.interpreter | |
| 360 xdict['scriptname'] = self.sfile | |
| 361 if self.opts.make_HTML: | |
| 362 xdict['command_outputs'] += ' --output_dir "$html_file.files_path" --output_html "$html_file" --make_HTML "yes"' | |
| 363 xdict['outputs'] += ' <data format="html" name="html_file"/>\n' | |
| 364 else: | |
| 365 xdict['command_outputs'] += ' --output_dir "./"' | |
| 366 #print self.opts.output_tab | |
| 367 if not self.opts.output_tab: | |
| 368 xdict['command_outputs'] += ' --output_tab "$tab_file"' | |
| 369 xdict['outputs'] += ' <data format="%s" name="tab_file"/>\n' % self.outFormats | |
| 370 xdict['command'] = newCommand % xdict | |
| 371 #print xdict['outputs'] | |
| 372 xmls = newXML % xdict | |
| 373 xf = open(self.xmlfile,'w') | |
| 374 xf.write(xmls) | |
| 375 xf.write('\n') | |
| 376 xf.close() | |
| 377 # ready for the tarball | |
| 378 | |
| 379 | |
| 380 def makeTooltar(self): | |
| 381 """ | |
| 382 a tool is a gz tarball with eg | |
| 383 /toolname/tool.xml /toolname/tool.py /toolname/test-data/test1_in.foo ... | |
| 384 """ | |
| 385 retval = self.run() | |
| 386 if retval: | |
| 387 print >> sys.stderr,'## Run failed. Cannot build yet. Please fix and retry' | |
| 388 sys.exit(1) | |
| 389 tdir = self.toolname | |
| 390 os.mkdir(tdir) | |
| 391 self.makeXML() | |
| 392 if self.opts.make_HTML: | |
| 393 if self.opts.help_text: | |
| 394 hlp = open(self.opts.help_text,'r').read() | |
| 395 else: | |
| 396 hlp = 'Please ask the tool author for help as none was supplied at tool generation\n' | |
| 397 if self.opts.include_dependencies: | |
| 398 tooldepcontent = toolhtmldepskel % hlp | |
| 399 depf = open(os.path.join(tdir,'tool_dependencies.xml'),'w') | |
| 400 depf.write(tooldepcontent) | |
| 401 depf.write('\n') | |
| 402 depf.close() | |
| 403 if self.opts.input_tab <> 'None': # no reproducible test otherwise? TODO: maybe.. | |
| 404 testdir = os.path.join(tdir,'test-data') | |
| 405 os.mkdir(testdir) # make tests directory | |
| 406 for i in self.opts.input_tab: | |
| 407 #print i | |
| 408 shutil.copyfile(i,os.path.join(testdir,self.test1Input)) | |
| 409 if not self.opts.output_tab: | |
| 410 shutil.copyfile(self.opts.output_tab,os.path.join(testdir,self.test1Output)) | |
| 411 if self.opts.make_HTML: | |
| 412 shutil.copyfile(self.opts.output_html,os.path.join(testdir,self.test1HTML)) | |
| 413 if self.opts.output_dir: | |
| 414 shutil.copyfile(self.tlog,os.path.join(testdir,'test1_out.log')) | |
| 415 outpif = '%s.py' % self.toolname # new name | |
| 416 outpiname = os.path.join(tdir,outpif) # path for the tool tarball | |
| 417 pyin = os.path.basename(self.pyfile) # our name - we rewrite ourselves (TM) | |
| 418 notes = ['# %s - a self annotated version of %s generated by running %s\n' % (outpiname,pyin,pyin),] | |
| 419 notes.append('# to make a new Galaxy tool called %s\n' % self.toolname) | |
| 420 notes.append('# User %s at %s\n' % (self.opts.user_email,timenow())) | |
| 421 pi=[line.replace('if opts.dockerized==0:', 'if False:') for line in open(self.pyfile)] #do not run docker in the generated tool | |
| 422 notes += pi | |
| 423 outpi = open(outpiname,'w') | |
| 424 outpi.write(''.join(notes)) | |
| 425 outpi.write('\n') | |
| 426 outpi.close() | |
| 427 stname = os.path.join(tdir,self.sfile) | |
| 428 if not os.path.exists(stname): | |
| 429 shutil.copyfile(self.sfile, stname) | |
| 430 xtname = os.path.join(tdir,self.xmlfile) | |
| 431 if not os.path.exists(xtname): | |
| 432 shutil.copyfile(self.xmlfile,xtname) | |
| 433 tarpath = "%s.gz" % self.toolname | |
| 434 tar = tarfile.open(tarpath, "w:gz") | |
| 435 tar.add(tdir,arcname=self.toolname) | |
| 436 tar.close() | |
| 437 shutil.copyfile(tarpath,self.opts.new_tool) | |
| 438 shutil.rmtree(tdir) | |
| 439 ## TODO: replace with optional direct upload to local toolshed? | |
| 440 return retval | |
| 441 | |
| 442 | |
| 443 def compressPDF(self,inpdf=None,thumbformat='png'): | |
| 444 """need absolute path to pdf | |
| 445 note that GS gets confoozled if no $TMP or $TEMP | |
| 446 so we set it | |
| 447 """ | |
| 448 assert os.path.isfile(inpdf), "## Input %s supplied to %s compressPDF not found" % (inpdf,self.myName) | |
| 449 hlog = os.path.join(self.opts.output_dir,"compress_%s.txt" % os.path.basename(inpdf)) | |
| 450 sto = open(hlog,'a') | |
| 451 our_env = os.environ.copy() | |
| 452 our_tmp = our_env.get('TMP',None) | |
| 453 if not our_tmp: | |
| 454 our_tmp = our_env.get('TEMP',None) | |
| 455 if not (our_tmp and os.path.exists(our_tmp)): | |
| 456 newtmp = os.path.join(self.opts.output_dir,'tmp') | |
| 457 try: | |
| 458 os.mkdir(newtmp) | |
| 459 except: | |
| 460 sto.write('## WARNING - cannot make %s - it may exist or permissions need fixing\n' % newtmp) | |
| 461 our_env['TEMP'] = newtmp | |
| 462 if not self.temp_warned: | |
| 463 sto.write('## WARNING - no $TMP or $TEMP!!! Please fix - using %s temporarily\n' % newtmp) | |
| 464 self.temp_warned = True | |
| 465 outpdf = '%s_compressed' % inpdf | |
| 466 cl = ["gs", "-sDEVICE=pdfwrite", "-dNOPAUSE", "-dUseCIEColor", "-dBATCH","-dPDFSETTINGS=/printer", "-sOutputFile=%s" % outpdf,inpdf] | |
| 467 x = subprocess.Popen(cl,stdout=sto,stderr=sto,cwd=self.opts.output_dir,env=our_env) | |
| 468 retval1 = x.wait() | |
| 469 sto.close() | |
| 470 if retval1 == 0: | |
| 471 os.unlink(inpdf) | |
| 472 shutil.move(outpdf,inpdf) | |
| 473 os.unlink(hlog) | |
| 474 hlog = os.path.join(self.opts.output_dir,"thumbnail_%s.txt" % os.path.basename(inpdf)) | |
| 475 sto = open(hlog,'w') | |
| 476 outpng = '%s.%s' % (os.path.splitext(inpdf)[0],thumbformat) | |
| 477 if self.useGM: | |
| 478 cl2 = ['gm', 'convert', inpdf, outpng] | |
| 479 else: # assume imagemagick | |
| 480 cl2 = ['convert', inpdf, outpng] | |
| 481 x = subprocess.Popen(cl2,stdout=sto,stderr=sto,cwd=self.opts.output_dir,env=our_env) | |
| 482 retval2 = x.wait() | |
| 483 sto.close() | |
| 484 if retval2 == 0: | |
| 485 os.unlink(hlog) | |
| 486 retval = retval1 or retval2 | |
| 487 return retval | |
| 488 | |
| 489 | |
| 490 def getfSize(self,fpath,outpath): | |
| 491 """ | |
| 492 format a nice file size string | |
| 493 """ | |
| 494 size = '' | |
| 495 fp = os.path.join(outpath,fpath) | |
| 496 if os.path.isfile(fp): | |
| 497 size = '0 B' | |
| 498 n = float(os.path.getsize(fp)) | |
| 499 if n > 2**20: | |
| 500 size = '%1.1f MB' % (n/2**20) | |
| 501 elif n > 2**10: | |
| 502 size = '%1.1f KB' % (n/2**10) | |
| 503 elif n > 0: | |
| 504 size = '%d B' % (int(n)) | |
| 505 return size | |
| 506 | |
| 507 def makeHtml(self): | |
| 508 """ Create an HTML file content to list all the artifacts found in the output_dir | |
| 509 """ | |
| 510 | |
| 511 galhtmlprefix = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
| 512 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
| 513 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
| 514 <meta name="generator" content="Galaxy %s tool output - see http://g2.trac.bx.psu.edu/" /> | |
| 515 <title></title> | |
| 516 <link rel="stylesheet" href="/static/style/base.css" type="text/css" /> | |
| 517 </head> | |
| 518 <body> | |
| 519 <div class="toolFormBody"> | |
| 520 """ | |
| 521 galhtmlattr = """<hr/><div class="infomessage">This tool (%s) was generated by the <a href="https://bitbucket.org/fubar/galaxytoolfactory/overview">Galaxy Tool Factory</a></div><br/>""" | |
| 522 galhtmlpostfix = """</div></body></html>\n""" | |
| 523 | |
| 524 flist = os.listdir(self.opts.output_dir) | |
| 525 flist = [x for x in flist if x <> 'Rplots.pdf'] | |
| 526 flist.sort() | |
| 527 html = [] | |
| 528 html.append(galhtmlprefix % progname) | |
| 529 html.append('<div class="infomessage">Galaxy Tool "%s" run at %s</div><br/>' % (self.toolname,timenow())) | |
| 530 fhtml = [] | |
| 531 if len(flist) > 0: | |
| 532 logfiles = [x for x in flist if x.lower().endswith('.log')] # log file names determine sections | |
| 533 logfiles.sort() | |
| 534 logfiles = [x for x in logfiles if abspath(x) <> abspath(self.tlog)] | |
| 535 logfiles.append(abspath(self.tlog)) # make it the last one | |
| 536 pdflist = [] | |
| 537 npdf = len([x for x in flist if os.path.splitext(x)[-1].lower() == '.pdf']) | |
| 538 for rownum,fname in enumerate(flist): | |
| 539 dname,e = os.path.splitext(fname) | |
| 540 sfsize = self.getfSize(fname,self.opts.output_dir) | |
| 541 if e.lower() == '.pdf' : # compress and make a thumbnail | |
| 542 thumb = '%s.%s' % (dname,self.thumbformat) | |
| 543 pdff = os.path.join(self.opts.output_dir,fname) | |
| 544 retval = self.compressPDF(inpdf=pdff,thumbformat=self.thumbformat) | |
| 545 if retval == 0: | |
| 546 pdflist.append((fname,thumb)) | |
| 547 else: | |
| 548 pdflist.append((fname,fname)) | |
| 549 if (rownum+1) % 2 == 0: | |
| 550 fhtml.append('<tr class="odd_row"><td><a href="%s">%s</a></td><td>%s</td></tr>' % (fname,fname,sfsize)) | |
| 551 else: | |
| 552 fhtml.append('<tr><td><a href="%s">%s</a></td><td>%s</td></tr>' % (fname,fname,sfsize)) | |
| 553 for logfname in logfiles: # expect at least tlog - if more | |
| 554 if abspath(logfname) == abspath(self.tlog): # handled later | |
| 555 sectionname = 'All tool run' | |
| 556 if (len(logfiles) > 1): | |
| 557 sectionname = 'Other' | |
| 558 ourpdfs = pdflist | |
| 559 else: | |
| 560 realname = os.path.basename(logfname) | |
| 561 sectionname = os.path.splitext(realname)[0].split('_')[0] # break in case _ added to log | |
| 562 ourpdfs = [x for x in pdflist if os.path.basename(x[0]).split('_')[0] == sectionname] | |
| 563 pdflist = [x for x in pdflist if os.path.basename(x[0]).split('_')[0] <> sectionname] # remove | |
| 564 nacross = 1 | |
| 565 npdf = len(ourpdfs) | |
| 566 | |
| 567 if npdf > 0: | |
| 568 nacross = math.sqrt(npdf) ## int(round(math.log(npdf,2))) | |
| 569 if int(nacross)**2 != npdf: | |
| 570 nacross += 1 | |
| 571 nacross = int(nacross) | |
| 572 width = min(400,int(1200/nacross)) | |
| 573 html.append('<div class="toolFormTitle">%s images and outputs</div>' % sectionname) | |
| 574 html.append('(Click on a thumbnail image to download the corresponding original PDF image)<br/>') | |
| 575 ntogo = nacross # counter for table row padding with empty cells | |
| 576 html.append('<div><table class="simple" cellpadding="2" cellspacing="2">\n<tr>') | |
| 577 for i,paths in enumerate(ourpdfs): | |
| 578 fname,thumb = paths | |
| 579 s= """<td><a href="%s"><img src="%s" title="Click to download a PDF of %s" hspace="5" width="%d" | |
| 580 alt="Image called %s"/></a></td>\n""" % (fname,thumb,fname,width,fname) | |
| 581 if ((i+1) % nacross == 0): | |
| 582 s += '</tr>\n' | |
| 583 ntogo = 0 | |
| 584 if i < (npdf - 1): # more to come | |
| 585 s += '<tr>' | |
| 586 ntogo = nacross | |
| 587 else: | |
| 588 ntogo -= 1 | |
| 589 html.append(s) | |
| 590 if html[-1].strip().endswith('</tr>'): | |
| 591 html.append('</table></div>\n') | |
| 592 else: | |
| 593 if ntogo > 0: # pad | |
| 594 html.append('<td> </td>'*ntogo) | |
| 595 html.append('</tr></table></div>\n') | |
| 596 logt = open(logfname,'r').readlines() | |
| 597 logtext = [x for x in logt if x.strip() > ''] | |
| 598 html.append('<div class="toolFormTitle">%s log output</div>' % sectionname) | |
| 599 if len(logtext) > 1: | |
| 600 html.append('\n<pre>\n') | |
| 601 html += logtext | |
| 602 html.append('\n</pre>\n') | |
| 603 else: | |
| 604 html.append('%s is empty<br/>' % logfname) | |
| 605 if len(fhtml) > 0: | |
| 606 fhtml.insert(0,'<div><table class="colored" cellpadding="3" cellspacing="3"><tr><th>Output File Name (click to view)</th><th>Size</th></tr>\n') | |
| 607 fhtml.append('</table></div><br/>') | |
| 608 html.append('<div class="toolFormTitle">All output files available for downloading</div>\n') | |
| 609 html += fhtml # add all non-pdf files to the end of the display | |
| 610 else: | |
| 611 html.append('<div class="warningmessagelarge">### Error - %s returned no files - please confirm that parameters are sane</div>' % self.opts.interpreter) | |
| 612 html.append(galhtmlpostfix) | |
| 613 htmlf = file(self.opts.output_html,'w') | |
| 614 htmlf.write('\n'.join(html)) | |
| 615 htmlf.write('\n') | |
| 616 htmlf.close() | |
| 617 self.html = html | |
| 618 | |
| 619 | |
| 620 def run(self): | |
| 621 """ | |
| 622 scripts must be small enough not to fill the pipe! | |
| 623 """ | |
| 624 if self.treatbashSpecial and self.opts.interpreter in ['bash','sh']: | |
| 625 retval = self.runBash() | |
| 626 else: | |
| 627 if self.opts.output_dir: | |
| 628 ste = open(self.elog,'w') | |
| 629 sto = open(self.tlog,'w') | |
| 630 sto.write('## Toolfactory generated command line = %s\n' % ' '.join(self.cl)) | |
| 631 sto.flush() | |
| 632 p = subprocess.Popen(self.cl,shell=False,stdout=sto,stderr=ste,stdin=subprocess.PIPE,cwd=self.opts.output_dir) | |
| 633 else: | |
| 634 p = subprocess.Popen(self.cl,shell=False,stdin=subprocess.PIPE) | |
| 635 p.stdin.write(self.script) | |
| 636 p.stdin.close() | |
| 637 retval = p.wait() | |
| 638 if self.opts.output_dir: | |
| 639 sto.close() | |
| 640 ste.close() | |
| 641 err = open(self.elog,'r').readlines() | |
| 642 if retval <> 0 and err: # problem | |
| 643 print >> sys.stderr,err #same problem, need to capture docker stdin/stdout | |
| 644 if self.opts.make_HTML: | |
| 645 self.makeHtml() | |
| 646 return retval | |
| 647 | |
| 648 def runBash(self): | |
| 649 """ | |
| 650 cannot use - for bash so use self.sfile | |
| 651 """ | |
| 652 if self.opts.output_dir: | |
| 653 s = '## Toolfactory generated command line = %s\n' % ' '.join(self.cl) | |
| 654 sto = open(self.tlog,'w') | |
| 655 sto.write(s) | |
| 656 sto.flush() | |
| 657 p = subprocess.Popen(self.cl,shell=False,stdout=sto,stderr=sto,cwd=self.opts.output_dir) | |
| 658 else: | |
| 659 p = subprocess.Popen(self.cl,shell=False) | |
| 660 retval = p.wait() | |
| 661 if self.opts.output_dir: | |
| 662 sto.close() | |
| 663 if self.opts.make_HTML: | |
| 664 self.makeHtml() | |
| 665 return retval | |
| 666 | |
| 667 | |
| 668 def main(): | |
| 669 u = """ | |
| 670 This is a Galaxy wrapper. It expects to be called by a special purpose tool.xml as: | |
| 671 <command interpreter="python">rgBaseScriptWrapper.py --script_path "$scriptPath" --tool_name "foo" --interpreter "Rscript" | |
| 672 </command> | |
| 673 """ | |
| 674 op = argparse.ArgumentParser() | |
| 675 a = op.add_argument | |
| 676 a('--script_path',default=None) | |
| 677 a('--tool_name',default=None) | |
| 678 a('--interpreter',default=None) | |
| 679 a('--output_dir',default='./') | |
| 680 a('--output_html',default=None) | |
| 681 a('--input_tab',default='None', nargs='*') | |
| 682 a('--output_tab',default='None') | |
| 683 a('--user_email',default='Unknown') | |
| 684 a('--bad_user',default=None) | |
| 685 a('--make_Tool',default=None) | |
| 686 a('--make_HTML',default=None) | |
| 687 a('--help_text',default=None) | |
| 688 a('--tool_desc',default=None) | |
| 689 a('--new_tool',default=None) | |
| 690 a('--tool_version',default=None) | |
| 691 a('--include_dependencies',default=None) | |
| 692 a('--dockerized',default=0) | |
| 693 a('--output_format', default='tabular') | |
| 694 a('--input_format', dest='input_formats', action='append', default=[]) | |
| 695 a('--additional_parameters', dest='additional_parameters', action='append', default=[]) | |
| 696 opts = op.parse_args() | |
| 697 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) | |
| 698 assert opts.tool_name,'## Tool Factory expects a tool name - eg --tool_name=DESeq' | |
| 699 assert opts.interpreter,'## Tool Factory wrapper expects an interpreter - eg --interpreter=Rscript' | |
| 700 assert os.path.isfile(opts.script_path),'## Tool Factory wrapper expects a script path - eg --script_path=foo.R' | |
| 701 if opts.output_dir: | |
| 702 try: | |
| 703 os.makedirs(opts.output_dir) | |
| 704 except: | |
| 705 pass | |
| 706 if opts.dockerized==0: | |
| 707 switch_to_docker(opts) | |
| 708 return | |
| 709 r = ScriptRunner(opts) | |
| 710 if opts.make_Tool: | |
| 711 retcode = r.makeTooltar() | |
| 712 else: | |
| 713 retcode = r.run() | |
| 714 os.unlink(r.sfile) | |
| 715 if retcode: | |
| 716 sys.exit(retcode) # indicate failure to job runner | |
| 717 | |
| 718 | |
| 719 if __name__ == "__main__": | |
| 720 main() | |
| 721 | |
| 722 |
