changeset 0:02d077c5627a

initial program
author Jeff Hammel <k0scist@gmail.com>
date Mon, 29 Feb 2016 13:48:39 -0800
parents
children a4680e54c481
files README.txt fail.py setup.py
diffstat 3 files changed, 107 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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
+
+
+
--- /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()
--- /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
+      """,
+      )
+