Skip to content

Commit

Permalink
Add rye fetch --force to re-fetch a toolchain (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 authored Feb 26, 2024
1 parent d735a72 commit ad705d0
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ _Unreleased_

- When `uv` is used the prompt is now set to the project name. #773

- Allow `rye fetch --force` to force re-fetch a downloaded toolchain. #778

<!-- released start -->

## 0.26.0
Expand Down
8 changes: 6 additions & 2 deletions docs/guide/commands/fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Fetch a specific version of Python:
$ rye fetch 3.8.13
Downloading [email protected]
Checking checksum
success: Downloaded [email protected]
Unpacking
Downloaded [email protected]
```

To fetch the pinned version of Python you can leave out the argument:
Expand All @@ -20,7 +21,8 @@ To fetch the pinned version of Python you can leave out the argument:
$ rye fetch
Downloading [email protected]
Checking checksum
success: Downloaded [email protected]
Unpacking
Downloaded [email protected]
```

## Arguments
Expand All @@ -31,6 +33,8 @@ success: Downloaded [email protected]

## Options

* `-f, --force`: Fetch the Python toolchain even if it is already installed.

* `-v, --verbose`: Enables verbose diagnostics

* `-q, --quiet`: Turns off all output
Expand Down
20 changes: 14 additions & 6 deletions rye/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ fn ensure_latest_self_toolchain(output: CommandOutput) -> Result<PythonVersion,
}
Ok(version)
} else {
fetch(&SELF_PYTHON_TARGET_VERSION, output)
fetch(&SELF_PYTHON_TARGET_VERSION, output, false)
}
}

Expand All @@ -301,7 +301,7 @@ fn ensure_specific_self_toolchain(
toolchain_version
);
}
fetch(&toolchain_version.into(), output)
fetch(&toolchain_version.into(), output, false)
} else {
if output != CommandOutput::Quiet {
echo!(
Expand All @@ -317,10 +317,11 @@ fn ensure_specific_self_toolchain(
pub fn fetch(
version: &PythonVersionRequest,
output: CommandOutput,
force: bool,
) -> Result<PythonVersion, Error> {
if let Ok(version) = PythonVersion::try_from(version.clone()) {
let py_bin = get_toolchain_python_bin(&version)?;
if py_bin.is_file() {
if !force && py_bin.is_file() {
if output == CommandOutput::Verbose {
echo!("Python version already downloaded. Skipping.");
}
Expand All @@ -339,10 +340,17 @@ pub fn fetch(
echo!("target dir: {}", target_dir.display());
}
if target_dir.is_dir() && target_py_bin.is_file() {
if output == CommandOutput::Verbose {
echo!("Python version already downloaded. Skipping.");
if !force {
if output == CommandOutput::Verbose {
echo!("Python version already downloaded. Skipping.");
}
return Ok(version);
}
if output != CommandOutput::Quiet {
echo!("Removing the existing Python version");
}
return Ok(version);
fs::remove_dir_all(&target_dir)
.with_context(|| format!("failed to remove target folder {}", target_dir.display()))?;
}

fs::create_dir_all(&target_dir).path_context(&target_dir, "failed to create target folder")?;
Expand Down
5 changes: 4 additions & 1 deletion rye/src/cli/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct Args {
///
/// If no version is provided, the requested version from local project or `.python-version` will be fetched.
version: Option<String>,
/// Fetch the Python toolchain even if it is already installed.
#[arg(short, long)]
force: bool,
/// Enables verbose diagnostics.
#[arg(short, long)]
verbose: bool,
Expand All @@ -40,6 +43,6 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
}
};

fetch(&version, output).context("error while fetching Python installation")?;
fetch(&version, output, cmd.force).context("error while fetching Python installation")?;
Ok(())
}
2 changes: 1 addition & 1 deletion rye/src/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn install(
uninstall_helper(&target_venv_path, &shim_dir)?;

// make sure we have a compatible python version
let py_ver = fetch(py_ver, output)?;
let py_ver = fetch(py_ver, output, false)?;

create_virtualenv(
output,
Expand Down
2 changes: 1 addition & 1 deletion rye/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn sync(mut cmd: SyncOptions) -> Result<(), Error> {

// make sure we have a compatible python version
let py_ver =
fetch(&py_ver.into(), output).context("failed fetching toolchain ahead of sync")?;
fetch(&py_ver.into(), output, false).context("failed fetching toolchain ahead of sync")?;

// kill the virtualenv if it's there and we need to get rid of it.
if recreate && venv.is_dir() {
Expand Down
38 changes: 38 additions & 0 deletions rye/tests/test_toolchain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::common::{rye_cmd_snapshot, Space};

mod common;

#[test]
fn test_fetch() {
let space = Space::new();
// Use a version not in use by other tests and will be supported for a long time.
let version = "[email protected]";

// Make sure the version is installed.
let status = space.rye_cmd().arg("fetch").arg(version).status().unwrap();
assert!(status.success());

// Fetching the same version again should be a no-op.
rye_cmd_snapshot!(space.rye_cmd().arg("fetch").arg(version).arg("--verbose"), @r###"
success: true
exit_code: 0
----- stdout -----
Python version already downloaded. Skipping.
----- stderr -----
"###);

// Fetching the same version again with --force should re-download it.
rye_cmd_snapshot!(space.rye_cmd().arg("fetch").arg(version).arg("--force"), @r###"
success: true
exit_code: 0
----- stdout -----
Removing the existing Python version
Downloading [email protected]
Checking checksum
Unpacking
Downloaded [email protected]
----- stderr -----
"###);
}

0 comments on commit ad705d0

Please sign in to comment.