Skip to content

Commit

Permalink
feat(projector): expose some apis
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon committed Dec 13, 2024
1 parent aa4eedd commit 7f0906e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 61 deletions.
94 changes: 94 additions & 0 deletions python-projector/src/python_projector/cli/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import tomllib
from os import PathLike
from pathlib import Path


class InvalidConfigError(Exception):
pass


def get_min_python_version(
project_dir: str | PathLike | None = None,
):
"""
Get the minimum python version from the pyproject.toml file.
"""
import tomllib

from python_projector.utils.files import find_pyproject_toml
from python_projector.utils.version import min_version_requires_python

pyproject_toml = find_pyproject_toml(project_dir)
with pyproject_toml.open("rb") as f:
pyproject = tomllib.load(f)

try:
version_range = pyproject["project"]["requires-python"]
except KeyError as e:
raise InvalidConfigError(
"Missing key project.requires-python in pyproject.toml"
) from e
min_version = min_version_requires_python(version_range)
return min_version


def get_src_dir(project_dir: str | PathLike | None = None):
"""
Print the `src/` directory based on the pyproject.toml file.
"""
from python_projector.utils.files import find_pyproject_toml, get_src_dir

pyproject_toml = find_pyproject_toml(project_dir)
src_directory = get_src_dir(pyproject_toml)

return src_directory


def get_versioneer_version(
project_dir: str | PathLike | None = None,
*,
chrome_compatible: bool = False,
):
"""
Similar to `versioneer.get_version()`, but with more options.
"""
import versioneer

from python_projector.utils.files import find_pyproject_toml
from python_projector.utils.version import (
versioneer_render_chrome_ext_compat_version,
)

pyproject_toml = find_pyproject_toml(project_dir)
with pyproject_toml.open("rb") as f:
pyproject = tomllib.load(f)

if project_dir is None:
project_dir = pyproject_toml.parent
elif isinstance(project_dir, str):
project_dir = Path(project_dir)

try:
tag_prefix = pyproject["tool"]["versioneer"]["tag_prefix"] # usually "v"
except KeyError as e:
raise InvalidConfigError(
f"Missing key tool.versioneer.tag_prefix in {pyproject_toml}"
) from e

try:
style = pyproject["tool"]["versioneer"]["style"] # pep440
except KeyError as e:
raise InvalidConfigError(
f"Missing key tool.versioneer.style in {pyproject_toml}"
) from e

pieces = versioneer.git_pieces_from_vcs(
tag_prefix=tag_prefix, root=project_dir, verbose=True
)

if chrome_compatible or style == "chrome-ext":
version = versioneer_render_chrome_ext_compat_version(pieces=pieces)
else:
version = versioneer.render(pieces=pieces, style=style)["version"]

return version
66 changes: 9 additions & 57 deletions python-projector/src/python_projector/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,19 @@ def get_min_python_version(
"""
Get the minimum python version from the pyproject.toml file.
"""
import tomllib
from . import api

from python_projector.utils.files import find_pyproject_toml
from python_projector.utils.version import min_version_requires_python

pyproject_toml = find_pyproject_toml(project_dir)
with pyproject_toml.open("rb") as f:
pyproject = tomllib.load(f)

try:
version_range = pyproject["project"]["requires-python"]
except KeyError as e:
raise InvalidConfigError(
"Missing key project.requires-python in pyproject.toml"
) from e
min_version = min_version_requires_python(version_range)
print(min_version)
print(api.get_min_python_version(project_dir))


@app.command()
def get_src_dir(project_dir: Annotated[Path | None, typer.Argument()] = None):
"""
Print the `src/` directory based on the pyproject.toml file.
"""
from python_projector.utils.files import find_pyproject_toml, get_src_dir
from . import api

pyproject_toml = find_pyproject_toml(project_dir)
src_directory = get_src_dir(pyproject_toml)

print(src_directory)
print(api.get_src_dir(project_dir))


@app.command()
Expand Down Expand Up @@ -230,45 +213,14 @@ def get_versioneer_version(
"""
Similar to `versioneer.get_version()`, but with more options.
"""
import versioneer

from python_projector.utils.files import find_pyproject_toml
from python_projector.utils.version import (
versioneer_render_chrome_ext_compat_version,
)
from . import api

pyproject_toml = find_pyproject_toml(project_dir)
with pyproject_toml.open("rb") as f:
pyproject = tomllib.load(f)

if project_dir is None:
project_dir = pyproject_toml.parent

try:
tag_prefix = pyproject["tool"]["versioneer"]["tag_prefix"] # usually "v"
except KeyError as e:
raise InvalidConfigError(
f"Missing key tool.versioneer.tag_prefix in {pyproject_toml}"
) from e

try:
style = pyproject["tool"]["versioneer"]["style"] # pep440
except KeyError as e:
raise InvalidConfigError(
f"Missing key tool.versioneer.style in {pyproject_toml}"
) from e

pieces = versioneer.git_pieces_from_vcs(
tag_prefix=tag_prefix, root=project_dir, verbose=True
print(
api.get_versioneer_version(
project_dir=project_dir, chrome_compatible=chrome_compatible
)
)

if chrome_compatible or style == "chrome-ext":
version = versioneer_render_chrome_ext_compat_version(pieces=pieces)
else:
version = versioneer.render(pieces=pieces, style=style)["version"]

print(version)


def main():
app()
Expand Down
8 changes: 4 additions & 4 deletions python-projector/src/python_projector/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def find_root_dir_with_file(
raise FileNotFoundError(f"File {marker} not found in any parent directory")


def find_pyproject_toml(source_dir: str | PathLike | None = None) -> Path:
def find_pyproject_toml(project_dir: str | PathLike | None = None) -> Path:
"""
Find the pyproject.toml file in the current directory or any parent directory.
"""
if source_dir is None:
source_dir = Path.cwd()
return find_root_dir_with_file(source_dir, "pyproject.toml") / "pyproject.toml"
if project_dir is None:
project_dir = Path.cwd()
return find_root_dir_with_file(project_dir, "pyproject.toml") / "pyproject.toml"


def get_src_dir(pyproject_toml_path: str | PathLike | None) -> Path:
Expand Down

0 comments on commit 7f0906e

Please sign in to comment.