Skip to content

Commit a9ee3a4

Browse files
sdispaterabn
authored andcommitted
Use a symlink to the venv-installed poetry script
1 parent 3921e4b commit a9ee3a4

1 file changed

Lines changed: 16 additions & 89 deletions

File tree

install-poetry.py

Lines changed: 16 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import re
2020
import shutil
2121
import site
22-
import stat
2322
import subprocess
2423
import sys
2524
import tempfile
@@ -30,7 +29,6 @@
3029
from io import UnsupportedOperation
3130
from pathlib import Path
3231
from typing import Optional
33-
from typing import Tuple
3432
from urllib.request import Request
3533
from urllib.request import urlopen
3634

@@ -238,24 +236,6 @@ def temporary_directory(*args, **kwargs):
238236
yield name
239237

240238

241-
BIN = """# -*- coding: utf-8 -*-
242-
import glob
243-
import sys
244-
import os
245-
246-
site_packages = "{site_packages}"
247-
248-
sys.path.insert(0, site_packages)
249-
250-
if __name__ == "__main__":
251-
from {main_module} import main
252-
253-
main()
254-
"""
255-
256-
BAT = '@echo off\r\n{python_executable} "{poetry_bin}" %*\r\n'
257-
258-
259239
PRE_MESSAGE = """# Welcome to {poetry}!
260240
261241
This will download and install the latest version of {poetry},
@@ -458,9 +438,9 @@ def install(self, version, upgrade=False):
458438
)
459439
)
460440

461-
env_path, site_packages = self.make_env(version)
441+
env_path = self.make_env(version)
462442
self.install_poetry(version, env_path)
463-
self.make_bin(site_packages, version)
443+
self.make_bin(version)
464444

465445
self._overwrite(
466446
"Installing {} ({}): {}".format(
@@ -497,11 +477,12 @@ def uninstall(self) -> int:
497477

498478
shutil.rmtree(str(self._data_dir))
499479
for script in ["poetry", "poetry.bat"]:
500-
self._bin_dir.joinpath(script).unlink(missing_ok=True)
480+
if self._bin_dir.joinpath(script).exists():
481+
self._bin_dir.joinpath(script).unlink()
501482

502483
return 0
503484

504-
def make_env(self, version: str) -> Tuple[Path, Path]:
485+
def make_env(self, version: str) -> Path:
505486
self._overwrite(
506487
"Installing {} ({}): {}".format(
507488
colorize("info", "Poetry"),
@@ -525,12 +506,9 @@ def make_env(self, version: str) -> Tuple[Path, Path]:
525506

526507
virtualenv.cli_run([str(env_path), "--clear"])
527508

528-
if WINDOWS:
529-
return env_path, list(env_path.glob("Lib/site-packages"))[0]
530-
531-
return env_path, list(env_path.glob("lib/python*/site-packages"))[0]
509+
return env_path
532510

533-
def make_bin(self, site_packages: Path, version: str) -> None:
511+
def make_bin(self, version: str) -> None:
534512
self._overwrite(
535513
"Installing {} ({}): {}".format(
536514
colorize("info", "Poetry"),
@@ -541,69 +519,18 @@ def make_bin(self, site_packages: Path, version: str) -> None:
541519

542520
self._bin_dir.mkdir(parents=True, exist_ok=True)
543521

544-
python_executable = sys.executable
545-
522+
script = "poetry"
523+
target_script = "venv/bin/poetry"
546524
if WINDOWS:
547-
with self._bin_dir.joinpath("poetry.bat").open("w") as f:
548-
f.write(
549-
BAT.format(
550-
python_executable=python_executable,
551-
poetry_bin=self._bin_dir.joinpath("poetry"),
552-
)
553-
)
554-
555-
# Versions of Poetry prior to 1.2.0 did not have the main()
556-
# function at the poetry.console.application level but et he poetry.console one.
557-
main_module = "poetry.console.application"
558-
version_content = site_packages.joinpath("poetry/__version__.py").read_text(
559-
encoding="utf-8"
560-
)
525+
script = "poetry.exe"
526+
target_script = "venv/Scripts/poetry.exe"
561527

562-
current_version_re = re.match('(?ms).*__version__ = "(.+)".*', version_content)
563-
if not current_version_re:
564-
self._write(
565-
colorize(
566-
"warning",
567-
"Unable to get the current Poetry version. Assuming None",
568-
)
569-
)
570-
if is_decorated():
571-
self._write("")
528+
if self._bin_dir.joinpath(script).exists():
529+
self._bin_dir.joinpath(script).unlink()
572530

573-
current_version = "1.2.0"
574-
else:
575-
current_version = current_version_re.group(1)
576-
577-
m = self.VERSION_REGEX.match(current_version)
578-
if tuple(int(p) for p in m.groups()[:2]) < (1, 2):
579-
main_module = "poetry.console"
580-
581-
with self._bin_dir.joinpath("poetry").open("w", encoding="utf-8") as f:
582-
f.write("#!/usr/bin/env {}\n".format(python_executable))
583-
584-
if WINDOWS:
585-
f.write(
586-
BIN.format(
587-
site_packages=str(site_packages.resolve()).replace(
588-
"\\", "\\\\"
589-
),
590-
main_module=main_module,
591-
)
592-
)
593-
else:
594-
f.write(
595-
BIN.format(
596-
site_packages=str(site_packages.resolve()),
597-
main_module=main_module,
598-
)
599-
)
600-
601-
if not WINDOWS:
602-
# Making the file executable
603-
st = os.stat(self._bin_dir.joinpath("poetry").as_posix())
604-
os.chmod(
605-
self._bin_dir.joinpath("poetry").as_posix(), st.st_mode | stat.S_IEXEC
606-
)
531+
self._bin_dir.joinpath(script).symlink_to(
532+
self._data_dir.joinpath(target_script)
533+
)
607534

608535
def install_poetry(self, version: str, env_path: Path) -> None:
609536
self._overwrite(

0 commit comments

Comments
 (0)