Skip to main content

Hatch build hook plugin to inject code that will automatically run

Project description

hatch-autorun

CI/CD CI - Test CD - Build
Package PyPI - Version PyPI - Python Version
Meta code style - black types - Mypy imports - isort License - MIT GitHub Sponsors

This provides a build hook plugin for Hatch that injects code into an installation that will automatically run before the first import.

Table of Contents

Configuration

The build hook plugin name is autorun.

  • pyproject.toml

    [tool.hatch.build.targets.wheel.hooks.autorun]
    dependencies = ["hatch-autorun"]
    
  • hatch.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:

[tool.hatch.build.targets.wheel.hooks.autorun]
file = "resources/code.emded"

Code

You can define the code itself with the code option:

[tool.hatch.build.targets.wheel.hooks.autorun]
code = """
print('Starting coverage collection')
coverage.process_startup()
"""

Template

The current implementation uses a .pth file to execute the code. As a result, any required imports must be defined there on one line rather than in the code itself.

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:

[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 to false:

[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 to true or 1.

License

hatch-autorun is distributed under the terms of the MIT license.

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

hatch_autorun-1.1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

hatch_autorun-1.1.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file hatch_autorun-1.1.0.tar.gz.

File metadata

  • Download URL: hatch_autorun-1.1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for hatch_autorun-1.1.0.tar.gz
Algorithm Hash digest
SHA256 70631dae9a6a72fe5bfe04847cd43b680fc027d2a910ad067d03a2977f5ef6ce
MD5 7a3349091a4e25c5db890ee821153cff
BLAKE2b-256 133762b2c258c5fb38be44807c0cf62add31c0d84ad1492aebeb0459fbb7d290

See more details on using hashes here.

File details

Details for the file hatch_autorun-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hatch_autorun-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 59a24b66d71af22b0a232e8d4496de3e92573c4295d2a1fdea40609a19cf56ad
MD5 a62f36b022f584d5327166205056ec76
BLAKE2b-256 3e84e77396f58b2375ca7d4fa08d4398f5821a181cb4a5790f30f05f498781f5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page