Skip to content

Implement VCS Tag Extraction: Git #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cppython/console/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

7 changes: 6 additions & 1 deletion cppython/console.py → cppython/console/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import tomlkit
from cppython_core.schema import GeneratorDataT, Interface, InterfaceConfiguration

from cppython.console.vcs.git import Git
from cppython.project import Project, ProjectConfiguration


Expand Down Expand Up @@ -54,7 +55,11 @@ def __init__(self):

configuration = InterfaceConfiguration()
self.interface = ConsoleInterface(configuration)
self.configuration = ProjectConfiguration(root_path=path)

# TODO: Don't assume git SCM. Implement importing and scm selection

version = Git().extract_version(path)
self.configuration = ProjectConfiguration(root_path=path, version=version.base_version)

def create_project(self) -> Project:
"""
Expand Down
1 change: 1 addition & 0 deletions cppython/console/vcs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

35 changes: 35 additions & 0 deletions cppython/console/vcs/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
TODO
"""


from abc import ABC, abstractmethod
from pathlib import Path

from packaging.version import Version


class VCS(ABC):
"""
Base class for version control systems
"""

subclasses = []

def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
cls.subclasses.append(cls)

@abstractmethod
def is_repository(self, path: Path) -> bool:
"""
TODO
"""
raise NotImplementedError()

@abstractmethod
def extract_version(self, path: Path) -> Version:
"""
TODO
"""
raise NotImplementedError()
43 changes: 43 additions & 0 deletions cppython/console/vcs/git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
TODO
"""

from pathlib import Path

from dulwich.porcelain import tag_list
from dulwich.repo import Repo
from packaging.version import Version

from cppython.console.vcs.base import VCS


class Git(VCS):
"""
Git implementation hooks
"""

def is_repository(self, path: Path) -> bool:
"""
TODO
"""

try:
Repo(str(path))
return True

except Exception:
return False

def extract_version(self, path: Path) -> Version:
"""
TODO
"""

repo = Repo(str(path))
tags = tag_list(repo)

try:
tag = tags[-1].decode("utf-8")
except Exception:
tag = "v0.1.0"
return Version(tag)
19 changes: 6 additions & 13 deletions cppython/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,21 @@ def validate_path(cls, values):
return None


@dataclass
class ProjectConfiguration:
class ProjectConfiguration(BaseModel):
"""
TODO
"""

root_path: Path # The path where the pyproject.toml lives
_verbosity: int = 0
version: str # The version number a 'dynamic' project version will resolve to
verbosity: int = 0

@property
def verbosity(self) -> int:
@validator("verbosity")
def min_max(cls, value):
"""
TODO
"""
return self._verbosity

@verbosity.setter
def verbosity(self, value: int) -> None:
"""
TODO
"""
self._verbosity = min(max(value, 0), 2)
return min(max(value, 0), 2)


class API:
Expand Down
55 changes: 54 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ dynamic = ["version"]
requires-python = ">=3.10"

dependencies = [
"click>=8.1.3",
"tomlkit>=0.10.2",
"cppython-core>=0.3.3.dev0",
"pydantic>=1.9.0",
"click>=8.1.3",
"tomlkit>=0.10.2",
"cppython-core>=0.3.3.dev0",
"pydantic>=1.9.0",
"dulwich>=0.20.42",
"packaging>=21.3",
]

[project.license-files]
Expand All @@ -44,7 +46,7 @@ test = [
]

[project.scripts]
cppython = "cppython.console:cli"
cppython = "cppython.console.interface:cli"

[tool.pytest.ini_options]
testpaths = [
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from cppython_core.schema import InterfaceConfiguration
from pytest_cppython.plugin import InterfaceIntegrationTests

from cppython.console import ConsoleInterface
from cppython.console.interface import ConsoleInterface


class TestCLIInterface(InterfaceIntegrationTests):
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pytest_cppython.plugin import InterfaceUnitTests
from pytest_mock.plugin import MockerFixture

from cppython.console import Config, ConsoleInterface, cli
from cppython.console.interface import Config, ConsoleInterface, cli
from cppython.schema import API

default_pep621 = PEP621(name="test_name", version="1.0")
Expand Down Expand Up @@ -56,7 +56,7 @@ def test_command(self, command: str, mocker: MockerFixture):
mocker.patch("cppython.project.Project.__init__", return_value=None)

# Patch the reading of data
mocker.patch("cppython.console._create_pyproject", return_value=default_pyproject)
mocker.patch("cppython.console.interface._create_pyproject", return_value=default_pyproject)

config = Config()

Expand Down
12 changes: 6 additions & 6 deletions tests/unit/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_construction(self, mocker: MockerFixture):
"""

interface_mock = mocker.MagicMock()
configuration = ProjectConfiguration(root_path=Path())
configuration = ProjectConfiguration(root_path=Path(), version="1.0.0")
Project(configuration, interface_mock, default_pyproject.dict(by_alias=True))


Expand All @@ -58,7 +58,7 @@ def test_plugin_gather(self):
TODO
"""

configuration = ProjectConfiguration(root_path=Path())
configuration = ProjectConfiguration(root_path=Path(), version="1.0.0")
builder = ProjectBuilder(configuration)
plugins = builder.gather_plugins(Generator)

Expand All @@ -69,7 +69,7 @@ def test_generator_data_construction(self, mocker: MockerFixture):
TODO
"""

configuration = ProjectConfiguration(root_path=Path())
configuration = ProjectConfiguration(root_path=Path(), version="1.0.0")
builder = ProjectBuilder(configuration)
model_type = builder.generate_model([])

Expand Down Expand Up @@ -97,7 +97,7 @@ def test_generator_creation(self, mocker: MockerFixture):
TODO
"""

configuration = ProjectConfiguration(root_path=Path())
configuration = ProjectConfiguration(root_path=Path(), version="1.0.0")
builder = ProjectBuilder(configuration)

generator_configuration = GeneratorConfiguration()
Expand All @@ -118,7 +118,7 @@ def test_presets(self, tmpdir):
"""

temporary_directory = Path(tmpdir)
configuration = ProjectConfiguration(root_path=temporary_directory)
configuration = ProjectConfiguration(root_path=temporary_directory, version="1.0.0")
builder = ProjectBuilder(configuration)

input_toolchain = temporary_directory / "input.cmake"
Expand Down Expand Up @@ -147,7 +147,7 @@ def test_root_unmodified(self, tmpdir):
"""

temporary_directory = Path(tmpdir)
configuration = ProjectConfiguration(root_path=temporary_directory)
configuration = ProjectConfiguration(root_path=temporary_directory, version="1.0.0")
builder = ProjectBuilder(configuration)

# TODO: Translate into reuseable testing data
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test_vcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
TODO
"""


from pathlib import Path

from cppython.console.vcs.git import Git


class TestGit:
"""
TODO
"""

def test_version(self):
"""
TODO
"""

directory = Path()

git = Git()

result = git.extract_version(directory)

assert result != ""