Skip to content

Commit

Permalink
Add system site package flag (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
cidrblock authored Apr 25, 2024
1 parent a84dd5c commit 6030021
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 9 deletions.
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fqcn
levelname
levelno
netcommon
purelib
reqs
setenv
specp
Expand Down
9 changes: 9 additions & 0 deletions src/ansible_dev_environment/arg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ def parse() -> argparse.Namespace:
help="Disable the use of ANSI codes for terminal hyperlink generation and color.",
)

level1.add_argument(
"--ssp",
"--system-site-packages",
help="When building a virtual environment, give access to the system site-packages dir.",
default=False,
dest="system_site_packages",
action="store_true",
)

common_args(level1)

_check = subparsers.add_parser(
Expand Down
25 changes: 16 additions & 9 deletions src/ansible_dev_environment/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,15 @@ def _set_interpreter(
msg = f"Creating virtual environment: {self.venv}"
self._output.debug(msg)
command = f"python -m venv {self.venv}"
work = "Creating virtual environment"
msg = f"Creating virtual environment: {self.venv}"
if self.args.system_site_packages:
command = f"{command} --system-site-packages"
msg = f"Creating virtual environment with system site packages: {self.venv}"
try:
subprocess_run(
command=command,
verbose=self.args.verbose,
msg=work,
msg=msg,
output=self._output,
)
msg = f"Created virtual environment: {self.venv}"
Expand All @@ -138,10 +141,10 @@ def _set_interpreter(
self.venv_interpreter = venv_interpreter

def _set_site_pkg_path(self: Config) -> None:
"""USe the interpreter to find the site packages path."""
"""Use the interpreter to find the site packages path."""
command = (
f"{self.venv_interpreter} -c"
" 'import json,site; print(json.dumps(site.getsitepackages()))'"
"'import json,sysconfig; print(json.dumps(sysconfig.get_paths()))'"
)
work = "Locating site packages directory"
try:
Expand All @@ -156,16 +159,20 @@ def _set_site_pkg_path(self: Config) -> None:
self._output.critical(err)

try:
site_pkg_dirs = json.loads(proc.stdout)
sysconfig_paths = json.loads(proc.stdout)
except json.JSONDecodeError as exc:
err = f"Failed to decode json: {exc}"
self._output.critical(err)

if not site_pkg_dirs:
if not sysconfig_paths:
err = "Failed to find site packages path."
self._output.critical(err)

msg = f"Found site packages path: {site_pkg_dirs[0]}"
self._output.debug(msg)
purelib = sysconfig_paths.get("purelib")
if not purelib:
err = "Failed to find purelib in sysconfig paths."
self._output.critical(err)

self.site_pkg_path = Path(site_pkg_dirs[0])
self.site_pkg_path = Path(purelib)
msg = f"Found site packages path: {self.site_pkg_path}"
self._output.debug(msg)
61 changes: 61 additions & 0 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Test the config module."""

from __future__ import annotations

import argparse

from typing import TYPE_CHECKING

import pytest

from ansible_dev_environment.config import Config
from ansible_dev_environment.output import Output
from ansible_dev_environment.utils import TermFeatures


if TYPE_CHECKING:
from pathlib import Path


@pytest.mark.parametrize(
"system_site_packages",
((True, False)),
ids=["ssp_true", "ssp_false"],
)
def test_paths(tmpdir: Path, system_site_packages: bool) -> None: # noqa: FBT001
"""Test the paths.
Several of the found directories should have a parent of the tmpdir / test_venv
Args:
tmpdir: A temporary directory.
system_site_packages: Whether to include system site packages.
"""
venv = tmpdir / "test_venv"
args = argparse.Namespace(
venv=str(venv),
system_site_packages=system_site_packages,
verbose=0,
)
term_features = TermFeatures(color=False, links=False)

output = Output(
log_file=str(tmpdir / "test_log.log"),
log_level="debug",
log_append="false",
term_features=term_features,
verbosity=0,
)

config = Config(args=args, output=output, term_features=term_features)
config.init()

assert config.venv == venv
for attr in (
"site_pkg_collections_path",
"site_pkg_path",
"venv_bindir",
"venv_cache_dir",
"venv_interpreter",
):
assert venv in getattr(config, attr).parents

0 comments on commit 6030021

Please sign in to comment.