Reland "[Autotest][Py3] Updating autotestd_monitor writing to support py3"
This is a reland of 889a3df1f84f25b910d53b013189b9f642194ab0
Original change's description:
> [Autotest][Py3] Updating autotestd_monitor writing to support py3
>
> To support python3, changing autotestd_monitor stderr logging from 0 to
> 2 (where 2 is a 2 byte buffer, the closest value to the original non
> buffer). In Python3 0 (no buffer) is only valid for
> binary type (aka, `with(open(file, 'ab'))` rather than normal type.
>
> Tested this both in python2 & 3 with success
>
> BUG=chromium:990593
> TEST=dummy_Pass in python 2 and 3
>
> Change-Id: I456eb8c54566b23e6db19b305e94bd1bb23becaf
> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2542650
> Reviewed-by: Prathmesh Prabhu <pprabhu@google.com>
> Tested-by: Derek Beckett <dbeckett@chromium.org>
> Auto-Submit: Derek Beckett <dbeckett@chromium.org>
> Commit-Queue: Derek Beckett <dbeckett@chromium.org>
Bug: chromium:990593
Change-Id: I5ab92bd3a54cf65cf2b6b0f5a81f3f5f17f99bac
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2577874
Commit-Queue: Derek Beckett <dbeckett@chromium.org>
Tested-by: Derek Beckett <dbeckett@chromium.org>
Auto-Submit: Derek Beckett <dbeckett@chromium.org>
Reviewed-by: Prathmesh Prabhu <pprabhu@google.com>
diff --git a/client/bin/autotestd_monitor b/client/bin/autotestd_monitor
index 1597fab..db2d3be 100755
--- a/client/bin/autotestd_monitor
+++ b/client/bin/autotestd_monitor
@@ -6,13 +6,20 @@
import common
import sys, os, signal, time, subprocess, fcntl
+
+def _print_to_file_and_flush(msg, file):
+ """Print to the provided file, and flush after printing."""
+ print(msg, file=file)
+ file.flush()
+
logdir = sys.argv[1]
stdout_start = int(sys.argv[2]) # number of bytes we can skip on stdout
stderr_start = int(sys.argv[3]) # nubmer of bytes we can skip on stderr
# TODO (crosbug.com/38224)- sbasi: Remove extra logging.
-stderr = open(os.path.join(logdir, 'stderr'), 'a', 0)
+stderr = open(os.path.join(logdir, 'stderr'), 'a', buffering=2)
-print('Entered autotestd_monitor.', file=stderr)
+_print_to_file_and_flush('Entered autotestd_monitor.', file=stderr)
+
# if any of our tail processes die, the monitor should die too
def kill_self(signum, frame):
os.kill(os.getpid(), signal.SIGTERM)
@@ -32,8 +39,7 @@
stdout_pump = launch_tail('stdout', sys.stdout, stdout_start)
stderr_pump = launch_tail('stderr', sys.stderr, stderr_start)
-print('Finished launching tail subprocesses.', file=stderr)
-
+_print_to_file_and_flush('Finished launching tail subprocesses.', file=stderr)
# wait for logdir/started to exist to be sure autotestd is started
start_time = time.time()
started_file_path = os.path.join(logdir, 'started')
@@ -42,12 +48,14 @@
if time.time() - start_time >= 30:
raise Exception("autotestd failed to start in %s" % logdir)
-print('Finished waiting on autotestd to start.', file=stderr)
+_print_to_file_and_flush('Finished waiting on autotestd to start.',
+ file=stderr)
# watch the exit code file for an exit
exit_code_file = open(os.path.join(logdir, 'exit_code'))
fcntl.flock(exit_code_file, fcntl.LOCK_EX)
-print('Got lock of exit_code_file.', file=stderr)
+_print_to_file_and_flush('Got lock of exit_code_file.', file=stderr)
+
try:
exit_code = exit_code_file.read()
if len(exit_code) != 4:
@@ -57,16 +65,21 @@
finally:
fcntl.flock(exit_code_file, fcntl.LOCK_UN)
exit_code_file.close()
- print('Released lock of exit_code_file and closed it.', file=stderr)
+ _print_to_file_and_flush('Released lock of exit_code_file and closed it.',
+ file=stderr)
+ stderr.flush()
# Give tail a tiny bit of time to finish.
time.sleep(0.01)
-print('Killing child processes.', file=stderr)
+_print_to_file_and_flush('Killing child processes.', file=stderr)
+stderr.flush()
+
# clear the SIGCHLD handler so that killing the tails doesn't kill us
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
os.kill(stdout_pump.pid, signal.SIGTERM)
os.kill(stderr_pump.pid, signal.SIGTERM)
+stderr.close()
# exit (with the same code as autotestd)
sys.exit(exit_code)