一个简单的任务执行者。
dut的Python项目详细描述
职责
一个简单的任务执行者。在
灵感来自Invoke。在
要求
职责要求Python3.6或更高版本。在
<详细信息># install pyenv git clone https://github.com/pyenv/pyenv ~/.pyenv # setup pyenv (you should also put these three lines in .bashrc or similar)exportPATH="${HOME}/.pyenv/bin:${PATH}"exportPYENV_ROOT="${HOME}/.pyenv"eval"$(pyenv init -)"# install Python 3.6 pyenv install 3.6.12 # make it available globally pyenv global system 3.6.12详细信息>
安装
使用pip
:
使用^{
python3.6 -m pip install --user pipx pipx install --python python3.6 duty
快速入门
将很快提供适当的文档页。在
配置
在存储库的根目录下创建一个duties.py
文件。在
使用duty.duty
修饰符将每个任务声明为“职责”。在
fromdutyimportduty@dutydefdocs(ctx):ctx.run("mkdocs build",title="Building documentation")
ctx
参数是职责的“上下文”。
它会自动创建并传递给您的函数。在
它只有一个用途:用它的run
方法运行命令。
run
方法接受字符串、字符串列表,甚至Python可调用项。在
上述责任可改写为:
fromdutyimportduty@dutydefdocs(ctx):ctx.run(["mkdocs","build"],title="Building documentation")# avoid the overhead of an extra shell process
或者:
fromdutyimportdutyfrommkdocsimportbuild,config@dutydefdocs(ctx):ctx.run(build.build,args=[config.load_config()],title="Building documentation")# avoid the overhead of an extra Python process
run
方法接受各种选项,
主要来自于其潜在的依赖性:
^{
run
方法的参数:
Name | Type | Description | Default |
---|---|---|---|
cmd | ^{ | The command to run. | required |
args | ^{ | Arguments to pass to the callable. | ^{ |
kwargs | ^{ | Keyword arguments to pass to the callable. | ^{ |
number | ^{ | The command number (useful for the ^{ | ^{ |
output_type | ^{ | The type of output: ^{ | ^{ |
title | ^{ | The command title. | cmd as a shell command or Python statement |
fmt | ^{ | The output format as a Jinja template: ^{ | ^{ |
pty | ^{ | Whether to run in a PTY. | ^{ |
progress | ^{ | Whether to show progress. | ^{ |
nofail | ^{ | Whether to always succeed. | ^{ |
quiet | ^{ | Don't print the command output, even if it failed. | ^{ |
silent | ^{ | Don't print anything. | ^{ |
silent
选项的用法示例:
@dutydefclean(ctx):ctx.run("find . -type d -name __pycache__ | xargs rm -rf",silent=True)
现在假设你有一个以上的命令,你想让所有命令静音:
@duty(silent=True)defclean(ctx):ctx.run("rm -rf .coverage*")ctx.run("rm -rf .mypy_cache")ctx.run("rm -rf .pytest_cache")ctx.run("rm -rf build")ctx.run("rm -rf dist")ctx.run("rm -rf pip-wheel-metadata")ctx.run("rm -rf site")ctx.run("find . -type d -name __pycache__ | xargs rm -rf")ctx.run("find . -name '*.rej' -delete")
运行
要执行任务,只需使用:
duty clean
如果您正在使用Poetry:
poetry run duty clean
您可以在一个命令中传递多个职责:
duty clean docs
如果你的职责之一接受争论, 也可以在命令行上传递它们:
@dutydefdocs(ctx,serve=False):command="serve"ifserveelse"build"ctx.run(f"mkdocs {command}")
duty docs serve=1
!!!笔记 请注意,参数不是类型强制转换的: 他们总是被认为是职责的附加条件。在
托多
- 更好地处理缺失职责的争论。
可能只需打印错误而不进行回溯:
release() missing 1 required positional argument: 'version'
- 参数类型转换,最好基于类型注释!在
- 项目
标签: