/
time.py
executable file
·59 lines (44 loc) · 1.51 KB
/
time.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
#!/usr/bin/env python
"""
time.py -- Replacement for coreutils 'time'.
The interface of this program is modelled after:
/usr/bin/time --append --output foo.txt --format '%x %e'
Problems with /usr/bin/time:
- elapsed time only has 2 digits of precision
Problems with bash time builtin
- has no way to get the exit code
- writes to stderr, so you it's annoying to get both process stderr and
and
This program also writes CSV directly, so you can have commas in fields, etc.
"""
import csv
import optparse
import sys
import subprocess
import time
def Options():
"""Returns an option parser instance."""
p = optparse.OptionParser('time.py [options] ARGV...')
p.add_option(
'-o', '--output', dest='output', default=None,
help='Name of output file to write to')
p.add_option(
'--field', dest='fields', default=[], action='append',
help='A string to append to each row, after the exit code and status')
return p
def main(argv):
(opts, child_argv) = Options().parse_args(argv[1:])
start_time = time.time()
exit_code = subprocess.call(child_argv)
elapsed = time.time() - start_time
fields = tuple(opts.fields)
with open(opts.output, 'a') as f:
out = csv.writer(f)
row = (exit_code, '%.4f' % elapsed) + fields
out.writerow(row)
# Preserve the command's exit code. (This means you can't distinguish
# between a failure of time.py and the command, but that's better than
# swallowing the error.)
return exit_code
if __name__ == '__main__':
sys.exit(main(sys.argv))