def GetExpectedDevices(known_devices_files): expected_devices = set() try: for path in known_devices_files: if os.path.exists(path): expected_devices.update( device_list.GetPersistentDeviceList(path)) else: logging.warning('Could not find known devices file: %s', path) except IOError: logging.warning('Problem reading %s, skipping.', path) logging.info('Expected devices:') for device in expected_devices: logging.info(' %s', device) return expected_devices
def _InitDevices(self): device_arg = 'default' if self._target_devices_file: device_arg = device_list.GetPersistentDeviceList( self._target_devices_file) if not device_arg: logging.warning('No target devices specified. Falling back to ' 'running on all available devices.') device_arg = 'default' else: logging.info( 'Read device list %s from target devices file.', str(device_arg)) elif self._device_serial: device_arg = self._device_serial self._devices = device_utils.DeviceUtils.HealthyDevices( self._blacklist, enable_device_files_cache=self._enable_device_cache, default_retries=self._max_tries - 1, device_arg=device_arg) if not self._devices: raise device_errors.NoDevicesError if self._logcat_output_file: self._logcat_output_dir = tempfile.mkdtemp() @handle_shard_failures_with(on_failure=self.BlacklistDevice) def prepare_device(d): if self._enable_device_cache: cache_path = _DeviceCachePath(d) if os.path.exists(cache_path): logging.info('Using device cache: %s', cache_path) with open(cache_path) as f: d.LoadCacheData(f.read()) # Delete cached file so that any exceptions cause it to be cleared. os.unlink(cache_path) if self._logcat_output_dir: logcat_file = os.path.join( self._logcat_output_dir, '%s_%s' % (d.adb.GetDeviceSerial(), datetime.datetime.utcnow().strftime('%Y%m%dT%H%M%S'))) monitor = logcat_monitor.LogcatMonitor( d.adb, clear=True, output_file=logcat_file) self._logcat_monitors.append(monitor) monitor.Start() self.parallel_devices.pMap(prepare_device)
def _GetAllDevices(active_devices, devices_path): try: if devices_path: devices = [device_utils.DeviceUtils(s) for s in device_list.GetPersistentDeviceList(devices_path)] if not devices and active_devices: logging.warning('%s is empty. Falling back to active devices.', devices_path) devices = active_devices else: logging.warning('Known devices file path not being passed. For device ' 'affinity to work properly, it must be passed.') devices = active_devices except IOError as e: logging.error('Unable to find %s [%s]', devices_path, e) devices = active_devices return sorted(devices)
def _GetAllDevices(active_devices, devices_path): # TODO(rnephew): Delete this when recipes change to pass file path. if not devices_path: logging.warning('Known devices file path not being passed. For device ' 'affinity to work properly, it must be passed.') devices_path = os.path.join(os.environ.get('CHROMIUM_OUT_DIR', 'out'), device_list.LAST_DEVICES_FILENAME) try: if devices_path: devices = [ device_utils.DeviceUtils(s) for s in device_list.GetPersistentDeviceList(devices_path) ] else: logging.warning( 'Known devices file path not being passed. For device ' 'affinity to work properly, it must be passed.') devices = active_devices except IOError as e: logging.error('Unable to find %s [%s]', devices_path, e) devices = active_devices return sorted(devices)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--out-dir', help='Directory where the device path is stored', default=os.path.join(host_paths.DIR_SOURCE_ROOT, 'out')) parser.add_argument('--restart-usb', action='store_true', help='DEPRECATED. ' 'This script now always tries to reset USB.') parser.add_argument('--json-output', help='Output JSON information into a specified file.') parser.add_argument('--adb-path', type=os.path.abspath, help='Absolute path to the adb binary to use.') parser.add_argument('--blacklist-file', help='Device blacklist JSON file.') parser.add_argument('--known-devices-file', action='append', default=[], dest='known_devices_files', help='Path to known device lists.') parser.add_argument('-v', '--verbose', action='count', default=1, help='Log more information.') args = parser.parse_args() run_tests_helper.SetLogLevel(args.verbose) devil_chromium.Initialize(adb_path=args.adb_path) blacklist = (device_blacklist.Blacklist(args.blacklist_file) if args.blacklist_file else None) expected_devices = set() try: for path in args.known_devices_files: if os.path.exists(path): expected_devices.update( device_list.GetPersistentDeviceList(path)) except IOError: logging.warning('Problem reading %s, skipping.', path) logging.info('Expected devices:') for device in expected_devices: logging.info(' %s', device) usb_devices = set(lsusb.get_android_devices()) devices = [ device_utils.DeviceUtils(s) for s in expected_devices.union(usb_devices) ] RecoverDevices(devices, blacklist) statuses = DeviceStatus(devices, blacklist) # Log the state of all devices. for status in statuses: logging.info(status['serial']) adb_status = status.get('adb_status') blacklisted = status.get('blacklisted') logging.info(' USB status: %s', 'online' if status.get('usb_status') else 'offline') logging.info(' ADB status: %s', adb_status) logging.info(' Blacklisted: %s', str(blacklisted)) if adb_status == 'device' and not blacklisted: logging.info(' Device type: %s', status.get('ro.build.product')) logging.info(' OS build: %s', status.get('ro.build.id')) logging.info(' OS build fingerprint: %s', status.get('ro.build.fingerprint')) logging.info(' Battery state:') for k, v in status.get('battery', {}).iteritems(): logging.info(' %s: %s', k, v) logging.info(' IMEI slice: %s', status.get('imei_slice')) logging.info(' WiFi IP: %s', status.get('wifi_ip')) # Update the last devices file(s). for path in args.known_devices_files: device_list.WritePersistentDeviceList( path, [status['serial'] for status in statuses]) # Write device info to file for buildbot info display. if os.path.exists('/home/chrome-bot'): with open('/home/chrome-bot/.adb_device_info', 'w') as f: for status in statuses: try: if status['adb_status'] == 'device': f.write( '{serial} {adb_status} {build_product} {build_id} ' '{temperature:.1f}C {level}%\n'.format( serial=status['serial'], adb_status=status['adb_status'], build_product=status['type'], build_id=status['build'], temperature=float( status['battery']['temperature']) / 10, level=status['battery']['level'])) elif status.get('usb_status', False): f.write('{serial} {adb_status}\n'.format( serial=status['serial'], adb_status=status['adb_status'])) else: f.write('{serial} offline\n'.format( serial=status['serial'])) except Exception: # pylint: disable=broad-except pass # Dump the device statuses to JSON. if args.json_output: with open(args.json_output, 'wb') as f: f.write(json.dumps(statuses, indent=4)) live_devices = [ status['serial'] for status in statuses if (status['adb_status'] == 'device' and not _IsBlacklisted(status['serial'], blacklist)) ] # If all devices failed, or if there are no devices, it's an infra error. if not live_devices: logging.error('No available devices.') return 0 if live_devices else exit_codes.INFRA
def main(): parser = argparse.ArgumentParser() parser.add_argument('--out-dir', help='Directory where the device path is stored', default=os.path.join(constants.DIR_SOURCE_ROOT, 'out')) parser.add_argument('--restart-usb', action='store_true', help='DEPRECATED. ' 'This script now always tries to reset USB.') parser.add_argument('--json-output', help='Output JSON information into a specified file.') parser.add_argument('--blacklist-file', help='Device blacklist JSON file.') parser.add_argument('-v', '--verbose', action='count', default=1, help='Log more information.') args = parser.parse_args() run_tests_helper.SetLogLevel(args.verbose) blacklist = (device_blacklist.Blacklist(args.blacklist_file) if args.blacklist_file else None) last_devices_path = os.path.join( args.out_dir, device_list.LAST_DEVICES_FILENAME) try: expected_devices = set( device_list.GetPersistentDeviceList(last_devices_path)) except IOError: expected_devices = set() usb_devices = set(lsusb.get_android_devices()) devices = [device_utils.DeviceUtils(s) for s in expected_devices.union(usb_devices)] RecoverDevices(devices, blacklist) statuses = DeviceStatus(devices, blacklist) # Log the state of all devices. for status in statuses: logging.info(status['serial']) adb_status = status.get('adb_status') blacklisted = status.get('blacklisted') logging.info(' USB status: %s', 'online' if status.get('usb_status') else 'offline') logging.info(' ADB status: %s', adb_status) logging.info(' Blacklisted: %s', str(blacklisted)) if adb_status == 'device' and not blacklisted: logging.info(' Device type: %s', status.get('ro.build.product')) logging.info(' OS build: %s', status.get('ro.build.id')) logging.info(' OS build fingerprint: %s', status.get('ro.build.fingerprint')) logging.info(' Battery state:') for k, v in status.get('battery', {}).iteritems(): logging.info(' %s: %s', k, v) logging.info(' IMEI slice: %s', status.get('imei_slice')) logging.info(' WiFi IP: %s', status.get('wifi_ip')) # Update the last devices file. device_list.WritePersistentDeviceList( last_devices_path, [status['serial'] for status in statuses]) # Write device info to file for buildbot info display. if os.path.exists('/home/chrome-bot'): with open('/home/chrome-bot/.adb_device_info', 'w') as f: for status in statuses: try: if status['adb_status'] == 'device': f.write('{serial} {adb_status} {build_product} {build_id} ' '{temperature:.1f}C {level}%\n'.format( serial=status['serial'], adb_status=status['adb_status'], build_product=status['type'], build_id=status['build'], temperature=float(status['battery']['temperature']) / 10, level=status['battery']['level'] )) else: f.write('{serial} {adb_status}'.format( serial=status['serial'], adb_status=status['adb_status'] )) except Exception: # pylint: disable=broad-except pass # Dump the device statuses to JSON. if args.json_output: with open(args.json_output, 'wb') as f: f.write(json.dumps(statuses, indent=4)) live_devices = [status['serial'] for status in statuses if (status['adb_status'] == 'device' and not _IsBlacklisted(status['serial'], blacklist))] # If all devices failed, or if there are no devices, it's an infra error. return 0 if live_devices else constants.INFRA_EXIT_CODE