Document API and move out of alpha

- Add doc/API.md API overview. Some sort of mkdocstrings/sphinx setup is
  planned.
- Update REAMDE and pyproject.toml classifiers saying fedrq is beta
  software.
- Add API examples.

Implements: https://todo.sr.ht/~gotmax23/fedrq/14
This commit is contained in:
Maxwell G 2023-02-19 12:01:23 -06:00
commit 61d8fef04b
Signed by: gotmax23
GPG key ID: F79E4E25E8C661F8
5 changed files with 256 additions and 5 deletions

View file

@ -52,12 +52,22 @@ as it needs to find dnf.
`fedrq check-config --dump` requires `tomli-w`.
The RPM package weakly depends on `python3-tomli-w`.
## Versioning
This project is in alpha. `fedrq.cli.*` is internal to this module. A stable
API for `fedrq.repoquery` and `fedrq.config` may be provided down the line. The
CLI interface is also subject to small changes.
This project is in beta and its versioning scheme follows semver.
## Python API
The `fedrq.config` module and the `fedrq.backend` package are public API.
Everything under `fedrq.cli` is internal. fedrq is a primarily a CLI tool and
its API abstracts the specific dnf/libdnf5 functionality that it uses. The
API's main purpose is repoquerying, but you can use the fedrq functionality
you'd like and then access the underlying dnf Base object to preform other
tasks if needed.
See [API.md] for more information. See [api-examples] for some example code. As
always, direct any feedback, questions, or issues to the mailing list (see ##
Contributing).
## Documentaion
@ -103,7 +113,9 @@ SPDX-License-Identifier: GPL-2.0-or-later AND Unlicense AND MIT AND PSF-2.0
```
[API.md]: https://git.sr.ht/~gotmax23/fedrq/tree/main/item/doc/API.md
[CONTRIBUTING.md]: https://git.sr.ht/~gotmax23/fedrq/tree/main/item/CONTRIBUTING.md
[api-examples]: https://git.sr.ht/~gotmax23/fedrq/tree/main/item/contrib/api_examples
[link-hub]: https://sr.ht/~gotmax23/fedrq
[badge-hub]: https://img.shields.io/badge/Project%20Hub-fedrq-blue?logo=data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjxzdmcKICAgdmlld0JveD0iMCAwIDUxMiA1MTIiCiAgIHZlcnNpb249IjEuMSIKICAga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emVkPSIxIgogICAgIGlua3NjYXBlOmN1cnJlbnQtbGF5ZXI9InN2ZzUxIiAvPgogIDxwYXRoCiAgICAgZD0iTTI1NiA4QzExOSA4IDggMTE5IDggMjU2czExMSAyNDggMjQ4IDI0OCAyNDgtMTExIDI0OC0yNDhTMzkzIDggMjU2IDh6bTAgNDQ4Yy0xMTAuNSAwLTIwMC04OS41LTIwMC0yMDBTMTQ1LjUgNTYgMjU2IDU2czIwMCA4OS41IDIwMCAyMDAtODkuNSAyMDAtMjAwIDIwMHoiCiAgICAgaWQ9InBhdGg0OSIKICAgICBzdHlsZT0iZmlsbDojZmZmZmZmIiAvPgo8L3N2Zz4K&style=for-the-badge
[link-repo]: https://git.sr.ht/~gotmax23/fedrq

View file

@ -0,0 +1,25 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: Unlicense
# SPDX-FileCopyrightText: None
# Roughly equivalent to:
# fedrq whatrequires --arch=noarch -b rawhide -r buildroot bash | grep '^a'
from fedrq.config import get_config, RQConfig
from fedrq.backends.base import RepoqueryBase
# Load config from filesystem and override some options
config: RQConfig = get_config(backend="libdnf5")
# Query the Fedora Rawhide koji buildroot repositories
# This supports any release configuration builtin to fedrq
# or configured on your local system.
rq: RepoqueryBase = config.get_rq("rawhide", "buildroot")
# Get all noarch packages that start with 'a' and depend on bash
query = rq.query(
name__glob="a*", arch="noarch", requires=rq.query(name="bash", arch="notsrc")
)
# By using sorted(), you'll get (relatively) consistent ordering between backends
for package in sorted(query):
print(package)

View file

@ -0,0 +1,82 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: Unlicense
# SPDX-FileCopyrightText: None
"""
This is a commented, fully typed example script showing fedrq's API.
It searches the Fedora repositories for packages that haven't been rebuilt
in the last few releases (i.e. Long Term FTBFS packages).
Based on https://github.com/hroncok/fedora-report-ftbfs-retirements.
"""
from __future__ import annotations
import argparse
from fedrq.backends.base import PackageCompat, PackageQueryCompat, RepoqueryBase
from fedrq.config import RQConfig, get_config
DEFAULT_RELEASES = ".fc36", ".fc37", ".fc38", ".fc39"
DEFAULT_IGNORES = (
"dummy-test-package",
"fedora-obsolete-packages",
"fedora-release",
"fedora-repos",
"generic-release",
"shim",
)
def parseargs() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"-R",
"--releases",
nargs="*",
help=f"DEFAULT: {DEFAULT_RELEASES}",
default=DEFAULT_RELEASES,
)
args = parser.parse_args()
return args
def main(*, releases: list[str]):
# get_config() loads the configuration from the filesystem as explained in
# fedrq(5).
# Extra kwargs may be passed to get_config() to override certain values.
# For example, you can pass `backend="libdnf5"` if you'd like to change the
# default backend.
config: RQConfig = get_config(backend="libdnf5")
# Query the Fedora Rawhide mirrored repositories.
# This supports any release configuration builtin to fedrq
# or configured on your local system.
rq: RepoqueryBase = config.get_rq("rawhide")
# PackageQueryCompat is a Protocol implemented by both backends.
# It is an iterable of PackageCompat objects with extra methods to
# filter packages and preform other similar tasks.
#
# rq.query() creates a PackageQuery instance and calls its filterm() method
# with the provided kwargs.
# This filters out all src packages
query: PackageQueryCompat = rq.query(arch="src")
# PackageCompat is a Protocol implemented by both the dnf and libdnf5
# backends.
packages: list[PackageCompat] = []
for package in query:
# fmt: off
if (
not any(package.name.startswith(i) for i in DEFAULT_IGNORES) and
not any(i in package.release for i in releases)
):
packages.append(package)
# fmt: on
for p in packages:
print(p.name)
if __name__ == "__main__":
main(**vars(parseargs()))

132
doc/API.md Normal file
View file

@ -0,0 +1,132 @@
<!--
SPDX-FileCopyrightText: 2023 Maxwell G <gotmax@e.email>
SPDX-License-Identifier: GPL-2.0-or-later
-->
# fedrq's Python API
The `fedrq.config` module and the `fedrq.backend` package are public API.
Everything under `fedrq.cli` is internal. fedrq is a primarily a CLI tool and
its API abstracts the specific dnf/libdnf5 functionality that it uses. Its main
purpose is repoquerying, but you can use the fedrq functionality you want and
then access the underlying dnf Base object to preform other tasks if needed.
Take a look at the docstrings for more information.
## fedrq.backends
This is the heart of fedrq's API. There is a dnf backend (`fedrq.backends.dnf`)
and a libdnf5 backend (`fedrq.backends.libdnf5`). This package provides an
interface to configure a Base session, load repositories, preform queries, and
related functionality. The main primitives are:
### BaseMaker
**Base class**: `fedrq.backends.base.BaseMakerBase`
**dnf backend:** `fedrq.backends.dnf.backend.BaseMaker`
**libdnf5 backend:** `fedrq.backends.libdnf5.backend.BaseMaker`
`BaseMaker` allows configuring a dnf Base session and loading repositories.
### Repoquery
**Base class**: `fedrq.backends.base.RepoqueryBase`
**dnf backend:** `fedrq.backends.dnf.backend.Repoquery`
**libdnf5 backend:** `fedrq.backends.libdnf5.backend.Repoquery`
`BaseMaker` takes an initialized Base object (see `BaseMaker`) and allows
preforming a large range of queries. Most of its methods return
`PackageQueryCompat` or `PackageCompat` objects.
### PackageQueryCompat
**Protocol:** `fedrq.backends.base.PackageCompat`
**dnf backend:** `fedrq.backends.dnf.backend.PackageQuery` -> `hawkey.Query`
**libdnf5 backend:** `fedrq.backends.libdnf5.backend.Package`
(`libdnf5.package.Package` subclass)
PackageQueryCompat is a set like object of PackageCompat objects. It contains
methods to filter its Packages based on certain criteria. Typically, you'd
access these through the Repoquery class.
- `fedrq.backends.libdnf5.backend.PackageQuery` - subclass of
`libdnf5.rpm.PackageQuery`. adds back missing `query` and `querym` methods.
### PackageCompat
**Protocol:** `fedrq.backends.base.PackageCompat`
**dnf backend:** `fedrq.backends.dnf.backend.Package` -> `dnf.package.Package`
**libdnf5 backend:** `fedrq.backends.libdnf5.backend.Package`
(`libdnf5.package.Package` subclass)
- `fedrq.backends.libdnf5.backend.Package` - subclass of `libdnf5.rpm.Package`
that implements missing functionality and compatability with
`dnf.package.Package`. includes properties to access Package attributes.
These properties were removed from libdnf5 in favor of `get_foo()` methods.
It also includes rich comparison support (`__lt__`, `__gt__`, etc.) and
implements roughly the same sort order as the dnf backend and adds a
`__hash__()` method so it can e.g. be used in a set or as a dictionary key.
Importing `fedrq.backends.libdnf5.backend` registers the `Package` subclass
so PackageQuery contains our subclass.
## fedrq.config
Most of the code here should not be called directly. Use `get_config()` to load
the configuration from the filesystem. Create an `RQConfig` object manually if
you must.
This exaple shows how to load the configuration and preform a basic query.
``` python
# SPDX-License-Identifier: Unlicense
# SPDX-FileCopyrightText: None
# Roughly equivalent to:
# fedrq whatrequires --arch=noarch -b rawhide -r buildroot bash | grep '^a'
from fedrq.config import get_config, RQConfig
from fedrq.backends.base import RepoqueryBase
# Load config from filesystem and override some options
config: RQConfig = get_config(backend="libdnf5")
# Query the Fedora Rawhide koji buildroot repositories
# This supports any release configuration builtin to fedrq
# or configured on your local system.
rq: RepoqueryBase = config.get_rq("rawhide", "buildroot")
# Get all noarch packages that start with 'a' and depend on bash
query = rq.query(
name__glob="a*", arch="noarch", requires=rq.query(name="bash", arch="notsrc")
)
# By using sorted(), you'll get (relatively) consistent ordering between backends
for package in sorted(query):
print(package)
```
## Examples
See [api-examples] for some simple example code.
Real world examples:
- [mkblocker.py] - given a list of source packages names, use jinja2 to
template a specfile that Conflicts on every subpackage produced by the source
packages. This was used as part of the [Mass_Retire_Golang_Leaves] Fedora Change.
[api-examples]: https://git.sr.ht/~gotmax23/fedrq/tree/main/item/contrib/api_examples
[mkblocker.py]: https://git.sr.ht/~gotmax23/fedora-scripts/tree/main/item/go-sig/blocker/mkblocker.py
[Mass_Retire_Golang_Leaves]: https://fedoraproject.org/wiki/Changes/Mass_Retire_Golang_Leaves#Implementation

View file

@ -24,7 +24,7 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Operating System :: POSIX :: Linux",
"Development Status :: 3 - Alpha",
"Development Status :: 4 - Beta",
"Typing :: Typed",
"Environment :: Console",
]