Skip to content

Commit

Permalink
Add function for reading and caching PEP 518 spec
Browse files Browse the repository at this point in the history
  • Loading branch information
cottsay committed Sep 20, 2024
1 parent fbbb8af commit d0e2336
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
Empty file.
49 changes: 49 additions & 0 deletions colcon_core/python_project/spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2022 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

try:
# Python 3.11+
from tomllib import load as toml_load
except ImportError:
from tomli import load as toml_load


SPEC_NAME = 'pyproject.toml'

_DEFAULT_BUILD_SYSTEM = {
'build-backend': 'setuptools.build_meta:__legacy__',
'requires': ['setuptools >= 40.8.0', 'wheel'],
}


def load_spec(project_path):
"""
Load build system specifications for a Python project.
:param project_path: Path to the root directory of the project
"""
spec_file = project_path / SPEC_NAME
try:
with spec_file.open('rb') as f:
spec = toml_load(f)
except FileNotFoundError:
spec = {}

spec.setdefault('build-system', _DEFAULT_BUILD_SYSTEM)

return spec


def load_and_cache_spec(desc):
"""
Get the cached spec for a package descriptor.
If the spec has not been loaded yet, load and cache it.
:param desc: The package descriptor
"""
spec = desc.metadata.get('python_project_spec')
if spec is None:
spec = load_spec(desc.path)
desc.metadata['python_project_spec'] = spec
return spec
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ install_requires =
pytest-repeat
pytest-rerunfailures
setuptools>=30.3.0
tomli>=1.0.0;python_version < "3.11"
packages = find:
zip_safe = false

Expand Down
5 changes: 5 additions & 0 deletions test/spell_check.words
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ apache
argparse
asyncio
autouse
backend
backported
basepath
bazqux
Expand Down Expand Up @@ -85,6 +86,7 @@ prepending
proactor
purelib
pydocstyle
pyproject
pytest
pytests
pythondontwritebytecode
Expand Down Expand Up @@ -136,6 +138,9 @@ testsuite
thomas
tmpdir
todo
toml
tomli
tomllib
traceback
tryfirst
tuples
Expand Down
53 changes: 53 additions & 0 deletions test/test_pyproject_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2024 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

from colcon_core.package_descriptor import PackageDescriptor
from colcon_core.python_project.spec import load_and_cache_spec


def test_pyproject_missing(tmp_path):
desc = PackageDescriptor(tmp_path)

spec = load_and_cache_spec(desc)
assert spec.get('build-system') == {
'build-backend': 'setuptools.build_meta:__legacy__',
'requires': ['setuptools >= 40.8.0', 'wheel'],
}


def test_pyproject_empty(tmp_path):
desc = PackageDescriptor(tmp_path)

(tmp_path / 'pyproject.toml').write_text('')

spec = load_and_cache_spec(desc)
assert spec.get('build-system') == {
'build-backend': 'setuptools.build_meta:__legacy__',
'requires': ['setuptools >= 40.8.0', 'wheel'],
}


def test_specified(tmp_path):
desc = PackageDescriptor(tmp_path)

(tmp_path / 'pyproject.toml').write_text('\n'.join((
'[build-system]',
'build-backend = "my_build_backend.meta"',
'requires = ["my-build-backend"]',
)))

spec = load_and_cache_spec(desc)
assert spec.get('build-system') == {
'build-backend': 'my_build_backend.meta',
'requires': ['my-build-backend'],
}

# truncate the pyproject.toml and call again
# this verifies that the spec is cached
(tmp_path / 'pyproject.toml').write_text('')

spec = load_and_cache_spec(desc)
assert spec.get('build-system') == {
'build-backend': 'my_build_backend.meta',
'requires': ['my-build-backend'],
}

0 comments on commit d0e2336

Please sign in to comment.