0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 '''
|
|
4 Test tool for running Galaxy workflows from Refinery
|
|
5
|
|
6 @author: Ilya Sytchev
|
|
7
|
|
8 Input: one or more text files
|
|
9 Output: concatenated input file(s) plus annotation
|
|
10
|
|
11 Requires Python v2.7
|
|
12
|
|
13 '''
|
|
14
|
|
15
|
|
16 import argparse
|
|
17 import random
|
|
18 import sys
|
|
19 import time
|
|
20
|
|
21
|
|
22 def main(args):
|
|
23 time.sleep(args.seconds)
|
|
24
|
|
25 if check_fail(args.p_fail):
|
|
26 cleanup(args)
|
|
27 quit("Processing failed by request", args)
|
|
28
|
|
29 input = read_files(args.input_files)
|
|
30 try:
|
|
31 for out_file in args.output_files:
|
|
32 out_file.write('\n' + "Output file name: " + out_file.name + '\n')
|
|
33 out_file.write(input)
|
|
34 except IOError as e:
|
|
35 cleanup(args)
|
|
36 parser.error(e)
|
|
37 else:
|
|
38 cleanup(args)
|
|
39
|
|
40
|
|
41 def check_fail(p_fail):
|
|
42 '''Determine success/failure state given a probability of failure
|
|
43
|
|
44 '''
|
|
45 random.seed()
|
|
46 if random.random() < p_fail:
|
|
47 return True
|
|
48 else:
|
|
49 return False
|
|
50
|
|
51
|
|
52 def read_files(file_list):
|
|
53 '''Read files from disk into a string
|
|
54
|
|
55 '''
|
|
56 str = ''
|
|
57 for in_file in file_list:
|
|
58 str += "Input file name: " + in_file.name + '\n'
|
|
59 str += in_file.read()
|
|
60 return str
|
|
61
|
|
62
|
|
63 def cleanup(args):
|
|
64 '''Close all open file handles
|
|
65
|
|
66 '''
|
|
67 file_list = []
|
|
68 if args.input_files:
|
|
69 file_list.extend(args.input_files)
|
|
70 if args.output_files:
|
|
71 file_list.extend(args.output_files)
|
|
72 for fh in file_list:
|
|
73 try:
|
|
74 fh.close()
|
|
75 except AttributeError:
|
|
76 continue
|
|
77
|
|
78
|
|
79 def quit(message, args):
|
|
80 '''Exit and optionally write to stdout and/or stderr
|
|
81
|
|
82 '''
|
|
83 if args.stdout:
|
|
84 sys.stdout.write(message + '\n')
|
|
85 if args.stderr:
|
|
86 sys.stderr.write(message + '\n')
|
|
87 sys.exit(args.exit_code)
|
|
88
|
|
89
|
|
90 if __name__ == '__main__':
|
|
91 version = "%(prog)s 0.1"
|
|
92 description = "Test tool for running workflows on Galaxy platform from Refinery"
|
|
93 parser = argparse.ArgumentParser(description=description, version=version)
|
|
94
|
|
95 parser.add_argument('-i', '--in-file', dest='input_files', nargs='+',
|
|
96 type=file, metavar='INPUT_FILE', required=True,
|
|
97 help='name of the input file')
|
|
98 parser.add_argument('-o', '--out-file', dest='output_files', nargs='+',
|
|
99 type=argparse.FileType('w'), metavar='OUTPUT_FILE',
|
|
100 required=True, help='name of the output file')
|
|
101 parser.add_argument('-e', '--exit_code', type=int, default=0,
|
|
102 help='code to return on exit, default: %(default)s')
|
|
103 parser.add_argument('--stdout', action='store_true',
|
|
104 help='write a message to stdout')
|
|
105 parser.add_argument('--stderr', action='store_true',
|
|
106 help='write a message to stderr')
|
|
107 parser.add_argument('-p', '--p-fail', type=float, default=0.0,
|
|
108 help='probability of execution failure, default: %(default)s')
|
|
109 parser.add_argument('-s', '--sleep', dest='seconds', type=int, default=0,
|
|
110 metavar='SECONDS',
|
|
111 help='number of seconds to sleep, default: %(default)s')
|
|
112
|
|
113 # check argument values for errors
|
|
114 try:
|
|
115 args = parser.parse_args()
|
|
116 except IOError as e:
|
|
117 parser.error(e)
|
|
118
|
|
119 if args.exit_code < 0 or args.exit_code > 255:
|
|
120 cleanup(args)
|
|
121 parser.error("Exit code value must be between 0 and 255")
|
|
122
|
|
123 if args.p_fail < 0.0 or args.p_fail > 1.0:
|
|
124 cleanup(args)
|
|
125 parser.error("Probability value must be between 0.0 and 1.0 inclusive")
|
|
126
|
|
127 main(args)
|