diff --git a/README.md b/README.md index 0788b78..d16ba34 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ # pinfo + +## Detect minimum version of python + +```python +from ppinfo import Project +p = Project(".") +print(p.min_python) # value from your setup.cfg +``` diff --git a/src/ppinfo/__init__.py b/src/ppinfo/__init__.py index 3dd0239..a9310f9 100644 --- a/src/ppinfo/__init__.py +++ b/src/ppinfo/__init__.py @@ -1 +1,29 @@ """ppinfo package.""" +import configparser +import os +from pathlib import Path +from typing import Union + + +# pylint: disable=too-few-public-methods +class Project: + """Class representing a python project.""" + + # 3.7 default value is used because at the time this library was + # written, this was the oldest still supported version of Python + min_python: str = "3.7" + + def __init__(self, path: Union[Path, str] = Path(".")) -> None: + """Construct a python Project instance.""" + if isinstance(path, str): + path = Path(path) + self.path = path + + if (path / "setup.cfg").exists(): + config = configparser.ConfigParser() + config.read(path / "setup.cfg") + if "options" in config: + value = config["options"].get("python_requires", None) + if not (value and value.startswith(">=")): + raise NotImplementedError("Unable to parse python_requires") + self.min_python = value[2:] diff --git a/test/fixtures/proj1/setup.cfg b/test/fixtures/proj1/setup.cfg new file mode 100644 index 0000000..4ee2701 --- /dev/null +++ b/test/fixtures/proj1/setup.cfg @@ -0,0 +1 @@ +# empty setup.cfg file diff --git a/test/fixtures/py310/setup.cfg b/test/fixtures/py310/setup.cfg new file mode 100644 index 0000000..bee13c8 --- /dev/null +++ b/test/fixtures/py310/setup.cfg @@ -0,0 +1,2 @@ +[options] +python_requires = >=3.10 diff --git a/test/test_ppinfo.py b/test/test_ppinfo.py index 0a3903c..9fc1727 100644 --- a/test/test_ppinfo.py +++ b/test/test_ppinfo.py @@ -1,7 +1,14 @@ """Tests for ppinfo package.""" +import pytest +from ppinfo import Project -def test_import() -> None: - """Test that we can import ppinfo.""" - # pylint: disable=unused-import,import-outside-toplevel - import ppinfo + +@pytest.mark.parametrize( + ("path", "expected_python"), + [("test/fixtures/proj1", "3.7"), ("test/fixtures/py310", "3.10")], +) +def test_min_version(path: str, expected_python: str) -> None: + """Test ability to find minimal version of python required.""" + project = Project(path) + assert project.min_python == expected_python