Skip to main content

A tool for writing shell scripts in python.

Project description

prey

#!/usr/bin/env prey

async def main():
    await _("cat pyproject.toml | grep name")

    branch = await _("git branch --show-current")
    await _(f"dep deploy --branch={branch}")

    await _(
        [
            "sleep 1; echo 1",
            "sleep 2; echo 2",
            "sleep 3; echo 3",
        ]
    )

    name = "foo bar"
    await _(f"mkdir /tmp/${name}")

A tool for writing shell scripts in Python. Inspired by google/zx. This package provides a wrapper around asyncio.subprocess and escapes arguments.

Install

pip install prey

Documentation

Wrap your scripts in an async function called main:

async def main():
    # script...

It must be called main as the executable looks for a function calls main and calls it. This is used so commands can be asynchronous.

You can add the shebang at the top of your script:

#!/usr/bin/env prey

and run it like so:

chmod +x ./script.py
./script.py

Or via the prey executable:

prey ./script.py

When using prey via the executable or a shebang, all of the functions (_, colorama, request, etc) are available wihtout any imports.

await _("command")

Asychronously executes a given string using the create_subprocess_shell function from the asyncio.subprocess module and returns the output.

count = int(await _("ls -1 | wc -l"))
print(f"Files count: {count}")

cd("filepath")

Changes the current working directory.

cd("/tmp")
await _('pwd') # outputs /tmp

colorama package

The colorama package is available without importing inside scripts.

print(f"{colorama.Fore.BLUE}Hello World!")

request package

A wrapper around aiohttp, aiohttp-requests.requests.session.request, is available without importing inside scripts.

response = await request("get", "http://python.org")
html = await response.text()

Importing from other scripts

It is possible to make use of _ and other functions via explicit imports:

#!/usr/bin/env prey
from prey import _
await _('date')

Passing env variables

os.environ["FOO"] = "bar"
await _('echo $FOO')

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

prey-0.1.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

prey-0.1.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file prey-0.1.1.tar.gz.

File metadata

  • Download URL: prey-0.1.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.4 Linux/5.8.0-53-generic

File hashes

Hashes for prey-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7f46988494852e41a924501b6b9010424d77c9641a68e1c618c9982c1b69cb72
MD5 c95bf9d6ecffa60e8a2e897201ba515a
BLAKE2b-256 29b805c846d3c892d506d8ba8022b7cc4cf1d94d8aa5dac62d11d107946bb890

See more details on using hashes here.

File details

Details for the file prey-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: prey-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.4 Linux/5.8.0-53-generic

File hashes

Hashes for prey-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c3c461bdc8c3a0da929c2089f8c06e158d15e626c26cac35c0ba9fc795820e0c
MD5 3dca453326aec72b1a86fb559606c5ac
BLAKE2b-256 f340bce816010159e8e3ab4a1717a0f5163cd32fe9bed1224dee41712a7da74f

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