1# Copyright 2014 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 5import os 6 7from pylib import constants 8from pylib.local.device import local_device_environment 9from pylib.local.device import skylab_environment 10 11from pylib.local.machine import local_machine_environment 12 13try: 14 # local_emulator_environment depends on //tools. 15 # If a client pulls in the //build subtree but not the //tools 16 # one, fail at emulator environment creation time. 17 from pylib.local.emulator import local_emulator_environment 18except ImportError: 19 local_emulator_environment = None 20 21 22def CreateEnvironment(args, output_manager, error_func): 23 24 if args.environment == 'local': 25 if args.command not in constants.LOCAL_MACHINE_TESTS: 26 if args.avd_config: 27 if not local_emulator_environment: 28 error_func('emulator environment requested but not available.') 29 raise RuntimeError('error_func must call exit inside.') 30 return local_emulator_environment.LocalEmulatorEnvironment( 31 args, output_manager, error_func) 32 33 if args.connect_over_ethernet: 34 swarming_server = os.environ.get('SWARMING_SERVER', '') 35 if skylab_environment.SWARMING_SERVER not in swarming_server: 36 error_func( 37 'connect-over-ethernet is only supported for tasks running on ' 38 f'{skylab_environment.SWARMING_SERVER}. ' 39 f'SWARMING_SERVER={swarming_server}') 40 raise RuntimeError('error_func must call exit inside.') 41 return skylab_environment.SkylabEnvironment(args, output_manager, 42 error_func) 43 44 return local_device_environment.LocalDeviceEnvironment( 45 args, output_manager, error_func) 46 return local_machine_environment.LocalMachineEnvironment( 47 args, output_manager, error_func) 48 49 error_func('Unable to create %s environment.' % args.environment) 50 raise RuntimeError('error_func must call exit inside.') 51