Mercurial > hg > fail
annotate fail.py @ 6:6496dae95f0d default tip
add a few more variables to track; we will want to output them
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 24 Aug 2016 17:05:10 -0700 |
parents | b407681e6f2c |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
4 run a program until it fails | |
5 """ | |
6 | |
7 # imports | |
8 import argparse | |
9 import subprocess | |
10 import sys | |
11 import time | |
12 | |
1 | 13 |
0 | 14 def main(args=sys.argv[1:]): |
15 """CLI""" | |
16 | |
17 # parse command line | |
18 parser = argparse.ArgumentParser(description=__doc__) | |
19 parser.add_argument('command', help="command to run") | |
5 | 20 parser.add_argument('--code', dest='codes', |
21 default=(0,), nargs='+', | |
22 type=int, | |
4
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
23 help="allowed exit codes [DEFAULT: 0]") |
0 | 24 parser.add_argument('-s', '--sleep', dest='sleep', |
25 type=float, default=1., | |
1 | 26 help="sleep between iterations [DEFAULT: %(default)s]") |
0 | 27 options = parser.parse_args(args) |
28 | |
29 try: | |
30 | |
4
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
31 # main loop |
0 | 32 ctr = 0 |
4
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
33 start_loop = time.time() |
0 | 34 while True: |
35 | |
4
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
36 # note program start time |
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
37 program_start_time = time.time() if ctr else start_loop |
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
38 |
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
39 # run the subcommand |
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
40 process = subprocess.Popen(options.command, shell=True) |
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
41 stdout, stderr = process.communicate() |
6
6496dae95f0d
add a few more variables to track; we will want to output them
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
42 |
6496dae95f0d
add a few more variables to track; we will want to output them
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
43 # update variables |
0 | 44 ctr += 1 |
6
6496dae95f0d
add a few more variables to track; we will want to output them
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
45 now = time.time() |
6496dae95f0d
add a few more variables to track; we will want to output them
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
46 duration = now - program_start_time |
6496dae95f0d
add a few more variables to track; we will want to output them
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
47 elapsed = now - start_loop |
0 | 48 |
4
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
49 # print iteration information |
6
6496dae95f0d
add a few more variables to track; we will want to output them
Jeff Hammel <k0scist@gmail.com>
parents:
5
diff
changeset
|
50 print ("Iteration {ctr}".format(ctr=ctr)) |
2
d1880117acb5
attempt to correct documentation display on pypi
Jeff Hammel <k0scist@gmail.com>
parents:
1
diff
changeset
|
51 |
0 | 52 # test it |
53 if process.returncode not in options.codes: | |
54 sys.exit(process.returncode) | |
55 | |
56 # loop control | |
57 time.sleep(options.sleep) | |
58 | |
59 | |
60 except KeyboardInterrupt: | |
61 sys.exit(0) | |
62 | |
63 if __name__ == '__main__': | |
64 main() |