Skip to main content

shx

PyPI version

Inspired by zx

#!/usr/bin/env shx

await $"cat setup.py | grep name"

branch = await $("git branch --show-current", capture='o')
await $f"dep deploy --branch={branch}"

await gather(
  $"sleep 1; echo 1",
  $"sleep 2; echo 2",
  $"sleep 3; echo 3",
)

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

(Take a look at more examples.)

shx makes your script writing experience better by taking the advantages of Python's sugary syntax, AsyncIO, and the extensive Python ecosystem. shx does three things:

  1. Wrap asyncio.create_subprocess_shell around with a syntax sugar. await $"command" returns an asyncio.subprocess.Process instance; on non-zero return code, raise subprocess.CalledProcessError.
  2. Provide a top-level async environment.
  3. Preload commonly used imports and utilities. Currently, the imports are:
import asyncio
from asyncio import *
from pathlib import Path
from shlex import quote as Q
import shutil

Note that shx does not perform quote escape automatically. Use function Q (alias of shlex.quote) to escape unsafe arguments.

Install

pip install shx

Settings and utility functions

Settings can either be task local (e.g. __.trace = True) or per-command (e.g. await $("echo 42", trace=True)):

  • shell (Default: $(which bash)): Shell to be used.
  • prefix (Default: set -euo pipefail;): String to be prepended to a command.
  • trace (Default: True): Display command if set to True. Same as set -x in bash.
  • capture (Default: False): If set to True, capture stdout and stderr instead of displaying them. The captured strings will replace the .stdout and .stderr attributes of the asyncio.subprocess.Process instance returned. await $("...", capture='o') and await $("...", capture='e') are the aliases of (await $("...", capture=True)).stdout and (await $("...", capture=True)).stderr, respectively.

Attributes:

  • __.argv: alias of sys.argv, a list of command line arguments
  • __.env: alias of os.environ, a dict of environment variables

cd(cwd: str)

Change working directory to cwd. Same as the task local settings, the changes are only effective within the current task.

question(prompt: str)

input() with KeyboardInterrupt handling.

About the subprocess syntax

No magic, no meta-programming, and no hacking, whatsoever. Prior execution, the script is tokenized, and the following replacements occur:

  • "str prefix" $"command" -> SHX("command")
  • "function" $("command", k1=v1, ...) -> SHX("command", k1=v1, ...)

where SHX is an async function wrapping around asyncio.create_subprocess_shell.

Metadata

Release files for shx 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shx 0.4.2
File Size Uploaded Provenance Actions
shx-0.4.2.tar.gz 5.7 kB Without provenance Details for shx-0.4.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for shx 0.4.2
File Interpreter ABI Platform Actions
shx-0.4.2-py3-none-any.whl Python 3 none any Details for shx-0.4.2-py3-none-any.whl

Total release size: 11.9 kB

Release files / shx-0.4.2.tar.gz

Download URL shx-0.4.2.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
bf7894d36d43fb07d550239c3364b4faf3e5d6a47f2c6e56891d53c1ce9c23a8
BLAKE2b-256 checksum
How to use checksums
968ad886c8b0469a954f59adf5b184dbae520d3c01e77427914c0ae7dcb5d476
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release files / shx-0.4.2-py3-none-any.whl

Download URL shx-0.4.2-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69f2424b77069edf23873ecce88af47bff1e1ad78595cc4e3301290c9497edd4
BLAKE2b-256 checksum
How to use checksums
9762078cc58b4fcac34cb234ad856900d26664eda810b7d84cf99fc3366f66ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Security for shx 0.4.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page