Mercurial > hg > fail
annotate fail.py @ 5:b407681e6f2c
fix parameter type
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 23 Aug 2016 16:36:39 -0700 |
parents | 8dc420754023 |
children | 6496dae95f0d |
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() |
0 | 42 ctr += 1 |
43 | |
4
8dc420754023
minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents:
2
diff
changeset
|
44 # print iteration information |
2
d1880117acb5
attempt to correct documentation display on pypi
Jeff Hammel <k0scist@gmail.com>
parents:
1
diff
changeset
|
45 print ("Iteration {}".format(ctr)) |
d1880117acb5
attempt to correct documentation display on pypi
Jeff Hammel <k0scist@gmail.com>
parents:
1
diff
changeset
|
46 |
0 | 47 # test it |
48 if process.returncode not in options.codes: | |
49 sys.exit(process.returncode) | |
50 | |
51 # loop control | |
52 time.sleep(options.sleep) | |
53 | |
54 | |
55 except KeyboardInterrupt: | |
56 sys.exit(0) | |
57 | |
58 if __name__ == '__main__': | |
59 main() |