Skip to content

Commit 6ba0b17

Browse files
committed
Add functions for checking and installing hwi
1 parent 3733731 commit 6ba0b17

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
@@ -244,6 +244,11 @@ mod tests {
244244
break;
245245
}
246246
}
247+
#[test]
248+
#[serial]
249+
fn test_get_version() {
250+
HWIClient::get_version().unwrap();
251+
}
247252

248253
#[test]
249254
#[serial]
@@ -260,4 +265,10 @@ mod tests {
260265
client.wipe_device().unwrap();
261266
}
262267
}
268+
#[test]
269+
#[serial]
270+
#[ignore]
271+
fn test_install_hwi() {
272+
HWIClient::install_hwilib(Some("2.1.1")).unwrap();
273+
}
263274
}

0 commit comments

Comments
 (0)