https://github.com/ofek/hatch-autorun
Hatch build hook plugin to inject code that will automatically run
https://github.com/ofek/hatch-autorun
Last synced: about 1 year ago
JSON representation
Hatch build hook plugin to inject code that will automatically run
- Host: GitHub
- URL: https://github.com/ofek/hatch-autorun
- Owner: ofek
- License: mit
- Created: 2022-08-26T00:57:19.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-06-01T13:57:59.000Z (about 3 years ago)
- Last Synced: 2025-05-01T08:09:06.583Z (about 1 year ago)
- Language: Python
- Size: 29.3 KB
- Stars: 12
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- Funding: .github/FUNDING.yml
- License: LICENSE.txt
Awesome Lists containing this project
README
# hatch-autorun
| | |
| --- | --- |
| CI/CD | [](https://github.com/ofek/hatch-autorun/actions/workflows/test.yml) [](https://github.com/ofek/hatch-autorun/actions/workflows/build.yml) |
| Package | [](https://pypi.org/project/hatch-autorun/) [](https://pypi.org/project/hatch-autorun/) |
| Meta | [](https://github.com/pypa/hatch) [](https://github.com/psf/black) [](https://github.com/ambv/black) [](https://spdx.org/licenses/) [](https://github.com/sponsors/ofek) |
-----
This provides a [build hook](https://hatch.pypa.io/latest/config/build/#build-hooks) plugin for [Hatch](https://github.com/pypa/hatch) that injects code into an installation that will automatically run before the first import.
**Table of Contents**
- [Configuration](#configuration)
- [File](#file)
- [Code](#code)
- [Template](#template)
- [Conditional execution](#conditional-execution)
- [License](#license)
## Configuration
The [build hook plugin](https://hatch.pypa.io/latest/plugins/build-hook/) name is `autorun`.
- ***pyproject.toml***
```toml
[tool.hatch.build.targets.wheel.hooks.autorun]
dependencies = ["hatch-autorun"]
```
- ***hatch.toml***
```toml
[build.targets.wheel.hooks.autorun]
dependencies = ["hatch-autorun"]
```
### File
You can select a relative path to a file containing the code with the `file` option:
```toml
[tool.hatch.build.targets.wheel.hooks.autorun]
file = "resources/code.emded"
```
### Code
You can define the code itself with the `code` option:
```toml
[tool.hatch.build.targets.wheel.hooks.autorun]
code = """
import coverage
coverage.process_startup()
"""
```
### Template
The current implementation uses a `.pth` file to execute the code. You can set the `.pth` file template with the `template` option, which will be formatted with a `code` variable representing the `code` option or the contents of the file defined by the `file` option. The following shows the default template:
```toml
[tool.hatch.build.targets.wheel.hooks.autorun]
template = "import os, sys;exec({code!r})"
```
## Conditional execution
Sometimes you'll only want builds to induce auto-run behavior when installed under certain circumstances, like for tests. In such cases, set the `enable-by-default` [option](https://hatch.pypa.io/latest/config/build/#conditional-execution) to `false`:
```toml
[tool.hatch.build.targets.wheel.hooks.autorun]
enable-by-default = false
```
Then when the desired build conditions are met, set the `HATCH_BUILD_HOOK_ENABLE_AUTORUN` [environment variable](https://hatch.pypa.io/latest/config/build/#environment-variables) to `true` or `1`.
## License
`hatch-autorun` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.