Skip to content

Commit

Permalink
Remove pyvenv.cfg patch (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 authored Dec 20, 2024
1 parent 6f2e9a9 commit 310477e
Showing 1 changed file with 0 additions and 41 deletions.
41 changes: 0 additions & 41 deletions src/languages/python/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ impl LanguageImpl for Python {

cmd.check(true).output().await?;

patch_cfg_version_info(&venv).await?;

// Install dependencies
uv_cmd("install dependencies")
.arg("pip")
Expand Down Expand Up @@ -157,42 +155,3 @@ fn bin_dir(venv: &Path) -> PathBuf {
venv.join("bin")
}
}

async fn get_full_version(path: &Path) -> anyhow::Result<String> {
let python = bin_dir(path).join("python");
let output = Cmd::new(&python, "run python")
.check(true)
.arg("-S")
.arg("-c")
.arg(r#"import sys; print(".".join(str(p) for p in sys.version_info))"#)
.output()
.await?;
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

// Patch pyvenv.cfg `version_info` to ".".join(str(p) for p in sys.version_info)
/// pre-commit use virtualenv to create venv, which sets `version_info` to the full version:
/// "3.12.5.final.0" instead of "3.12.5"
async fn patch_cfg_version_info(path: &Path) -> anyhow::Result<()> {
let full_version = get_full_version(path).await?;

let cfg = path.join("pyvenv.cfg");
let content = fs_err::read_to_string(&cfg)?;
let mut patched = String::new();
for line in content.lines() {
let Some((key, _)) = line.split_once('=') else {
patched.push_str(line);
patched.push('\n');
continue;
};
if key.trim() == "version_info" {
patched.push_str(&format!("version_info = {full_version}\n"));
} else {
patched.push_str(line);
patched.push('\n');
}
}

fs_err::write(&cfg, patched)?;
Ok(())
}

0 comments on commit 310477e

Please sign in to comment.