1# Copyright 2022 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4"""Provides a class for managing emulators.""" 5 6import argparse 7 8from contextlib import AbstractContextManager 9 10from ffx_emulator import FfxEmulator 11 12 13def register_emulator_args(parser: argparse.ArgumentParser, 14 enable_graphics: bool = False) -> None: 15 """Register emulator specific arguments.""" 16 femu_args = parser.add_argument_group('emulator', 17 'emulator startup arguments.') 18 femu_args.add_argument('--custom-image', 19 dest='product_bundle', 20 help='Backwards compatible flag that specifies an ' 21 'image used for booting up the emulator.') 22 if enable_graphics: 23 femu_args.add_argument('--disable-graphics', 24 action='store_false', 25 dest='enable_graphics', 26 help='Start emulator in headless mode.') 27 else: 28 femu_args.add_argument('--enable-graphics', 29 action='store_true', 30 help='Start emulator with graphics.') 31 femu_args.add_argument( 32 '--product', 33 help='Specify a product bundle used for booting the ' 34 'emulator. Defaults to the terminal product.') 35 femu_args.add_argument('--with-network', 36 action='store_true', 37 help='Run emulator with emulated nic via tun/tap.') 38 femu_args.add_argument('--everlasting', 39 action='store_true', 40 help='If the emulator should be long-living.') 41 femu_args.add_argument( 42 '--device-spec', 43 help='Configure the virtual device to use. They are usually defined in ' 44 'the product-bundle/virtual_devices/manifest.json. If this flag is not ' 45 'provided or is an empty string, ffx emu will decide the recommended ' 46 'spec.') 47 48 49def create_emulator_from_args( 50 args: argparse.Namespace) -> AbstractContextManager: 51 """Helper method for initializing an FfxEmulator class with parsed 52 arguments.""" 53 return FfxEmulator(args) 54