Mercurial > repos > mvdbeek > docker_scriptrunner
annotate scriptrunner.py @ 2:495946ffc2d6 draft default tip
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
author | mvdbeek |
---|---|
date | Sun, 22 Jul 2018 13:38:01 -0400 |
parents | 21d312776891 |
children |
rev | line source |
---|---|
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
1 # DockerToolFactory.py |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
2 # see https://github.com/mvdbeek/scriptrunner |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
3 |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
4 from __future__ import print_function |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
5 import sys |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
6 import shutil |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
7 import subprocess |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
8 import os |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
9 import time |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
10 import tempfile |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
11 import argparse |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
12 import math |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
13 from os.path import abspath |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
14 |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
15 progname = os.path.split(sys.argv[0])[1] |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
16 verbose = False |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
17 debug = False |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
18 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
19 html_escape_table = { |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
20 "&": "&", |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
21 ">": ">", |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
22 "<": "<", |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
23 "$": "\$" |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
24 } |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
25 |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
26 |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
27 def timenow(): |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
28 """Return current time as a string.""" |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
29 return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time())) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
30 |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
31 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
32 def html_escape(text): |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
33 """Produce entities within text.""" |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
34 return "".join(html_escape_table.get(c, c) for c in text) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
35 |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
36 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
37 def cmd_exists(cmd): |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
38 return subprocess.call("type " + cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0 |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
39 |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
40 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
41 def construct_bind(host_path, container_path=False, binds=None, ro=True): |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
42 # TODO remove container_path if it's alwyas going to be the same as host_path |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
43 """Build or extend binds dictionary with container path. binds is used |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
44 to mount all files using the docker-py client.""" |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
45 if not binds: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
46 binds = {} |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
47 if isinstance(host_path, list): |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
48 for k, v in enumerate(host_path): |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
49 if not container_path: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
50 container_path = host_path[k] |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
51 binds[host_path[k]] = {'bind': container_path, 'ro': ro} |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
52 container_path = False # could be more elegant |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
53 return binds |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
54 else: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
55 if not container_path: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
56 container_path = host_path |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
57 binds[host_path] = {'bind': container_path, 'ro': ro} |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
58 return binds |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
59 |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
60 |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
61 def switch_to_docker(opts): |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
62 import docker # need local import, as container does not have docker-py |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
63 user_id = os.getuid() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
64 group_id = os.getgid() |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
65 docker_client = docker.APIClient() |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
66 toolfactory_path = abspath(sys.argv[0]) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
67 binds = construct_bind(host_path=opts.script_path, ro=False) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
68 binds = construct_bind(binds=binds, host_path=abspath(opts.output_dir), ro=False) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
69 if len(opts.input_file) > 0: |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
70 binds = construct_bind(binds=binds, host_path=opts.input_file, ro=True) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
71 if not opts.output_file == 'None': |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
72 binds = construct_bind(binds=binds, host_path=opts.output_file, ro=False) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
73 if opts.make_HTML: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
74 binds = construct_bind(binds=binds, host_path=opts.output_html, ro=False) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
75 binds = construct_bind(binds=binds, host_path=toolfactory_path) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
76 volumes = list(binds.keys()) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
77 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 |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
78 cmd = ['python', '-u'] + sys.argv + ['--dockerized', '1', "--user_id", str(user_id), "--group_id", str(group_id)] |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
79 image_exists = [True for image in docker_client.images() if opts.docker_image in image['RepoTags']] |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
80 if not image_exists: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
81 docker_client.pull(opts.docker_image) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
82 container = docker_client.create_container( |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
83 image=opts.docker_image, |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
84 volumes=volumes, |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
85 command=cmd, |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
86 host_config=docker_client.create_host_config(binds=binds)) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
87 docker_client.start(container=container[u'Id']) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
88 exit_code = docker_client.wait(container=container[u'Id'])['StatusCode'] |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
89 logs = docker_client.logs(container=container[u'Id']) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
90 print(logs, end="", file=sys.stderr) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
91 docker_client.remove_container(container[u'Id']) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
92 return exit_code |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
93 |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
94 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
95 class ScriptRunner: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
96 """class is a wrapper for an arbitrary script |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
97 """ |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
98 |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
99 def __init__(self, opts=None, treatbashSpecial=True, image_tag='base'): |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
100 """ |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
101 cleanup inputs, setup some outputs |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
102 """ |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
103 self.opts = opts |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
104 self.scriptname = 'script' |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
105 self.treatbashSpecial = treatbashSpecial |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
106 self.image_tag = image_tag |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
107 os.chdir(abspath(opts.output_dir)) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
108 self.thumbformat = 'png' |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
109 s = open(self.opts.script_path, 'r').readlines() |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
110 s = [x.rstrip() for x in s] # remove pesky dos line endings if needed |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
111 self.script = '\n'.join(s) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
112 fhandle, self.sfile = tempfile.mkstemp(prefix='script', suffix=".%s" % (opts.interpreter)) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
113 tscript = open(self.sfile, 'w') # use self.sfile as script source for Popen |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
114 tscript.write(self.script) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
115 tscript.close() |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
116 self.indentedScript = '\n'.join([' %s' % html_escape(x) for x in s]) # for restructured text in help |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
117 self.escapedScript = '\n'.join([html_escape(x) for x in s]) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
118 self.elog = os.path.join(self.opts.output_dir, "%s_error.log" % self.scriptname) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
119 if opts.output_dir: # may not want these complexities |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
120 self.tlog = os.path.join(self.opts.output_dir, "%s_runner.log" % self.scriptname) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
121 art = '%s.%s' % (self.scriptname, opts.interpreter) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
122 artpath = os.path.join(self.opts.output_dir, art) # need full path |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
123 artifact = open(artpath, 'w') # use self.sfile as script source for Popen |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
124 artifact.write(self.script) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
125 artifact.close() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
126 self.cl = [] |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
127 self.html = [] |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
128 self.cl.append(opts.interpreter) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
129 if self.treatbashSpecial and opts.interpreter in ['bash', 'sh']: |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
130 self.cl.append(self.sfile) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
131 else: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
132 self.cl.append('-') # stdin |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
133 for input in opts.input_file: |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
134 self.cl.append(input) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
135 if opts.output_file == 'None': # If tool generates only HTML, set output name to toolname |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
136 self.cl.append(str(self.scriptname) + '.out') |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
137 self.cl.append(opts.output_file) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
138 for param in opts.additional_parameters: |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
139 param, value = param.split(',') |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
140 self.cl.append('--' + param) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
141 self.cl.append(value) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
142 self.outFormats = opts.output_format |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
143 self.inputFormats = [formats for formats in opts.input_formats] |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
144 self.test1Input = '%s_test1_input.xls' % self.scriptname |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
145 self.test1Output = '%s_test1_output.xls' % self.scriptname |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
146 self.test1HTML = '%s_test1_output.html' % self.scriptname |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
147 |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
148 def compressPDF(self, inpdf=None, thumbformat='png'): |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
149 """ |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
150 inpdf is absolute path to PDF |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
151 """ |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
152 assert os.path.isfile(inpdf), "## Input %s supplied to %s compressPDF not found" % (inpdf, self.myName) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
153 hlog = os.path.join(self.opts.output_dir, "compress_%s.txt" % os.path.basename(inpdf)) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
154 sto = open(hlog, 'a') |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
155 our_env = os.environ.copy() |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
156 our_tmp = our_env.get('TMP', None) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
157 if not our_tmp: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
158 our_env['TMP'] = tempfile.gettempdir() |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
159 outpdf = '%s_compressed' % inpdf |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
160 cl = ["gs", "-sDEVICE=pdfwrite", "-dNOPAUSE", "-dUseCIEColor", "-dBATCH", "-dPDFSETTINGS=/printer", "-sOutputFile=%s" % outpdf, inpdf] |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
161 x = subprocess.Popen(cl, stdout=sto, stderr=sto, cwd=self.opts.output_dir, env=our_env) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
162 retval1 = x.wait() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
163 sto.close() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
164 if retval1 == 0: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
165 os.unlink(inpdf) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
166 shutil.move(outpdf, inpdf) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
167 os.unlink(hlog) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
168 hlog = os.path.join(self.opts.output_dir, "thumbnail_%s.txt" % os.path.basename(inpdf)) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
169 sto = open(hlog, 'w') |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
170 outpng = '%s.%s' % (os.path.splitext(inpdf)[0], thumbformat) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
171 cl2 = ['convert', inpdf, outpng] |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
172 x = subprocess.Popen(cl2, stdout=sto, stderr=sto, cwd=self.opts.output_dir, env=our_env) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
173 retval2 = x.wait() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
174 sto.close() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
175 if retval2 == 0: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
176 os.unlink(hlog) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
177 retval = retval1 or retval2 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
178 return retval |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
179 |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
180 def getfSize(self, fpath, outpath): |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
181 """ |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
182 format a nice file size string |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
183 """ |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
184 size = '' |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
185 fp = os.path.join(outpath, fpath) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
186 if os.path.isfile(fp): |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
187 size = '0 B' |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
188 n = float(os.path.getsize(fp)) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
189 if n > 2**20: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
190 size = '%1.1f MB' % (n / 2**20) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
191 elif n > 2**10: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
192 size = '%1.1f KB' % (n / 2**10) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
193 elif n > 0: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
194 size = '%d B' % (int(n)) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
195 return size |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
196 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
197 def makeHtml(self): |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
198 """ Create an HTML file content to list all the artifacts found in the output_dir |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
199 """ |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
200 |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
201 galhtmlprefix = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
202 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
203 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
204 <meta name="generator" content="Galaxy %s tool output - see http://g2.trac.bx.psu.edu/" /> |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
205 <title></title> |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
206 <link rel="stylesheet" href="/static/style/base.css" type="text/css" /> |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
207 </head> |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
208 <body> |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
209 <div class="toolFormBody"> |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
210 """ |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
211 galhtmlpostfix = """</div></body></html>\n""" |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
212 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
213 flist = os.listdir(self.opts.output_dir) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
214 flist = [x for x in flist if x != 'Rplots.pdf'] |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
215 flist.sort() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
216 html = [] |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
217 html.append(galhtmlprefix % progname) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
218 html.append('<div class="infomessage">Galaxy Tool "%s" run at %s</div><br/>' % (self.scriptname, timenow())) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
219 fhtml = [] |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
220 if len(flist) > 0: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
221 logfiles = [x for x in flist if x.lower().endswith('.log')] # log file names determine sections |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
222 logfiles.sort() |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
223 logfiles = [x for x in logfiles if abspath(x) != abspath(self.tlog)] |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
224 logfiles.append(abspath(self.tlog)) # make it the last one |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
225 pdflist = [] |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
226 npdf = len([x for x in flist if os.path.splitext(x)[-1].lower() == '.pdf']) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
227 for rownum, fname in enumerate(flist): |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
228 dname, e = os.path.splitext(fname) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
229 sfsize = self.getfSize(fname, self.opts.output_dir) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
230 if e.lower() == '.pdf': # compress and make a thumbnail |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
231 thumb = '%s.%s' % (dname, self.thumbformat) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
232 pdff = os.path.join(self.opts.output_dir, fname) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
233 retval = self.compressPDF(inpdf=pdff, thumbformat=self.thumbformat) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
234 if retval == 0: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
235 pdflist.append((fname, thumb)) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
236 else: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
237 pdflist.append((fname, fname)) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
238 if (rownum + 1) % 2 == 0: |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
239 fhtml.append('<tr class="odd_row"><td><a href="%s">%s</a></td><td>%s</td></tr>' % (fname, fname, sfsize)) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
240 else: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
241 fhtml.append('<tr><td><a href="%s">%s</a></td><td>%s</td></tr>' % (fname, fname, sfsize)) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
242 for logfname in logfiles: # expect at least tlog - if more |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
243 if abspath(logfname) == abspath(self.tlog): # handled later |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
244 sectionname = 'All tool run' |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
245 if (len(logfiles) > 1): |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
246 sectionname = 'Other' |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
247 ourpdfs = pdflist |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
248 else: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
249 realname = os.path.basename(logfname) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
250 sectionname = os.path.splitext(realname)[0].split('_')[0] # break in case _ added to log |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
251 ourpdfs = [x for x in pdflist if os.path.basename(x[0]).split('_')[0] == sectionname] |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
252 pdflist = [x for x in pdflist if os.path.basename(x[0]).split('_')[0] != sectionname] # remove |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
253 nacross = 1 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
254 npdf = len(ourpdfs) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
255 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
256 if npdf > 0: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
257 nacross = math.sqrt(npdf) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
258 if int(nacross)**2 != npdf: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
259 nacross += 1 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
260 nacross = int(nacross) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
261 width = min(400, int(1200 / nacross)) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
262 html.append('<div class="toolFormTitle">%s images and outputs</div>' % sectionname) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
263 html.append('(Click on a thumbnail image to download the corresponding original PDF image)<br/>') |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
264 ntogo = nacross # counter for table row padding with empty cells |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
265 html.append('<div><table class="simple" cellpadding="2" cellspacing="2">\n<tr>') |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
266 for i, paths in enumerate(ourpdfs): |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
267 fname, thumb = paths |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
268 s = """<td><a href="%s"><img src="%s" title="Click to download a PDF of %s" hspace="5" width="%d" |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
269 alt="Image called %s"/></a></td>\n""" % (fname, thumb, fname, width, fname) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
270 if ((i + 1) % nacross == 0): |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
271 s += '</tr>\n' |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
272 ntogo = 0 |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
273 if i < (npdf - 1): # more to come |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
274 s += '<tr>' |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
275 ntogo = nacross |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
276 else: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
277 ntogo -= 1 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
278 html.append(s) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
279 if html[-1].strip().endswith('</tr>'): |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
280 html.append('</table></div>\n') |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
281 else: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
282 if ntogo > 0: # pad |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
283 html.append('<td> </td>' * ntogo) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
284 html.append('</tr></table></div>\n') |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
285 logt = open(logfname, 'r').readlines() |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
286 logtext = [x for x in logt if x.strip() > ''] |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
287 html.append('<div class="toolFormTitle">%s log output</div>' % sectionname) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
288 if len(logtext) > 1: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
289 html.append('\n<pre>\n') |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
290 html += logtext |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
291 html.append('\n</pre>\n') |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
292 else: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
293 html.append('%s is empty<br/>' % logfname) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
294 if len(fhtml) > 0: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
295 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') |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
296 fhtml.append('</table></div><br/>') |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
297 html.append('<div class="toolFormTitle">All output files available for downloading</div>\n') |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
298 html += fhtml # add all non-pdf files to the end of the display |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
299 else: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
300 html.append('<div class="warningmessagelarge">### Error - %s returned no files - please confirm that parameters are sane</div>' % self.opts.interpreter) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
301 html.append(galhtmlpostfix) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
302 with open(self.opts.output_html, 'w') as htmlf: |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
303 htmlf.write('\n'.join(html)) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
304 htmlf.write('\n') |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
305 self.html = html |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
306 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
307 def run(self): |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
308 """ |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
309 scripts must be small enough not to fill the pipe! |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
310 """ |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
311 if self.treatbashSpecial and self.opts.interpreter in ['bash', 'sh']: |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
312 retval = self.runBash() |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
313 else: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
314 if self.opts.output_dir: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
315 ste = open(self.elog, 'w') |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
316 sto = open(self.tlog, 'w') |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
317 sto.write('## Toolfactory generated command line = %s\n' % ' '.join(self.cl)) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
318 sto.flush() |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
319 p = subprocess.Popen(self.cl, shell=False, stdout=sto, stderr=ste, stdin=subprocess.PIPE, cwd=self.opts.output_dir) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
320 else: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
321 p = subprocess.Popen(self.cl, shell=False, stdin=subprocess.PIPE) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
322 p.stdin.write(self.script) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
323 p.stdin.close() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
324 retval = p.wait() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
325 if self.opts.output_dir: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
326 sto.close() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
327 ste.close() |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
328 err = open(self.elog, 'r').readlines() |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
329 if retval != 0 and err: # problem |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
330 print(err, end="", file=sys.stderr) # same problem, need to capture docker stdin/stdout |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
331 if self.opts.make_HTML: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
332 self.makeHtml() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
333 return retval |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
334 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
335 def runBash(self): |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
336 """ |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
337 cannot use - for bash so use self.sfile |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
338 """ |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
339 if self.opts.output_dir: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
340 s = '## Toolfactory generated command line = %s\n' % ' '.join(self.cl) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
341 sto = open(self.tlog, 'w') |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
342 sto.write(s) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
343 sto.flush() |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
344 p = subprocess.Popen(self.cl, shell=False, stdout=sto, stderr=sto, cwd=self.opts.output_dir) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
345 else: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
346 p = subprocess.Popen(self.cl, shell=False) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
347 retval = p.wait() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
348 if self.opts.output_dir: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
349 sto.close() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
350 if self.opts.make_HTML: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
351 self.makeHtml() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
352 return retval |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
353 |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
354 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
355 def change_user_id(new_uid, new_gid): |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
356 """ |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
357 To avoid issues with wrong user ids, we change the user id of the 'galaxy' user in the container |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
358 to the user id with which the script has been called initially. |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
359 """ |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
360 cmd1 = ["/usr/sbin/usermod", "-d", "/var/home/galaxy", "galaxy"] |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
361 cmd2 = ["/usr/sbin/usermod", "-u", new_uid, "galaxy"] |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
362 cmd3 = ["/usr/sbin/groupmod", "-g", new_gid, "galaxy"] |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
363 cmd4 = ["/usr/sbin/usermod", "-d", "/home/galaxy", "galaxy"] |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
364 [subprocess.call(cmd) for cmd in [cmd1, cmd2, cmd3, cmd4]] |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
365 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
366 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
367 def main(): |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
368 op = argparse.ArgumentParser() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
369 a = op.add_argument |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
370 a('--docker_image', default=None) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
371 a('--script_path', default=None) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
372 a('--tool_name', default=None) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
373 a('--interpreter', default=None) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
374 a('--output_dir', default='./') |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
375 a('--output_html', default=None) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
376 a('--input_file', default='None', nargs='*') |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
377 a('--output_file', default='None') |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
378 a('--user_email', default='Unknown') |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
379 a('--bad_user', default=None) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
380 a('--make_HTML', default=None) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
381 a('--new_tool', default=None) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
382 a('--dockerized', default=0) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
383 a('--group_id', default=None) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
384 a('--user_id', default=None) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
385 a('--output_format', default='tabular') |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
386 a('--input_format', dest='input_formats', action='append', default=[]) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
387 a('--additional_parameters', dest='additional_parameters', action='append', default=[]) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
388 opts = op.parse_args() |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
389 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) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
390 assert os.path.isfile(opts.script_path), '## Tool Factory wrapper expects a script path - eg --script_path=foo.R' |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
391 if opts.output_dir: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
392 try: |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
393 os.makedirs(opts.output_dir) |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
394 except Exception: |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
395 pass |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
396 if opts.dockerized == 0: |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
397 retcode = switch_to_docker(opts) |
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
398 sys.exit(retcode) |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
399 change_user_id(opts.user_id, opts.group_id) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
400 os.setgid(int(opts.group_id)) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
401 os.setuid(int(opts.user_id)) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
402 r = ScriptRunner(opts) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
403 retcode = r.run() |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
404 os.unlink(r.sfile) |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
405 if retcode: |
2
495946ffc2d6
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit dded837d19aeb3f06b84e5076282cedeeaf713fa
mvdbeek
parents:
0
diff
changeset
|
406 sys.exit(retcode) # indicate failure to job runner |
0
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
407 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
408 |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
409 if __name__ == "__main__": |
21d312776891
planemo upload for repository https://github.com/mvdbeek/docker_scriptrunner/ commit 30f8264cdd67d40dec8acde6407f32152e6a29c1-dirty
mvdbeek
parents:
diff
changeset
|
410 main() |