Skip to main content

Pysh/Pype

Python source file preprocessor/interpreter and subprocess pipe manager to enable running in-line bash/shell code during python runtime.

#!/usr/bin/env python
from pysh import pysh
pysh()

#$@ echo "Pysh activated"
stdout = ""#$ echo "This is standard out"
print(stdout)

##@!python print("Python through a pysh pype")

Use #$ flagged directives to signify shell code to be piped through a subprocess.

When you run pysh() execution stops, and the source file will be processed with regex to extract and replace the code blocks with a subprocess wrapper, and then the source file itself is run through it.

Real usage
# Script your system in parallel with your python code execution
# Do anything you can in bash e.g.

build_service()
#$ cd ~/hosted/myservice && docker compose up

aggregate_assets()
crf = "23"
in_file = "/path/in.mp4"
out_file = "/path/out.mp4"
fmpg_result = ""#$ ffmpeg -i {$in_file$} \
#$ -crf {$crf$} -o {$out_file$} \
#$ && rm {$in_file$}
process_assets(process_fmpg_stdout(fmpg_result))

print("Process complete")

Installation

From PyPI:

pip3 install pyshpype

Git to local folder:

pip3 install -e "git+https://github.com/blipk/pysh.git#egg=pysh"

General syntax
#!/usr/bin/python
from pysh import pysh
pysh()
#$ echo "pysh activated" >> .pysh

# Use the @ flag to always print(stdout) to main sys.stdout
#$@ echo "hello from bash"

# This is a python comment
#$@ ls .            # shell eol comment
##$ sudo rm -rf /   # disable pysh line with pysh comment

# Capture stdout from the shell subprocess
stout = ""#$ echo "I'm actually a bytes string"
print(stdout.decode("UTF-8"))

# Pass any python variable thats in scope to the pysh script
my_var = "hello"
stdout = ""#$ echo "{$myvar$}"

# run external script with double $
#$$ my_script.sh

# optionally pass arguments to it
#$$ argumentative_script.sh arg1 arg2

# Use the ! flag to change the shell that interprets the script
# must support -c command_strings or filepath if external $$
#$!sh echo "simple"
#$!perl oysters.pl
#$$@!bash printscript.sh

# Multiple flags/features
stdout = ""#$@!python import time
#$ print("The time and date", time.asctime())

# Use the % flag to catch errors,
# otherwise they will be printed but not raised
try:
    result = ""#$$% tests/dinger/notfoundscript.sh "argone"
except SystemExit as e:
    print("Error", e)

if __name__ == "main":
    print("Before the above script blocks are run")
Multiple inline pysh
# Pysh runs code in blocks that are executed in-place

# Block 0
#$ cd $HOME

stdout_block1 = ""#$ echo "first block is multiline"
#$ echo "line1"
#$ echo "line2"

# The last script block won't be run
sys.exit(1)
stdout_block2 == ""#$ echo "Second"
#$ echo "Block"
Advanced usage
# run pysh manually
from pysh import Pysh
source_file = __file__
pysher = Pysh(source_file)
blocks = pysher.findblocks()

# Run a a single block
blocks[0].run()  # Not run in-place, no stdout. Silent.

# Run script block again, and print stdout with label for block
blocks[0].runp()

# Run all wanted blocks sequentially at this point,
# and print their stdout with labels
run_blocks = [block.runp() for block in blocks
              if "/root" in block.srcs]

# Start the python interpreter with pysh on source_file
# This is the same as running pysh(__file__)
pysher.shyp()
#$ echo "pysh enabled"

# Switch to another source file and run it through pysh
pysher.pysh(__file__)

# Equivalent to above
pysher.updatesrc(__file__)
pysher.pysh()

# Get information about the script blocks at runtime
t_block = pysher.blocks[0]
print(t_block.hasrun, t_block.returncode)
print(t_block.srcs, "\n--\n", t_block.stdout)

Metadata

Release files for pyshpype 0.1.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 pyshpype 0.1.2
File Size Uploaded Provenance
pyshpype-0.1.2.tar.gz 11.6 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 21.8 kB

Release files / pyshpype-0.1.2.tar.gz

Download URL pyshpype-0.1.2.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3a9e79ac956259b528925cca741120452bf4cc72bf61c33cf84656bbaca28992
BLAKE2b-256 checksum
How to use checksums
c1e74d2badbdd80b1ba925348d89f210e0d4ee013bca2295e368bf26e2e6d4e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.9 Linux/5.17.15-1-MANJARO

Release files / pyshpype-0.1.2-py3-none-any.whl

Download URL pyshpype-0.1.2-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
332c829d04b93bc406cf1a5befd97ebd0e8e120bf93f83e7bbdb0cfd63d2091c
BLAKE2b-256 checksum
How to use checksums
a2fb01cc18cd004fad20add8e7e2d536122c12e82a59ae274b154b1596398c50
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.9 Linux/5.17.15-1-MANJARO

Security for pyshpype 0.1.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