1#!/usr/bin/env python3.4 2# 3# Copyright 2016 - 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 17from subprocess import Popen, PIPE 18 19def exe_cmd(*cmds): 20 """Executes commands in a new shell. Directing stderr to PIPE. 21 22 This is fastboot's own exe_cmd because of its peculiar way of writing 23 non-error info to stderr. 24 25 Args: 26 cmds: A sequence of commands and arguments. 27 28 Returns: 29 The output of the command run. 30 31 Raises: 32 Exception is raised if an error occurred during the command execution. 33 """ 34 cmd = ' '.join(cmds) 35 proc = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) 36 (out, err) = proc.communicate() 37 if not err: 38 return out 39 return err 40 41class FastbootError(Exception): 42 """Raised when there is an error in fastboot operations.""" 43 44class FastbootProxy(): 45 """Proxy class for fastboot. 46 47 For syntactic reasons, the '-' in fastboot commands need to be replaced 48 with '_'. Can directly execute fastboot commands on an object: 49 >> fb = FastbootProxy(<serial>) 50 >> fb.devices() # will return the console output of "fastboot devices". 51 """ 52 def __init__(self, serial=""): 53 self.serial = serial 54 if serial: 55 self.fastboot_str = "fastboot -s {}".format(serial) 56 else: 57 self.fastboot_str = "fastboot" 58 59 def _exec_fastboot_cmd(self, name, arg_str): 60 return exe_cmd(' '.join((self.fastboot_str, name, arg_str))) 61 62 def args(self, *args): 63 return exe_cmd(' '.join((self.fastboot_str,) + args)) 64 65 def __getattr__(self, name): 66 def fastboot_call(*args): 67 clean_name = name.replace('_', '-') 68 arg_str = ' '.join(str(elem) for elem in args) 69 return self._exec_fastboot_cmd(clean_name, arg_str) 70 return fastboot_call 71