-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function for reading and caching PEP 518 spec (#668)
- Loading branch information
Showing
7 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,5 @@ Author: Dirk Thomas <[email protected]> | |
+ # pytest-repeat | ||
+ # pytest-rerunfailures | ||
setuptools>=30.3.0 | ||
tomli>=1.0.0; python_version < "3.11" | ||
packages = find: | ||
zip_safe = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
} |