1#!/usr/bin/env python 2# 3# Copyright (C) 2015 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17"""ADB handling for NDK tests.""" 18import os 19import re 20import subprocess 21 22 23def push(src, dst): 24 with open(os.devnull, 'wb') as dev_null: 25 subprocess.check_call(['adb', 'push', src, dst], stdout=dev_null, 26 stderr=dev_null) 27 28 29def shell(command): 30 # Work around the fact that adb doesn't return shell exit status. We also 31 # redirect the process's stderr to stdout so we can be sure all the output 32 # is printed before the result code. 33 adb_command = command + ' 2>&1; echo "x$?"' 34 p = subprocess.Popen(['adb', 'shell', adb_command], stdout=subprocess.PIPE) 35 out, _ = p.communicate() 36 if p.returncode != 0: 37 raise RuntimeError('adb shell failed') 38 39 out, _, encoded_result = out.rpartition('x') 40 result = int(encoded_result) 41 out = out.rstrip() 42 out = re.split(r'[\r\n]+', out) 43 return result, out 44 45 46def get_prop(prop_name): 47 result, output = shell('getprop {}'.format(prop_name)) 48 if result != 0: 49 raise RuntimeError('getprop failed:\n' + '\n'.join(output)) 50 if len(output) != 1: 51 raise RuntimeError('Too many lines in getprop output:\n' + 52 '\n'.join(output)) 53 value = output[0] 54 if not value.strip(): 55 return None 56 return value 57