|
2 | 2 |
|
3 | 3 | Python library for parsing and manipulating RPM spec files. Main focus is on modifying existing spec files, any change should result in a minimal diff. |
4 | 4 |
|
5 | | -This project is still a work in progress. |
6 | | - |
7 | 5 | ## Motivation |
8 | 6 |
|
9 | 7 | Originally, [rebase-helper](https://github.com/rebase-helper/rebase-helper/) provided an API for spec file modifications that was also used by [packit](https://github.com/packit/packit). The goal of this project is to make the interface more general and convenient to use by not only packit but also by other Python projects that need to interact with RPM spec files. |
@@ -252,11 +250,43 @@ print(tags.release.expanded_value) |
252 | 250 | print(len(specfile.sources().content)) |
253 | 251 | ``` |
254 | 252 |
|
255 | | -## Caveats |
| 253 | +### Validity |
| 254 | + |
| 255 | +Macro definitions, tags, `%sourcelist`/`%patchlist` entries and sources/patches have a `valid` attribute. An entity is considered valid if it isn't present in a false branch of any condition. |
| 256 | + |
| 257 | +Consider the following in a spec file: |
| 258 | + |
| 259 | +```specfile |
| 260 | +%if 0%{?fedora} >= 36 |
| 261 | +Recommends: %{name}-selinux |
| 262 | +%endif |
| 263 | +``` |
| 264 | + |
| 265 | +Provided there are no other `Recommends` tags, the following would print `True` or `False` depending on the value of the `%fedora` macro: |
| 266 | + |
| 267 | +```python |
| 268 | +with specfile.tags() as tags: |
| 269 | + print(tags.recommends.valid) |
| 270 | +``` |
| 271 | + |
| 272 | +You can define macros or redefine/undefine system macros using the `macros` argument of the constructor or by modifying the `macros` attribute of a `Specfile` instance. |
| 273 | + |
| 274 | +The same applies to `%ifarch`/`%ifos` statements: |
| 275 | + |
| 276 | +```specfile |
| 277 | +%ifarch %{java_arches} |
| 278 | +BuildRequires: java-devel |
| 279 | +%endif |
| 280 | +``` |
256 | 281 |
|
257 | | -### RPM macros |
| 282 | +Provided there are no other `BuildRequires` tags, the following would print `True` in case the current platform was part of `%java_arches`: |
| 283 | + |
| 284 | +```python |
| 285 | +with specfile.tags() as tags: |
| 286 | + print(tags.buildrequires.valid) |
| 287 | +``` |
258 | 288 |
|
259 | | -specfile uses RPM for parsing spec files and macro expansion. Unfortunately, macros are always stored in a global context, which poses a problem for multiple instances of Specfile. |
| 289 | +To override this, you would have to redefine the `%_target_cpu` system macro (or `%_target_os` in case of `%ifos`). |
260 | 290 |
|
261 | 291 | ## Videos |
262 | 292 |
|
|
0 commit comments