Skip to main content

A task runner that works well with poetry.

Project description

A task runner that works well with poetry.

Features

✅ Straight foward declaration of project tasks in your pyproject.toml (kind of like npm scripts)

✅ Task are run in poetry’s virtualenv by default

✅ Tasks can be commands (with or without a shell) or references to python functions (like tool.poetry.scripts)

✅ Short and sweet commands with extra arguments passed to the task poe [options] task [task_args]

✅ Tasks can specify and reference environmental variables as if they were evaluated by a shell

✅ Tasks are self documenting, with optional help messages (just run poe without arguments)

Installation

Into your project (so it works inside poetry shell):

poetry add --dev poethepoet

And into your default python environment (so it works outside of poetry shell)

pip install poethepoet

Basic Usage

Define tasks in your pyproject.toml

See a real example

[tool.poe.tasks]
test       = "pytest --cov=poethepoet"                                # simple command based task
mksandwich = { script = "my_package.sandwich:build" }                 # python script based task
tunnel     = { shell = "ssh -N -L 0.0.0.0:8080:$PROD:8080 $PROD &" }  # shell script based task

Run tasks with the poe cli

poe test

Additional argument are passed to the task so

poe test -v tests/favorite_test.py

results in the following be run inside poetry’s virtualenv

pytest --cov=poethepoet -v tests/favorite_test.py

You can also run it like so if you fancy

python -m poethepoet [options] task [task_args]

Or install it as a dev dependency with poetry and run it like

poetry add --dev poethepoet
poetry run poe [options] task [task_args]

Though it that case you might like to do alias poe='poetry run poe'.

Types of task

There are three types of task: simple commands (cmd), python scripts (script), and shell scripts (shell).

  • Command tasks contain a single command that will be executed without a shell. This covers most basic use cases for example:

    [tool.poe.tasks]
    format = "black ."  # strings are interpreted as commands by default
    clean = """
    # Multiline commands including comments work too. Unescaped whitespace is ignored.
    rm -rf .coverage
           .mypy_cache
           .pytest_cache
           dist
           ./**/__pycache__
    """
    lint = { "cmd": "pylint poethepoet" }  # Inline tables with a cmd key work too
    greet = "echo Hello $USER"  # Environmental variables work, even though there's no shell!
  • Script tasks contain a reference to a python callable to import and execute, for example:

    [tool.poe.tasks]
    fetch-assets = { "script" = "my_package.assets:fetch" }

    If extra arguments are passed to task, then they will be available to the called python function via sys.argv.

  • Shell tasks are similar to simple command tasks except that they are executed inside a new shell, and can consist of multiple seperate commands, command substitution, pipes, background processes, etc

    An example use case for this might be opening some ssh tunnels in the background with one task and closing them with another like so:

    [tool.poe.tasks]
    pfwd = { "shell" = "ssh -N -L 0.0.0.0:8080:$STAGING:8080 $STAGING & ssh -N -L 0.0.0.0:5432:$STAGINGDB:5432 $STAGINGDB &" }
    pfwdstop = { "shell" = "kill $(pgrep -f "ssh -N -L .*:(8080|5432)")" }

Task level configuration

Task help text

You can specifiy help text to be shown alongside the task name in the list of available tasks (such as when executing poe with no arguments), by adding a help key like so:

[tool.poe.tasks]
style = {cmd = "black . --check --diff", help = "Check code style"}

Environmental variables

You can specify arbitrary environmental variables to be set for a task by providing the env key like so:

[tool.poe.tasks]
serve.script = "myapp:run"
serve.env = { PORT = 9001 }

Notice this exame uses deep keys which can be more convenient but aren’t as well supported by some toml implementations.

Project-wide configuration options

Run poe from anywhere

By default poe will detect when you’re inside a project with a pyproject.toml in the root. However if you want to run it from elsewhere that is supported too by using the –root option to specify an alternate location for the toml file.

By default poe will set the working directory to run tasks. If you want tasks to inherit the working directory from the environment that you disable this by setting the following in your pyproject.toml.

[tool.poe]
run_in_project_root = false

In all cases the path to project root (where the pyproject.toml resides) is be available as $POE_ROOT within the command line and process.

Change the default task type

By default tasks defined as strings are interpreted as shell commands, and script tasks require the more verbose table syntax to specify. For example:

my_cmd_task = "cmd args"
my_script_task = { "script" = "my_package.my_module:run" }

This behavoir can be reversed by setting the default_task_type option in your pyproject.toml like so:

[tool.poe]
default_task_type = "script"

[tool.poe.tasks]
my_cmd_task = { "cmd" = "cmd args" }
my_script_task = "my_package.my_module:run"

Contributing

There’s plenty to do, come say hi in the issues! 👋

TODO

☐ task composition/aliases

☐ support declaring specific arguments for a task

☐ command line completion

☐ support running tasks outside of poetry’s virtualenv (or in another?)

☐ maybe try work well without poetry too

☐ maybe support alternative toml formats (e.g. table arrays)

☐ maybe support third party task types

Licence

MIT.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

poethepoet-0.4.0.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

poethepoet-0.4.0-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file poethepoet-0.4.0.tar.gz.

File metadata

  • Download URL: poethepoet-0.4.0.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.9 CPython/3.7.7 Linux/5.3.0-1031-azure

File hashes

Hashes for poethepoet-0.4.0.tar.gz
Algorithm Hash digest
SHA256 848329048591c9a8ac86d558771f8be8610e189cd3eec17ea77028a667c5dcec
MD5 be3325c2ba9924feaddf076b45f30044
BLAKE2b-256 6bf66eba17db5e2c3a2e9fd6c3cb03deed895ab56504074ca9ffc3354412b433

See more details on using hashes here.

File details

Details for the file poethepoet-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: poethepoet-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 16.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.9 CPython/3.7.7 Linux/5.3.0-1031-azure

File hashes

Hashes for poethepoet-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 02f8f5dff501f6e2e998898d41d892838381ccb2437f2924daa5e587278b3f37
MD5 25a6474a2206983c5f3f10e68b639be8
BLAKE2b-256 4378f91f68ff39f39b4ca06d1d177223ede6cd0ca78b9858a5baa6e09c1f8971

See more details on using hashes here.

Supported by

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