1# Copyright 2016 Google Inc. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import logging 16from subprocess import Popen, PIPE 17 18from mobly import utils 19 20 21def exe_cmd(*cmds): 22 """Executes commands in a new shell. Directing stderr to PIPE. 23 24 This is fastboot's own exe_cmd because of its peculiar way of writing 25 non-error info to stderr. 26 27 Args: 28 cmds: A sequence of commands and arguments. 29 30 Returns: 31 The output of the command run. 32 33 Raises: 34 Exception: An error occurred during the command execution. 35 """ 36 cmd = ' '.join(cmds) 37 proc = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) 38 (out, err) = proc.communicate() 39 ret = proc.returncode 40 logging.debug( 41 'cmd: %s, stdout: %s, stderr: %s, ret: %s', 42 utils.cli_cmd_to_string(cmds), 43 out, 44 err, 45 ret, 46 ) 47 if not err: 48 return out 49 return err 50 51 52class FastbootProxy: 53 """Proxy class for fastboot. 54 55 For syntactic reasons, the '-' in fastboot commands need to be replaced 56 with '_'. Can directly execute fastboot commands on an object: 57 >> fb = FastbootProxy(<serial>) 58 >> fb.devices() # will return the console output of "fastboot devices". 59 """ 60 61 def __init__(self, serial=''): 62 self.serial = serial 63 if serial: 64 self.fastboot_str = 'fastboot -s {}'.format(serial) 65 else: 66 self.fastboot_str = 'fastboot' 67 68 def _exec_fastboot_cmd(self, name, arg_str): 69 return exe_cmd(' '.join((self.fastboot_str, name, arg_str))) 70 71 def args(self, *args): 72 return exe_cmd(' '.join((self.fastboot_str,) + args)) 73 74 def __getattr__(self, name): 75 def fastboot_call(*args): 76 clean_name = name.replace('_', '-') 77 arg_str = ' '.join(str(elem) for elem in args) 78 return self._exec_fastboot_cmd(clean_name, arg_str) 79 80 return fastboot_call 81