Skip to content

Commit

Permalink
Omit --find-links from annotation header unless requested (#1898)
Browse files Browse the repository at this point in the history
## Summary

Like #1835, but for `--find-links` (for consistency).
  • Loading branch information
charliermarsh authored Feb 23, 2024
1 parent 3bd4cca commit 73ed0f0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
52 changes: 36 additions & 16 deletions crates/uv/src/commands/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ pub(crate) async fn pip_compile(
writeln!(
writer,
"{}",
format!("# {}", cmd(include_index_url)).green()
format!("# {}", cmd(include_index_url, include_find_links)).green()
)?;
}

Expand Down Expand Up @@ -393,29 +393,49 @@ pub(crate) async fn pip_compile(
}

/// Format the `uv` command used to generate the output file.
fn cmd(include_index_url: bool) -> String {
fn cmd(include_index_url: bool, include_find_links: bool) -> String {
let args = env::args_os()
.skip(1)
.map(|arg| arg.normalized_display().to_string())
.scan(None, move |skip_next, arg| {
if let Some(true) = skip_next {
if matches!(skip_next, Some(true)) {
// Reset state; skip this iteration.
*skip_next = None;
Some(None)
} else if !include_index_url
&& (arg.starts_with("--extra-index-url=") || arg.starts_with("--index-url="))
{
// Reset state; skip this iteration.
*skip_next = None;
Some(None)
} else if !include_index_url && (arg == "--extra-index-url" || arg == "--index-url") {
return Some(None);
}

// Skip any index URLs, unless requested.
if !include_index_url {
if arg.starts_with("--extra-index-url=") || arg.starts_with("--index-url=") {
// Reset state; skip this iteration.
*skip_next = None;
return Some(None);
}

// Mark the next item as (to be) skipped.
*skip_next = Some(true);
Some(None)
} else {
// Return the argument.
Some(Some(arg))
if arg == "--index-url" || arg == "--extra-index-url" {
*skip_next = Some(true);
return Some(None);
}
}

// Skip any `--find-links` URLs, unless requested.
if !include_find_links {
if arg.starts_with("--find-links=") || arg.starts_with("-f=") {
// Reset state; skip this iteration.
*skip_next = None;
return Some(None);
}

// Mark the next item as (to be) skipped.
if arg == "--find-links" || arg == "-f" {
*skip_next = Some(true);
return Some(None);
}
}

// Return the argument.
Some(Some(arg))
})
.flatten()
.join(" ");
Expand Down
4 changes: 2 additions & 2 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2889,7 +2889,7 @@ fn find_links_directory() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in --find-links [PROJECT_ROOT]/scripts/wheels
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in
markupsafe==2.1.3
# via werkzeug
numpy==1.26.2
Expand Down Expand Up @@ -2919,7 +2919,7 @@ fn find_links_url() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in --no-index --find-links https://download.pytorch.org/whl/torch_stable.html
# uv pip compile --cache-dir [CACHE_DIR] --exclude-newer 2023-11-18T12:00:00Z requirements.in --no-index
tqdm==4.64.1
----- stderr -----
Expand Down
6 changes: 3 additions & 3 deletions crates/uv/tests/pip_compile_scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn requires_incompatible_python_version_compatible_override() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.11
# uv pip compile requirements.in --cache-dir [CACHE_DIR] --python-version=3.11
albatross==1.0.0
----- stderr -----
Expand Down Expand Up @@ -244,7 +244,7 @@ fn requires_incompatible_python_version_compatible_override_no_wheels_available_
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.11
# uv pip compile requirements.in --cache-dir [CACHE_DIR] --python-version=3.11
albatross==1.0.0
----- stderr -----
Expand Down Expand Up @@ -464,7 +464,7 @@ fn requires_python_patch_version_override_patch_compatible() -> Result<()> {
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in --find-links https://raw.githubusercontent.com/zanieb/packse/de0bab473eeaa4445db5a8febd732c655fad3d52/vendor/links.html --cache-dir [CACHE_DIR] --python-version=3.8.0
# uv pip compile requirements.in --cache-dir [CACHE_DIR] --python-version=3.8.0
albatross==1.0.0
----- stderr -----
Expand Down

0 comments on commit 73ed0f0

Please sign in to comment.