How google/python-adb works
Main Components
a. ADB and Fastboot Core Modules:
• The “adb” directory holds the core implementation. It includes modules for:
– Command handling (adb_commands.py)
– Debugging functions (adb_debug.py, fastboot_debug.py)
– Protocol implementations (adb_protocol.py, fastboot.py, filesync_protocol.py)
– Cryptographic signing (sign_cryptography.py, sign_pycryptodome.py, sign_pythonrsa.py)
– Common utilities (common.py, common_cli.py) and USB-specific exception handling (usb_exceptions.py)
b. CLI Tools Build:
• The “make_tools.py” script packages the core modules into executables (adb.zip and fastboot.zip), which then provide the CLI tools (pyadb and pyfastboot). This layer is critical to show how users interact with the system at a command-line level.
c. Testing Suite:
• The “test” directory contains tests (e.g., adb_test.py, fastboot_test.py, common_stub.py) that validate the functionality of the modules.
d. Project Packaging and CI:
• Files like setup.py, .travis.yml, and tox.ini indicate build configuration, dependency management, and continuous integration settings.
Relationships and Interactions between Components
a. User Interface/CLI Layer:
• The executable zips (pyadb and pyfastboot) serve as entry points. They invoke the underlying functions and classes in the adb modules.
b. Library/API Layer:
• For developers, the library exposes functionality (via classes like AdbCommands) that encapsulate USB communications, command execution, and device interactions.
c. Cryptography Layer:
• The signing modules (sign_cryptography, etc.) integrate with the API’s authentication requirements (especially for devices that need verified connections).
d. USB Communication:
• The core modules interface with external dependencies (libusb1 and python-libusb1) to establish and maintain USB communications with Android devices.
e. Testing & CI:
• The test directory ensures stability and expected behavior; CI files handle automated testing and integration.
Architectural Patterns and Design Principles
a. Modularity: The code cleanly separates protocol-specific logic (ADB vs. Fastboot), cryptography, and common utilities.
b. Layering: There is a clear distinction between the CLI interface, underlying library/API, and the hardware communication layer.
c. Reusability: The library is designed to be imported and used in other Python code (see usage examples), demonstrating good API design practices.
d. Direct Communication: Unlike traditional ADB daemons, this project opens a direct command channel to the device, which is a unique design decision that should be noted.
Relevant Technologies, Frameworks, and Libraries
• Python (both Python 2 and Python 3 compatibility as noted in the README)
• libusb1 (and python-libusb1 for USB communications)
• Cryptographic libraries (py-cryptography, python-rsa, and optionally M2Crypto)
• CI tools (Travis CI, as deduced from .travis.yml) and test frameworks for automated testing.
Summary
• The hierarchy from the CLI interface to the core library, then down to hardware-level communication.
• The modular separation of ADB and Fastboot functionalities.
• External interactions (with USB libraries, cryptographic libraries, and Android devices).
• How testing and CI support the development lifecycle.