Skip to main content

Boot.py is an small set of tools to build simple scripts. Python3 only, and really small: 2Kb!

Project description

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

Install and Use

Install with pip.

pip install boot.py

Create a file and import boot. For example this will install a virtual environment, install requirements, and create some files.

#!/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('')

This will output.

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

Simple!

You can also compose tasks to decide what to execute and what order.

#!/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()

There are helpers to build scripts with less code, for example we can replace in the code above the main call with a a simple task argument parser:

from boot.cli import ActionsCommand

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

This parse –help

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

boot.py-0.15.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

boot.py-0.15-py2.py3-none-any.whl (10.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file boot.py-0.15.tar.gz.

File metadata

  • Download URL: boot.py-0.15.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for boot.py-0.15.tar.gz
Algorithm Hash digest
SHA256 e1eb64871a437252ffb9d03dfb4027a87c533cd2eaf8876f2bcc795a72791d62
MD5 fe3d404ff44e87c6406c8724a300ff65
BLAKE2b-256 3b2c3122dfe4ffb0ac975cc289b42f38b82e2eea6f40acbfaee5ef8eb4074434

See more details on using hashes here.

File details

Details for the file boot.py-0.15-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for boot.py-0.15-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8995d7bc6d33e11c01d51e91e5e32e044fd4833d3a66c961e43b4e02a94f194b
MD5 064b96634f44785b86b8bf00fce1e606
BLAKE2b-256 6de9a89fb569d5285874b96862bd414cb520cdcd2a4910ae9a1d6f3ae9373be9

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