3
3
import os
4
4
import subprocess
5
5
import tempfile
6
+ import json
6
7
7
8
app = Flask (__name__ )
8
9
9
- @app .route ("/<workflow>" , methods = ['GET' , 'POST' , 'PUT' ])
10
- def runjob (workflow ):
10
+ @app .route ("/<path: workflow>" , methods = ['GET' , 'POST' , 'PUT' ])
11
+ def handlecwl (workflow ):
11
12
try :
13
+ if ".." in workflow :
14
+ return "Path cannot contain .." , 400 , {"Content-Type" : "text/plain" }
15
+
12
16
if request .method == 'PUT' :
13
- with open (os .path .join ("files" , workflow ), "w" ) as f :
17
+ (dr , fn ) = os .path .split (workflow )
18
+ dr = os .path .join ("files" , dr )
19
+ if dr and not os .path .exists (dr ):
20
+ os .makedirs (dr )
21
+
22
+ with open (os .path .join (dr , fn ), "w" ) as f :
14
23
f .write (request .stream .read ())
15
24
return "Ok"
16
25
17
26
wf = os .path .join ("files" , workflow )
18
27
19
28
if not os .path .exists (wf ):
20
- return "Not found" , 404
29
+ return "Not found" , 404 , { "Content-Type" : "text/plain" }
21
30
22
31
if request .method == 'POST' :
23
32
with tempfile .NamedTemporaryFile () as f :
@@ -32,14 +41,23 @@ def runjob(workflow):
32
41
(stdoutdata , stderrdata ) = proc .communicate ()
33
42
proc .wait ()
34
43
if proc .returncode == 0 :
35
- return stdoutdata
44
+ return stdoutdata , 200 , { "Content-Type" : "application/json" }
36
45
else :
37
- return stderrdata , 400
46
+ return json . dumps ({ "cwl:error" : stderrdata }) , 400 , { "Content-Type" : "application/json" }
38
47
else :
39
48
with open (wf , "r" ) as f :
40
- return f .read ()
49
+ return f .read (), 200 , {"Content-Type" : "application/x-common-workflow-language" }
50
+ except Exception as e :
51
+ print e
52
+ return str (e ), 500 , {"Content-Type" : "text/plain" }
53
+
54
+ @app .route ("/" )
55
+ def index ():
56
+ try :
57
+ return json .dumps (["%s/%s" % (r [5 :], f2 ) for r , _ , f in os .walk ("files" ) for f2 in f ]), 200 , {"Content-Type" : "application/json" }
41
58
except Exception as e :
42
- return str (e ), 500
59
+ print e
60
+ return str (e ), 500 , {"Content-Type" : "text/plain" }
43
61
44
62
if __name__ == "__main__" :
45
63
if not os .path .exists ("files" ):
0 commit comments