diff --git a/src/pip4a/base.py b/src/pip4a/base.py index e8cca4b..d25a296 100644 --- a/src/pip4a/base.py +++ b/src/pip4a/base.py @@ -32,7 +32,6 @@ def __init__(self: Base, app: App) -> None: self.interpreter: Path self.site_pkg_path: Path self.python_path: Path - self._pip_include_test: bool = False def _set_interpreter( # noqa: PLR0912 self: Base, diff --git a/src/pip4a/cli.py b/src/pip4a/cli.py index feddbcf..c88eccf 100644 --- a/src/pip4a/cli.py +++ b/src/pip4a/cli.py @@ -91,6 +91,7 @@ def ensure_isolated(self: Cli) -> None: def run(self: Cli) -> None: """Run the application.""" + logger = logging.getLogger("pip4a") collection_name, dependencies = get_galaxy() self.app = App( @@ -98,6 +99,10 @@ def run(self: Cli) -> None: collection_name=collection_name, collection_dependencies=dependencies, ) + if "," in self.app.args.collection_specifier: + err = "Multiple optional dependencies are not supported at this time." + logger.critical(err) + if self.app.args.subcommand == "install": installer = Installer(self.app) installer.run() diff --git a/src/pip4a/constants.py b/src/pip4a/constants.py index 7960e04..0ce81a0 100644 --- a/src/pip4a/constants.py +++ b/src/pip4a/constants.py @@ -6,7 +6,6 @@ class Constants: """Constants, for now, for pip4a.""" - TEST_REQUIREMENTS_PY = Path("./test-requirements.txt").resolve() WORKING_DIR = Path("./.pip4a_cache").resolve() COLLECTION_BUILD_DIR = WORKING_DIR / "src" DISCOVERED_PYTHON_REQS = WORKING_DIR / "discovered_requirements.txt" diff --git a/src/pip4a/installer.py b/src/pip4a/installer.py index 50fb87e..4c027cd 100644 --- a/src/pip4a/installer.py +++ b/src/pip4a/installer.py @@ -12,7 +12,7 @@ from .base import Base from .constants import Constants as C # noqa: N817 -from .utils import oxford_join, subprocess_run +from .utils import opt_deps_to_files, oxford_join, subprocess_run logger = logging.getLogger(__name__) @@ -36,9 +36,6 @@ def run(self: Installer) -> None: if self.app.args.editable: self._swap_editable_collection() - if "[test]" in self.app.args.collection_specifier: - self._pip_include_test = True - self._discover_deps() self._pip_install() self._check_bindep() @@ -82,8 +79,10 @@ def _discover_deps(self: Installer) -> None: f" --write-bindep {C.DISCOVERED_BINDEP_REQS}" " --sanitize" ) - if self._pip_include_test and C.TEST_REQUIREMENTS_PY.exists(): - command += f" --user-pip {C.TEST_REQUIREMENTS_PY}" + opt_deps = re.match(r".*\[(.*)\]", self.app.args.collection_specifier) + if opt_deps: + for dep in opt_deps_to_files(opt_deps.group(1)): + command += f" --user-pip {dep}" msg = f"Writing discovered python requirements to: {C.DISCOVERED_PYTHON_REQS}" logger.info(msg) msg = f"Writing discovered system package requirements to: {C.DISCOVERED_BINDEP_REQS}" diff --git a/src/pip4a/utils.py b/src/pip4a/utils.py index e21e657..14380e1 100644 --- a/src/pip4a/utils.py +++ b/src/pip4a/utils.py @@ -83,3 +83,29 @@ def oxford_join(words: list[str]) -> str: if len(words) == 2: # noqa: PLR2004 return " and ".join(words) return ", ".join(words[:-1]) + ", and " + words[-1] + + +def opt_deps_to_files(dep_str: str) -> list[Path]: + """Convert a string of optional dependencies to a list of files. + + :param dep_str: A string of optional dependencies + :return: A list of files + """ + deps = dep_str.split(",") + files = [] + for dep in deps: + _dep = dep.strip() + variant1 = Path.cwd() / f"{_dep}-requirements.txt" + if variant1.exists(): + files.append(variant1) + continue + variant2 = Path.cwd() / f"requirements-{_dep}.txt" + if variant2.exists(): + files.append(variant2) + continue + msg = ( + f"Failed to find optional dependency file for '{_dep}'." + f" Checked for '{variant1.name}' and '{variant2.name}'. Skipping." + ) + logger.warning(msg) + return files