Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb04f9064c | ||
|
|
69107e2333 | ||
|
|
17bb77a472 | ||
|
|
f211023b24 | ||
|
|
728d9603dc | ||
|
|
00c387d85c | ||
|
|
79d96d4c76 |
16
.github/workflows/python-build.yml
vendored
16
.github/workflows/python-build.yml
vendored
@@ -7,6 +7,14 @@ on:
|
|||||||
types: [created]
|
types: [created]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
gen-stubs:
|
||||||
|
name: "build-release"
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Build project
|
||||||
|
run: cargo run --bin stub_gen
|
||||||
|
|
||||||
build-python-packages:
|
build-python-packages:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
@@ -14,10 +22,10 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install Python build dependencies
|
- name: Install Python build dependencies
|
||||||
run: pip install ".[build]"
|
run: pip install maturin==1.8.2
|
||||||
|
|
||||||
- name: Build Python packages
|
- name: Build Python packages
|
||||||
run: maturin build --release --compatibility manylinux_2_25 --auditwheel=skip
|
run: maturin build --sdist --release -m pyadb_client/Cargo.toml
|
||||||
|
|
||||||
publish-python-packages:
|
publish-python-packages:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -28,10 +36,10 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Install Python build dependencies
|
- name: Install Python build dependencies
|
||||||
run: pip install ".[build]"
|
run: pip install maturin==1.8.2
|
||||||
|
|
||||||
- name: Publish Python packages
|
- name: Publish Python packages
|
||||||
run: maturin publish --non-interactive --compatibility manylinux_2_25 --auditwheel=skip
|
run: maturin publish -m pyadb_client/Cargo.toml --non-interactive
|
||||||
env:
|
env:
|
||||||
MATURIN_PYPI_TOKEN: ${{ secrets.MATURIN_PYPI_TOKEN }}
|
MATURIN_PYPI_TOKEN: ${{ secrets.MATURIN_PYPI_TOKEN }}
|
||||||
|
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,4 +2,5 @@ target
|
|||||||
/Cargo.lock
|
/Cargo.lock
|
||||||
/.vscode
|
/.vscode
|
||||||
venv
|
venv
|
||||||
/.mypy_cache
|
/.mypy_cache
|
||||||
|
pyadb_client/pyadb_client.pyi
|
||||||
@@ -9,7 +9,7 @@ homepage = "https://github.com/cocool97/adb_client"
|
|||||||
keywords = ["adb", "android", "tcp", "usb"]
|
keywords = ["adb", "android", "tcp", "usb"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
repository = "https://github.com/cocool97/adb_client"
|
repository = "https://github.com/cocool97/adb_client"
|
||||||
version = "2.1.5"
|
version = "2.1.7"
|
||||||
|
|
||||||
# To build locally when working on a new release
|
# To build locally when working on a new release
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ impl Drop for ADBTermios {
|
|||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
// Custom drop implementation, restores previous termios structure.
|
// Custom drop implementation, restores previous termios structure.
|
||||||
if let Err(e) = tcsetattr(self.fd, TCSANOW, &self.old_termios) {
|
if let Err(e) = tcsetattr(self.fd, TCSANOW, &self.old_termios) {
|
||||||
log::error!("Error while droping ADBTermios: {e}")
|
log::error!("Error while dropping ADBTermios: {e}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,6 +133,10 @@ fn main() -> Result<()> {
|
|||||||
log::info!("Starting installation of APK {}...", path.display());
|
log::info!("Starting installation of APK {}...", path.display());
|
||||||
device.install(&path)?;
|
device.install(&path)?;
|
||||||
}
|
}
|
||||||
|
DeviceCommands::Uninstall { package } => {
|
||||||
|
log::info!("Uninstalling the package {}...", package);
|
||||||
|
device.uninstall(&package)?;
|
||||||
|
}
|
||||||
DeviceCommands::Framebuffer { path } => {
|
DeviceCommands::Framebuffer { path } => {
|
||||||
device.framebuffer(&path)?;
|
device.framebuffer(&path)?;
|
||||||
log::info!("Successfully dumped framebuffer at path {path}");
|
log::info!("Successfully dumped framebuffer at path {path}");
|
||||||
|
|||||||
@@ -33,6 +33,11 @@ pub enum DeviceCommands {
|
|||||||
/// Path to APK file. Extension must be ".apk"
|
/// Path to APK file. Extension must be ".apk"
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
},
|
},
|
||||||
|
/// Uninstall a package from the device
|
||||||
|
Uninstall {
|
||||||
|
/// Name of the package to uninstall
|
||||||
|
package: String,
|
||||||
|
},
|
||||||
/// Dump framebuffer of device
|
/// Dump framebuffer of device
|
||||||
Framebuffer {
|
Framebuffer {
|
||||||
/// Framebuffer image destination path
|
/// Framebuffer image destination path
|
||||||
|
|||||||
@@ -18,17 +18,17 @@ homedir = { version = "0.3.4" }
|
|||||||
image = { version = "0.25.5" }
|
image = { version = "0.25.5" }
|
||||||
lazy_static = { version = "1.5.0" }
|
lazy_static = { version = "1.5.0" }
|
||||||
log = { version = "0.4.22" }
|
log = { version = "0.4.22" }
|
||||||
mdns-sd = { version = "0.13.1" }
|
mdns-sd = { version = "0.13.2" }
|
||||||
num-bigint = { version = "0.8.4", package = "num-bigint-dig" }
|
num-bigint = { version = "0.8.4", package = "num-bigint-dig" }
|
||||||
num-traits = { version = "0.2.19" }
|
num-traits = { version = "0.2.19" }
|
||||||
quick-protobuf = { version = "0.8.1" }
|
quick-protobuf = { version = "0.8.1" }
|
||||||
rand = { version = "0.8.5" }
|
rand = { version = "0.9.0" }
|
||||||
rcgen = { version = "0.13.1" }
|
rcgen = { version = "0.13.1" }
|
||||||
regex = { version = "1.11.1", features = ["perf", "std", "unicode"] }
|
regex = { version = "1.11.1", features = ["perf", "std", "unicode"] }
|
||||||
rsa = { version = "0.9.7" }
|
rsa = { version = "0.9.7" }
|
||||||
rusb = { version = "0.9.4", features = ["vendored"] }
|
rusb = { version = "0.9.4", features = ["vendored"] }
|
||||||
rustls = { version = "0.23.18" }
|
rustls = { version = "0.23.22" }
|
||||||
rustls-pki-types = "1.10.0"
|
rustls-pki-types = "1.11.0"
|
||||||
serde = { version = "1.0.216", features = ["derive"] }
|
serde = { version = "1.0.216", features = ["derive"] }
|
||||||
serde_repr = { version = "0.1.19" }
|
serde_repr = { version = "0.1.19" }
|
||||||
sha1 = { version = "0.10.6", features = ["oid"] }
|
sha1 = { version = "0.10.6", features = ["oid"] }
|
||||||
|
|||||||
@@ -41,6 +41,9 @@ pub trait ADBDeviceExt {
|
|||||||
/// Install an APK pointed to by `apk_path` on device.
|
/// Install an APK pointed to by `apk_path` on device.
|
||||||
fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()>;
|
fn install(&mut self, apk_path: &dyn AsRef<Path>) -> Result<()>;
|
||||||
|
|
||||||
|
/// Uninstall the package `package` from device.
|
||||||
|
fn uninstall(&mut self, package: &str) -> Result<()>;
|
||||||
|
|
||||||
/// Inner method requesting framebuffer from an Android device
|
/// Inner method requesting framebuffer from an Android device
|
||||||
fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>>;
|
fn framebuffer_inner(&mut self) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>>;
|
||||||
|
|
||||||
|
|||||||
@@ -213,11 +213,11 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn open_session(&mut self, data: &[u8]) -> Result<ADBTransportMessage> {
|
pub(crate) fn open_session(&mut self, data: &[u8]) -> Result<ADBTransportMessage> {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::rng();
|
||||||
|
|
||||||
let message = ADBTransportMessage::new(
|
let message = ADBTransportMessage::new(
|
||||||
MessageCommand::Open,
|
MessageCommand::Open,
|
||||||
rng.gen(), // Our 'local-id'
|
rng.random(), // Our 'local-id'
|
||||||
0,
|
0,
|
||||||
data,
|
data,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -35,6 +35,10 @@ impl<T: ADBMessageTransport> ADBDeviceExt for ADBMessageDevice<T> {
|
|||||||
self.install(apk_path)
|
self.install(apk_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn uninstall(&mut self, package: &str) -> Result<()> {
|
||||||
|
self.uninstall(package)
|
||||||
|
}
|
||||||
|
|
||||||
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
|
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
|
||||||
self.framebuffer_inner()
|
self.framebuffer_inner()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,6 +96,11 @@ impl ADBDeviceExt for ADBTcpDevice {
|
|||||||
self.inner.install(apk_path)
|
self.inner.install(apk_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn uninstall(&mut self, package: &str) -> Result<()> {
|
||||||
|
self.inner.uninstall(package)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
|
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
|
||||||
self.inner.framebuffer_inner()
|
self.inner.framebuffer_inner()
|
||||||
|
|||||||
@@ -273,6 +273,11 @@ impl ADBDeviceExt for ADBUSBDevice {
|
|||||||
self.inner.install(apk_path)
|
self.inner.install(apk_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn uninstall(&mut self, package: &str) -> Result<()> {
|
||||||
|
self.inner.uninstall(package)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
|
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
|
||||||
self.inner.framebuffer_inner()
|
self.inner.framebuffer_inner()
|
||||||
|
|||||||
@@ -16,9 +16,9 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
|||||||
|
|
||||||
let file_size = apk_file.metadata()?.len();
|
let file_size = apk_file.metadata()?.len();
|
||||||
|
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::rng();
|
||||||
|
|
||||||
let local_id = rng.gen();
|
let local_id = rng.random();
|
||||||
|
|
||||||
self.open_session(format!("exec:cmd package 'install' -S {}\0", file_size).as_bytes())?;
|
self.open_session(format!("exec:cmd package 'install' -S {}\0", file_size).as_bytes())?;
|
||||||
|
|
||||||
|
|||||||
@@ -5,3 +5,4 @@ mod push;
|
|||||||
mod reboot;
|
mod reboot;
|
||||||
mod shell;
|
mod shell;
|
||||||
mod stat;
|
mod stat;
|
||||||
|
mod uninstall;
|
||||||
|
|||||||
19
adb_client/src/device/commands/uninstall.rs
Normal file
19
adb_client/src/device/commands/uninstall.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
use crate::{device::adb_message_device::ADBMessageDevice, ADBMessageTransport, Result};
|
||||||
|
|
||||||
|
impl<T: ADBMessageTransport> ADBMessageDevice<T> {
|
||||||
|
pub(crate) fn uninstall(&mut self, package_name: &str) -> Result<()> {
|
||||||
|
self.open_session(format!("exec:cmd package 'uninstall' {}\0", package_name).as_bytes())?;
|
||||||
|
|
||||||
|
let final_status = self.get_transport_mut().read_message()?;
|
||||||
|
|
||||||
|
match final_status.into_payload().as_slice() {
|
||||||
|
b"Success\n" => {
|
||||||
|
log::info!("Package {} successfully uninstalled", package_name);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8(
|
||||||
|
d.to_vec(),
|
||||||
|
)?)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ use base64::{engine::general_purpose::STANDARD, Engine};
|
|||||||
use num_bigint::{BigUint, ModInverse};
|
use num_bigint::{BigUint, ModInverse};
|
||||||
use num_traits::cast::ToPrimitive;
|
use num_traits::cast::ToPrimitive;
|
||||||
use num_traits::FromPrimitive;
|
use num_traits::FromPrimitive;
|
||||||
use rand::rngs::OsRng;
|
|
||||||
use rsa::pkcs8::DecodePrivateKey;
|
use rsa::pkcs8::DecodePrivateKey;
|
||||||
use rsa::traits::PublicKeyParts;
|
use rsa::traits::PublicKeyParts;
|
||||||
use rsa::{Pkcs1v15Sign, RsaPrivateKey};
|
use rsa::{Pkcs1v15Sign, RsaPrivateKey};
|
||||||
@@ -52,7 +51,7 @@ pub struct ADBRsaKey {
|
|||||||
impl ADBRsaKey {
|
impl ADBRsaKey {
|
||||||
pub fn new_random() -> Result<Self> {
|
pub fn new_random() -> Result<Self> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
private_key: RsaPrivateKey::new(&mut OsRng, ADB_PRIVATE_KEY_SIZE)?,
|
private_key: RsaPrivateKey::new(&mut rsa::rand_core::OsRng, ADB_PRIVATE_KEY_SIZE)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ pub(crate) enum AdbServerCommand {
|
|||||||
MDNSServices,
|
MDNSServices,
|
||||||
ServerStatus,
|
ServerStatus,
|
||||||
ReconnectOffline,
|
ReconnectOffline,
|
||||||
|
Uninstall(String),
|
||||||
Install(u64),
|
Install(u64),
|
||||||
// Local commands
|
// Local commands
|
||||||
ShellCommand(String),
|
ShellCommand(String),
|
||||||
@@ -83,6 +84,9 @@ impl Display for AdbServerCommand {
|
|||||||
}
|
}
|
||||||
AdbServerCommand::Usb => write!(f, "usb:"),
|
AdbServerCommand::Usb => write!(f, "usb:"),
|
||||||
AdbServerCommand::Install(size) => write!(f, "exec:cmd package 'install' -S {size}"),
|
AdbServerCommand::Install(size) => write!(f, "exec:cmd package 'install' -S {size}"),
|
||||||
|
AdbServerCommand::Uninstall(package) => {
|
||||||
|
write!(f, "exec:cmd package 'uninstall' {package}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -115,6 +115,10 @@ impl ADBDeviceExt for ADBServerDevice {
|
|||||||
self.install(apk_path)
|
self.install(apk_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn uninstall(&mut self, package: &str) -> Result<()> {
|
||||||
|
self.uninstall(package)
|
||||||
|
}
|
||||||
|
|
||||||
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
|
fn framebuffer_inner(&mut self) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
|
||||||
self.framebuffer_inner()
|
self.framebuffer_inner()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,4 +12,5 @@ mod send;
|
|||||||
mod stat;
|
mod stat;
|
||||||
mod tcpip;
|
mod tcpip;
|
||||||
mod transport;
|
mod transport;
|
||||||
|
mod uninstall;
|
||||||
mod usb;
|
mod usb;
|
||||||
|
|||||||
28
adb_client/src/server_device/commands/uninstall.rs
Normal file
28
adb_client/src/server_device/commands/uninstall.rs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
use crate::{models::AdbServerCommand, server_device::ADBServerDevice, Result};
|
||||||
|
|
||||||
|
impl ADBServerDevice {
|
||||||
|
/// Uninstall a package from device
|
||||||
|
pub fn uninstall(&mut self, package_name: &str) -> Result<()> {
|
||||||
|
let serial: String = self.identifier.clone();
|
||||||
|
self.connect()?
|
||||||
|
.send_adb_request(AdbServerCommand::TransportSerial(serial))?;
|
||||||
|
|
||||||
|
self.transport
|
||||||
|
.send_adb_request(AdbServerCommand::Uninstall(package_name.to_string()))?;
|
||||||
|
|
||||||
|
let mut data = [0; 1024];
|
||||||
|
let read_amount = self.transport.get_raw_connection()?.read(&mut data)?;
|
||||||
|
|
||||||
|
match &data[0..read_amount] {
|
||||||
|
b"Success\n" => {
|
||||||
|
log::info!("Package {} successfully uninstalled", package_name);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
d => Err(crate::RustADBError::ADBRequestFailed(String::from_utf8(
|
||||||
|
d.to_vec(),
|
||||||
|
)?)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
use adb_client::ADBServer;
|
use adb_client::ADBServer;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
|
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{rng, Rng};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
@@ -14,7 +14,7 @@ const REMOTE_TEST_FILE_PATH: &str = "/data/local/tmp/test_file.bin";
|
|||||||
fn generate_test_file(size_in_bytes: usize) -> Result<()> {
|
fn generate_test_file(size_in_bytes: usize) -> Result<()> {
|
||||||
let mut test_file = File::create(LOCAL_TEST_FILE_PATH)?;
|
let mut test_file = File::create(LOCAL_TEST_FILE_PATH)?;
|
||||||
|
|
||||||
let mut rng = thread_rng();
|
let mut rng = rng();
|
||||||
|
|
||||||
const BUFFER_SIZE: usize = 64 * 1024;
|
const BUFFER_SIZE: usize = 64 * 1024;
|
||||||
let mut buffer = [0u8; BUFFER_SIZE];
|
let mut buffer = [0u8; BUFFER_SIZE];
|
||||||
|
|||||||
@@ -1,20 +1,30 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "pyadb_client"
|
|
||||||
description = "Python wrapper for adb_client library"
|
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
|
description = "Python wrapper for adb_client library"
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
homepage.workspace = true
|
homepage.workspace = true
|
||||||
keywords.workspace = true
|
keywords.workspace = true
|
||||||
license.workspace = true
|
license.workspace = true
|
||||||
|
name = "pyadb_client"
|
||||||
|
readme = "README.md"
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
version.workspace = true
|
version.workspace = true
|
||||||
readme = "README.md"
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
crate-type = ["cdylib", "rlib"]
|
||||||
name = "pyadb_client"
|
name = "pyadb_client"
|
||||||
crate-type = ["cdylib"]
|
|
||||||
|
[[bin]]
|
||||||
|
doc = false
|
||||||
|
name = "stub_gen"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = { version = "1.0.94" }
|
adb_client = { path = "../adb_client" }
|
||||||
adb_client = { version = "2.0.6" }
|
anyhow = { version = "1.0.95" }
|
||||||
pyo3 = { version = "0.23.4", features = ["extension-module", "anyhow", "abi3-py37"] }
|
pyo3 = { version = "0.23.4", features = [
|
||||||
|
"abi3-py37",
|
||||||
|
"anyhow",
|
||||||
|
"extension-module",
|
||||||
|
] }
|
||||||
|
pyo3-stub-gen = "0.7.0"
|
||||||
|
pyo3-stub-gen-derive = "0.7.0"
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ pip install pyadb_client
|
|||||||
### Use ADB server
|
### Use ADB server
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from pyadb_client import pyadb_client
|
from pyadb_client import PyADBServer
|
||||||
|
|
||||||
server = pyadb_client.PyADBServer("127.0.0.1:5037")
|
server = PyADBServer("127.0.0.1:5037")
|
||||||
for i, device in enumerate(server.devices()):
|
for i, device in enumerate(server.devices()):
|
||||||
print(i, device.identifier, device.state)
|
print(i, device.identifier, device.state)
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ print(device, device.identifier)
|
|||||||
### Push a file on device
|
### Push a file on device
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from pyadb_client import pyadb_client
|
from pyadb_client import PyADBUSBDevice
|
||||||
|
|
||||||
usb_device = PyADBUSBDevice.autodetect()
|
usb_device = PyADBUSBDevice.autodetect()
|
||||||
usb_device.push("file.txt", "/data/local/tmp/file.txt")
|
usb_device.push("file.txt", "/data/local/tmp/file.txt")
|
||||||
@@ -42,11 +42,14 @@ python3 -m venv .venv
|
|||||||
source .venv/bin/activate
|
source .venv/bin/activate
|
||||||
|
|
||||||
# Install needed build dependencies
|
# Install needed build dependencies
|
||||||
pip install ".[build]"
|
pip install maturin
|
||||||
|
|
||||||
# Build development package
|
# Build development package
|
||||||
maturin develop
|
maturin develop
|
||||||
|
|
||||||
|
# Build stub file (.pyi)
|
||||||
|
cargo run --bin stub_gen
|
||||||
|
|
||||||
# Build release Python package
|
# Build release Python package
|
||||||
maturin build --release
|
maturin build --release -m pyadb_client/Cargo.toml
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -8,9 +8,5 @@ dynamic = ["authors", "keywords", "version"]
|
|||||||
name = "pyadb_client"
|
name = "pyadb_client"
|
||||||
requires-python = ">= 3.7"
|
requires-python = ">= 3.7"
|
||||||
|
|
||||||
[project.optional-dependencies]
|
|
||||||
build = ["maturin", "patchelf"]
|
|
||||||
|
|
||||||
[tool.maturin]
|
[tool.maturin]
|
||||||
include = [{ path = "adb_client/**/*", format = "sdist" }]
|
features = ["pyo3/extension-module"]
|
||||||
manifest-path = "pyadb_client/Cargo.toml"
|
|
||||||
@@ -3,28 +3,36 @@ use std::net::SocketAddrV4;
|
|||||||
use adb_client::ADBServer;
|
use adb_client::ADBServer;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use pyo3::{pyclass, pymethods, PyResult};
|
use pyo3::{pyclass, pymethods, PyResult};
|
||||||
|
use pyo3_stub_gen_derive::{gen_stub_pyclass, gen_stub_pymethods};
|
||||||
|
|
||||||
use crate::{PyADBServerDevice, PyDeviceShort};
|
use crate::{PyADBServerDevice, PyDeviceShort};
|
||||||
|
|
||||||
|
#[gen_stub_pyclass]
|
||||||
#[pyclass]
|
#[pyclass]
|
||||||
|
/// Represent an instance of an ADB Server
|
||||||
pub struct PyADBServer(ADBServer);
|
pub struct PyADBServer(ADBServer);
|
||||||
|
|
||||||
|
#[gen_stub_pymethods]
|
||||||
#[pymethods]
|
#[pymethods]
|
||||||
impl PyADBServer {
|
impl PyADBServer {
|
||||||
#[new]
|
#[new]
|
||||||
|
/// Instantiate a new PyADBServer instance
|
||||||
pub fn new(address: String) -> PyResult<Self> {
|
pub fn new(address: String) -> PyResult<Self> {
|
||||||
let address = address.parse::<SocketAddrV4>()?;
|
let address = address.parse::<SocketAddrV4>()?;
|
||||||
Ok(ADBServer::new(address).into())
|
Ok(ADBServer::new(address).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// List available devices
|
||||||
pub fn devices(&mut self) -> Result<Vec<PyDeviceShort>> {
|
pub fn devices(&mut self) -> Result<Vec<PyDeviceShort>> {
|
||||||
Ok(self.0.devices()?.into_iter().map(|v| v.into()).collect())
|
Ok(self.0.devices()?.into_iter().map(|v| v.into()).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a device, assuming that only one is currently connected
|
||||||
pub fn get_device(&mut self) -> Result<PyADBServerDevice> {
|
pub fn get_device(&mut self) -> Result<PyADBServerDevice> {
|
||||||
Ok(self.0.get_device()?.into())
|
Ok(self.0.get_device()?.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a device by its name, as shown in `.devices()` output
|
||||||
pub fn get_device_by_name(&mut self, name: String) -> Result<PyADBServerDevice> {
|
pub fn get_device_by_name(&mut self, name: String) -> Result<PyADBServerDevice> {
|
||||||
Ok(self.0.get_device_by_name(&name)?.into())
|
Ok(self.0.get_device_by_name(&name)?.into())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,24 @@
|
|||||||
use adb_client::{ADBDeviceExt, ADBServerDevice};
|
use adb_client::{ADBDeviceExt, ADBServerDevice};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use pyo3::{pyclass, pymethods};
|
use pyo3::{pyclass, pymethods};
|
||||||
|
use pyo3_stub_gen_derive::{gen_stub_pyclass, gen_stub_pymethods};
|
||||||
use std::{fs::File, path::PathBuf};
|
use std::{fs::File, path::PathBuf};
|
||||||
|
|
||||||
|
#[gen_stub_pyclass]
|
||||||
#[pyclass]
|
#[pyclass]
|
||||||
|
/// Represent a device connected to the ADB server
|
||||||
pub struct PyADBServerDevice(pub ADBServerDevice);
|
pub struct PyADBServerDevice(pub ADBServerDevice);
|
||||||
|
|
||||||
|
#[gen_stub_pymethods]
|
||||||
#[pymethods]
|
#[pymethods]
|
||||||
impl PyADBServerDevice {
|
impl PyADBServerDevice {
|
||||||
#[getter]
|
#[getter]
|
||||||
|
/// Device identifier
|
||||||
pub fn identifier(&self) -> String {
|
pub fn identifier(&self) -> String {
|
||||||
self.0.identifier.clone()
|
self.0.identifier.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Run shell commands on device and return the output (stdout + stderr merged)
|
||||||
pub fn shell_command(&mut self, commands: Vec<String>) -> Result<Vec<u8>> {
|
pub fn shell_command(&mut self, commands: Vec<String>) -> Result<Vec<u8>> {
|
||||||
let mut output = Vec::new();
|
let mut output = Vec::new();
|
||||||
let commands: Vec<&str> = commands.iter().map(|x| &**x).collect();
|
let commands: Vec<&str> = commands.iter().map(|x| &**x).collect();
|
||||||
@@ -20,15 +26,27 @@ impl PyADBServerDevice {
|
|||||||
Ok(output)
|
Ok(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Push a local file from input to dest
|
||||||
pub fn push(&mut self, input: PathBuf, dest: PathBuf) -> Result<()> {
|
pub fn push(&mut self, input: PathBuf, dest: PathBuf) -> Result<()> {
|
||||||
let mut reader = File::open(input)?;
|
let mut reader = File::open(input)?;
|
||||||
Ok(self.0.push(&mut reader, dest.to_string_lossy())?)
|
Ok(self.0.push(&mut reader, dest.to_string_lossy())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Pull a file from device located at input, and drop it to dest
|
||||||
pub fn pull(&mut self, input: PathBuf, dest: PathBuf) -> Result<()> {
|
pub fn pull(&mut self, input: PathBuf, dest: PathBuf) -> Result<()> {
|
||||||
let mut writer = File::create(dest)?;
|
let mut writer = File::create(dest)?;
|
||||||
Ok(self.0.pull(&input.to_string_lossy(), &mut writer)?)
|
Ok(self.0.pull(&input.to_string_lossy(), &mut writer)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Install a package installed on the device
|
||||||
|
pub fn install(&mut self, apk_path: PathBuf) -> Result<()> {
|
||||||
|
Ok(self.0.install(&apk_path)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Uninstall a package installed on the device
|
||||||
|
pub fn uninstall(&mut self, package: &str) -> Result<()> {
|
||||||
|
Ok(self.0.uninstall(package)?)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ADBServerDevice> for PyADBServerDevice {
|
impl From<ADBServerDevice> for PyADBServerDevice {
|
||||||
|
|||||||
@@ -3,17 +3,24 @@ use std::{fs::File, path::PathBuf};
|
|||||||
use adb_client::{ADBDeviceExt, ADBUSBDevice};
|
use adb_client::{ADBDeviceExt, ADBUSBDevice};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use pyo3::{pyclass, pymethods};
|
use pyo3::{pyclass, pymethods};
|
||||||
|
use pyo3_stub_gen_derive::{gen_stub_pyclass, gen_stub_pymethods};
|
||||||
|
|
||||||
|
#[gen_stub_pyclass]
|
||||||
#[pyclass]
|
#[pyclass]
|
||||||
|
/// Represent a device directly reachable over USB.
|
||||||
pub struct PyADBUSBDevice(ADBUSBDevice);
|
pub struct PyADBUSBDevice(ADBUSBDevice);
|
||||||
|
|
||||||
|
#[gen_stub_pymethods]
|
||||||
#[pymethods]
|
#[pymethods]
|
||||||
impl PyADBUSBDevice {
|
impl PyADBUSBDevice {
|
||||||
#[staticmethod]
|
#[staticmethod]
|
||||||
|
/// Autodetect a device reachable over USB.
|
||||||
|
/// This method raises an error if multiple devices or none are connected.
|
||||||
pub fn autodetect() -> Result<Self> {
|
pub fn autodetect() -> Result<Self> {
|
||||||
Ok(ADBUSBDevice::autodetect()?.into())
|
Ok(ADBUSBDevice::autodetect()?.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Run shell commands on device and return the output (stdout + stderr merged)
|
||||||
pub fn shell_command(&mut self, commands: Vec<String>) -> Result<Vec<u8>> {
|
pub fn shell_command(&mut self, commands: Vec<String>) -> Result<Vec<u8>> {
|
||||||
let mut output = Vec::new();
|
let mut output = Vec::new();
|
||||||
let commands: Vec<&str> = commands.iter().map(|x| &**x).collect();
|
let commands: Vec<&str> = commands.iter().map(|x| &**x).collect();
|
||||||
@@ -21,15 +28,27 @@ impl PyADBUSBDevice {
|
|||||||
Ok(output)
|
Ok(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Push a local file from input to dest
|
||||||
pub fn push(&mut self, input: PathBuf, dest: PathBuf) -> Result<()> {
|
pub fn push(&mut self, input: PathBuf, dest: PathBuf) -> Result<()> {
|
||||||
let mut reader = File::open(input)?;
|
let mut reader = File::open(input)?;
|
||||||
Ok(self.0.push(&mut reader, &dest.to_string_lossy())?)
|
Ok(self.0.push(&mut reader, &dest.to_string_lossy())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Pull a file from device located at input, and drop it to dest
|
||||||
pub fn pull(&mut self, input: PathBuf, dest: PathBuf) -> Result<()> {
|
pub fn pull(&mut self, input: PathBuf, dest: PathBuf) -> Result<()> {
|
||||||
let mut writer = File::create(dest)?;
|
let mut writer = File::create(dest)?;
|
||||||
Ok(self.0.pull(&input.to_string_lossy(), &mut writer)?)
|
Ok(self.0.pull(&input.to_string_lossy(), &mut writer)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Install a package installed on the device
|
||||||
|
pub fn install(&mut self, apk_path: PathBuf) -> Result<()> {
|
||||||
|
Ok(self.0.install(&apk_path)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Uninstall a package installed on the device
|
||||||
|
pub fn uninstall(&mut self, package: &str) -> Result<()> {
|
||||||
|
Ok(self.0.uninstall(package)?)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ADBUSBDevice> for PyADBUSBDevice {
|
impl From<ADBUSBDevice> for PyADBUSBDevice {
|
||||||
|
|||||||
5
pyadb_client/src/bin/stub_gen.rs
Normal file
5
pyadb_client/src/bin/stub_gen.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
use pyo3_stub_gen::Result;
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
pyadb_client::stub_info()?.generate()
|
||||||
|
}
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
#![forbid(missing_docs)]
|
||||||
|
#![doc = include_str!("../README.md")]
|
||||||
|
|
||||||
mod adb_server;
|
mod adb_server;
|
||||||
mod adb_server_device;
|
mod adb_server_device;
|
||||||
mod adb_usb_device;
|
mod adb_usb_device;
|
||||||
@@ -8,6 +11,7 @@ pub use adb_usb_device::*;
|
|||||||
pub use models::*;
|
pub use models::*;
|
||||||
|
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
|
use pyo3_stub_gen::StubInfo;
|
||||||
|
|
||||||
#[pymodule]
|
#[pymodule]
|
||||||
fn pyadb_client(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
fn pyadb_client(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||||
@@ -18,3 +22,9 @@ fn pyadb_client(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get stub informations for this package.
|
||||||
|
pub fn stub_info() -> anyhow::Result<StubInfo> {
|
||||||
|
// Need to be run from workspace root directory
|
||||||
|
StubInfo::from_pyproject_toml(format!("{}/pyproject.toml", env!("CARGO_MANIFEST_DIR")))
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,19 +1,25 @@
|
|||||||
use adb_client::DeviceShort;
|
use adb_client::DeviceShort;
|
||||||
use pyo3::{pyclass, pymethods};
|
use pyo3::{pyclass, pymethods};
|
||||||
|
use pyo3_stub_gen_derive::{gen_stub_pyclass, gen_stub_pymethods};
|
||||||
|
|
||||||
// Check https://docs.rs/rigetti-pyo3/latest/rigetti_pyo3 to automatically build this code
|
// Check https://docs.rs/rigetti-pyo3/latest/rigetti_pyo3 to automatically build this code
|
||||||
|
|
||||||
|
#[gen_stub_pyclass]
|
||||||
#[pyclass]
|
#[pyclass]
|
||||||
|
/// Represent a device output as shown when running `adb devices`
|
||||||
pub struct PyDeviceShort(DeviceShort);
|
pub struct PyDeviceShort(DeviceShort);
|
||||||
|
|
||||||
|
#[gen_stub_pymethods]
|
||||||
#[pymethods]
|
#[pymethods]
|
||||||
impl PyDeviceShort {
|
impl PyDeviceShort {
|
||||||
#[getter]
|
#[getter]
|
||||||
|
/// Device identifier
|
||||||
pub fn identifier(&self) -> String {
|
pub fn identifier(&self) -> String {
|
||||||
self.0.identifier.clone()
|
self.0.identifier.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[getter]
|
#[getter]
|
||||||
|
/// Device state
|
||||||
pub fn state(&self) -> String {
|
pub fn state(&self) -> String {
|
||||||
self.0.state.to_string()
|
self.0.state.to_string()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user