Commit d4c3c6e9 authored by LANORE Vincent's avatar LANORE Vincent
Browse files

Updated try_again (debug on stderr + only last stdout is displayed + exponential timeout)

parent 6ab3f384
......@@ -34,6 +34,8 @@ import random
try: input = raw_input
except NameError: pass
myprint = print
# Color-related functions
if sys.stdout.isatty():
class bcolors:
......@@ -114,13 +116,13 @@ def ask_input(string):
return "-- ["+boldcyan("INPUT")+"] "+str(string)
def STEP(string):
print(step(string))
myprint(step(string))
def MESSAGE(string):
print("-- "+string)
myprint("-- "+string)
def SUBMESSAGE(string):
print(" * "+string)
myprint(" * "+string)
def WARNING(string):
print(warning_str(string))
myprint(warning_str(string))
......@@ -27,6 +27,7 @@
# its terms.
"""Run a command and re-run it if it did not stop after some time.
Timeout is multiplied by 2 each time.
Usage:
try_again.py [-t <time>] <command>
......@@ -39,20 +40,26 @@ Options:
-t <time>, --timeout <time> time in seconds to wait for
before re-running [default: 10]"""
from diffsel_script_utils import *
from diffsel_script_utils import STEP, MESSAGE, param, data
import diffsel_script_utils
import sys
def printerr(*args):
sys.stderr.write(*args)
sys.stderr.write("\n")
diffsel_script_utils.myprint = printerr
#===================================================================================================
print(step("Parsing command line arguments"))
STEP("Parsing command line arguments")
from docopt import docopt
args = docopt(__doc__)
command = args["<command>"]
MESSAGE("Command is " + param(command))
timeout = int(args["--timeout"])
MESSAGE("Timeout is " + param(timeout) + "s")
MESSAGE("Timeout is " + param(timeout) + " seconds")
#===================================================================================================
print(step("Running command"))
STEP("Running command")
import subprocess as sub
import threading
......@@ -62,7 +69,7 @@ class RunCmd():
self.timeout = timeout
def run(self):
self.p = sub.Popen(self.cmd, shell=True)
self.p = sub.Popen(self.cmd, shell=True, stdout=sub.PIPE)
self.p.wait()
def Run(self):
......@@ -73,11 +80,14 @@ class RunCmd():
thread.join(self.timeout)
if thread.is_alive():
MESSAGE("Timeout. Killed thread. Trying again.")
self.timeout = self.timeout * 2
MESSAGE("New timeout is " + data(self.timeout) + " seconds")
self.p.kill()
thread.join()
else:
MESSAGE("Command terminated within timeout.")
okay = True
sys.stdout.write(self.p.stdout.read().decode('ascii'))
MESSAGE("Running command...")
RunCmd(command, timeout).Run()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment