annotate fail.py @ 2:d1880117acb5

attempt to correct documentation display on pypi
author Jeff Hammel <k0scist@gmail.com>
date Tue, 23 Aug 2016 14:58:00 -0700
parents a4680e54c481
children 8dc420754023
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='+',
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 help="allowed exit codes")
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
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 ctr = 0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 while True:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 ctr += 1
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 # run it
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35 process = subprocess.Popen(options.command, shell=True)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 _, _ = process.communicate()
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37
2
d1880117acb5 attempt to correct documentation display on pypi
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
38
d1880117acb5 attempt to correct documentation display on pypi
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
39 print ("Iteration {}".format(ctr))
d1880117acb5 attempt to correct documentation display on pypi
Jeff Hammel <k0scist@gmail.com>
parents: 1
diff changeset
40
0
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 # test it
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 if process.returncode not in options.codes:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 sys.exit(process.returncode)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 # loop control
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46 time.sleep(options.sleep)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 except KeyboardInterrupt:
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 sys.exit(0)
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 if __name__ == '__main__':
02d077c5627a initial program
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53 main()