Skip to content

Commit

Permalink
Use a symlink to the venv-installed poetry script
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed Feb 19, 2021
1 parent fa21ac9 commit e2d63d0
Showing 1 changed file with 18 additions and 66 deletions.
84 changes: 18 additions & 66 deletions install-poetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import re
import shutil
import site
import stat
import subprocess
import sys
import tempfile
Expand All @@ -30,7 +29,6 @@
from io import UnsupportedOperation
from pathlib import Path
from typing import Optional
from typing import Tuple
from urllib.request import Request
from urllib.request import urlopen

Expand Down Expand Up @@ -253,7 +251,7 @@ def temporary_directory(*args, **kwargs):
main()
"""

BAT = '@echo off\r\n{python_executable} "{poetry_bin}" %*\r\n'
BAT = "@echo off\r\n{poetry_bin} %*\r\n"


PRE_MESSAGE = """# Welcome to {poetry}!
Expand Down Expand Up @@ -458,9 +456,9 @@ def install(self, version, upgrade=False):
)
)

env_path, site_packages = self.make_env(version)
env_path = self.make_env(version)
self.install_poetry(version, env_path)
self.make_bin(site_packages, version)
self.make_bin(version)

self._overwrite(
"Installing {} ({}): {}".format(
Expand Down Expand Up @@ -497,11 +495,12 @@ def uninstall(self) -> int:

shutil.rmtree(str(self._data_dir))
for script in ["poetry", "poetry.bat"]:
self._bin_dir.joinpath(script).unlink(missing_ok=True)
if self._bin_dir.joinpath(script).exists():
self._bin_dir.joinpath(script).unlink()

return 0

def make_env(self, version: str) -> Tuple[Path, Path]:
def make_env(self, version: str) -> Path:
self._overwrite(
"Installing {} ({}): {}".format(
colorize("info", "Poetry"),
Expand All @@ -525,12 +524,9 @@ def make_env(self, version: str) -> Tuple[Path, Path]:

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

if WINDOWS:
return env_path, list(env_path.glob("Lib/site-packages"))[0]

return env_path, list(env_path.glob("lib/python*/site-packages"))[0]
return env_path

def make_bin(self, site_packages: Path, version: str) -> None:
def make_bin(self, version: str) -> None:
self._overwrite(
"Installing {} ({}): {}".format(
colorize("info", "Poetry"),
Expand All @@ -541,69 +537,25 @@ def make_bin(self, site_packages: Path, version: str) -> None:

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

python_executable = sys.executable

if WINDOWS:
print(list(self._data_dir.joinpath("venv").glob("**/*")))
with self._bin_dir.joinpath("poetry.bat").open("w") as f:
f.write(
BAT.format(
python_executable=python_executable,
poetry_bin=self._bin_dir.joinpath("poetry"),
poetry_bin=str(
self._data_dir.joinpath("venv/Scripts/poetry.exe")
),
)
)

# Versions of Poetry prior to 1.2.0 did not have the main()
# function at the poetry.console.application level but et he poetry.console one.
main_module = "poetry.console.application"
version_content = site_packages.joinpath("poetry/__version__.py").read_text(
encoding="utf-8"
)

current_version_re = re.match('(?ms).*__version__ = "(.+)".*', version_content)
if not current_version_re:
self._write(
colorize(
"warning",
"Unable to get the current Poetry version. Assuming None",
)
)
if is_decorated():
self._write("")

current_version = "1.2.0"
else:
current_version = current_version_re.group(1)

m = self.VERSION_REGEX.match(current_version)
if tuple(int(p) for p in m.groups()[:2]) < (1, 2):
main_module = "poetry.console"
return

with self._bin_dir.joinpath("poetry").open("w", encoding="utf-8") as f:
f.write("#!/usr/bin/env {}\n".format(python_executable))
if self._bin_dir.joinpath("poetry").exists():
self._bin_dir.joinpath("poetry").unlink()

if WINDOWS:
f.write(
BIN.format(
site_packages=str(site_packages.resolve()).replace(
"\\", "\\\\"
),
main_module=main_module,
)
)
else:
f.write(
BIN.format(
site_packages=str(site_packages.resolve()),
main_module=main_module,
)
)

if not WINDOWS:
# Making the file executable
st = os.stat(self._bin_dir.joinpath("poetry").as_posix())
os.chmod(
self._bin_dir.joinpath("poetry").as_posix(), st.st_mode | stat.S_IEXEC
)
self._bin_dir.joinpath("poetry").symlink_to(
self._data_dir.joinpath("venv/bin/poetry")
)

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

0 comments on commit e2d63d0

Please sign in to comment.