diff --git a/setup.py b/setup.py index 1580b02..2e34eb0 100755 --- a/setup.py +++ b/setup.py @@ -68,6 +68,6 @@ 'check-python-versions = check_python_versions.cli:main', ], }, - install_requires=['pyyaml'], + install_requires=['pyyaml', 'python-active-versions>=1.3.0'], zip_safe=False, ) diff --git a/src/check_python_versions/cli.py b/src/check_python_versions/cli.py index 1ddc494..99370f5 100644 --- a/src/check_python_versions/cli.py +++ b/src/check_python_versions/cli.py @@ -14,6 +14,10 @@ from io import StringIO from typing import Callable, Collection, Dict, List, Optional, Tuple +from python_active_versions.python_active_versions import ( + get_active_python_versions, +) + from . import __version__ from .sources.all import ALL_SOURCES from .sources.base import SourceFile @@ -370,6 +374,9 @@ def _main() -> None: parser.add_argument('--skip-non-packages', action='store_true', help='skip arguments that are not Python packages' ' without warning about them') + parser.add_argument('--python-versions', action='store_true', + default=False, + help='show active python versions') parser.add_argument('--only', metavar='FILES', help='check only the specified files' ' (comma-separated list, e.g.' @@ -457,6 +464,14 @@ def _main() -> None: elif multiple: print("\n\nall ok!") + if args.python_versions and not (args.add or args.drop or args.update): + _python_versions = get_active_python_versions(docker_images=False, + log_level='WARNING') + print('\n\nActive Python versions:') + for _version in _python_versions: + print(f"version: {_version['version']} - " + f"latest software: {_version['latest_sw']}") + def main() -> None: """The main function. diff --git a/tests/test_cli_python_versions.py b/tests/test_cli_python_versions.py new file mode 100644 index 0000000..9966d8d --- /dev/null +++ b/tests/test_cli_python_versions.py @@ -0,0 +1,39 @@ +import sys +import textwrap + +from unittest.mock import Mock, patch + + +@patch('check_python_versions.cli.get_active_python_versions', + Mock(return_value=[{'version': '3.10', 'latest_sw': '3.10.5'}, + {'version': '3.11', 'latest_sw': '3.11.1'}]) + ) +def test_main_show_python(monkeypatch, tmp_path, capsys): + monkeypatch.setattr(sys, 'argv', [ + 'check-python-versions', + str(tmp_path), + '--python-versions', + '--only', 'setup.py', + ]) + setup_py = tmp_path / "setup.py" + setup_py.write_text(textwrap.dedent("""\ + from setuptools import setup + setup( + name='foo', + classifiers=[ + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.6', + ], + ) + """)) + import check_python_versions.cli as cpv + + cpv.main() + assert capsys.readouterr().out == textwrap.dedent("""\ + setup.py says: 2.7, 3.6 + + + Active Python versions: + version: 3.10 - latest software: 3.10.5 + version: 3.11 - latest software: 3.11.1 + """) diff --git a/tox.ini b/tox.ini index f1bd26f..5b0988a 100644 --- a/tox.ini +++ b/tox.ini @@ -32,6 +32,7 @@ skip_install = true deps = mypy types-pyyaml + python_active_versions commands = mypy src setup.py {posargs} # not checking tests/ because I don't want strict mode there, and mypy