# HG changeset patch # User Jeff Hammel # Date 1456782519 28800 # Node ID 02d077c5627ace16f19421358213092cbf81abb4 initial program diff -r 000000000000 -r 02d077c5627a README.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.txt Mon Feb 29 13:48:39 2016 -0800 @@ -0,0 +1,22 @@ +fail +==== + +run a program until it fails and gather statistics + +Elaborations from a favorite bash one-liner:: + + I=0; while ./do_some_things.py --param $arg; do I=$((I+1)); echo Iteration ${I}; sleep 1; done + +fun things to try +---------------- + +Russian roulette (with two chambers):: + + fail 'exit $(expr $RANDOM % 2)' + +---- + +Jeff Hammel + + + diff -r 000000000000 -r 02d077c5627a fail.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/fail.py Mon Feb 29 13:48:39 2016 -0800 @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +""" +run a program until it fails +""" + +# imports +import argparse +import subprocess +import sys +import time + +def main(args=sys.argv[1:]): + """CLI""" + + # parse command line + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('command', help="command to run") + parser.add_argument('--code', dest='codes', default=(0,), nargs='+', + help="allowed exit codes") + parser.add_argument('-s', '--sleep', dest='sleep', + type=float, default=1., + help="sleep between iterations [DEFAULT: %(default)]") + options = parser.parse_args(args) + + try: + + ctr = 0 + while True: + + print ("Iteration {}".format(ctr)) + ctr += 1 + + # run it + process = subprocess.Popen(options.command, shell=True) + _, _ = process.communicate() + + # test it + if process.returncode not in options.codes: + sys.exit(process.returncode) + + # loop control + time.sleep(options.sleep) + + + except KeyboardInterrupt: + sys.exit(0) + +if __name__ == '__main__': + main() diff -r 000000000000 -r 02d077c5627a setup.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.py Mon Feb 29 13:48:39 2016 -0800 @@ -0,0 +1,35 @@ +import os +from setuptools import setup + +try: + here = os.path.dirname(os.path.abspath(__file__)) + description = file(os.path.join(here, 'README.txt')).read() +except IOError: + description = None + +version = '0.0' + +deps = [] + +setup(name='fail', + version=version, + description="run a program until it fails and gather statistics", + long_description=description, + classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers + keywords='', + author='Jeff Hammel', + author_email='k0scist@gmail.com', + url='', + license='', + py_modules=['fail'], + packages=[], + include_package_data=True, + zip_safe=False, + install_requires=deps, + entry_points=""" + # -*- Entry points: -*- + [console_scripts] + fail = fail:main + """, + ) +