Skip to content

Commit

Permalink
find activate script with sysconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Nov 8, 2024
1 parent 3ec5371 commit 70b2168
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ echo "::group::Install dependencies"
curl-harder https://astral.sh/uv/0.4.30/install.sh -o uv-install.sh
UV_UNMANAGED_INSTALL="./.uv-bin" sh uv-install.sh
./.uv-bin/uv venv .venv
source .venv/bin/activate
source $(python ./src/trio/_tools/find_activate_script.py .venv)
./.uv-bin/uv pip install uv -c test-requirements.txt
python -m uv pip install 'build[uv]' -c test-requirements.txt
python -m uv --version
Expand Down
21 changes: 21 additions & 0 deletions src/trio/_tests/tools/test_find_activate_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import pathlib
import venv

import pytest

from trio._tools import find_activate_script


def test_find_activate_script(
tmp_path: pathlib.Path,
capsys: pytest.CaptureFixture[str],
) -> None:
env_dir = tmp_path / "venv"
venv.create(env_dir)
find_activate_script.main([os.fsdecode(env_dir)])
captured = capsys.readouterr()
assert captured.err == ""
activate = pathlib.Path(captured.out.removesuffix("\n"))
assert activate.exists()
assert activate.name == "activate"
58 changes: 58 additions & 0 deletions src/trio/_tools/find_activate_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Script to find the activate script in a virtual environment.
Has only stdlib dependencies because it runs before any package is installed.
"""

from __future__ import annotations

import argparse
import os
import pathlib
import sys
import sysconfig


def _venv_path(env_dir: str, name: str) -> str:
vars_ = {
"base": env_dir,
"platbase": env_dir,
"installed_base": env_dir,
"installed_platbase": env_dir,
}
return sysconfig.get_path(name, scheme="venv", vars=vars_)


if sys.version_info >= (3, 11):

def _get_binpath(v: pathlib.Path) -> pathlib.Path:
return pathlib.Path(_venv_path(env_dir=os.fsdecode(v), name="scripts"))

elif sys.platform == "win32":

def _get_binpath(v: pathlib.Path) -> pathlib.Path:
return v / "Scripts"

else:

def _get_binpath(v: pathlib.Path) -> pathlib.Path:
return v / "bin"


def _get_activate_script(v: pathlib.Path) -> pathlib.Path:
return _get_binpath(v) / "activate"


def main(args: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description="Find the activate script for a given virtual environment",
)
parser.add_argument("filename", type=pathlib.Path)
parsed_args = parser.parse_args(args=args)
activate = _get_activate_script(parsed_args.filename)
print(activate)
return 0


if __name__ == "__main__":
sys.exit(main())

Check warning on line 58 in src/trio/_tools/find_activate_script.py

View check run for this annotation

Codecov / codecov/patch

src/trio/_tools/find_activate_script.py#L58

Added line #L58 was not covered by tests

0 comments on commit 70b2168

Please sign in to comment.