Skip to content

Commit 704964c

Browse files
committed
Uv install python into tools path
1 parent 75e7f4c commit 704964c

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

src/env_vars.rs

+3
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ impl EnvVars {
99
pub const PRE_COMMIT_ALLOW_NO_CONFIG: &'static str = "PRE_COMMIT_ALLOW_NO_CONFIG";
1010
pub const PRE_COMMIT_NO_CONCURRENCY: &'static str = "PRE_COMMIT_NO_CONCURRENCY";
1111
pub const _PRE_COMMIT_SKIP_POST_CHECKOUT: &'static str = "_PRE_COMMIT_SKIP_POST_CHECKOUT";
12+
13+
pub const UV_NO_CACHE: &'static str = "UV_NO_CACHE";
14+
pub const UV_PYTHON_INSTALL_DIR: &'static str = "UV_PYTHON_INSTALL_DIR";
1215
}

src/languages/python/impl.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
use std::collections::HashMap;
2+
use std::path::{Path, PathBuf};
3+
use std::sync::Arc;
4+
15
use crate::config::LanguageVersion;
26
use crate::env_vars::EnvVars;
37
use crate::hook::Hook;
48
use crate::languages::python::uv::UvInstaller;
59
use crate::languages::LanguageImpl;
610
use crate::process::Cmd;
711
use crate::run::run_by_batch;
8-
use std::collections::HashMap;
9-
use std::path::{Path, PathBuf};
10-
use std::sync::Arc;
12+
use crate::store::{Store, ToolBucket};
1113

1214
#[derive(Debug, Copy, Clone)]
1315
pub struct Python;
@@ -23,17 +25,21 @@ impl LanguageImpl for Python {
2325

2426
let uv = UvInstaller::install().await?;
2527

28+
let store = Store::from_settings()?;
29+
let python_install_dir = store.tools_path(ToolBucket::Python);
30+
2631
let uv_cmd = |summary| {
2732
#[allow(unused_mut)]
2833
let mut cmd = Cmd::new(&uv, summary);
2934
// Don't use cache in Windows, multiple uv instances will conflict with each other.
3035
// See https://github.com/astral-sh/uv/issues/8664
3136
#[cfg(windows)]
32-
cmd.env("UV_NO_CACHE", "1");
37+
cmd.env(EnvVars::UV_NO_CACHE, "1");
38+
39+
cmd.env(EnvVars::UV_PYTHON_INSTALL_DIR, &python_install_dir);
3340
cmd
3441
};
3542

36-
// TODO: Set uv cache dir? tools dir? python dir?
3743
// Create venv
3844
let mut cmd = uv_cmd("create venv");
3945
cmd.arg("venv").arg(&venv);

src/languages/python/uv.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tracing::{debug, enabled, trace, warn};
99

1010
use crate::fs::LockedFile;
1111
use crate::process::Cmd;
12-
use crate::store::Store;
12+
use crate::store::{Store, ToolBucket};
1313

1414
// The version of `uv` to install. Should update periodically.
1515
const UV_VERSION: &str = "0.5.8";
@@ -207,7 +207,7 @@ impl UvInstaller {
207207
// 2) Check if `uv` is installed by `prefligit`
208208
let store = Store::from_settings()?;
209209

210-
let uv_dir = store.uv_path();
210+
let uv_dir = store.tools_path(ToolBucket::Uv);
211211
let uv = uv_dir.join("uv").with_extension(env::consts::EXE_EXTENSION);
212212
if uv.is_file() {
213213
trace!(uv = %uv.display(), "Found managed uv");

src/store.rs

+46-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::path::{Path, PathBuf};
2+
use std::sync::LazyLock;
23

34
use anyhow::Result;
45
use rusqlite::Connection;
@@ -29,6 +30,28 @@ pub enum Error {
2930
Git(#[from] crate::git::Error),
3031
}
3132

33+
static STORE_HOME: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
34+
if let Some(path) = std::env::var_os(EnvVars::PRE_COMMIT_HOME) {
35+
debug!(
36+
path = %path.to_string_lossy(),
37+
"Loading store from PRE_COMMIT_HOME",
38+
);
39+
Some(path.into())
40+
} else if let Some(path) = std::env::var_os(EnvVars::XDG_CACHE_HOME) {
41+
let path = PathBuf::from(path).join("pre-commit");
42+
debug!(
43+
path = %path.to_string_lossy(),
44+
"Loading store from XDG_CACHE_HOME",
45+
);
46+
Some(path)
47+
} else {
48+
let home = home::home_dir()?;
49+
let path = home.join(".cache").join("pre-commit");
50+
debug!(path = %path.display(), "Loading store from ~/.cache");
51+
Some(path)
52+
}
53+
});
54+
3255
/// A store for managing repos.
3356
#[derive(Debug)]
3457
pub struct Store {
@@ -38,25 +61,9 @@ pub struct Store {
3861

3962
impl Store {
4063
pub fn from_settings() -> Result<Self, Error> {
41-
if let Some(path) = std::env::var_os(EnvVars::PRE_COMMIT_HOME) {
42-
debug!(
43-
path = %path.to_string_lossy(),
44-
"Loading store from PRE_COMMIT_HOME",
45-
);
46-
return Ok(Self::from_path(path));
47-
} else if let Some(path) = std::env::var_os(EnvVars::XDG_CACHE_HOME) {
48-
let path = PathBuf::from(path).join("pre-commit");
49-
debug!(
50-
path = %path.to_string_lossy(),
51-
"Loading store from XDG_CACHE_HOME",
52-
);
53-
return Ok(Self::from_path(path));
54-
}
55-
56-
let home = home::home_dir().ok_or(Error::HomeNotFound)?;
57-
let path = home.join(".cache").join("pre-commit");
58-
debug!(path = %path.display(), "Loading store from ~/.cache");
59-
Ok(Self::from_path(path))
64+
Ok(Self::from_path(
65+
STORE_HOME.as_ref().ok_or(Error::HomeNotFound)?,
66+
))
6067
}
6168

6269
pub fn from_path(path: impl Into<PathBuf>) -> Self {
@@ -267,8 +274,26 @@ impl Store {
267274
LockedFile::acquire(self.path.join(".lock"), "store").await
268275
}
269276

270-
pub fn uv_path(&self) -> PathBuf {
271-
self.path.join("tools").join("uv")
277+
/// The path to the tool directory in the store.
278+
pub fn tools_path(&self, tool: ToolBucket) -> PathBuf {
279+
self.path.join(tool.as_str())
280+
}
281+
}
282+
283+
#[derive(Copy, Clone)]
284+
pub enum ToolBucket {
285+
Uv,
286+
Python,
287+
Node,
288+
}
289+
290+
impl ToolBucket {
291+
pub fn as_str(&self) -> &str {
292+
match self {
293+
ToolBucket::Uv => "uv",
294+
ToolBucket::Python => "python",
295+
ToolBucket::Node => "node",
296+
}
272297
}
273298
}
274299

0 commit comments

Comments
 (0)