Support dotted function paths for script entrypoints (#1622)

Co-authored-by: markm <mark.mcmahon@autodesk.com>
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
markmmm 2024-02-19 18:09:12 +08:00 committed by GitHub
parent 4dfcf32e4c
commit b76efc62a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 10 deletions

View file

@ -1,3 +1,4 @@
use once_cell::sync::Lazy;
use regex::Regex;
use rustc_hash::FxHashSet;
use serde::Serialize;
@ -43,9 +44,11 @@ impl Script {
// between the object reference and the left square bracket, between the extra names and the square brackets and colons delimiting them,
// and after the right square bracket."
// https://packaging.python.org/en/latest/specifications/entry-points/#file-format
let script_regex = Regex::new(r"^(?P<module>[\w\d_\-.]+)\s*:\s*(?P<function>[\w\d_\-.]+)(?:\s*\[\s*(?P<extras>(?:[^,]+,?\s*)+)\])?\s*$").unwrap();
static SCRIPT_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^(?P<module>[\w\d_\-.]+)\s*:\s*(?P<function>[\w\d_\-.]+)(?:\s*\[\s*(?P<extras>(?:[^,]+,?\s*)+)\])?\s*$").unwrap()
});
let captures = script_regex
let captures = SCRIPT_REGEX
.captures(value)
.ok_or_else(|| Error::InvalidWheel(format!("invalid console script: '{value}'")))?;
if let Some(script_extras) = captures.name("extras") {
@ -67,6 +70,12 @@ impl Script {
function: captures.name("function").unwrap().as_str().to_string(),
}))
}
pub fn import_name(&self) -> &str {
self.function
.split_once('.')
.map_or(&self.function, |(import_name, _)| import_name)
}
}
#[cfg(test)]
@ -98,4 +107,15 @@ mod test {
);
}
}
#[test]
fn test_split_of_import_name_from_function() {
let entrypoint = "foomod:mod_bar.sub_foo.func_baz";
let script = Script::from_value("script", entrypoint, None)
.unwrap()
.unwrap();
assert_eq!(script.function, "mod_bar.sub_foo.func_baz");
assert_eq!(script.import_name(), "mod_bar");
}
}