From bb32b6d5bb3b6d39610bd61636cda131f5c7dc3f Mon Sep 17 00:00:00 2001 From: Jeff Irion Date: Sun, 15 Sep 2019 11:10:06 -0700 Subject: [PATCH 1/2] 'documentation' branch: remove comments and apply black formatting Re-run black_remove_comments.sh Re-run black_remove_comments.sh --- adb/adb_commands.py | 35 +++++++++++++------------ adb/adb_debug.py | 41 ++++++++++++++--------------- adb/adb_protocol.py | 56 ++++++++++++++++++++++++---------------- adb/common.py | 21 ++++++++++++--- adb/common_cli.py | 31 +++++++++++----------- adb/fastboot.py | 28 +++++++++++--------- adb/fastboot_debug.py | 9 +++---- adb/filesync_protocol.py | 10 +++++-- adb/sign_cryptography.py | 4 +-- adb/sign_pycryptodome.py | 4 +-- adb/sign_pythonrsa.py | 15 ++++++----- black_remove_comments.sh | 4 +-- 12 files changed, 146 insertions(+), 112 deletions(-) diff --git a/adb/adb_commands.py b/adb/adb_commands.py index 5464b41..d55218b 100644 --- a/adb/adb_commands.py +++ b/adb/adb_commands.py @@ -7,31 +7,33 @@ from adb import common from adb import filesync_protocol +try: + file_types = (file, io.IOBase) +except NameError: + file_types = (io.IOBase,) + CLASS = 0xFF + SUBCLASS = 0x42 + PROTOCOL = 0x01 DeviceIsAvailable = common.InterfaceMatcher(CLASS, SUBCLASS, PROTOCOL) -try: - from adb.sign_cryptography import CryptographySigner -except ImportError: - pass - class AdbCommands(object): protocol_handler = adb_protocol.AdbMessage filesync_handler = filesync_protocol.FilesyncProtocol def __init__(self): - self.__reset() - - def __reset(self): self.build_props = None - self._handle = None self._device_state = None + self._handle = None self._service_connections = {} + def __reset(self): + self.__init__() + def _get_service_connection( self, service, service_command=None, create=True, timeout_ms=None ): @@ -124,7 +126,7 @@ def Install( cmd.append('"{}"'.format(destination_path)) ret = self.Shell(" ".join(cmd), timeout_ms=timeout_ms) rm_cmd = ["rm", destination_path] - rmret = self.Shell(" ".join(rm_cmd), timeout_ms=timeout_ms) + self.Shell(" ".join(rm_cmd), timeout_ms=timeout_ms) return ret def Uninstall(self, package_name, keep_data=False, timeout_ms=None): @@ -178,10 +180,10 @@ def Pull( dest_file = io.BytesIO() elif isinstance(dest_file, str): dest_file = open(dest_file, "wb") - elif isinstance(dest_file, file): + elif isinstance(dest_file, file_types): pass else: - raise ValueError("destfile is of unknown type") + raise ValueError("dest_file is of unknown type") conn = self.protocol_handler.Open( self._handle, destination=b"sync:", timeout_ms=timeout_ms ) @@ -189,11 +191,10 @@ def Pull( conn.Close() if isinstance(dest_file, io.BytesIO): return dest_file.getvalue() - else: - dest_file.close() - if hasattr(dest_file, "name"): - return os.path.exists(dest_file.name) - return True + dest_file.close() + if hasattr(dest_file, "name"): + return os.path.exists(dest_file.name) + return True def Stat(self, device_filename): connection = self.protocol_handler.Open(self._handle, destination=b"sync:") diff --git a/adb/adb_debug.py b/adb/adb_debug.py index 44af1ab..b43b927 100644 --- a/adb/adb_debug.py +++ b/adb/adb_debug.py @@ -81,24 +81,23 @@ def Logcat(device, *options): def Shell(device, *command): if command: return device.StreamingShell(" ".join(command)) - else: - terminal_prompt = device.InteractiveShell() - print(terminal_prompt.decode("utf-8")) - while True: - cmd = input("> ") - if not cmd: - continue - elif cmd == "exit": - break - else: - stdout = device.InteractiveShell( - cmd, strip_cmd=True, delim=terminal_prompt, strip_delim=True - ) - if stdout: - if isinstance(stdout, bytes): - stdout = stdout.decode("utf-8") - print(stdout) - device.Close() + terminal_prompt = device.InteractiveShell() + print(terminal_prompt.decode("utf-8")) + while True: + cmd = input("> ") + if not cmd: + continue + elif cmd == "exit": + break + else: + stdout = device.InteractiveShell( + cmd, strip_cmd=True, delim=terminal_prompt, strip_delim=True + ) + if stdout: + if isinstance(stdout, bytes): + stdout = stdout.decode("utf-8") + print(stdout) + device.Close() def main(): @@ -115,8 +114,7 @@ def main(): default=60.0, metavar="60", type=int, - help="Seconds to wait for the dialog to be accepted when using " - "authenticated ADB.", + help="Seconds to wait for the dialog to be accepted when using authenticated ADB.", ) device = common_cli.GetDeviceArguments() parents = [common, device] @@ -148,8 +146,7 @@ def main(): parents, adb_commands.AdbCommands.Pull, { - "dest_file": "Filename to write to on the host, if not specified, " - "prints the content to stdout." + "dest_file": "Filename to write to on the host, if not specified, prints the content to stdout." }, ) common_cli.MakeSubparser(subparsers, parents, adb_commands.AdbCommands.Reboot) diff --git a/adb/adb_protocol.py b/adb/adb_protocol.py index e011087..a5fad78 100644 --- a/adb/adb_protocol.py +++ b/adb/adb_protocol.py @@ -8,22 +8,10 @@ VERSION = 0x01000000 AUTH_TOKEN = 1 -AUTH_SIGNATURE = 2 -AUTH_RSAPUBLICKEY = 3 +AUTH_SIGNATURE = 2 -def find_backspace_runs(stdout_bytes, start_pos): - first_backspace_pos = stdout_bytes[start_pos:].find(b"\x08") - if first_backspace_pos == -1: - return -1, 0 - end_backspace_pos = (start_pos + first_backspace_pos) + 1 - while True: - if chr(stdout_bytes[end_backspace_pos]) == "\b": - end_backspace_pos += 1 - else: - break - num_backspaces = end_backspace_pos - (start_pos + first_backspace_pos) - return (start_pos + first_backspace_pos), num_backspaces +AUTH_RSAPUBLICKEY = 3 class InvalidCommandError(Exception): @@ -42,6 +30,20 @@ class InvalidChecksumError(Exception): class InterleavedDataError(Exception): +def find_backspace_runs(stdout_bytes, start_pos): + first_backspace_pos = stdout_bytes[start_pos:].find(b"\x08") + if first_backspace_pos == -1: + return -1, 0 + end_backspace_pos = (start_pos + first_backspace_pos) + 1 + while True: + if chr(stdout_bytes[end_backspace_pos]) == "\b": + end_backspace_pos += 1 + else: + break + num_backspaces = end_backspace_pos - (start_pos + first_backspace_pos) + return (start_pos + first_backspace_pos), num_backspaces + + def MakeWireIDs(ids): id_to_wire = { cmd_id: sum(c << (i * 8) for i, c in enumerate(bytearray(cmd_id))) @@ -79,7 +81,11 @@ def Write(self, data): "Command failed.", okay_data ) raise InvalidCommandError( - "Expected an OKAY in response to a WRITE, got %s (%s)", cmd, okay_data + "Expected an OKAY in response to a WRITE, got {0} ({1})".format( + cmd, okay_data + ), + cmd, + okay_data, ) return len(data) @@ -90,11 +96,13 @@ def ReadUntil(self, *expected_cmds): cmd, remote_id, local_id, data = AdbMessage.Read( self.usb, expected_cmds, self.timeout_ms ) - if local_id != 0 and self.local_id != local_id: + if local_id not in (0, self.local_id): raise InterleavedDataError("We don't support multiple streams...") - if remote_id != 0 and self.remote_id != remote_id: + if remote_id not in (0, self.remote_id): raise InvalidResponseError( - "Incorrect remote id, expected %s got %s" % (self.remote_id, remote_id) + "Incorrect remote id, expected {0} got {1}".format( + self.remote_id, remote_id + ) ) if cmd == b"WRTE": self.Okay() @@ -112,7 +120,9 @@ def ReadUntilClose(self): "Command failed.", data ) raise InvalidCommandError( - "Expected a WRITE or a CLOSE, got %s (%s)", cmd, data + "Expected a WRITE or a CLOSE, got {0} ({1})".format(cmd, data), + cmd, + data, ) yield data @@ -123,7 +133,7 @@ def Close(self): if cmd == b"FAIL": raise usb_exceptions.AdbCommandFailureException("Command failed.", data) raise InvalidCommandError( - "Expected a CLSE response, got %s (%s)", cmd, data + "Expected a CLSE response, got {0} ({1})".format(cmd, data), cmd, data ) @@ -217,7 +227,9 @@ def Read(cls, usb, expected_cmds, timeout_ms=None, total_timeout_ms=None): actual_checksum = cls.CalculateChecksum(data) if actual_checksum != data_checksum: raise InvalidChecksumError( - "Received checksum %s != %s", (actual_checksum, data_checksum) + "Received checksum {0} != {1}".format( + actual_checksum, data_checksum + ) ) else: data = b"" @@ -344,7 +356,7 @@ def InteractiveShellCommand( original_cmd = str(cmd) cmd += "\r" cmd = cmd.encode("utf8") - bytes_written = conn.Write(cmd) + conn.Write(cmd) if delim: data = b"" while partial_delim not in data: diff --git a/adb/common.py b/adb/common.py index 860b78e..cf5b9a0 100644 --- a/adb/common.py +++ b/adb/common.py @@ -1,17 +1,26 @@ import logging import platform +import re +import select import socket import threading import weakref -import select import libusb1 import usb1 +try: + from libusb1 import LIBUSB_ERROR_NOT_FOUND, LIBUSB_ERROR_TIMEOUT +except ImportError: + LIBUSB_ERROR_NOT_FOUND = "LIBUSB_ERROR_NOT_FOUND" + LIBUSB_ERROR_TIMEOUT = "LIBUSB_ERROR_TIMEOUT" + from adb import usb_exceptions DEFAULT_TIMEOUT_MS = 10000 +SYSFS_PORT_SPLIT_RE = re.compile("[,/:.-]") + _LOG = logging.getLogger("android_usb") @@ -26,6 +35,7 @@ def Matcher(device): for setting in device.iterSettings(): if GetInterface(setting) == interface: return setting + return None return Matcher @@ -38,6 +48,9 @@ def __init__(self, device, setting, usb_info=None, timeout_ms=None): self._setting = setting self._device = device self._handle = None + self._interface_number = None + self._read_endpoint = None + self._write_endpoint = None self._usb_info = usb_info or "" self._timeout_ms = timeout_ms if timeout_ms else DEFAULT_TIMEOUT_MS self._max_read_packet_len = 0 @@ -77,7 +90,7 @@ def Open(self): ): handle.detachKernelDriver(iface_number) except libusb1.USBError as e: - if e.value == libusb1.LIBUSB_ERROR_NOT_FOUND: + if e.value == LIBUSB_ERROR_NOT_FOUND: _LOG.warning("Kernel driver not found for interface: %s.", iface_number) else: raise @@ -117,7 +130,7 @@ def FlushBuffers(self): try: self.BulkRead(self._max_read_packet_len, timeout_ms=10) except usb_exceptions.ReadFailedError as e: - if e.usb_error.value == libusb1.LIBUSB_ERROR_TIMEOUT: + if e.usb_error.value == LIBUSB_ERROR_TIMEOUT: break raise @@ -158,7 +171,7 @@ def BulkRead(self, length, timeout_ms=None): ) def BulkReadAsync(self, length, timeout_ms=None): - return + raise NotImplementedError @classmethod def PortPathMatcher(cls, port_path): diff --git a/adb/common_cli.py b/adb/common_cli.py index 1fa9e2f..7dc7c1f 100644 --- a/adb/common_cli.py +++ b/adb/common_cli.py @@ -49,24 +49,25 @@ def GetCommonArguments(): def _DocToArgs(doc): - m = None offset = None - in_arg = False + param = None out = {} for l in doc.splitlines(): - if l.strip() == "Args:": - in_arg = True - elif in_arg: - if not l.strip(): + if l.strip() == "Parameters": + offset = len(l.rstrip()) - len(l.strip()) + elif offset: + if l.strip() == "-" * len("Parameters"): + continue + if l.strip() in ["Returns", "Yields", "Raises"]: break - if offset is None: - offset = len(l) - len(l.lstrip()) - l = l[offset:] - if l[0] == " " and m: - out[m.group(1)] += " " + l.lstrip() - else: - m = re.match(r"^([a-z_]+): (.+)$", l.strip()) - out[m.group(1)] = m.group(2) + if len(l.rstrip()) - len(l.strip()) == offset: + param = l.strip().split()[0] + out[param] = "" + elif l.strip(): + if out[param]: + out[param] += " " + l.strip() + else: + out[param] = l.strip() return out @@ -77,7 +78,7 @@ def MakeSubparser(subparsers, parents, method, arguments=None): name=name, description=help, help=help.rstrip("."), parents=parents ) subparser.set_defaults(method=method, positional=[]) - argspec = inspect.getargspec(method) + argspec = inspect.getfullargspec(method) offset = len(argspec.args) - len(argspec.defaults or []) - 1 positional = [] for i in range(1, len(argspec.args)): diff --git a/adb/fastboot.py b/adb/fastboot.py index 75b4b36..45988fa 100644 --- a/adb/fastboot.py +++ b/adb/fastboot.py @@ -11,6 +11,7 @@ _LOG = logging.getLogger("fastboot") DEFAULT_MESSAGE_CALLBACK = lambda m: logging.info("Got %s from device", m) + FastbootMessage = collections.namedtuple("FastbootMessage", ["message", "header"]) VENDORS = { @@ -26,8 +27,11 @@ 0x0BB4, 0x8087, } + CLASS = 0xFF + SUBCLASS = 0x42 + PROTOCOL = 0x03 DeviceIsAvailable = common.InterfaceMatcher(CLASS, SUBCLASS, PROTOCOL) @@ -38,8 +42,7 @@ class FastbootRemoteFailure(usb_exceptions.FormatMessageWithArgumentsException): class FastbootStateMismatch(usb_exceptions.FormatMessageWithArgumentsException): -# class FastbootInvalidResponse( - usb_exceptions.FormatMessageWithArgumentsException): +# class FastbootInvalidResponse(usb_exceptions.FormatMessageWithArgumentsException): class FastbootProtocol(object): @@ -76,9 +79,9 @@ def HandleDataSending( accepted_size, = struct.unpack(b">I", accepted_size) if accepted_size != source_len: raise FastbootTransferError( - "Device refused to download %s bytes of data (accepts %s bytes)", - source_len, - accepted_size, + "Device refused to download {0} bytes of data (accepts {1} bytes)".format( + source_len, accepted_size + ) ) self._Write(source_file, accepted_size, progress_callback) return self._AcceptResponses(b"OKAY", info_cb, timeout_ms=timeout_ms) @@ -93,20 +96,21 @@ def _AcceptResponses(self, expected_header, info_cb, timeout_ms=None): elif header in self.FINAL_HEADERS: if header != expected_header: raise FastbootStateMismatch( - "Expected %s, got %s", expected_header, header + "Expected {0}, got {1}".format(expected_header, header) ) if header == b"OKAY": info_cb(FastbootMessage(remaining, header)) return remaining elif header == b"FAIL": info_cb(FastbootMessage(remaining, header)) - raise FastbootRemoteFailure("FAIL: %s", remaining) + raise FastbootRemoteFailure("FAIL: {0}".format(remaining)) else: raise FastbootInvalidResponse( - "Got unknown header %s and response %s", header, remaining + "Got unknown header {0} and response {1}".format(header, remaining) ) - def _HandleProgress(self, total, progress_callback): + @staticmethod + def _HandleProgress(total, progress_callback): current = 0 while True: current += yield @@ -132,12 +136,12 @@ def _Write(self, data, length, progress_callback=None): class FastbootCommands(object): def __init__(self): - self.__reset() - - def __reset(self): self._handle = None self._protocol = None + def __reset(self): + self.__init__() + @property def usb_handle(self): return self._handle diff --git a/adb/fastboot_debug.py b/adb/fastboot_debug.py index 971088a..f25f07d 100644 --- a/adb/fastboot_debug.py +++ b/adb/fastboot_debug.py @@ -12,7 +12,7 @@ progressbar = None -def Devices(args): +def Devices(): for device in fastboot.FastbootCommands.Devices(): print("%s\tdevice" % device.serial_number) return 0 @@ -33,8 +33,7 @@ def main(): type=int, default=1024, metavar="1024", - help="Size of packets to write in Kb. For older devices, it may be " - "required to use 4.", + help="Size of packets to write in Kb. For older devices, it may be required to use 4.", ) parents = [common, device] parser = argparse.ArgumentParser( @@ -64,12 +63,12 @@ def main(): if args.verbose: logging.basicConfig(level=logging.DEBUG) if args.command_name == "devices": - return Devices(args) + return Devices() if args.command_name == "help": parser.print_help() return 0 kwargs = {} - argspec = inspect.getargspec(args.method) + argspec = inspect.getfullargspec(args.method) if "info_cb" in argspec.args: kwargs["info_cb"] = _InfoCb if "progress_callback" in argspec.args and progressbar: diff --git a/adb/filesync_protocol.py b/adb/filesync_protocol.py index cc2c0b4..fd8a029 100644 --- a/adb/filesync_protocol.py +++ b/adb/filesync_protocol.py @@ -1,4 +1,5 @@ import collections +import io import os import stat import struct @@ -9,6 +10,11 @@ from adb import adb_protocol from adb import usb_exceptions +try: + file_types = (file, io.IOBase) +except NameError: + file_types = (io.IOBase,) + DEFAULT_PUSH_MODE = stat.S_IFREG | stat.S_IRWXU | stat.S_IRWXG MAX_PUSH_DATA = 2 * 1024 @@ -94,7 +100,7 @@ def Push( if progress_callback: total_bytes = ( os.fstat(datafile.fileno()).st_size - if isinstance(datafile, file) + if isinstance(datafile, file_types) else -1 ) progress = cls._HandleProgress( @@ -191,7 +197,7 @@ def _Flush(self): try: self.adb.Write(self.send_buffer[: self.send_idx]) except libusb1.USBError as e: - raise adb_protocol.SendFailedError( + raise usb_exceptions.WriteFailedError( "Could not send data %s" % self.send_buffer, e ) self.send_idx = 0 diff --git a/adb/sign_cryptography.py b/adb/sign_cryptography.py index 4b0d540..f476689 100644 --- a/adb/sign_cryptography.py +++ b/adb/sign_cryptography.py @@ -1,11 +1,11 @@ -from adb import adb_protocol - from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import padding from cryptography.hazmat.primitives.asymmetric import utils +from adb import adb_protocol + class CryptographySigner(adb_protocol.AuthSigner): def __init__(self, rsa_key_path): diff --git a/adb/sign_pycryptodome.py b/adb/sign_pycryptodome.py index 05b6374..6db996e 100644 --- a/adb/sign_pycryptodome.py +++ b/adb/sign_pycryptodome.py @@ -1,9 +1,9 @@ -from adb import adb_protocol - from Crypto.Hash import SHA256 from Crypto.PublicKey import RSA from Crypto.Signature import pkcs1_15 +from adb import adb_protocol + class PycryptodomeAuthSigner(adb_protocol.AuthSigner): def __init__(self, rsa_key_path=None): diff --git a/adb/sign_pythonrsa.py b/adb/sign_pythonrsa.py index 8b176da..8fbe605 100644 --- a/adb/sign_pythonrsa.py +++ b/adb/sign_pythonrsa.py @@ -1,9 +1,10 @@ -import rsa - from pyasn1.codec.der import decoder from pyasn1.type import univ +import rsa from rsa import pkcs1 +from adb import adb_protocol + class _Accum(object): def __init__(self): @@ -32,7 +33,11 @@ def _load_rsa_private_key(pem): return rsa.PrivateKey.load_pkcs1(private_key_der, format="DER") -class PythonRSASigner(object): +class PythonRSASigner(adb_protocol.AuthSigner): + def __init__(self, pub=None, priv=None): + self.priv_key = _load_rsa_private_key(priv) + self.pub_key = pub + @classmethod def FromRSAKeyPath(cls, rsa_key_path): with open(rsa_key_path + ".pub") as f: @@ -41,10 +46,6 @@ def FromRSAKeyPath(cls, rsa_key_path): priv = f.read() return cls(pub, priv) - def __init__(self, pub=None, priv=None): - self.priv_key = _load_rsa_private_key(priv) - self.pub_key = pub - def Sign(self, data): return rsa.sign(data, self.priv_key, "SHA-1-PREHASHED") diff --git a/black_remove_comments.sh b/black_remove_comments.sh index 236d1f2..6e6cf59 100755 --- a/black_remove_comments.sh +++ b/black_remove_comments.sh @@ -1,7 +1,7 @@ #!/bin/bash -REMOTE="upstream" -BRANCH="master" +REMOTE="origin" +BRANCH="documentation" function comment_black_failures() From 83eb2602a6e46ffe8b410378fb70235dfec40314 Mon Sep 17 00:00:00 2001 From: Jeff Irion Date: Fri, 18 Oct 2019 19:04:46 -0700 Subject: [PATCH 2/2] Re-run black_remove_comments.sh --- adb/adb_debug.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/adb/adb_debug.py b/adb/adb_debug.py index b43b927..d79b94a 100644 --- a/adb/adb_debug.py +++ b/adb/adb_debug.py @@ -87,16 +87,15 @@ def Shell(device, *command): cmd = input("> ") if not cmd: continue - elif cmd == "exit": + if cmd == "exit": break - else: - stdout = device.InteractiveShell( - cmd, strip_cmd=True, delim=terminal_prompt, strip_delim=True - ) - if stdout: - if isinstance(stdout, bytes): - stdout = stdout.decode("utf-8") - print(stdout) + stdout = device.InteractiveShell( + cmd, strip_cmd=True, delim=terminal_prompt, strip_delim=True + ) + if stdout: + if isinstance(stdout, bytes): + stdout = stdout.decode("utf-8") + print(stdout) device.Close()