blob: 067890e870259d60e31e0626c98fa03958b83fc1 [file] [log] [blame]
Alex Light6717e5f2018-03-12 11:54:48 -07001#!/usr/bin/env python3
2#
3# Copyright 2018, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import argparse
18import subprocess
19import sys
20import shlex
21
22def main():
23 parser = argparse.ArgumentParser(
24 description='Runs a command while collecting logcat in the background')
25 parser.add_argument('--output', '-o',
26 type=lambda f: open(f,'w'),
27 required=True,
28 action='store',
29 help='File to store the logcat to. Will be created if not already existing')
30 parser.add_argument('--logcat-invoke',
31 action='store',
32 default='adb logcat',
33 help="""Command to run to retrieve logcat data. Defaults to 'adb logcat'.
34 It will be run in the background and killed by SIGTERM when the 'command'
35 finishes.""")
36 parser.add_argument('command',
37 action='store',
Alex Light99d8f892018-03-13 15:38:11 -070038 nargs=argparse.REMAINDER,
Alex Light6717e5f2018-03-12 11:54:48 -070039 help='The command to run with logcat in the background.')
40 args = parser.parse_args()
Alex Light99d8f892018-03-13 15:38:11 -070041 if len(args.command) == 0:
42 print("Must have some command to run.", file=sys.stderr)
43 parser.print_help(file=sys.stderr)
44 return 1
Alex Light6717e5f2018-03-12 11:54:48 -070045 # Send all output from logcat to the file.
46 with subprocess.Popen(shlex.split(args.logcat_invoke),
47 stdout=args.output,
48 stderr=subprocess.STDOUT,
49 shell=False,
50 universal_newlines=True) as logcat_proc:
51 # Let the run-test-proc inherit our stdout FDs
Alex Light99d8f892018-03-13 15:38:11 -070052 with subprocess.Popen(shlex.split(args.command[0]) if len(args.command) == 1 else args.command,
Alex Light6717e5f2018-03-12 11:54:48 -070053 stdout=None,
54 stderr=None,
55 shell=False) as run_test_proc:
56 # Don't actually do anything. Just let the run-test proc finish.
57 pass
58 # Send SIGTERM to the logcat process.
59 logcat_proc.kill()
60 return 0
61
62if __name__ == '__main__':
63 sys.exit(main())
64