Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow non-nested archives for hexdump and others #1564

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions crates/uv-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl Pep517Backend {
import sys
sys.path = [{backend_path}] + sys.path

{import}
{import}
"#, backend_path = backend_path_encoded}
}
}
Expand Down Expand Up @@ -305,8 +305,11 @@ impl SourceBuild {
.map_err(|err| Error::Extraction(extracted.clone(), err))?;

// Extract the top-level directory from the archive.
uv_extract::strip_component(&extracted)
.map_err(|err| Error::Extraction(extracted.clone(), err))?
match uv_extract::strip_component(&extracted) {
Ok(top_level) => top_level,
Err(uv_extract::Error::NonSingularArchive(_)) => extracted,
Err(err) => return Err(Error::Extraction(extracted.clone(), err)),
}
};
let source_tree = if let Some(subdir) = subdirectory {
source_root.join(subdir)
Expand Down Expand Up @@ -614,7 +617,7 @@ impl SourceBuild {
let script = formatdoc! {
r#"{}
print(backend.build_{}("{}", metadata_directory={}))
"#, pep517_backend.backend_import(), self.build_kind, escaped_wheel_dir, metadata_directory
"#, pep517_backend.backend_import(), self.build_kind, escaped_wheel_dir, metadata_directory
};
let span = info_span!(
"run_python_script",
Expand Down
6 changes: 5 additions & 1 deletion crates/uv-distribution/src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,11 @@ impl<'a, T: BuildContext> SourceDistCachedBuilder<'a, T> {
drop(span);

// Extract the top-level directory.
let extracted = uv_extract::strip_component(temp_dir.path())?;
let extracted = match uv_extract::strip_component(temp_dir.path()) {
Ok(top_level) => top_level,
Err(uv_extract::Error::NonSingularArchive(_)) => temp_dir.into_path(),
Err(err) => return Err(err.into()),
};

// Persist it to the cache.
fs_err::tokio::create_dir_all(cache_path.parent().expect("Cache entry to have parent"))
Expand Down
6 changes: 4 additions & 2 deletions crates/uv-extract/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pub enum Error {
#[error("Unsupported archive type: {0}")]
UnsupportedArchive(PathBuf),
#[error(
"The top level of the archive must only contain a list directory, but it contains: {0:?}"
"The top-level of the archive must only contain a list directory, but it contains: {0:?}"
)]
InvalidArchive(Vec<OsString>),
NonSingularArchive(Vec<OsString>),
#[error("The top-level of the archive must only contain a list directory, but it's empty")]
EmptyArchive,
}
16 changes: 10 additions & 6 deletions crates/uv-extract/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,14 @@ pub fn strip_component(source: impl AsRef<Path>) -> Result<PathBuf, Error> {
// TODO(konstin): Verify the name of the directory.
let top_level =
fs_err::read_dir(source.as_ref())?.collect::<std::io::Result<Vec<fs_err::DirEntry>>>()?;
let [root] = top_level.as_slice() else {
return Err(Error::InvalidArchive(
top_level.into_iter().map(|e| e.file_name()).collect(),
));
};
Ok(root.path())
match top_level.as_slice() {
[root] => Ok(root.path()),
[] => Err(Error::EmptyArchive),
_ => Err(Error::NonSingularArchive(
top_level
.into_iter()
.map(|entry| entry.file_name())
.collect(),
)),
}
}