annotate fail.py @ 4:8dc420754023

minor upgrades to prepare for streamlining
author Jeff Hammel <k0scist@gmail.com>
date Tue, 23 Aug 2016 16:29:13 -0700
parents d1880117acb5
children b407681e6f2c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 run a program until it fails
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 """
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 # imports
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 import argparse
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 import subprocess
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 import sys
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 import time
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12
1
a4680e54c481 fix formatting
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
13
0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 def main(args=sys.argv[1:]):
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 """CLI"""
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 # parse command line
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 parser = argparse.ArgumentParser(description=__doc__)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 parser.add_argument('command', help="command to run")
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 parser.add_argument('--code', dest='codes', default=(0,), nargs='+',
4
8dc420754023 minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
21 help="allowed exit codes [DEFAULT: 0]")
0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 parser.add_argument('-s', '--sleep', dest='sleep',
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 type=float, default=1.,
1
a4680e54c481 fix formatting
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
24 help="sleep between iterations [DEFAULT: %(default)s]")
0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 options = parser.parse_args(args)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 try:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28
4
8dc420754023 minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
29 # main loop
0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 ctr = 0
4
8dc420754023 minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
31 start_loop = time.time()
0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 while True:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
4
8dc420754023 minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
34 # note program start time
8dc420754023 minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
35 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
36
8dc420754023 minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
37 # run the subcommand
8dc420754023 minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
38 process = subprocess.Popen(options.command, shell=True)
8dc420754023 minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
39 stdout, stderr = process.communicate()
0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 ctr += 1
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41
4
8dc420754023 minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
42 # print iteration information
2
d1880117acb5 attempt to correct documentation display on pypi
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
43 print ("Iteration {}".format(ctr))
d1880117acb5 attempt to correct documentation display on pypi
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
44
0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 # test it
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 if process.returncode not in options.codes:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 sys.exit(process.returncode)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 # loop control
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 time.sleep(options.sleep)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 except KeyboardInterrupt:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 sys.exit(0)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 if __name__ == '__main__':
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 main()