-
-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||