Skip to content

Commit 82ccd75

Browse files
committed
feat: use shasum as cache key for venv
Signed-off-by: Nick Mitchell <[email protected]>
1 parent 1d065e5 commit 82ccd75

File tree

6 files changed

+74
-72
lines changed

6 files changed

+74
-72
lines changed

pdl-live-react/src-tauri/Cargo.lock

Lines changed: 26 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdl-live-react/src-tauri/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ serde_json = "1"
2727
tempdir = "0.3.7"
2828
urlencoding = "2.1.3"
2929
tempfile = "3.16.0"
30-
file_diff = "1.0.0"
3130
duct = "0.13.7"
3231
rayon = "1.10.0"
3332
yaml-rust2 = "0.10.0"
3433
futures = "0.3.31"
34+
sha2 = "0.10.8"
35+
base64ct = { version = "1.7.1", features = ["alloc"] }
3536

3637
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
3738
tauri-plugin-cli = "2"

pdl-live-react/src-tauri/src/cli/run.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use duct::cmd;
33
use futures::executor::block_on;
44
use yaml_rust2::yaml::LoadError;
55

6-
use crate::interpreter::pip::pip_install_if_needed;
6+
use crate::interpreter::pip::pip_install_interpreter_if_needed;
77
use crate::interpreter::pull::pull_if_needed;
88

99
#[cfg(desktop)]
@@ -20,7 +20,7 @@ pub fn run_pdl_program(
2020
);
2121

2222
let pull_future = pull_if_needed(&source_file_path);
23-
let bin_path_future = pip_install_if_needed(app_handle);
23+
let bin_path_future = pip_install_interpreter_if_needed(app_handle);
2424

2525
let trace_arg = if let Some(arg) = trace_file {
2626
if let serde_json::Value::String(f) = &arg.value {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod pip;
22
pub mod pull;
3+
pub mod shasum;
Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
use ::std::fs::{copy, create_dir_all};
2-
use ::std::path::PathBuf;
2+
use ::std::path::{Path, PathBuf};
33

44
use duct::cmd;
5-
use file_diff::diff;
65
use tauri::path::BaseDirectory;
76
use tauri::Manager;
87

9-
#[cfg(desktop)]
10-
pub async fn pip_install_if_needed(app_handle: tauri::AppHandle) -> Result<PathBuf, tauri::Error> {
11-
let cache_path = app_handle.path().cache_dir()?.join("pdl");
8+
use crate::interpreter::shasum;
129

10+
#[cfg(desktop)]
11+
pub async fn pip_install_if_needed(
12+
cache_path: &Path,
13+
requirements_path: &Path,
14+
) -> Result<PathBuf, tauri::Error> {
1315
create_dir_all(&cache_path)?;
14-
let venv_path = cache_path.join("interpreter-python");
15-
let activate_path = if cfg!(windows) {
16-
venv_path.join("Scripts").join("Activate.ps1")
17-
} else {
18-
venv_path.join("bin/activate")
19-
};
20-
let cached_requirements_path = venv_path
21-
.join("requirements.txt")
22-
.into_os_string()
23-
.into_string()
24-
.unwrap();
25-
/* println!(
26-
"RUN PATHS activate={:?} cached_reqs={:?}",
27-
activate_path, cached_requirements_path
28-
); */
16+
17+
let hash = shasum::sha256sum(&requirements_path)?;
18+
let venv_path = cache_path.join(hash);
19+
let bin_path = venv_path.join(if cfg!(windows) { "Scripts" } else { "bin" });
2920

3021
if !venv_path.exists() {
3122
println!("Creating virtual environment...");
@@ -35,34 +26,26 @@ pub async fn pip_install_if_needed(app_handle: tauri::AppHandle) -> Result<PathB
3526
"python3"
3627
};
3728
cmd!(python, "-mvenv", &venv_path).run()?;
29+
30+
cmd!(bin_path.join("pip"), "install", "-r", &requirements_path,).run()?;
31+
32+
let cached_requirements_path = venv_path.join("requirements.txt");
33+
copy(requirements_path, cached_requirements_path)?;
3834
}
3935

36+
Ok(bin_path.to_path_buf())
37+
}
38+
39+
#[cfg(desktop)]
40+
pub async fn pip_install_interpreter_if_needed(
41+
app_handle: tauri::AppHandle,
42+
) -> Result<PathBuf, tauri::Error> {
43+
// the interpreter requirements.txt
4044
let requirements_path = app_handle
4145
.path()
42-
.resolve("interpreter/requirements.txt", BaseDirectory::Resource)?
43-
.into_os_string()
44-
.into_string()
45-
.unwrap();
46+
.resolve("interpreter/requirements.txt", BaseDirectory::Resource)?;
4647

47-
if !diff(
48-
requirements_path.as_str(),
49-
cached_requirements_path.as_str(),
50-
) {
51-
cmd!(
52-
venv_path
53-
.join(if cfg!(windows) { "Scripts" } else { "bin" })
54-
.join("pip"),
55-
"install",
56-
"-r",
57-
&requirements_path,
58-
)
59-
.run()?;
60-
61-
copy(requirements_path, cached_requirements_path)?;
62-
}
48+
let cache_path = app_handle.path().cache_dir()?.join("pdl");
6349

64-
match activate_path.parent() {
65-
Some(parent) => Ok(parent.to_path_buf()),
66-
_ => Err(tauri::Error::UnknownPath),
67-
}
50+
pip_install_if_needed(&cache_path, &requirements_path).await
6851
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use ::std::fs::File;
2+
use ::std::io::{copy, Result};
3+
use ::std::path::Path;
4+
5+
use base64ct::{Base64, Encoding};
6+
use sha2::{Digest, Sha256};
7+
8+
pub fn sha256sum(path: &Path) -> Result<String> {
9+
let mut hasher = Sha256::new();
10+
let mut file = File::open(path)?;
11+
12+
copy(&mut file, &mut hasher)?;
13+
let hash_bytes = hasher.finalize();
14+
15+
Ok(Base64::encode_string(&hash_bytes))
16+
}

0 commit comments

Comments
 (0)