|
1 | 1 | # License: MIT |
2 | 2 | # Copyright © 2023 Frequenz Energy-as-a-Service GmbH |
3 | 3 |
|
4 | | -"""Frequenz project setup tools and common configuration.""" |
| 4 | +"""Frequenz project setup tools and common configuration. |
5 | 5 |
|
6 | | -from . import nox |
| 6 | +The tools are provided to configure 4 main types of repositories: |
| 7 | +
|
| 8 | +- APIs (api) |
| 9 | +- Actors (actor) |
| 10 | +- Applications (app) |
| 11 | +- Libraries (lib) |
| 12 | +
|
| 13 | +# Common |
| 14 | +
|
| 15 | +## `nox` |
| 16 | +
|
| 17 | +Projects wanting to use `nox` to run lint checkers and other utilities can use |
| 18 | +the [`frequenz.repo.config.nox`][] package. |
| 19 | +
|
| 20 | +Make sure to add this package as `dev` dependency to your project's |
| 21 | +`pyproject.tom` file, for example: |
| 22 | +
|
| 23 | +```toml |
| 24 | +[project.optional-dependencies] |
| 25 | +nox = [ |
| 26 | + "nox == 2022.11.21", |
| 27 | + "frequenz-repo-config[lib] == 0.1.0", |
| 28 | +] |
| 29 | +``` |
| 30 | +
|
| 31 | +Please note the `lib` optional dependency. Make sure you specify the correct |
| 32 | +one based on the type of your project (`api`, `actor`, `app`, `lib`). Also make |
| 33 | +sure to adjust the versions. |
| 34 | +
|
| 35 | +When writing the `noxfile.py` you should import the `nox` module from this |
| 36 | +package and use the [`frequenz.repo.config.nox.configure`][] function, |
| 37 | +which will configure all nox sessions. |
| 38 | +
|
| 39 | +You should call `configure()` using one of the default configurations provided |
| 40 | +in the [`frequenz.repo.config.nox.default`][] module. For example: |
| 41 | +
|
| 42 | +```python |
| 43 | +from frequenz.repo import config |
| 44 | +
|
| 45 | +config.nox.configure(config.nox.default.lib_config) |
| 46 | +``` |
| 47 | +
|
| 48 | +Again, make sure to pick the correct default configuration based on the type of |
| 49 | +your project (`api_config`, `actor_config`, `app_config`, `lib_config`). |
| 50 | +
|
| 51 | +If you need to modify the configuration, you can copy one of the default |
| 52 | +configurations by using the |
| 53 | +[`copy()`][frequenz.repo.config.nox.config.Config.copy] method: |
| 54 | +
|
| 55 | +```python |
| 56 | +from frequenz.repo import config |
| 57 | +
|
| 58 | +conf = config.nox.default.lib_config.copy() |
| 59 | +conf.opts.black.append("--diff") |
| 60 | +config.nox.configure(conf) |
| 61 | +``` |
| 62 | +
|
| 63 | +If you need further customization or to define new sessions, you can use the |
| 64 | +following modules: |
| 65 | +
|
| 66 | +- [`frequenz.repo.config.nox.config`][]: Low-level utilities to configure nox |
| 67 | + sessions. It defines the `Config` and CommandsOptions` classes and the actual |
| 68 | + implementation of the `configure()` function. It also defines the `get()` |
| 69 | + function, which can be used to get the currently used configuration object. |
| 70 | +
|
| 71 | +- [`frequenz.repo.config.nox.session`][]: Predefined nox sessions. These are |
| 72 | + the sessions that are used by default. |
| 73 | +
|
| 74 | +- [`frequenz.repo.config.nox.util`][]: General purpose utility functions. |
| 75 | +
|
| 76 | +
|
| 77 | +# APIs |
| 78 | +
|
| 79 | +## `setuptools` gRPC support |
| 80 | +
|
| 81 | +When configuring APIs it is assumed that they have a gRPC interface. |
| 82 | +
|
| 83 | +Normally Frequenz APIs use basic types from |
| 84 | +[`google/api-common-protos`](https://github.com/googleapis/api-common-protos), |
| 85 | +so you need to make sure the proper submodule is added to your project: |
| 86 | +
|
| 87 | +```sh |
| 88 | +mkdir submodules |
| 89 | +git submodule add https://github.com:googleapis/api-common-protos.git submodules/api-common-protos |
| 90 | +git commit -m "Add Google api-common-protos submodule" submodules/api-common-protos |
| 91 | +``` |
| 92 | +
|
| 93 | +Then you need to create a `setup.py` file with the following content: |
| 94 | +
|
| 95 | +```py |
| 96 | +import setuptools |
| 97 | +
|
| 98 | +from frequenz.repo.config.setuptools import grpc_tools |
| 99 | +
|
| 100 | +if __name__ == "__main__": |
| 101 | + setuptools.setup(cmdclass=grpc_tools.build_proto_cmdclass()) |
| 102 | +``` |
| 103 | +
|
| 104 | +Then you need to add this package as a build dependency and a few extra |
| 105 | +dependencies to your project, for example: |
| 106 | +
|
| 107 | +```toml |
| 108 | +requires = [ |
| 109 | + "setuptools >= 67.3.2, < 68", |
| 110 | + "setuptools_scm[toml] >= 7.1.0, < 8", |
| 111 | + "frequenz-repo-config[api] >= 0.1.0, < 0.2.0", |
| 112 | +] |
| 113 | +build-backend = "setuptools.build_meta" |
| 114 | +
|
| 115 | +[project] |
| 116 | +dependencies = [ |
| 117 | + "googleapis-common-protos == 1.56.2", |
| 118 | + "grpcio == 1.51.0", |
| 119 | +] |
| 120 | +``` |
| 121 | +
|
| 122 | +Note the `api` extra in `frequenz-repo-config[api]`, this will ensure all |
| 123 | +dependencies to build the protocol files will be installed when building the |
| 124 | +package. Of course you need to replace the version numbers with the correct |
| 125 | +ones too. |
| 126 | +
|
| 127 | +Finally you need to make sure to include the generated `*.pyi` files in the |
| 128 | +source distribution, as well as the Google api-common-protos files, as it |
| 129 | +is not handled automatically yet |
| 130 | +([#13](https://github.com/frequenz-floss/frequenz-repo-config-python/issues/13)): |
| 131 | +
|
| 132 | +``` |
| 133 | +recursive-include py *.pyi |
| 134 | +recursive-include submodules/api-common-protos/google *.proto |
| 135 | +``` |
| 136 | +
|
| 137 | +If you need to customize how the protobuf files are compiled, you can pass |
| 138 | +a path to look for the protobuf files, a glob pattern to find the protobuf |
| 139 | +files, and a list of paths to include when compiling the protobuf files. By |
| 140 | +default `submodules/api-common-protos` is used as an include path, so if |
| 141 | +yout configured the submodules in a different path, you need to pass the |
| 142 | +correct path to the `include_paths` argument. |
| 143 | +""" |
| 144 | + |
| 145 | +from . import nox, setuptools |
7 | 146 |
|
8 | 147 | __all__ = [ |
9 | 148 | "nox", |
| 149 | + "setuptools", |
10 | 150 | ] |
0 commit comments