Skip to content

Commit

Permalink
Merge pull request #52 from cidrblock/feature_add_python_dep
Browse files Browse the repository at this point in the history
Add python deps
  • Loading branch information
cidrblock authored Sep 19, 2023
2 parents fa03ad0 + 439c73c commit b08dce0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/pip4a/subcommands/lister.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def run(self: Lister) -> None:
homepage = ci.get("homepage")
repository = ci.get("repository")
issues = ci.get("issues")
link = docs or homepage or repository or issues or "http://ansible.com"
link = repository or homepage or docs or issues or "http://ansible.com"
if not isinstance(link, str):
msg = "Link is not a string."
raise TypeError(msg)
Expand Down
106 changes: 83 additions & 23 deletions src/pip4a/subcommands/treemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import TYPE_CHECKING, Union

from pip4a.tree import Tree
from pip4a.utils import collect_manifests
from pip4a.utils import builder_introspect, collect_manifests


if TYPE_CHECKING:
Expand All @@ -33,6 +33,11 @@ def __init__(self: TreeMaker, config: Config, output: Output) -> None:
def run(self: TreeMaker) -> None: # noqa: C901, PLR0912, PLR0915
"""Run the command."""
# pylint: disable=too-many-locals
builder_introspect(self._config)

with self._config.discovered_python_reqs.open("r") as reqs_file:
python_deps = reqs_file.read().splitlines()

collections = collect_manifests(
target=self._config.site_pkg_collections_path,
venv_cache_dir=self._config.venv_cache_dir,
Expand Down Expand Up @@ -68,35 +73,90 @@ def run(self: TreeMaker) -> None: # noqa: C901, PLR0912, PLR0915
homepage = collection["collection_info"].get("homepage")
repository = collection["collection_info"].get("repository")
issues = collection["collection_info"].get("issues")
link = docs or homepage or repository or issues or "http://ansible.com"
link = repository or homepage or docs or issues or "http://ansible.com"
if not isinstance(link, str):
msg = "Link is not a string."
raise TypeError(msg)
links[collection_name] = link

if self._config.args.verbose:
if self._config.args.verbose >= 1:
add_python_reqs(
tree_dict=tree_dict,
collection_name=collection_name,
python_deps=python_deps,
)
green: list[str] = []
if self._config.args.verbose >= 1:
green.append("python requirements")
for line in python_deps:
if "#" not in line:
green.append(line.strip())
green.append(line.split("#", 1)[0].strip())

more_verbose = 2
if self._config.args.verbose >= more_verbose:
tree = Tree(obj=tree_dict, term_features=self._config.term_features)
tree.links = links
tree.green.extend(green)
rendered = tree.render()
print(rendered) # noqa: T201
else:
pruned_tree_dict: JSONVal = {}
if not isinstance(pruned_tree_dict, dict):
msg = "Tree dict is not a dict."
raise TypeError(msg)
for collection_name in list(tree_dict.keys()):
found = False
for value in tree_dict.values():
if not isinstance(value, dict):
msg = "Tree dict is not a dict."
raise TypeError(msg)
if collection_name in value:
found = True
if not found:
pruned_tree_dict[collection_name] = tree_dict[collection_name]

tree = Tree(obj=pruned_tree_dict, term_features=self._config.term_features)
tree.links = links
tree.green.extend(green)
rendered = tree.render()
print(rendered) # noqa: T201
return

pruned_tree_dict: JSONVal = {}
if not isinstance(pruned_tree_dict, dict):
msg = "Tree dict is not a dict."
raise TypeError(msg)
for collection_name in list(tree_dict.keys()):
found = False
for value in tree_dict.values():
if not isinstance(value, dict):
msg = "Tree dict is not a dict."
raise TypeError(msg)
if collection_name in value:
found = True
if not found:
pruned_tree_dict[collection_name] = tree_dict[collection_name]

tree = Tree(obj=pruned_tree_dict, term_features=self._config.term_features)
tree.links = links
rendered = tree.render()
print(rendered) # noqa: T201
if self._config.args.verbose >= 1:
msg = "Only direct python dependencies are shown."
self._output.info(msg)
hint = "Run `pip show <pkg>` to see indirect dependencies."
self._output.hint(hint)


def add_python_reqs(
tree_dict: dict[str, JSONVal],
collection_name: str,
python_deps: list[str],
) -> None:
"""Add Python dependencies to the tree.
Args:
tree_dict: The tree dict.
collection_name: The collection name.
python_deps: The Python dependencies.
Raises:
TypeError: If the tree dict is not a dict.
"""
if not isinstance(tree_dict, dict):
msg = "Tree dict is not a dict."
raise TypeError(msg)
collection = tree_dict[collection_name]
if not isinstance(collection, dict):
msg = "Tree dict is not a dict."
raise TypeError(msg)
collection["python requirements"] = []

for dep in sorted(python_deps):
name, comment = dep.split("#", 1)
if collection_name in comment:
if not isinstance(collection["python requirements"], list):
msg = "Python requirements is not a list."
raise TypeError(msg)
collection["python requirements"].append(name.strip())
2 changes: 1 addition & 1 deletion src/pip4a/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def __init__(
term_features: Terminal features
delay: The delay between characters
"""
self.spinner = itertools.cycle(["-", "/", "|", "\\"])
self.spinner = itertools.cycle(("|", "/", "-", "\\", "|", "/", "-"))
self.delay = delay
self.busy = False
self.spinner_visible = False
Expand Down

0 comments on commit b08dce0

Please sign in to comment.