From 30695e8dd7084cc112097997d990e2ca3d6630a4 Mon Sep 17 00:00:00 2001 From: "Bradley A. Thornton" Date: Tue, 19 Sep 2023 10:43:38 -0700 Subject: [PATCH 1/3] Add python deps --- src/pip4a/subcommands/lister.py | 2 +- src/pip4a/subcommands/treemaker.py | 89 ++++++++++++++++++++++-------- src/pip4a/utils.py | 2 +- 3 files changed, 68 insertions(+), 25 deletions(-) diff --git a/src/pip4a/subcommands/lister.py b/src/pip4a/subcommands/lister.py index aadd14e..52a1c85 100644 --- a/src/pip4a/subcommands/lister.py +++ b/src/pip4a/subcommands/lister.py @@ -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) diff --git a/src/pip4a/subcommands/treemaker.py b/src/pip4a/subcommands/treemaker.py index 80be82c..e9aa0ba 100644 --- a/src/pip4a/subcommands/treemaker.py +++ b/src/pip4a/subcommands/treemaker.py @@ -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: @@ -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, @@ -68,35 +73,73 @@ 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) + + if self._config.args.verbose >= 1: + green = ["python requirements"] + for line in python_deps: + if "#" not in line: + green.append(line).strip() + green.append(line.split("#", 1)[0].strip()) + else: + green = [] + + + 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 = 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 = 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 ` 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. + python_deps: The Python dependencies. + """ + for dep in sorted(python_deps): + name, comment = dep.split("#", 1) + if collection_name in comment: + if "python requirements" not in tree_dict[collection_name]: + tree_dict[collection_name]["python requirements"] = [] + tree_dict[collection_name]["python requirements"].append(name.strip()) + + diff --git a/src/pip4a/utils.py b/src/pip4a/utils.py index cd2193c..a260a74 100644 --- a/src/pip4a/utils.py +++ b/src/pip4a/utils.py @@ -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 From f4d7566dfaf720800d5eda16d2fb47918b061c1a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 19 Sep 2023 17:44:36 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pip4a/subcommands/treemaker.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/pip4a/subcommands/treemaker.py b/src/pip4a/subcommands/treemaker.py index e9aa0ba..c4d71e3 100644 --- a/src/pip4a/subcommands/treemaker.py +++ b/src/pip4a/subcommands/treemaker.py @@ -80,7 +80,11 @@ def run(self: TreeMaker) -> None: # noqa: C901, PLR0912, PLR0915 links[collection_name] = link if self._config.args.verbose >= 1: - add_python_reqs(tree_dict=tree_dict, collection_name=collection_name, python_deps=python_deps) + add_python_reqs( + tree_dict=tree_dict, + collection_name=collection_name, + python_deps=python_deps, + ) if self._config.args.verbose >= 1: green = ["python requirements"] @@ -91,7 +95,6 @@ def run(self: TreeMaker) -> None: # noqa: C901, PLR0912, PLR0915 else: green = [] - more_verbose = 2 if self._config.args.verbose >= more_verbose: tree = Tree(obj=tree_dict, term_features=self._config.term_features) @@ -121,14 +124,16 @@ def run(self: TreeMaker) -> None: # noqa: C901, PLR0912, PLR0915 rendered = tree.render() print(rendered) # noqa: T201 - if self._config.args.verbose >= 1: + if self._config.args.verbose >= 1: msg = "Only direct python dependencies are shown." self._output.info(msg) hint = "Run `pip show ` 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: +def add_python_reqs( + tree_dict: dict[str, JSONVal], collection_name: str, python_deps: list[str], +) -> None: """Add Python dependencies to the tree. Args: @@ -141,5 +146,3 @@ def add_python_reqs(tree_dict: dict[str, JSONVal], collection_name: str, python_ if "python requirements" not in tree_dict[collection_name]: tree_dict[collection_name]["python requirements"] = [] tree_dict[collection_name]["python requirements"].append(name.strip()) - - From 439c73c23687f0e9a751765f2143019d16fcdd06 Mon Sep 17 00:00:00 2001 From: "Bradley A. Thornton" Date: Tue, 19 Sep 2023 12:50:31 -0700 Subject: [PATCH 3/3] Lint fixes --- src/pip4a/subcommands/treemaker.py | 36 +++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/pip4a/subcommands/treemaker.py b/src/pip4a/subcommands/treemaker.py index c4d71e3..8a2e2eb 100644 --- a/src/pip4a/subcommands/treemaker.py +++ b/src/pip4a/subcommands/treemaker.py @@ -85,21 +85,19 @@ def run(self: TreeMaker) -> None: # noqa: C901, PLR0912, PLR0915 collection_name=collection_name, python_deps=python_deps, ) - + green: list[str] = [] if self._config.args.verbose >= 1: - green = ["python requirements"] + green.append("python requirements") for line in python_deps: if "#" not in line: - green.append(line).strip() + green.append(line.strip()) green.append(line.split("#", 1)[0].strip()) - else: - green = [] 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 = green + tree.green.extend(green) rendered = tree.render() print(rendered) # noqa: T201 else: @@ -120,7 +118,7 @@ def run(self: TreeMaker) -> None: # noqa: C901, PLR0912, PLR0915 tree = Tree(obj=pruned_tree_dict, term_features=self._config.term_features) tree.links = links - tree.green = green + tree.green.extend(green) rendered = tree.render() print(rendered) # noqa: T201 @@ -132,17 +130,33 @@ def run(self: TreeMaker) -> None: # noqa: C901, PLR0912, PLR0915 def add_python_reqs( - tree_dict: dict[str, JSONVal], collection_name: str, python_deps: list[str], + 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 "python requirements" not in tree_dict[collection_name]: - tree_dict[collection_name]["python requirements"] = [] - tree_dict[collection_name]["python requirements"].append(name.strip()) + if not isinstance(collection["python requirements"], list): + msg = "Python requirements is not a list." + raise TypeError(msg) + collection["python requirements"].append(name.strip())