Skip to content

Commit 231b9da

Browse files
wszdexdrfWszdexdrf
authored andcommitted
Add functions for checking and installing hwi
1 parent 8285f66 commit 231b9da

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
hwi>=2.1.1,<3
22
protobuf==3.20.1
3+
requests>=2.27.1

src/interface.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::ops::Deref;
2+
use std::process::Command;
23

34
use bitcoin::consensus::encode::serialize;
45
use bitcoin::util::bip32::DerivationPath;
@@ -360,4 +361,38 @@ impl HWIClient {
360361
status.into()
361362
})
362363
}
364+
365+
/// Get installed version of hwilib. Returns None if not found, or if version couldn't be known
366+
pub fn get_version() -> Option<String> {
367+
match || -> Result<String, Error> {
368+
Python::with_gil(|py| {
369+
Ok(PyModule::import(py, "hwilib")?
370+
.getattr("__version__")?
371+
.to_string())
372+
})
373+
}() {
374+
Ok(s) => Some(s),
375+
Err(_) => None,
376+
}
377+
}
378+
379+
/// Install hwi for the current user via pip. If no version is specified, the default version from pip will be installed.
380+
pub fn install_hwilib(version: Option<&str>) -> Result<(), Error> {
381+
let hwi_with_version = match version {
382+
Some(ver) => "hwi==".to_owned() + ver,
383+
None => "hwi".to_owned(),
384+
};
385+
let output = Command::new("pip")
386+
.args(vec!["install", "--user", hwi_with_version.as_str()])
387+
.output()?;
388+
if output.status.success() {
389+
Ok(())
390+
} else {
391+
Err(Error::HWIError(
392+
std::str::from_utf8(&output.stderr)
393+
.expect("Non UTF-8 error while installing")
394+
.to_string(),
395+
))
396+
}
397+
}
363398
}

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ mod tests {
304304
break;
305305
}
306306
}
307+
#[test]
308+
#[serial]
309+
fn test_get_version() {
310+
HWIClient::get_version().unwrap();
311+
}
307312

308313
#[test]
309314
#[serial]
@@ -320,4 +325,10 @@ mod tests {
320325
client.wipe_device().unwrap();
321326
}
322327
}
328+
#[test]
329+
#[serial]
330+
#[ignore]
331+
fn test_install_hwi() {
332+
HWIClient::install_hwilib(Some("2.1.1")).unwrap();
333+
}
323334
}

0 commit comments

Comments
 (0)