Skip to content

Commit

Permalink
Report the actual self python version
Browse files Browse the repository at this point in the history
Inspect the venv marker for the self python environment and report its
version in rye --version output.
  • Loading branch information
bluss committed Mar 6, 2024
1 parent 11af741 commit 8eb07d9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
38 changes: 32 additions & 6 deletions rye/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ fn is_up_to_date() -> bool {
*UP_TO_UPDATE || FORCED_TO_UPDATE.load(atomic::Ordering::Relaxed)
}

#[derive(Debug, Clone)]
pub(crate) enum SelfVenvStatus {
NotUpToDate,
DoesNotExist,
}

/// Get self venv path and check if it exists and is up to date
pub fn get_self_venv_status() -> Result<PathBuf, (PathBuf, SelfVenvStatus)> {
let app_dir = get_app_dir();
let venv_dir = app_dir.join("self");

if venv_dir.is_dir() {
if is_up_to_date() {
Ok(venv_dir)
} else {
Err((venv_dir, SelfVenvStatus::NotUpToDate))
}
}
else
{
Err((venv_dir, SelfVenvStatus::DoesNotExist))
}
}


/// Bootstraps the venv for rye itself
pub fn ensure_self_venv(output: CommandOutput) -> Result<PathBuf, Error> {
ensure_self_venv_with_toolchain(output, None)
Expand All @@ -75,19 +100,20 @@ pub fn ensure_self_venv_with_toolchain(
toolchain_version_request: Option<PythonVersionRequest>,
) -> Result<PathBuf, Error> {
let app_dir = get_app_dir();
let venv_dir = app_dir.join("self");

if venv_dir.is_dir() {
if is_up_to_date() {
return Ok(venv_dir);
} else {
let venv_dir = match get_self_venv_status() {
Ok(venv_dir) => return Ok(venv_dir),
Err((venv_dir, SelfVenvStatus::DoesNotExist)) => venv_dir,
Err((venv_dir, SelfVenvStatus::NotUpToDate)) => {
if output != CommandOutput::Quiet {
echo!("Detected outdated rye internals. Refreshing");
}
fs::remove_dir_all(&venv_dir)
.path_context(&venv_dir, "could not remove self-venv for update")?;

venv_dir
}
}
};

if output != CommandOutput::Quiet {
echo!("Bootstrapping rye internals");
Expand Down
16 changes: 14 additions & 2 deletions rye/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ mod version;

use git_testament::git_testament;

use crate::bootstrap::SELF_PYTHON_TARGET_VERSION;
use crate::bootstrap::{SELF_PYTHON_TARGET_VERSION, get_self_venv_status};
use crate::config::Config;
use crate::platform::symlinks_supported;
use crate::pyproject::read_venv_marker;

git_testament!(TESTAMENT);

Expand Down Expand Up @@ -151,7 +152,18 @@ fn print_version() -> Result<(), Error> {
std::env::consts::OS,
std::env::consts::ARCH
);
echo!("self-python: {}", SELF_PYTHON_TARGET_VERSION);

let self_venv_python = match get_self_venv_status() {
Ok(venv_dir) | Err((venv_dir, _)) => {
read_venv_marker(&venv_dir).map(|mark| mark.python)
}
};

if let Some(python) = self_venv_python {
echo!("self-python: {}", python);
} else {
echo!("self-python: not bootstrapped (target: {})", SELF_PYTHON_TARGET_VERSION);
}
echo!("symlink support: {}", symlinks_supported());
echo!("uv enabled: {}", Config::current().use_uv());
Ok(())
Expand Down

0 comments on commit 8eb07d9

Please sign in to comment.