66# you may not use this file except in compliance with the License.
77# You may obtain a copy of the License at
88# http://www.apache.org/licenses/LICENSE-2.0
9- #
9+ #
1010# Unless required by applicable law or agreed to in writing, software
1111# distributed under the License is distributed on an "AS IS" BASIS,
1212# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1818import threading
1919import subprocess
2020
21- # This script executes a long-running command while outputing "still running ..." periodically
21+ # This script executes a long-running command while printing "still running ..." periodically
2222# to notify Travis build system that the program has not hanged
2323
2424PING_INTERVAL = 300 # 5 minutes
25+ SLEEP_INTERVAL = 1 # 1 second
2526
26- def monitor (stop ):
27- wait_time = 0
28- while True :
29- time .sleep (PING_INTERVAL )
30- wait_time += PING_INTERVAL
31- print (" + still running (" + str (wait_time ) + "s) ..." )
32- sys .stdout .flush ()
33- if stop ():
34- break
27+ def monitor (stop_event ):
28+ wait_time = 0
29+ elapsed_time = 0
30+ while not stop_event .is_set ():
31+ time .sleep (SLEEP_INTERVAL )
32+ elapsed_time += SLEEP_INTERVAL
33+ if elapsed_time >= PING_INTERVAL :
34+ wait_time += elapsed_time
35+ print (" + still running (" + str (wait_time ) + "s) ..." )
36+ sys .stdout .flush ()
37+ elapsed_time = 0
3538
36- def execute (command ):
39+ def execute_verbose (command ):
3740 process = subprocess .Popen (command , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
3841 while True :
3942 output = process .stdout .readline ()
@@ -46,23 +49,30 @@ def execute(command):
4649 process .stdout .flush ()
4750 ret = process .poll ()
4851 if ret is not None :
49- return ret
52+ print (" + exitcode=" + str (ret ))
53+ return ret
5054 return - 1
5155
56+ def execute (command ):
57+ process = subprocess .Popen (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
58+ ret = process .wait ()
59+ return ret
60+
5261def main (argv ):
62+ if not argv :
63+ print ("Usage: travis_run.py <command>" )
64+ sys .exit (1 )
5365
5466 # start monitoring thread
55- stop_monitor = False
56- t = threading .Thread (target = monitor , args = ( lambda : stop_monitor , ))
67+ stop_event = threading . Event ()
68+ t = threading .Thread (target = monitor , args = ( stop_event , ))
5769 t .start ()
5870
5971 # execute command
60- exitcode = execute (argv )
61- print (" + exitcode=" + str (exitcode ))
62- sys .stdout .flush ()
63-
72+ exitcode = execute (argv )
73+
6474 # terminate monitoring thread
65- stop_monitor = True
75+ stop_event . set ()
6676 t .join ()
6777
6878 sys .exit (exitcode )
0 commit comments