Skip to content

Commit dd969ff

Browse files
author
Peter Amstutz
committed
Proof of concept flask service and client to accept CWL files and run CWL jobs.
1 parent fed4ece commit dd969ff

File tree

5 files changed

+108
-47
lines changed

5 files changed

+108
-47
lines changed

cwl_client.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
import argparse
22
import requests
3+
import urlparse
4+
import os
35

46
def main():
57
parser = argparse.ArgumentParser()
68

7-
parser.add_argument("--upload", action="store_true")
9+
parser.add_argument("--upload", action="store_true", help="Upload new CWL tool or workflow.")
810
parser.add_argument("endpoint")
9-
parser.add_argument("object")
11+
parser.add_argument("item", nargs="?")
1012

1113
args = parser.parse_args()
1214

15+
if not args.item:
16+
r = requests.get(args.endpoint)
17+
print r.text
18+
return
19+
20+
with open(args.item) as f:
21+
data = f.read()
22+
1323
if args.upload:
14-
r = requests.put(args.endpoint)
24+
dest = urlparse.urljoin(args.endpoint, os.path.basename(args.item))
25+
r = requests.put(dest, data=data)
1526
else:
16-
r = requests.post(args.endpoint)
27+
r = requests.post(args.endpoint, data=data)
28+
29+
print r.text
30+
1731

1832
if __name__ == "__main__":
1933
main()

cwl_flask.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from flask import Flask
2+
from flask import request
3+
import os
4+
import subprocess
5+
import tempfile
6+
7+
app = Flask(__name__)
8+
9+
@app.route("/<workflow>", methods=['GET', 'POST', 'PUT'])
10+
def runjob(workflow):
11+
try:
12+
if request.method == 'PUT':
13+
with open(os.path.join("files", workflow), "w") as f:
14+
f.write(request.stream.read())
15+
return "Ok"
16+
17+
wf = os.path.join("files", workflow)
18+
19+
if not os.path.exists(wf):
20+
return "Not found", 404
21+
22+
if request.method == 'POST':
23+
with tempfile.NamedTemporaryFile() as f:
24+
f.write(request.stream.read())
25+
f.flush()
26+
outdir = tempfile.mkdtemp()
27+
proc = subprocess.Popen(["cwl-runner", os.path.abspath(wf), f.name],
28+
stdout=subprocess.PIPE,
29+
stderr=subprocess.PIPE,
30+
close_fds=True,
31+
cwd=outdir)
32+
(stdoutdata, stderrdata) = proc.communicate()
33+
proc.wait()
34+
if proc.returncode == 0:
35+
return stdoutdata
36+
else:
37+
return stderrdata, 400
38+
else:
39+
with open(wf, "r") as f:
40+
return f.read()
41+
except Exception as e:
42+
return str(e), 500
43+
44+
if __name__ == "__main__":
45+
if not os.path.exists("files"):
46+
os.mkdir("files")
47+
app.run()

cwltool_flask.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

cwltool_stream.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
import cwltool.main
5+
import tempfile
6+
import logging
7+
import StringIO
8+
import json
9+
10+
_logger = logging.getLogger("cwltool")
11+
_logger.setLevel(logging.ERROR)
12+
13+
def main(args=None):
14+
if args is None:
15+
args = sys.argv[1:]
16+
17+
if len(args) == 0:
18+
print "Workflow must be on command line"
19+
return 1
20+
21+
parser = cwltool.main.arg_parser()
22+
parsedargs = parser.parse_args(args)
23+
24+
a = True
25+
while a:
26+
a = True
27+
msg = ""
28+
while a and a != "\n":
29+
a = sys.stdin.readline()
30+
msg += a
31+
32+
outdir = tempfile.mkdtemp("", parsedargs.tmp_outdir_prefix)
33+
34+
t = StringIO.StringIO(msg)
35+
err = StringIO.StringIO()
36+
if cwltool.main.main(["--outdir="+outdir] + args + ["-"], stdin=t, stderr=err) != 0:
37+
sys.stdout.write(json.dumps({"cwl:error": err.getvalue()}))
38+
sys.stdout.write("\n\n")
39+
sys.stdout.flush()
40+
41+
if __name__ == "__main__":
42+
sys.exit(main(sys.argv[1:]))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
url="https://github.com/common-workflow-language/common-workflow-language",
2020
download_url="https://github.com/common-workflow-language/common-workflow-language",
2121
license='Apache 2.0',
22-
py_modules=["cwltool_stream", "cwltool_flask", "cwltool_client"],
22+
py_modules=["cwltool_stream", "cwl_flask", "cwltool_client"],
2323
install_requires=[
2424
'cwltool >= 1.0.20151013135545',
2525
'Flask',

0 commit comments

Comments
 (0)