Skip to content

Commit

Permalink
Merge pull request #13 from cidrblock/feature_better_opt_deps
Browse files Browse the repository at this point in the history
Feature better opt deps
  • Loading branch information
cidrblock authored Sep 5, 2023
2 parents 4423d87 + edc9ed3 commit 24b02a4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/pip4a/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions src/pip4a/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,18 @@ 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(
args=self.args,
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()
Expand Down
1 change: 0 additions & 1 deletion src/pip4a/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 5 additions & 6 deletions src/pip4a/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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()
Expand Down Expand Up @@ -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}"
Expand Down
26 changes: 26 additions & 0 deletions src/pip4a/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 24b02a4

Please sign in to comment.