def devices(): result = {'android': []} good_devices = adb.devices(status='good') for se, name in adb.devices(status=request.params.get("status", "all")).items(): device = {'adb': {'serial': se, 'device': name}} if se in good_devices: props = adb.getprop(se) device.update({ 'product': { 'brand': props.get('ro.product.brand'), 'manufacturer': props.get('ro.product.manufacturer'), 'model': props.get('ro.product.model'), 'board': props.get('ro.product.board'), 'device': props.get('ro.product.device') }, 'locale': { 'language': props.get('ro.product.locale.language'), 'region': props.get('ro.product.locale.region') }, 'build': { 'fingerprint': props.get('ro.build.fingerprint'), 'type': props.get('ro.build.type'), 'date_utc': props.get('ro.build.date.utc'), 'display_id': props.get('ro.build.display.id'), 'id': props.get('ro.build.id'), 'version': { 'incremental': props.get('ro.build.version.incremental'), 'release': props.get('ro.build.version.release'), 'sdk': props.get('ro.build.version.sdk'), 'codename': props.get('ro.build.version.codename') } } }) result['android'].append(device) return result
def mainThread(): args = sys.argv[1:] if OPTION_HELP in args or OPTION_HELP_LONG in args: printUsage() return ExitCode.Help if not adb.isAvailable(): printError("'adb' not found, please add its location to $PATH.") return ExitCode.AdbNotFound adb.startServer() devices = adb.devices() if len(devices) == 0: printError("No attached devices.") return ExitCode.NoDevices if OPTION_DEVICE in args: try: serial = args[args.index(OPTION_DEVICE) + 1] except IndexError: printError("Must specify a device serial number.") return ExitCode.WrongUsage if serial in devices: adb.setTargetDevice(serial) else: printError("Device " + serial + " not found.") return ExitCode.UnknownDevice else: if len(devices) > 1: printError("Multiple devices attached, one must be specified.") return ExitCode.MultipleDevices printInfo("RemoteJS - Remote JavaScript Console for Android.") printInfo("Please wait...") if not adb.targetDevice(): adb.setTargetDevice(devices[0]) if not OPTION_NOHOSTUPDATE in args: error = adb.installDeviceTool() if exitCode() > ExitCode.Normal: if exitCode() == ExitCode.DeviceToolFailed: printError("Device tool installation failed - " + error) else: printError("Aborted while installing host tool.") return exitCode() printInfo("Target device is " + adb.targetDevice() + ".") thread.start_new_thread(logcatThread, ()) thread.start_new_thread(inputThread, ()) try: while True: pass except: if exitCode() == ExitCode.Undefined or exitCode() == ExitCode.Normal: printInfo("Exiting...") elif exitCode() == ExitCode.DeviceDisconnected: printError("Device disconnected.") return exitCode()
def test_devices_p(self): #todo: add logic for checking whether device ID has been returned result = adb.devices() #don't check output code in result but presence of "device" string self.assertRegexpMatches(result[1], '\\tdevice')
def create_job(job_id, job_url): repo = request.json.get('repo') if repo is None: abort(400, 'The "repo" is mandatory for creating a new job!') exclusive = get_boolean(request.json.get('exclusive', True)) env = request.json.get('env', {}) env.setdefault('ANDROID_SERIAL', 'no_device') global jobs if exclusive and any(job['job_info']['env']['ANDROID_SERIAL'] == env['ANDROID_SERIAL'] and job['job_info']['exclusive'] for job in jobs): abort(409, 'A job on device with the same ANDROID_SERIAL is running!') if env['ANDROID_SERIAL'] not in adb.devices(status='ok') and env['ANDROID_SERIAL'] != 'no_device': abort(404, 'No specified device attached!') if any(job['job_info']['job_id'] == job_id for job in jobs): abort(409, 'A job with the same job_id is running! If you want to re-run the job, please stop the running one firestly.') job_path = os.path.abspath(os.path.join(app.config.get('jobs.path'), job_id)) shutil.rmtree(job_path, ignore_errors=True) workspace = os.path.join(job_path, 'workspace') os.makedirs(workspace) # make the working directory for the job env.update({ 'WORKSPACE': workspace, 'JOB_ID': job_id }) filenames = ['repo', 'output', 'error', 'run.sh', 'job.json'] local_repo, job_out, job_err, job_script, job_info = [os.path.join(job_path, f) for f in filenames] with open(job_script, "w") as script_f: script_f.write(template( 'run_script', repo=repo, local_repo=local_repo, init_script='%s/init_script/%s' % ( job_url, repo.get('init_script', request.app.config.get('jobs.init_script')) ), env=env )) proc = sh.bash(job_script, _out=job_out, _err=job_err, _bg=True) timestamp = time.time() result = { 'repo': repo, 'job_id': job_id, 'job_pid': proc.pid, 'job_path': job_path, 'env': env, 'exclusive': exclusive, 'started_at': str(timestamp), 'started_datetime': str(datetime.fromtimestamp(timestamp)) } job = {'proc': proc, 'job_info': result} jobs.append(job) callback = request.json.get('callback') def proc_clear(): @lock def check(): global jobs if job and job['proc'].process.alive: return True else: jobs.remove(job) try: result['exit_code'] = job['proc'].exit_code # catch the exception while touching the exit_code first time. except: result['exit_code'] = job['proc'].exit_code finally: timestamp = time.time() result['finished_at'] = str(timestamp) result['finished_datetime'] = str(datetime.fromtimestamp(timestamp)) write_json(job_info, result) if callback: import requests try: requests.get(callback, params={'job_id': job_id}) except: pass return False while check(): time.sleep(1) threading.Thread(target=proc_clear).start() write_json(job_info, result) return result