Skip to content

Commit

Permalink
feat: add support of package inspection using dependency graph
Browse files Browse the repository at this point in the history
  • Loading branch information
esynr3z committed Jan 23, 2024
1 parent 88d2779 commit 8ebb6f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
44 changes: 28 additions & 16 deletions pip_hdl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from pathlib import Path
from typing import Any, Sequence

from packaging.requirements import Requirement

from .graph import DependencyGraph
from .metainfo import PackageMetaInfo
from .version import __version__

Expand All @@ -23,11 +26,11 @@ class _CliInspectTarget(str, Enum):
"""Specify target for inspect operation."""

FILELIST = "filelist"
SOURCES_DIR = "sources_dir"
SOURCES_VAR = "sources_var"
ALL_FILELISTS = "all_filelists"
ALL_FILELISTS_AS_ARGS = "all_filelists_as_args"
SOURCES_DIR = "sources_dir"
ALL_SOURCES_DIRS = "all_sources_dirs"
SOURCES_VAR = "sources_var"
ALL_SOURCES_VARS = "all_sources_vars"


Expand Down Expand Up @@ -62,12 +65,10 @@ def configure(self) -> None:
def _configure_inspect_subparser(self, subparser: argparse.ArgumentParser) -> None:
"""Configure subparser for `inspect` command."""
subparser.description = """
avaliable attributes for package inspection:
avaliable attributes for inspection:
filelist - show absolute path to filelist
sources_dir - show absolute path to sources root
sources_var - show environment variable to setup sources root (in NAME=VAL format)
avaliable attributes for requirements.txt inspection:
all_filelists - show all filelists in the dependency-resolved order
all_filelists_as_args - show all filelists as above, but format them as EDA arguments (with -f)
all_sources_dirs - show absolute paths to all sources directories
Expand Down Expand Up @@ -117,15 +118,26 @@ def enter_cli() -> None:
def _do_inspect(obj: str, attr: _CliInspectTarget) -> None:
"""Do `inspect` command."""
if Path(obj).exists():
raise NotImplementedError("Handler for file with requirements is not implemented yet")
with Path(obj).open("r") as f:
packages = [PackageMetaInfo(Requirement(line).name) for line in f.readlines()]
else:
packages = [PackageMetaInfo(obj)]
graph = DependencyGraph(packages)

if attr == _CliInspectTarget.FILELIST:
print(packages[0].filelist)
elif attr == _CliInspectTarget.SOURCES_DIR:
print(packages[0].sources_dir)
elif attr == _CliInspectTarget.SOURCES_VAR:
var = packages[0].sources_var
print(f"{var.name}={var.value}")
elif attr == _CliInspectTarget.ALL_FILELISTS:
print(" ".join([str(p.metainfo.filelist) for p in graph]))
elif attr == _CliInspectTarget.ALL_FILELISTS_AS_ARGS:
print("-f " + " -f ".join([str(p.metainfo.filelist) for p in graph]))
elif attr == _CliInspectTarget.ALL_SOURCES_DIRS:
print(" ".join([str(p.metainfo.sources_dir) for p in graph]))
elif attr == _CliInspectTarget.ALL_SOURCES_VARS:
print(" ".join([f"{p.metainfo.sources_var.name}={p.metainfo.sources_var.value}" for p in graph]))
else:
metainfo = PackageMetaInfo(obj)
if attr == _CliInspectTarget.FILELIST:
print(metainfo.filelist)
elif attr == _CliInspectTarget.SOURCES_DIR:
print(metainfo.sources_dir)
elif attr == _CliInspectTarget.SOURCES_VAR:
var = metainfo.sources_var
print(f"{var.name}={var.value}")
else:
raise ValueError(f"Attribute '{attr.value}' is not supported for package inspection!")
raise ValueError(f"Attribute '{attr.value}' is not supported yet!")
5 changes: 0 additions & 5 deletions pip_hdl/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,3 @@ def render_dag(self, **kwargs: Any) -> Path:
graph.edge(node.id, downstream.id)

return Path(graph.render(**kwargs))


def requirements_txt_to_graph(req_txt: Path) -> DependencyGraph:
"""Parse `requirements.txt` and form a graph with all dependencies."""
raise NotImplementedError
10 changes: 0 additions & 10 deletions pip_hdl/metainfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,3 @@ def sources_var(self) -> EnvVar:
if self._sources_var is None:
self._sources_var = EnvVar(f"{self.name.upper()}_{self.SOURCES_VAR_SUFFIX}", str(self.sources_dir))
return self._sources_var

@property
def all_sources_vars(self) -> List[EnvVar]:
"""Environment variables for all sources, including dependencies first."""
if self._all_sources_vars is None:
self._all_sources_vars = []
for d in self.dependencies:
self._all_sources_vars.extend(d.metainfo.all_sources_vars)
self._all_sources_vars.append(self.sources_var)
return self._all_sources_vars

0 comments on commit 8ebb6f3

Please sign in to comment.