-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Support modules with different casing in build backend #12240
Open
konstin
wants to merge
4
commits into
main
Choose a base branch
from
konsti/build-backend-normalize-module-name
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−13
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,7 +128,7 @@ fn write_wheel( | |
if settings.module_root.is_absolute() { | ||
return Err(Error::AbsoluteModuleRoot(settings.module_root.clone())); | ||
} | ||
let strip_root = source_tree.join(settings.module_root); | ||
let src_root = source_tree.join(settings.module_root); | ||
|
||
let module_name = if let Some(module_name) = settings.module_name { | ||
module_name | ||
|
@@ -139,10 +139,8 @@ fn write_wheel( | |
}; | ||
debug!("Module name: `{:?}`", module_name); | ||
|
||
let module_root = strip_root.join(module_name.as_ref()); | ||
if !module_root.join("__init__.py").is_file() { | ||
return Err(Error::MissingModule(module_root)); | ||
} | ||
let module_root = find_module_root(&src_root, module_name)?; | ||
|
||
let mut files_visited = 0; | ||
for entry in WalkDir::new(module_root) | ||
.into_iter() | ||
|
@@ -169,7 +167,7 @@ fn write_wheel( | |
.expect("walkdir starts with root"); | ||
let wheel_path = entry | ||
.path() | ||
.strip_prefix(&strip_root) | ||
.strip_prefix(&src_root) | ||
.expect("walkdir starts with root"); | ||
if exclude_matcher.is_match(match_path) { | ||
trace!("Excluding from module: `{}`", match_path.user_display()); | ||
|
@@ -243,6 +241,38 @@ fn write_wheel( | |
Ok(()) | ||
} | ||
|
||
/// Match the module name to its module directory with potentially different casing. | ||
/// | ||
/// For example, a package may have the dist-info-normalized package name `pil_util`, but the | ||
/// importable module is named `PIL_util`. | ||
/// | ||
/// We get the module either as dist-info-normalized package name, or explicitly from the user. | ||
/// For dist-info-normalizing a package name, the rules are lowercasing, replacing `.` with `_` and | ||
/// replace `-` with `_`. Since `.` and `-` are not allowed in module names, we can check whether a | ||
/// directory name matches our expected module name by lowercasing it. | ||
fn find_module_root(src_root: &Path, module_name: Identifier) -> Result<PathBuf, Error> { | ||
let normalized = module_name.to_string(); | ||
let modules = fs_err::read_dir(src_root)? | ||
.filter_ok(|entry| { | ||
entry.file_name().to_string_lossy().to_lowercase() == normalized | ||
&& entry.path().join("__init__.py").is_file() | ||
}) | ||
.map_ok(|entry| entry.path()) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
match modules.as_slice() { | ||
[] => Err(Error::MissingModule { | ||
module_name, | ||
project_src: src_root.to_path_buf(), | ||
}), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest adding an error for the case in which a matching directory exists, but there's no |
||
[module_root] => Ok(module_root.clone()), | ||
multiple => { | ||
let mut paths = multiple.to_vec(); | ||
paths.sort(); | ||
Err(Error::MultipleModules { module_name, paths }) | ||
} | ||
} | ||
} | ||
|
||
/// Build a wheel from the source tree and place it in the output directory. | ||
pub fn build_editable( | ||
source_tree: &Path, | ||
|
@@ -292,10 +322,9 @@ pub fn build_editable( | |
}; | ||
debug!("Module name: `{:?}`", module_name); | ||
|
||
let module_root = src_root.join(module_name.as_ref()); | ||
if !module_root.join("__init__.py").is_file() { | ||
return Err(Error::MissingModule(module_root)); | ||
} | ||
// Check that a module root exists in the directory we're linking from the `.pth` file | ||
find_module_root(&src_root, module_name)?; | ||
|
||
wheel_writer.write_bytes( | ||
&format!("{}.pth", pyproject_toml.name().as_dist_info_name()), | ||
src_root.as_os_str().as_encoded_bytes(), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can probably use
to_str
here since if it's not Unicode it would never match, right?