-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathrunner.py
executable file
·229 lines (196 loc) · 6.47 KB
/
runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python
# Copyright 2018 Adobe. All rights reserved.
"""
Helper script for running a command line tool.
Used as part of the integration tests.
"""
from __future__ import print_function, division, absolute_import
import argparse
import logging
import os
import subprocess32 as subprocess
import sys
import tempfile
__version__ = '0.6.1'
logger = logging.getLogger('runner')
TIMEOUT = 240 # seconds
def _write_file(file_path, data):
with open(file_path, "wb") as f:
f.write(data)
def _get_input_dir_path(tool_name):
input_dir = os.path.join(
os.path.split(__file__)[0], '{}_data'.format(tool_name), 'input')
return os.path.abspath(os.path.realpath(input_dir))
def run_tool(opts):
"""
Runs the tool using the parameters provided.
"""
input_dir = _get_input_dir_path(opts.tool)
args = [opts.tool]
for opt in opts.options:
if opt.startswith('='):
args.append('--{}'.format(opt[1:]))
elif opt.startswith('*'):
args.append('+{}'.format(opt[1:]))
elif opt.startswith('_'):
args.append('{}'.format(opt[1:]))
else:
args.append('-{}'.format(opt))
if opts.files:
if opts.abs_paths:
files = opts.files
# validate only the first path; the other paths may not exist yet
# as they can be the paths to which the user wants the tool to
# write its output to
assert os.path.exists(files[0]), "Invalid input path found."
else:
files = [os.path.join(input_dir, fname) for fname in opts.files]
assert all([os.path.exists(fpath) for fpath in files]), (
"Invalid input path found.")
args.extend(files)
stderr = None
if opts.std_error:
stderr = subprocess.STDOUT
logger.debug(
"About to run the command below\n==>{}<==".format(' '.join(args)))
try:
if opts.save_path:
output = subprocess.check_output(args, stderr=stderr,
timeout=TIMEOUT)
_write_file(opts.save_path, output)
return opts.save_path
else:
return subprocess.check_call(args, timeout=TIMEOUT)
except (subprocess.CalledProcessError, OSError) as err:
if opts.save_path:
_write_file(opts.save_path, err.output)
return opts.save_path
logger.error(err)
raise
def _check_tool(tool_name):
"""
Checks if the invoked tool produces a known command.
Returns the tool's name if the check is successful,
or a tuple with the tool's name and the error if it's not.
"""
try:
subprocess.check_output([tool_name, '-h'], timeout=TIMEOUT)
return tool_name
except (subprocess.CalledProcessError, OSError) as err:
logger.error(err)
return tool_name, err
def _check_save_path(path_str):
check_path = os.path.abspath(os.path.realpath(path_str))
del_test_file = True
try:
if os.path.exists(check_path):
del_test_file = False
open(check_path, 'a').close()
if del_test_file:
os.remove(check_path)
except (IOError, OSError):
raise argparse.ArgumentTypeError(
"{} is not a valid path to write to.".format(check_path))
return check_path
def get_options(args):
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description=__doc__
)
parser.add_argument(
'--version',
action='version',
version=__version__
)
parser.add_argument(
'-v',
'--verbose',
action='count',
default=0,
help='verbose mode\n'
'Use -vv for debug mode'
)
parser.add_argument(
'-t',
'--tool',
required=True,
type=_check_tool,
metavar='NAME',
help='name of the tool to run'
)
parser.add_argument(
'-o',
'--options',
nargs='+',
metavar='OPTION',
default=[],
help='options to run the tool with\n'
'By default the option will be prepended with one minus (-).\n'
'To request the use of two minus (--), prepend the option with '
'an equal (=).\n'
'To request the use of a plus (+), prepend the option with an '
'asterisk (*).\n'
'To request the use of nothing, prepend the option with an '
'underscore (_).'
)
parser.add_argument(
'-f',
'--files',
nargs='+',
metavar='FILE_NAME/PATH',
help='names or full paths of the files to provide to the tool\n'
"The files will be sourced from the 'input' folder inside the\n"
"'{tool_name}_data' directory, unless the option '--abs-paths' "
"is used."
)
parser.add_argument(
'-a',
'--abs-paths',
action='store_true',
help="treat the input paths as absolute\n"
"(instead of deriving them from the tool's name)"
)
parser.add_argument(
'-s',
'--save',
dest='save_path',
nargs='?',
default=False,
type=_check_save_path,
metavar='blank or PATH',
help="path to save the tool's standard output (stdout) to\n"
'The default is to use a temporary location.'
)
parser.add_argument(
'-e',
'--std-error',
action='store_true',
help="capture stderr instead of stdout"
)
options = parser.parse_args(args)
if not options.verbose:
level = "WARNING"
elif options.verbose == 1:
level = "INFO"
else:
level = "DEBUG"
logging.basicConfig(level=level)
if options.save_path is None:
# runner.py was called with '-s' but the option is NOT followed by
# a command-line argument; this means a temp file must be created.
# When option '-s' is NOT used, the value of options.save_path is False
file_descriptor, temp_path = tempfile.mkstemp()
os.close(file_descriptor)
options.save_path = temp_path
if isinstance(options.tool, tuple):
parser.error("'{}' is an unknown command.".format(options.tool[0]))
return options
def main(args=None):
"""
Returns the path to the result/output file if the command is successful,
and an exception if it isn't.
"""
opts = get_options(args)
return run_tool(opts)
if __name__ == "__main__":
sys.exit(main())