pyshpype


Namepyshpype JSON
Version 0.1.2 PyPI version JSON
download
home_pagehttps://github.com/blipk/pysh
SummaryRun shell in python
upload_time2023-03-20 12:10:32
maintainer
docs_urlNone
authorBlipk A.D.
requires_python>=3.10
licenseMIT
keywords pysh shell bash
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## Pysh/Pype

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

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

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/blipk/pysh",
    "name": "pyshpype",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "pysh,shell,bash",
    "author": "Blipk A.D.",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/c1/e7/4d2badbdd80b1ba925348d89f210e0d4ee013bca2295e368bf26e2e6d4e2/pyshpype-0.1.2.tar.gz",
    "platform": null,
    "description": "## Pysh/Pype\n\nPython source file preprocessor/interpreter and subprocess pipe manager to enable running in-line bash/shell code during python runtime.\n\n```Python\n#!/usr/bin/env python\nfrom pysh import pysh\npysh()\n\n#$@ echo \"Pysh activated\"\nstdout = \"\"#$ echo \"This is standard out\"\nprint(stdout)\n\n##@!python print(\"Python through a pysh pype\")\n```\n\nUse #$ flagged directives to signify shell code to be piped through a subprocess.\n\nWhen 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.\n\n##### Real usage\n```Python\n# Script your system in parallel with your python code execution\n# Do anything you can in bash e.g.\n\nbuild_service()\n#$ cd ~/hosted/myservice && docker compose up\n\naggregate_assets()\ncrf = \"23\"\nin_file = \"/path/in.mp4\"\nout_file = \"/path/out.mp4\"\nfmpg_result = \"\"#$ ffmpeg -i {$in_file$} \\\n#$ -crf {$crf$} -o {$out_file$} \\\n#$ && rm {$in_file$}\nprocess_assets(process_fmpg_stdout(fmpg_result))\n\nprint(\"Process complete\")\n```\n\n### Installation\nFrom PyPI:\n\n`pip3 install pyshpype`\n\nGit to local folder:\n\n`pip3 install -e \"git+https://github.com/blipk/pysh.git#egg=pysh\"`\n\n\n\n###### General syntax\n```Python\n#!/usr/bin/python\nfrom pysh import pysh\npysh()\n#$ echo \"pysh activated\" >> .pysh\n\n# Use the @ flag to always print(stdout) to main sys.stdout\n#$@ echo \"hello from bash\"\n\n# This is a python comment\n#$@ ls .            # shell eol comment\n##$ sudo rm -rf /   # disable pysh line with pysh comment\n\n# Capture stdout from the shell subprocess\nstout = \"\"#$ echo \"I'm actually a bytes string\"\nprint(stdout.decode(\"UTF-8\"))\n\n# Pass any python variable thats in scope to the pysh script\nmy_var = \"hello\"\nstdout = \"\"#$ echo \"{$myvar$}\"\n\n# run external script with double $\n#$$ my_script.sh\n\n# optionally pass arguments to it\n#$$ argumentative_script.sh arg1 arg2\n\n# Use the ! flag to change the shell that interprets the script\n# must support -c command_strings or filepath if external $$\n#$!sh echo \"simple\"\n#$!perl oysters.pl\n#$$@!bash printscript.sh\n\n# Multiple flags/features\nstdout = \"\"#$@!python import time\n#$ print(\"The time and date\", time.asctime())\n\n# Use the % flag to catch errors,\n# otherwise they will be printed but not raised\ntry:\n    result = \"\"#$$% tests/dinger/notfoundscript.sh \"argone\"\nexcept SystemExit as e:\n    print(\"Error\", e)\n\nif __name__ == \"main\":\n    print(\"Before the above script blocks are run\")\n```\n\n\n###### Multiple inline pysh\n```Python\n# Pysh runs code in blocks that are executed in-place\n\n# Block 0\n#$ cd $HOME\n\nstdout_block1 = \"\"#$ echo \"first block is multiline\"\n#$ echo \"line1\"\n#$ echo \"line2\"\n\n# The last script block won't be run\nsys.exit(1)\nstdout_block2 == \"\"#$ echo \"Second\"\n#$ echo \"Block\"\n```\n\n\n##### Advanced usage\n```Python\n# run pysh manually\nfrom pysh import Pysh\nsource_file = __file__\npysher = Pysh(source_file)\nblocks = pysher.findblocks()\n\n# Run a a single block\nblocks[0].run()  # Not run in-place, no stdout. Silent.\n\n# Run script block again, and print stdout with label for block\nblocks[0].runp()\n\n# Run all wanted blocks sequentially at this point,\n# and print their stdout with labels\nrun_blocks = [block.runp() for block in blocks\n              if \"/root\" in block.srcs]\n\n# Start the python interpreter with pysh on source_file\n# This is the same as running pysh(__file__)\npysher.shyp()\n#$ echo \"pysh enabled\"\n\n# Switch to another source file and run it through pysh\npysher.pysh(__file__)\n\n# Equivalent to above\npysher.updatesrc(__file__)\npysher.pysh()\n\n# Get information about the script blocks at runtime\nt_block = pysher.blocks[0]\nprint(t_block.hasrun, t_block.returncode)\nprint(t_block.srcs, \"\\n--\\n\", t_block.stdout)\n```",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Run shell in python",
    "version": "0.1.2",
    "split_keywords": [
        "pysh",
        "shell",
        "bash"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a2fb01cc18cd004fad20add8e7e2d536122c12e82a59ae274b154b1596398c50",
                "md5": "5fc9b6777e6708ff69ebb1733beeb6e6",
                "sha256": "332c829d04b93bc406cf1a5befd97ebd0e8e120bf93f83e7bbdb0cfd63d2091c"
            },
            "downloads": -1,
            "filename": "pyshpype-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5fc9b6777e6708ff69ebb1733beeb6e6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 10182,
            "upload_time": "2023-03-20T12:10:30",
            "upload_time_iso_8601": "2023-03-20T12:10:30.295195Z",
            "url": "https://files.pythonhosted.org/packages/a2/fb/01cc18cd004fad20add8e7e2d536122c12e82a59ae274b154b1596398c50/pyshpype-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c1e74d2badbdd80b1ba925348d89f210e0d4ee013bca2295e368bf26e2e6d4e2",
                "md5": "576c6f1502d0c9b9907ea9a6525ec8e5",
                "sha256": "3a9e79ac956259b528925cca741120452bf4cc72bf61c33cf84656bbaca28992"
            },
            "downloads": -1,
            "filename": "pyshpype-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "576c6f1502d0c9b9907ea9a6525ec8e5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 11592,
            "upload_time": "2023-03-20T12:10:32",
            "upload_time_iso_8601": "2023-03-20T12:10:32.741849Z",
            "url": "https://files.pythonhosted.org/packages/c1/e7/4d2badbdd80b1ba925348d89f210e0d4ee013bca2295e368bf26e2e6d4e2/pyshpype-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-20 12:10:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "blipk",
    "github_project": "pysh",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pyshpype"
}
        
Elapsed time: 0.05345s