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
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")
5
b407681e6f2c fix parameter type
Jeff Hammel <k0scist@gmail.com>
parents: 4
diff changeset
20 parser.add_argument('--code', dest='codes',
b407681e6f2c fix parameter type
Jeff Hammel <k0scist@gmail.com>
parents: 4
diff changeset
21 default=(0,), nargs='+',
b407681e6f2c fix parameter type
Jeff Hammel <k0scist@gmail.com>
parents: 4
diff changeset
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
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 parser.add_argument('-s', '--sleep', dest='sleep',
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 type=float, default=1.,
1
a4680e54c481 fix formatting
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
26 help="sleep between iterations [DEFAULT: %(default)s]")
0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 options = parser.parse_args(args)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 try:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30
4
8dc420754023 minor upgrades to prepare for streamlining
Jeff Hammel <k0scist@gmail.com>
parents: 2
diff changeset
31 # main loop
0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
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
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 while True:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
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
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
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
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
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
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 # test it
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 if process.returncode not in options.codes:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
54 sys.exit(process.returncode)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
55
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
56 # loop control
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
57 time.sleep(options.sleep)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
58
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
59
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
60 except KeyboardInterrupt:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61 sys.exit(0)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 if __name__ == '__main__':
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 main()