jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import common |
| 4 | import sys, os, signal, time, subprocess, fcntl |
| 5 | |
| 6 | logdir = sys.argv[1] |
| 7 | stdout_start = int(sys.argv[2]) # number of bytes we can skip on stdout |
| 8 | stderr_start = int(sys.argv[3]) # nubmer of bytes we can skip on stderr |
| 9 | |
| 10 | devnull = open(os.devnull, 'w') |
| 11 | |
| 12 | # launch some tail processes to pump the std* streams |
| 13 | def launch_tail(filename, outstream, start): |
| 14 | path = os.path.join(logdir, filename) |
| 15 | argv = ['tail', '--retry', '--follow=name', '--bytes=+%d' % start, path] |
| 16 | # stdout=sys.stdout fails on pre-2.5 python (bug in subprocess module) |
| 17 | if outstream != subprocess.PIPE and outstream.fileno() == 1: |
| 18 | return subprocess.Popen(argv, stderr=devnull) |
| 19 | else: |
| 20 | return subprocess.Popen(argv, stdout=outstream, stderr=devnull) |
| 21 | stdout_pump = launch_tail('stdout', sys.stdout, stdout_start) |
| 22 | stderr_pump = launch_tail('stderr', sys.stderr, stderr_start) |
| 23 | |
| 24 | # watch the exit code file for an exit |
| 25 | exit_code_file = open(os.path.join(logdir, 'exit_code')) |
| 26 | fcntl.flock(exit_code_file, fcntl.LOCK_EX) |
| 27 | try: |
| 28 | exit_code = exit_code_file.read() |
| 29 | if len(exit_code) != 4: |
| 30 | exit_code = -signal.SIGKILL # autotestd was nuked |
| 31 | else: |
| 32 | exit_code = int(exit_code) |
| 33 | finally: |
| 34 | fcntl.flock(exit_code_file, fcntl.LOCK_UN) |
| 35 | exit_code_file.close() |
| 36 | |
| 37 | time.sleep(2) # tail runs in 1s polling loop, so give them a chance to finish |
| 38 | os.kill(stdout_pump.pid, signal.SIGTERM) |
| 39 | os.kill(stderr_pump.pid, signal.SIGTERM) |
| 40 | |
| 41 | # exit (with the same code as autotestd) |
| 42 | sys.exit(exit_code) |