A library for parsing CMakePresets.json
Project description
CMakePresets 0.4.0
A Python library and CLI tool for working with CMakePresets.json configuration files in CMake projects.
About
CMakePresets is a utility that helps you inspect and work with CMake preset configurations. It provides both a Python API for programmatic access and a command-line interface for interactive use.
Features
- Parse and analyze CMakePresets.json files
- List all available presets of different types (configure, build, test, package, workflow)
- Show detailed information about specific presets
- Find related presets (e.g., build presets that use a specific configure preset)
- Display inheritance relationships between presets
- Output in different formats (rich tables, plain text, JSON)
Installation
pip install cmakepresets
CLI Usage
List all presets
cmakepresets --file CMakePresets.json list
cmakepresets --directory /path/to/project list
List specific types of presets
cmakepresets --file CMakePresets.json list --type configure
Show details of a specific preset
cmakepresets --file CMakePresets.json show my-preset
cmakepresets --file CMakePresets.json show my-preset --type configure
Show in JSON format
cmakepresets --file CMakePresets.json show my-preset --json
Find related presets
cmakepresets --file CMakePresets.json related my-configure-preset
cmakepresets --file CMakePresets.json related my-configure-preset --type build
Script-friendly output
cmakepresets --file CMakePresets.json related my-configure-preset --plain
Python API
>>> # Set up the test environment (only needed for doctest)
>>> import os
>>> import sys
>>> sys.path.insert(0, '.')
>>> # Create a proper test environment
>>> from tests.decorators import CMakePresets_json
>>>
>>> # Create test preset content
>>> preset_content = '''{
... "version": 4,
... "cmakeMinimumRequired": {"major": 3, "minor": 23, "patch": 0},
... "configurePresets": [
... {
... "name": "base",
... "generator": "Ninja",
... "binaryDir": "${sourceDir}/build/${presetName}",
... "hidden": true
... },
... {
... "name": "my-config",
... "inherits": "base",
... "cacheVariables": {
... "CMAKE_BUILD_TYPE": "Debug"
... }
... }
... ],
... "buildPresets": [
... {
... "name": "my-build",
... "configurePreset": "my-config"
... }
... ]
... }'''
>>>
>>> # Set up the test environment
>>> patcher = CMakePresets_json(preset_content)
>>> test_env = patcher.__enter__() # This creates a fake filesystem with CMakePresets.json
>>> ###################################################################
>>> # Now we can import and use CMakePresets normally as in real code #
>>> ###################################################################
>>> # Python API Examples
>>> ####
>>> from cmakepresets import CMakePresets
>>> from cmakepresets.constants import CONFIGURE, PACKAGE
>>> # Load presets from a file (uses the fake filesystem)
>>> presets = CMakePresets("CMakePresets.json")
>>> print(len(presets.configure_presets))
2
>>> # Or load from a project directory
>>> presets = CMakePresets(".")
>>> print(len(presets.build_presets))
1
>>> # Access preset collections
>>> configure_presets = presets.configure_presets
>>> # List names of all configure presets
>>> [preset["name"] for preset in configure_presets]
['base', 'my-config']
>>> # Get related prests to the configurePreset 'my-config'
>>> related = presets.find_related_presets("my-config")
>>> print(related)
{'build': [{'name': 'my-build', 'configurePreset': 'my-config'}], 'test': [], 'package': []}
>>> # Get related packagePrests to 'my-config'
>>> related = presets.find_related_presets("my-config", PACKAGE)
>>> print(len(related[PACKAGE]))
0
>>> # Get a specific preset by name
>>> my_config = presets.get_preset_by_name(CONFIGURE, "my-config")
>>> my_config["name"]
'my-config'
>>> # Get flattened preset with all inherited properties resolved
>>> flattened = presets.flatten_preset(CONFIGURE, "my-config")
>>> # Print the original preset
>>> print(my_config)
{'name': 'my-config', 'inherits': 'base', 'cacheVariables': {'CMAKE_BUILD_TYPE': 'Debug'}}
>>> # Compared to flattened
>>> print(flattened)
{'name': 'my-config', 'generator': 'Ninja', 'binaryDir': '${sourceDir}/build/${presetName}', 'cacheVariables': {'CMAKE_BUILD_TYPE': 'Debug'}}
>>> # Get flattened preset with "pseudo" resolved macros
>>> resolved = presets.resolve_macro_values(CONFIGURE, "my-config")
>>> print(resolved)
{'name': 'my-config', 'generator': 'Ninja', 'binaryDir': '/home/user/project/build/my-config', 'cacheVariables': {'CMAKE_BUILD_TYPE': 'Debug'}}
>>> # Clean up test environment (important to avoid resource leaks)
>>> patcher.__exit__(None, None, None)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cmakepresets-0.4.0.tar.gz.
File metadata
- Download URL: cmakepresets-0.4.0.tar.gz
- Upload date:
- Size: 87.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f8351b9a98a61d8ecb9bd41dfbfd7007b95956af9773ec8ce0759e8d0e2f4d9
|
|
| MD5 |
a9dfff258e37c74d98b7ee4b8fc346ad
|
|
| BLAKE2b-256 |
2073c062431c7aeea46c32168be04ef5e887bc943e1aba981510aebe3af900c6
|
File details
Details for the file cmakepresets-0.4.0-py3-none-any.whl.
File metadata
- Download URL: cmakepresets-0.4.0-py3-none-any.whl
- Upload date:
- Size: 25.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
086d912d25b9e1b795ab69f4e752cfdc385dd4a1ffa6172ff410a9556b0db8cd
|
|
| MD5 |
a3a73b310c8201d196d7db450c21f252
|
|
| BLAKE2b-256 |
c9cd3dc3c0f082fe5e3a3837e581212f3fe3bbe4f238f1ac0d647fe048e89459
|