1#
2#   Copyright 2016 - The Android Open Source Project
3#
4#   Licensed under the Apache License, Version 2.0 (the "License");
5#   you may not use this file except in compliance with the License.
6#   You may obtain a copy of the License at
7#
8#       http://www.apache.org/licenses/LICENSE-2.0
9#
10#   Unless required by applicable law or agreed to in writing, software
11#   distributed under the License is distributed on an "AS IS" BASIS,
12#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#   See the License for the specific language governing permissions and
14#   limitations under the License.
15
16from subprocess import Popen, PIPE
17
18import socket
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 is raised if 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    if not err:
40        return out
41    return err
42
43def isFastbootOverTcp(serial):
44    token = serial.split(':')
45    if len(token) == 2:
46        try:
47            socket.inet_aton(token[0])
48            return True
49        except socket.error:
50            return False
51    return False
52
53class FastbootError(Exception):
54    """Raised when there is an error in fastboot operations."""
55
56
57class FastbootProxy():
58    """Proxy class for fastboot.
59
60    For syntactic reasons, the '-' in fastboot commands need to be replaced
61    with '_'. Can directly execute fastboot commands on an object:
62    >> fb = FastbootProxy(<serial>)
63    >> fb.devices() # will return the console output of "fastboot devices".
64    """
65
66    def __init__(self, serial=""):
67        self.serial = serial
68        if serial:
69            if isFastbootOverTcp(serial):
70                self.fastboot_str = "fastboot -s tcp:{}".format(serial[:serial.index(':')])
71            else:
72                self.fastboot_str = "fastboot -s {}".format(serial)
73        else:
74            self.fastboot_str = "fastboot"
75
76    def _exec_fastboot_cmd(self, name, arg_str):
77        return exe_cmd(' '.join((self.fastboot_str, name, arg_str)))
78
79    def args(self, *args):
80        return exe_cmd(' '.join((self.fastboot_str, ) + args))
81
82    def __getattr__(self, name):
83        def fastboot_call(*args):
84            clean_name = name.replace('_', '-')
85            arg_str = ' '.join(str(elem) for elem in args)
86            return self._exec_fastboot_cmd(clean_name, arg_str)
87
88        return fastboot_call
89