From 6030021ab6f318b054086e186ff0a3eca16c6021 Mon Sep 17 00:00:00 2001 From: "Bradley A. Thornton" Date: Thu, 25 Apr 2024 08:00:30 -0700 Subject: [PATCH] Add system site package flag (#140) --- .config/dictionary.txt | 1 + src/ansible_dev_environment/arg_parser.py | 9 ++++ src/ansible_dev_environment/config.py | 25 ++++++---- tests/unit/test_config.py | 61 +++++++++++++++++++++++ 4 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 tests/unit/test_config.py diff --git a/.config/dictionary.txt b/.config/dictionary.txt index d12e60e..ba84c9e 100644 --- a/.config/dictionary.txt +++ b/.config/dictionary.txt @@ -15,6 +15,7 @@ fqcn levelname levelno netcommon +purelib reqs setenv specp diff --git a/src/ansible_dev_environment/arg_parser.py b/src/ansible_dev_environment/arg_parser.py index b3d9226..ffa4dda 100644 --- a/src/ansible_dev_environment/arg_parser.py +++ b/src/ansible_dev_environment/arg_parser.py @@ -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( diff --git a/src/ansible_dev_environment/config.py b/src/ansible_dev_environment/config.py index 0629646..b06677c 100644 --- a/src/ansible_dev_environment/config.py +++ b/src/ansible_dev_environment/config.py @@ -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}" @@ -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: @@ -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) diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py new file mode 100644 index 0000000..b1f9cc2 --- /dev/null +++ b/tests/unit/test_config.py @@ -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