From 2611a0c1469d0cf3c664f352259c80c36f527172 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Wed, 15 Jan 2025 10:23:00 +0200 Subject: [PATCH 1/2] Remove `import re` from entrypoint wrapper scripts --- crates/uv-install-wheel/src/wheel.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/uv-install-wheel/src/wheel.rs b/crates/uv-install-wheel/src/wheel.rs index 9e5698e0db8a..03904201a4f1 100644 --- a/crates/uv-install-wheel/src/wheel.rs +++ b/crates/uv-install-wheel/src/wheel.rs @@ -26,6 +26,8 @@ use crate::{Error, Layout}; /// Wrapper script template function /// /// +/// +/// Script template slightly modified: removed `import re`, allowing scripts that never import `re` to load faster. fn get_script_launcher(entry_point: &Script, shebang: &str) -> String { let Script { module, function, .. @@ -36,11 +38,13 @@ fn get_script_launcher(entry_point: &Script, shebang: &str) -> String { format!( r#"{shebang} # -*- coding: utf-8 -*- -import re import sys from {module} import {import_name} if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit({function}()) "# ) From 1869837fda33cb4ac89c191c9afeca7181261ab7 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Wed, 15 Jan 2025 11:38:22 +0200 Subject: [PATCH 2/2] Update test --- crates/uv/tests/it/tool_install.rs | 72 ++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/crates/uv/tests/it/tool_install.rs b/crates/uv/tests/it/tool_install.rs index e951cafc2f40..688e26a364bf 100644 --- a/crates/uv/tests/it/tool_install.rs +++ b/crates/uv/tests/it/tool_install.rs @@ -62,11 +62,13 @@ fn tool_install() { assert_snapshot!(fs_err::read_to_string(executable).unwrap(), @r###" #![TEMP_DIR]/tools/black/bin/python # -*- coding: utf-8 -*- - import re import sys from black import patched_main if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit(patched_main()) "###); @@ -135,11 +137,13 @@ fn tool_install() { assert_snapshot!(fs_err::read_to_string(bin_dir.join("flask")).unwrap(), @r###" #![TEMP_DIR]/tools/flask/bin/python # -*- coding: utf-8 -*- - import re import sys from flask.cli import main if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit(main()) "###); }); @@ -327,11 +331,13 @@ fn tool_install_version() { assert_snapshot!(fs_err::read_to_string(executable).unwrap(), @r###" #![TEMP_DIR]/tools/black/bin/python # -*- coding: utf-8 -*- - import re import sys from black import patched_main if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit(patched_main()) "###); @@ -409,11 +415,13 @@ fn tool_install_editable() { assert_snapshot!(fs_err::read_to_string(&executable).unwrap(), @r###" #![TEMP_DIR]/tools/black/bin/python # -*- coding: utf-8 -*- - import re import sys from black import main if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit(main()) "###); @@ -705,11 +713,13 @@ fn tool_install_editable_from() { assert_snapshot!(fs_err::read_to_string(&executable).unwrap(), @r###" #![TEMP_DIR]/tools/black/bin/python # -*- coding: utf-8 -*- - import re import sys from black import main if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit(main()) "###); @@ -856,11 +866,13 @@ fn tool_install_already_installed() { assert_snapshot!(fs_err::read_to_string(executable).unwrap(), @r###" #![TEMP_DIR]/tools/black/bin/python # -*- coding: utf-8 -*- - import re import sys from black import patched_main if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit(patched_main()) "###); }); @@ -1218,11 +1230,13 @@ fn tool_install_force() { assert_snapshot!(fs_err::read_to_string(executable).unwrap(), @r###" #![TEMP_DIR]/tools/black/bin/python3 # -*- coding: utf-8 -*- - import re import sys from black import patched_main if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit(patched_main()) "###); @@ -1545,11 +1559,13 @@ fn tool_install_unnamed_package() { assert_snapshot!(fs_err::read_to_string(executable).unwrap(), @r###" #![TEMP_DIR]/tools/black/bin/python # -*- coding: utf-8 -*- - import re import sys from black import patched_main if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit(patched_main()) "###); @@ -1658,11 +1674,13 @@ fn tool_install_unnamed_from() { assert_snapshot!(fs_err::read_to_string(executable).unwrap(), @r###" #![TEMP_DIR]/tools/black/bin/python # -*- coding: utf-8 -*- - import re import sys from black import patched_main if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit(patched_main()) "###); @@ -1747,11 +1765,13 @@ fn tool_install_unnamed_with() { assert_snapshot!(fs_err::read_to_string(executable).unwrap(), @r###" #![TEMP_DIR]/tools/black/bin/python # -*- coding: utf-8 -*- - import re import sys from black import patched_main if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit(patched_main()) "###); @@ -2618,11 +2638,13 @@ fn tool_install_malformed_dist_info() { assert_snapshot!(fs_err::read_to_string(executable).unwrap(), @r###" #![TEMP_DIR]/tools/babel/bin/python # -*- coding: utf-8 -*- - import re import sys from babel.messages.frontend import main if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit(main()) "###); @@ -2696,11 +2718,13 @@ fn tool_install_settings() { assert_snapshot!(fs_err::read_to_string(executable).unwrap(), @r###" #![TEMP_DIR]/tools/flask/bin/python # -*- coding: utf-8 -*- - import re import sys from flask.cli import main if __name__ == "__main__": - sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) + if sys.argv[0].endswith("-script.pyw"): + sys.argv[0] = sys.argv[0][:-11] + elif sys.argv[0].endswith(".exe"): + sys.argv[0] = sys.argv[0][:-4] sys.exit(main()) "###);