Skip to content

Commit

Permalink
Introduce common parameters
Browse files Browse the repository at this point in the history
Also change the verbose option to debug to make pips verbose mode accesible
  • Loading branch information
realshouzy committed Jun 11, 2024
1 parent faf362b commit b2ccb37
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pip_manage/_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def format(self, record: logging.LogRecord) -> str:
def setup_logging(
logger_name: Literal["pip-review", "pip-purge"],
*,
verbose: bool,
debugging: bool,
) -> None:
level: Literal["DEBUG", "INFO"] = "DEBUG" if verbose else "INFO"
level: Literal["DEBUG", "INFO"] = "DEBUG" if debugging else "INFO"
logging.config.dictConfig(
{
"version": 1,
Expand Down
30 changes: 30 additions & 0 deletions pip_manage/_pip_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"LIST_ONLY",
"INSTALL_ONLY",
"UNINSTALL_ONLY",
"COMMON_PARAMETERS",
]

import dataclasses
Expand Down Expand Up @@ -98,6 +99,35 @@
),
)

COMMON_PARAMETERS: Final[frozenset[str]] = frozenset(
(
"isolated",
"require-virtualenv",
"python",
"v",
"verbose",
"q",
"quiet",
"log",
"no-input",
"keyring-provider",
"proxy",
"retries",
"timeout",
"exists-action",
"trusted-host",
"cert",
"client-cert",
"cache-dir",
"no-cache-dir",
"disable-pip-version-check",
"no-color",
"no-python-version-warning",
"use-feature",
"use-deprecated",
),
)


