Skip to content

Commit

Permalink
Use Uv::venv() wherever we create venvs with uv (astral-sh#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsp authored Feb 25, 2024
1 parent a731a63 commit ae22370
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 45 deletions.
59 changes: 16 additions & 43 deletions rye/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::platform::get_toolchain_python_bin;
use crate::pyproject::{read_venv_marker, write_venv_marker, ExpandedSources, PyProject};
use crate::sources::py::PythonVersion;
use crate::utils::{
get_venv_python_bin, mark_path_sync_ignore, set_proxy_variables, symlink_dir, CommandOutput,
get_venv_python_bin, set_proxy_variables, symlink_dir, update_venv_sync_marker, CommandOutput,
IoPathContext,
};
use crate::uv::Uv;
Expand Down Expand Up @@ -334,19 +334,14 @@ pub fn create_virtualenv(
) -> Result<(), Error> {
let py_bin = get_toolchain_python_bin(py_ver)?;

let mut venv_cmd = if Config::current().use_uv() {
if Config::current().use_uv() {
// try to kill the empty venv if there is one as uv can't work otherwise.
fs::remove_dir(venv).ok();
let mut venv_cmd = Uv::ensure_exists(output)?.cmd();
venv_cmd.arg("venv");
if output == CommandOutput::Verbose {
venv_cmd.arg("--verbose");
} else {
venv_cmd.arg("-q");
}
venv_cmd.arg("-p");
venv_cmd.arg(&py_bin);
venv_cmd
let uv = Uv::ensure_exists(output.quieter())?
.venv(venv, &py_bin, py_ver)
.context("failed to initialize virtualenv")?;
uv.write_marker()?;
uv.sync_marker();
} else {
// create the venv folder first so we can manipulate some flags on it.
fs::create_dir_all(venv).path_context(venv, "unable to create virtualenv folder")?;
Expand All @@ -364,24 +359,16 @@ pub fn create_virtualenv(
venv_cmd.arg("--no-seed");
venv_cmd.arg("--prompt");
venv_cmd.arg(prompt);
venv_cmd
};

venv_cmd.arg("--").arg(venv);

let status = venv_cmd
.status()
.context("unable to invoke virtualenv command")?;
if !status.success() {
bail!("failed to initialize virtualenv");
}

write_venv_marker(venv, py_ver)?;
venv_cmd.arg("--").arg(venv);
let status = venv_cmd
.status()
.context("unable to invoke virtualenv command")?;
if !status.success() {
bail!("failed to initialize virtualenv");
}

// uv can only do it now
if Config::current().use_uv() {
update_venv_sync_marker(output, venv);
}
write_venv_marker(venv, py_ver)?;
};

// On UNIX systems Python is unable to find the tcl config that is placed
// outside of the virtualenv. It also sometimes is entirely unable to find
Expand All @@ -394,20 +381,6 @@ pub fn create_virtualenv(
Ok(())
}

/// Update the cloud synchronization marker for the given path
/// based on the config flag.
fn update_venv_sync_marker(output: CommandOutput, venv_path: &Path) {
if let Err(err) = mark_path_sync_ignore(venv_path, Config::current().venv_mark_sync_ignore()) {
if output != CommandOutput::Quiet && Config::current().venv_mark_sync_ignore() {
warn!(
"unable to mark virtualenv {} ignored for cloud sync: {}",
venv_path.display(),
err
);
}
}
}

#[cfg(unix)]
fn inject_tcl_config(venv: &Path, py_bin: &Path) -> Result<(), Error> {
let lib_path = match py_bin
Expand Down
22 changes: 22 additions & 0 deletions rye/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ impl CommandOutput {
CommandOutput::Normal
}
}

pub fn quieter(self) -> CommandOutput {
match self {
CommandOutput::Normal => CommandOutput::Quiet,
CommandOutput::Verbose => CommandOutput::Normal,
CommandOutput::Quiet => CommandOutput::Quiet,
}
}
}

/// Given a path checks if that path is executable.
Expand Down Expand Up @@ -447,6 +455,20 @@ pub struct CopyDirOptions {
pub exclude: Vec<PathBuf>,
}

/// Update the cloud synchronization marker for the given path
/// based on the config flag.
pub fn update_venv_sync_marker(output: CommandOutput, venv_path: &Path) {
if let Err(err) = mark_path_sync_ignore(venv_path, Config::current().venv_mark_sync_ignore()) {
if output != CommandOutput::Quiet && Config::current().venv_mark_sync_ignore() {
warn!(
"unable to mark virtualenv {} ignored for cloud sync: {}",
venv_path.display(),
err
);
}
}
}

#[test]
fn test_quiet_exit_display() {
let quiet_exit = QuietExit(0);
Expand Down
18 changes: 16 additions & 2 deletions rye/src/uv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::pyproject::write_venv_marker;
use crate::sources::py::PythonVersion;
use crate::sources::uv::{UvDownload, UvRequest};
use crate::utils::{
check_checksum, set_proxy_variables, unpack_archive, CommandOutput, IoPathContext,
check_checksum, set_proxy_variables, unpack_archive, update_venv_sync_marker, CommandOutput,
IoPathContext,
};
use anyhow::{anyhow, Context, Error};
use std::fs::{self, remove_dir_all};
Expand Down Expand Up @@ -132,7 +133,8 @@ impl Uv {
py_bin: &Path,
version: &PythonVersion,
) -> Result<UvWithVenv, Error> {
self.cmd()
let status = self
.cmd()
.arg("venv")
.arg("--python")
.arg(py_bin)
Expand All @@ -147,6 +149,13 @@ impl Uv {
)
})?;

if !status.success() {
return Err(anyhow!(
"Failed to create self venv using {}. uv exited with status: {}",
py_bin.display(),
status
));
}
Ok(UvWithVenv::new(self.clone(), venv_dir, version))
}
}
Expand Down Expand Up @@ -235,4 +244,9 @@ impl UvWithVenv {
.path_context(&tool_version_path, "could not write tool version")?;
Ok(())
}

/// Update the cloud synchronization marker for the given path
pub fn sync_marker(&self) {
update_venv_sync_marker(self.uv.output, &self.venv_path)
}
}

0 comments on commit ae22370

Please sign in to comment.