1#!/usr/bin/python2
2
3import common
4import sys, os, subprocess, fcntl
5
6
7bindir = os.path.dirname(__file__)
8autotest = os.path.join(bindir, 'autotest')
9
10logdir = sys.argv[1]
11
12
13# We want to simulate the behaviour of autotest_client, where fd3 would be
14# routed to stderr and fd1 & fd2 to stdout
15
16# HACK: grab fd3 for now
17os.dup2(2, 3)
18
19# open up log files to use for std*
20stdout = open(os.path.join(logdir, 'stdout'), 'a', buffering=2)
21stderr = open(os.path.join(logdir, 'stderr'), 'a', buffering=2)
22
23# set up the file descriptors now, simulating the old behaviour
24os.dup2(stdout.fileno(), 1)
25os.dup2(stdout.fileno(), 2)
26os.dup2(stderr.fileno(), 3)
27
28# we don't need the file objects any more
29stdout.close()
30stderr.close()
31
32
33args = [autotest] + sys.argv[2:]
34if '-H' not in args:
35    args[1:1] = ['-H', 'autoserv']
36cmd = ' '.join(args)
37
38# open up a log file for saving off the exit code
39exit_file = open(os.path.join(logdir, 'exit_code'), 'wb', buffering=0)
40fcntl.flock(exit_file, fcntl.LOCK_EX)
41
42# touch a 'started' file to indicate we've been initialized
43open(os.path.join(logdir, 'started'), 'w').close()
44
45# run the actual autotest client and write the exit code into the log file
46# close_fds must be False to support python 2 and 3. In 3 the default changes
47# to True, and will break fd writing used elsewhere (e.g. harness_autoserv)
48exit_code = subprocess.call("{} {}".format(sys.executable, cmd),
49                            shell=True,
50                            close_fds=False)
51exit_file.write('%+04d' % exit_code)
52exit_file.flush()
53fcntl.flock(exit_file, fcntl.LOCK_UN)
54exit_file.close()
55