Skip to content

Commit

Permalink
Omit --find-links from annotation header unless requested
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 23, 2024
1 parent eef3ca5 commit 26ba88a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 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 @@ -352,7 +352,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 @@ -399,29 +399,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 @@ -2891,7 +2891,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 @@ -2921,7 +2921,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

0 comments on commit 26ba88a

Please sign in to comment.