Package: pyshpype
Pysh/PypePython source file preprocessor/interpreter and subprocess pipe manager to enable running in-line bash/shell code during python runtime.#!/usr/bin/env pythonfrompyshimportpyshpysh()#$@ 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 upaggregate_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")InstallationFrom PyPI:pip3 install pyshpypeGit to local folder:pip3 install -e "git+https://github.com/blipk/pysh.git#egg=pysh"General syntax#!/usr/bin/pythonfrompyshimportpyshpysh()#$ 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 subprocessstout=""#$ echo "I'm actually a bytes string"print(stdout.decode("UTF-8"))# Pass any python variable thats in scope to the pysh scriptmy_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/featuresstdout=""#$@!python import time#$ print("The time and date", time.asctime())# Use the % flag to catch errors,# otherwise they will be printed but not raisedtry:result=""#$$% tests/dinger/notfoundscript.sh "argone"exceptSystemExitase: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 $HOMEstdout_block1=""#$ echo "first block is multiline"#$ echo "line1"#$ echo "line2"# The last script block won't be runsys.exit(1)stdout_block2==""#$ echo "Second"#$ echo "Block"Advanced usage# run pysh manuallyfrompyshimportPyshsource_file=__file__pysher=Pysh(source_file)blocks=pysher.findblocks()# Run a a single blockblocks[0].run()# Not run in-place, no stdout. Silent.# Run script block again, and print stdout with label for blockblocks[0].runp()# Run all wanted blocks sequentially at this point,# and print their stdout with labelsrun_blocks=[block.runp()forblockinblocksif"/root"inblock.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 pyshpysher.pysh(__file__)# Equivalent to abovepysher.updatesrc(__file__)pysher.pysh()# Get information about the script blocks at runtimet_block=pysher.blocks[0]print(t_block.hasrun,t_block.returncode)print(t_block.srcs,"\n--\n",t_block.stdout)