def filter_forwards_exclude(args: list[str], exclude: AbstractSet[str]) -> list[str]:
"""Return only the parts of `args` that do not appear in `exclude`."""
Expand Down
15 changes: 10 additions & 5 deletions pip_manage/pip_purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from pip_manage._logging import setup_logging
from pip_manage._pip_interface import (
COMMON_PARAMETERS,
PIP_CMD,
UNINSTALL_ONLY,
filter_forwards_include,
Expand Down Expand Up @@ -57,11 +58,12 @@ def _parse_args(
),
)
parser.add_argument(
"--verbose",
"-v",
"--debug",
"-d",
dest="debugging",
action="store_true",
default=False,
help="Show more output",
help="Show debug information",
)
parser.add_argument(
"--ignore-extra",
Expand Down Expand Up @@ -188,8 +190,11 @@ def main( # pylint: disable=R0914, R0915 # noqa: PLR0915
argv: Sequence[str] | None = None,
) -> int:
args, forwarded = _parse_args(argv)
uninstall_args: list[str] = filter_forwards_include(forwarded, UNINSTALL_ONLY)
setup_logging(__title__, verbose=args.verbose)
uninstall_args: list[str] = filter_forwards_include(
forwarded,
UNINSTALL_ONLY.union(COMMON_PARAMETERS),
)
setup_logging(__title__, debugging=args.debugging)
logger: logging.Logger = logging.getLogger(__title__)

logger.debug("Forwarded arguments: %s", forwarded)
Expand Down
14 changes: 8 additions & 6 deletions pip_manage/pip_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from pip_manage._logging import setup_logging
from pip_manage._pip_interface import (
COMMON_PARAMETERS,
INSTALL_ONLY,
LIST_ONLY,
filter_forwards,
Expand Down Expand Up @@ -43,11 +44,12 @@ def _parse_args(
epilog=_EPILOG,
)
parser.add_argument(
"--verbose",
"-v",
"--debug",
"-d",
dest="debugging",
action="store_true",
default=False,
help="Show more output",
help="Show debug information",
)
parser.add_argument(
"--raw",
Expand Down Expand Up @@ -242,14 +244,14 @@ def main( # pylint: disable=R0915 # noqa: PLR0915
list_args: list[str] = filter_forwards(
forwarded,
exclude=INSTALL_ONLY,
include=LIST_ONLY,
include=LIST_ONLY.union(COMMON_PARAMETERS),
)
install_args: list[str] = filter_forwards(
forwarded,
exclude=LIST_ONLY,
include=INSTALL_ONLY,
include=INSTALL_ONLY.union(COMMON_PARAMETERS),
)
setup_logging(__title__, verbose=args.verbose)
setup_logging(__title__, debugging=args.debugging)
logger: logging.Logger = logging.getLogger(__title__)

logger.debug("Forwarded arguments: %s", forwarded)
Expand Down
2 changes: 1 addition & 1 deletion tests/logging_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_setup_logging(
verbose: bool, # noqa: FBT001
level: int,
) -> None:
setup_logging(name, verbose=verbose)
setup_logging(name, debugging=verbose)
root_logger: logging.Logger = logging.getLogger()
assert root_logger.level == logging.DEBUG
assert len(root_logger.handlers) == 2
Expand Down
33 changes: 33 additions & 0 deletions tests/pip_interface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from pip_manage._pip_interface import (
COMMON_PARAMETERS,
INSTALL_ONLY,
LIST_ONLY,
PIP_CMD,
Expand Down Expand Up @@ -102,6 +103,38 @@
),
id="UNINSTALL_ONLY",
),
pytest.param(
COMMON_PARAMETERS,
frozenset(
(
"isolated",
"require-virtualenv",
"python",
"v",
"verbose",
"q",
"quiet",
"log",
"no-input",
"keyring-provider",
"proxy",
"retries",
"timeout",
"exists-action",
"trusted-host",
"cert",
"client-cert",
"cache-dir",
"no-cache-dir",
"disable-pip-version-check",
"no-color",
"no-python-version-warning",
"use-feature",
"use-deprecated",
),
),
id="COMMON_PARAMETERS",
),
pytest.param(
PIP_CMD,
(sys.executable, "-m", "pip"),
Expand Down
12 changes: 6 additions & 6 deletions tests/pip_purge_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_parse_args_empty_args() -> None:
argparse.Namespace(
packages=[],
requirements=[],
verbose=False,
debugging=False,
ignore_extra=False,
continue_on_fail=False,
exclude=[],
Expand All @@ -77,8 +77,8 @@ def test_parse_args_empty_args() -> None:
@pytest.mark.parametrize(
("args", "field"),
[
pytest.param(["--verbose"], "verbose", id="--verbose"),
pytest.param(["-v"], "verbose", id="-v"),
pytest.param(["--debug"], "debugging", id="--debug"),
pytest.param(["-d"], "debugging", id="-d"),
pytest.param(["--ignore-extra"], "ignore_extra", id="--ignore-extra"),
pytest.param(
["--continue-on-fail"],
Expand Down Expand Up @@ -410,7 +410,7 @@ def test_read_requirements(tmp_path: Path) -> None:
]


@pytest.mark.parametrize("arg", ["--verbose", "-v"])
@pytest.mark.parametrize("arg", ["--debug", "-d"])
def test_main_verbose_flag_sets_logger_level_to_debug(
dummy_dependencies: list[SimpleNamespace],
arg: str,
Expand Down Expand Up @@ -495,7 +495,7 @@ def test_main_warn_about_unrecognized_args_before_error_exit_when_no_packages_pr
), mock.patch(
"subprocess.call",
) as mock_subprocess_call:
exit_code: int = pip_purge.main(["-v", "-a", "-b", "-y"])
exit_code: int = pip_purge.main(["-d", "-a", "-b", "-y"])
assert (
"pip-purge",
30,
Expand Down Expand Up @@ -527,7 +527,7 @@ def test_main_warn_about_unrecognized_args(
), mock.patch(
"subprocess.call",
) as mock_subprocess_call:
exit_code: int = pip_purge.main(["package_a", "-v", "-a", "-b", "-y"])
exit_code: int = pip_purge.main(["package_a", "-d", "-a", "-b", "-y"])
assert (
"pip-purge",
30,
Expand Down
9 changes: 5 additions & 4 deletions tests/pip_review_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_constants(
def test_parse_args_empty_args() -> None:
assert pip_review._parse_args([]) == (
argparse.Namespace(
verbose=False,
debugging=False,
raw=False,
interactive=False,
auto=False,
Expand All @@ -79,8 +79,8 @@ def test_parse_args_empty_args() -> None:
@pytest.mark.parametrize(
("args", "field"),
[
pytest.param(["--verbose"], "verbose", id="--verbose"),
pytest.param(["-v"], "verbose", id="-v"),
pytest.param(["--debug"], "debugging", id="--debug"),
pytest.param(["-d"], "debugging", id="-d"),
pytest.param(["--raw"], "raw", id="--raw"),
pytest.param(["-r"], "raw", id="-r"),
pytest.param(["--interactive"], "interactive", id="--interactive"),
Expand Down Expand Up @@ -400,7 +400,7 @@ def test_format_table() -> None:
assert pip_review._format_table(test_columns) == expected_result


@pytest.mark.parametrize("arg", ["--verbose", "-v"])
@pytest.mark.parametrize("arg", ["--debug", "-d"])
def test_main_verbose_flag_sets_logger_level_to_debug(
sample_subprocess_output: bytes,
arg: str,
Expand Down Expand Up @@ -498,6 +498,7 @@ def test_main_warn_about_unrecognized_args(
*PIP_CMD,
"install",
"-U",
"-v",
"test1",
"test2",
]
Expand Down

0 comments on commit b2ccb37

Please sign in to comment.