• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env vpython3
2# Copyright 2022 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Provides a class for managing emulators."""
6
7import argparse
8import logging
9import sys
10import time
11
12from contextlib import AbstractContextManager
13
14from common import catch_sigterm, register_log_args
15from ffx_emulator import FfxEmulator
16
17
18def register_emulator_args(parser: argparse.ArgumentParser,
19                           enable_graphics: bool = False) -> None:
20    """Register emulator specific arguments."""
21    femu_args = parser.add_argument_group('emulator',
22                                          'emulator startup arguments.')
23    femu_args.add_argument('--custom-image',
24                           dest='product_bundle',
25                           help='Backwards compatible flag that specifies an '
26                           'image used for booting up the emulator.')
27    if enable_graphics:
28        femu_args.add_argument('--disable-graphics',
29                               action='store_false',
30                               dest='enable_graphics',
31                               help='Start emulator in headless mode.')
32    else:
33        femu_args.add_argument('--enable-graphics',
34                               action='store_true',
35                               help='Start emulator with graphics.')
36    femu_args.add_argument(
37        '--hardware-gpu',
38        action='store_true',
39        help='Use host GPU hardware instead of Swiftshader.')
40    femu_args.add_argument(
41        '--product-bundle',
42        help='Specify a product bundle used for booting the '
43        'emulator. Defaults to the terminal product.')
44    femu_args.add_argument('--with-network',
45                           action='store_true',
46                           help='Run emulator with emulated nic via tun/tap.')
47    femu_args.add_argument('--everlasting',
48                           action='store_true',
49                           help='If the emulator should be long-living.')
50
51
52def create_emulator_from_args(
53        args: argparse.Namespace) -> AbstractContextManager:
54    """Helper method for initializing an FfxEmulator class with parsed
55    arguments."""
56    return FfxEmulator(args)
57
58
59def main():
60    """Stand-alone function for starting an emulator."""
61
62    catch_sigterm()
63    logging.basicConfig(level=logging.INFO)
64    parser = argparse.ArgumentParser()
65    register_emulator_args(parser, True)
66    register_log_args(parser)
67    args = parser.parse_args()
68    with create_emulator_from_args(args) as target_id:
69        logging.info(
70            'Emulator successfully started. You can now run Chrome '
71            'Fuchsia tests with --target-id=%s to target this emulator.',
72            target_id)
73        try:
74            while True:
75                time.sleep(10000)
76        except KeyboardInterrupt:
77            logging.info('Ctrl-C received; shutting down the emulator.')
78        except SystemExit:
79            logging.info('SIGTERM received; shutting down the emulator.')
80
81
82if __name__ == '__main__':
83    sys.exit(main())
84