tasks

This module contains the core Task class & convenience decorators used to generate new tasks.

class invoke.tasks.Call(task, called_as=None, args=None, kwargs=None)

Represents a call/execution of a Task with given (kw)args.

Similar to partial with some added functionality (such as the delegation to the inner task, and optional tracking of the name it’s being called by.)

__init__(task, called_as=None, args=None, kwargs=None)

Create a new Call object.

Parameters:
  • task – The Task object to be executed.
  • called_as (str) – The name the task is being called as, e.g. if it was called by an alias or other rebinding. Defaults to None, aka, the task was referred to by its default name.
  • args (tuple) – Positional arguments to call with, if any. Default: None.
  • kwargs (dict) – Keyword arguments to call with, if any. Default: None.
__weakref__

list of weak references to the object (if defined)

clone(into=None)

Return a standalone copy of this Call.

Useful when parameterizing task executions.

Parameters:into – A subclass to generate instead of the current class.
make_context(config)

Generate a Context appropriate for this call, with given config.

invoke.tasks.NO_DEFAULT = <object object>

Sentinel object representing a truly blank value (vs None).

class invoke.tasks.Task(body, name=None, aliases=(), positional=None, optional=(), default=False, auto_shortflags=True, help=None, pre=None, post=None, autoprint=False)

Core object representing an executable task & its argument specification.

__weakref__

list of weak references to the object (if defined)

argspec(body)

Returns two-tuple:

  • First item is list of arg names, in order defined.

    • I.e. we cannot simply use a dict’s keys() method here.
  • Second item is dict mapping arg names to default values or NO_DEFAULT (an ‘empty’ value distinct from None, since None is a valid value on its own).

get_arguments()

Return a list of Argument objects representing this task’s signature.

invoke.tasks.call(task, *args, **kwargs)

Describes execution of a Task, typically with pre-supplied arguments.

Useful for setting up pre/post task invocations. It’s actually just a convenient wrapper around the Call class, which may be used directly instead if desired.

For example, here’s two build-like tasks that both refer to a setup pre-task, one with no baked-in argument values (and thus no need to use call), and one that toggles a boolean flag:

@task
def setup(ctx, clean=False):
    if clean:
        ctx.run("rm -rf target")
    # ... setup things here ...
    ctx.run("tar czvf target.tgz target")

@task(pre=[setup])
def build(ctx):
    ctx.run("build, accounting for leftover files...")

@task(pre=[call(setup, clean=True)])
def clean_build(ctx):
    ctx.run("build, assuming clean slate...")

Please see the constructor docs for Call for details - this function’s args and kwargs map directly to the same arguments as in that method.

invoke.tasks.task(*args, **kwargs)

Marks wrapped callable object as a valid Invoke task.

May be called without any parentheses if no extra options need to be specified. Otherwise, the following keyword arguments are allowed in the parenthese’d form:

  • name: Default name to use when binding to a Collection. Useful for avoiding Python namespace issues (i.e. when the desired CLI level name can’t or shouldn’t be used as the Python level name.)
  • aliases: Specify one or more aliases for this task, allowing it to be invoked as multiple different names. For example, a task named mytask with a simple @task wrapper may only be invoked as "mytask". Changing the decorator to be @task(aliases=['myothertask']) allows invocation as "mytask" or "myothertask".
  • positional: Iterable overriding the parser’s automatic “args with no default value are considered positional” behavior. If a list of arg names, no args besides those named in this iterable will be considered positional. (This means that an empty list will force all arguments to be given as explicit flags.)
  • optional: Iterable of argument names, declaring those args to have optional values. Such arguments may be given as value-taking options (e.g. --my-arg=myvalue, wherein the task is given "myvalue") or as Boolean flags (--my-arg, resulting in True).
  • default: Boolean option specifying whether this task should be its collection’s default task (i.e. called if the collection’s own name is given.)
  • auto_shortflags: Whether or not to automatically create short flags from task options; defaults to True.
  • help: Dict mapping argument names to their help strings. Will be displayed in --help output.
  • pre, post: Lists of task objects to execute prior to, or after, the wrapped task whenever it is executed.
  • autoprint: Boolean determining whether to automatically print this task’s return value to standard output when invoked directly via the CLI. Defaults to False.

If any non-keyword arguments are given, they are taken as the value of the pre kwarg for convenience’s sake. (It is an error to give both *args and pre at the same time.)