跳转到主要内容

Boot.py是一组用于构建简单脚本的工具。仅适用于Python3,非常小巧:2Kb!

项目描述

https://badge.fury.io/py/boot.py.svg https://travis-ci.org/mariocesar/boot.py.svg?branch=master

安装和使用

使用pip安装。

pip install boot.py

创建一个文件并导入boot。例如,这将安装虚拟环境,安装需求,并创建一些文件。

#!/usr/bin/env python3
import os
import venv

from boot import step, run
from pathlib import Path

root_path = Path(__file__).parent.resolve()
venv_dir = root_path / '.venv'

with step(f'Creating virtualenv in {venv_dir.name}'):
    if not venv_dir.exists():
        venv.create(venv_dir, with_pip=True)

with step('Installing requirements'):
    run(f'{venv_dir / "bin/pip"} install -r requirements.txt')

with step('Creating directories'):
    run(f'mkdir -p public/media')
    run(f'mkdir -p public/static')

with step('Environment file'):
    envfile = root_path / '.env'

    if not envfile.exists():
        with open(envfile, 'w') as handle:
            os.chmod(envfile, 0o600)
            handle.write('')

这将输出。

$ ./script.py
Creating virtualenv in .venv ... [Ok]
Installing requirements ... [Ok]
Installing project ... [Ok]
Creating directories ... [Ok]
Environment file ... [Ok]

简单!

您还可以组合任务来决定要执行的内容以及执行顺序。

#!/usr/bin/env python3
import os
import venv

from boot import step, run, task
from pathlib import Path

root_path = Path(__file__).parent.resolve()
venv_dir = root_path / '.venv'


@task
def build(this)
    with step(f'Creating virtualenv in {venv_dir.name}'):
        if not venv_dir.exists():
            venv.create(venv_dir, with_pip=True)

    with step('Creating directories'):
        run(f'mkdir -p public/media')
        run(f'mkdir -p public/static')

    with step('Environment file'):
        envfile = root_path / '.env'

        if not envfile.exists():
            with open(envfile, 'w') as handle:
                os.chmod(envfile, 0o600)
                handle.write('')


@task
def requirements(this)
    with step('Installing requirements'):
        run(f'{venv_dir / "bin/pip"} install -r requirements.txt')


@task
def backup(this)
    with step(f'Backup db'):
        run('pg_dump -d database -f output.sql')


if __name__ == '__main__':
    tasks = {
        'default': build >> requirements,
        'build': build,
        'requirements': requirements,
    }

    if len(sys.argv) == 1:
        if sys.argv[0] in tasks:
            tasks[sys.argv[0]]()
        else:
            print(f'Unknown task: {sys.argv[0]}')
            print(f'Available tasks are: {tasks.keys()}')
    else:
        default()

有一些助手可以以更少的代码构建脚本,例如,我们可以用简单的任务参数解析器替换上面的代码中的主调用

from boot.cli import ActionsCommand

if __name__ == '__main__':
    ActionsCommand.main(
        default=build >> requirements,
        build=build,
        requirements=requirements,
    }

这解析–help

项目详情


下载文件

下载适用于您平台的应用程序。如果您不确定要选择哪一个,请了解更多关于安装软件包的信息。

源分布

boot.py-0.16.tar.gz (7.3 kB 查看哈希)

上传时间:

构建分布

boot.py-0.16-py2.py3-none-any.whl (9.0 kB 查看哈希值)

上传时间: Python 2 Python 3

支持