diff --git a/src/poetry/masonry/builders/editable.py b/src/poetry/masonry/builders/editable.py index 6a077af7d77..2412c4df3c1 100644 --- a/src/poetry/masonry/builders/editable.py +++ b/src/poetry/masonry/builders/editable.py @@ -9,6 +9,8 @@ from pathlib import Path from typing import TYPE_CHECKING +from installer.scripts import Script +from installer.utils import get_launcher_kind from poetry.core.masonry.builders.builder import Builder from poetry.core.masonry.builders.sdist import SdistBuilder from poetry.core.masonry.utils.package_include import PackageInclude @@ -27,15 +29,6 @@ from poetry.poetry import Poetry from poetry.utils.env import Env -SCRIPT_TEMPLATE = """\ -#!{python} -import sys -from {module} import {callable_holder} - -if __name__ == '__main__': - sys.exit({callable_}()) -""" - WINDOWS_CMD_TEMPLATE = """\ @echo off\r\n"{python}" "%~dp0\\{script}" %*\r\n """ @@ -176,21 +169,16 @@ def _add_scripts(self) -> list[Path]: raise ValueError(msg) - callable_holder = callable_.split(".", 1)[0] - script_file = scripts_path.joinpath(name) self._debug( f" - Adding the {name} script to {scripts_path}" ) with script_file.open("w", encoding="utf-8") as f: f.write( - decode( - SCRIPT_TEMPLATE.format( - python=self._env.python, - module=module, - callable_holder=callable_holder, - callable_=callable_, - ) + str( + Script(name, module, callable_, "console") + .generate(str(self._env.python), get_launcher_kind())[1] + .decode() ) ) diff --git a/tests/masonry/builders/test_editable_builder.py b/tests/masonry/builders/test_editable_builder.py index 82c63426b90..994dd6abbe9 100644 --- a/tests/masonry/builders/test_editable_builder.py +++ b/tests/masonry/builders/test_editable_builder.py @@ -212,10 +212,12 @@ def test_builder_installs_proper_files_for_standard_packages( baz_script = f"""\ #!{tmp_venv.python} +# -*- coding: utf-8 -*- +import re import sys from bar import baz - -if __name__ == '__main__': +if __name__ == "__main__": + sys.argv[0] = re.sub(r"(-script\\.pyw|\\.exe)?$", "", sys.argv[0]) sys.exit(baz.boom.bim()) """ @@ -223,10 +225,12 @@ def test_builder_installs_proper_files_for_standard_packages( foo_script = f"""\ #!{tmp_venv.python} +# -*- coding: utf-8 -*- +import re import sys from foo import bar - -if __name__ == '__main__': +if __name__ == "__main__": + sys.argv[0] = re.sub(r"(-script\\.pyw|\\.exe)?$", "", sys.argv[0]) sys.exit(bar()) """ @@ -234,10 +238,12 @@ def test_builder_installs_proper_files_for_standard_packages( fox_script = f"""\ #!{tmp_venv.python} +# -*- coding: utf-8 -*- +import re import sys from fuz.foo import bar - -if __name__ == '__main__': +if __name__ == "__main__": + sys.argv[0] = re.sub(r"(-script\\.pyw|\\.exe)?$", "", sys.argv[0]) sys.exit(bar.baz()) """