PyBash


NamePyBash JSON
Version 0.3.5 PyPI version JSON
download
home_page
Summary>execute bash commands from python easily
upload_time2024-02-19 00:35:07
maintainer
docs_urlNone
authorJay
requires_python>=3.9,<4.0
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # PyBash

![GitHub Workflow Status (with branch)](https://img.shields.io/github/actions/workflow/status/jaykv/pybash/python-app.yml?branch=main)
![PyPI - Downloads](https://img.shields.io/pypi/dm/pybash)
![PyPI](https://img.shields.io/pypi/v/pybash)
![GitHub](https://img.shields.io/github/license/jaykv/pybash)

Streamline bash-command execution from python with a new syntax. It combines the simplicity of writing bash scripts with the flexibility of python. Under the hood, any line or variable assignment starting with `$` or surrounded by parentheses is transformed to python `subprocess` calls and then injected into `sys.meta_path` as an import hook. All possible thanks to the wonderful [ideas](https://github.com/aroberge/ideas) project!

For security and performance reasons, PyBash will NOT execute as shell, unless explicitly specified with a `>` instead of a single `$` before the command. While running commands as shell can be convenient, it can also spawn security risks if you're not too careful. If you're curious about the transformations, look at the [unit tests](test_pybash.py) for some quick examples.

Note: this is a mainly experimental library.

# Setup

## As standalone transformer
`pip install pybash`


```python
from pybash.transformer import transform

transform("$echo hello world") # returns the python code for the bash command as string
```

## As script runner
`pip install "pybash[script]"`


### Example 
```py
text = "HELLO WORLD"
$echo f{text}
```

### Run script:
```bash
python -m pybash hello.py
```

# Supported transforms

### 1. Simple execution with output
```python
$python --version
$echo \\nthis is an echo
```
outputs:
```
Python 3.9.15

this is an echo
```

### 2. Set output to variable and parse
```python
out = $cat test.txt
test_data = out.decode('utf-8').strip()
print(test_data.replace("HELLO", "HOWDY"))
```
outputs:
```
HOWDY WORLD
```

### 3. Wrapped, in-line execution and parsing
```python
print(($cat test.txt).decode('utf-8').strip())
```
outputs:
```
HELLO WORLD
```

### 4. Redirection
```python
$echo "hello" >> test4.txt
```

### 5. Pipe chaining
```python
$cat test.txt | sed 's/HELLO/HOWDY/g' | sed 's/HOW/WHY/g' | sed 's/WHY/WHEN/g'
```
outputs:
```
WHENDY WORLD
```

### 6. Redirection chaining
```python
$cat test.txt | sed 's/HELLO/HOWDY\\n/g' > test1.txt >> test2.txt > test3.txt
```

### 7. Chaining pipes and redirection- works in tandem!
```python
$cat test.txt | sed 's/HELLO/HOWDY\\n/g' > test5.txt
```

### 8. Input redirection
```python
$sort < test.txt >> sorted_test.txt
```

```python
$sort < test.txt | sed 's/SORT/TEST\\n/g'
```
### 9. Glob patterns with shell
```python
>ls .github/*
```

### 10. Direct interpolation
Denoted by {{code here}}. Interpolated as direct code replace. The value/output of the variable, function call, or the expression must not include spaces.

```python
## GOOD
command = "status"
def get_option(command):
    return "-s" if command == "status" else "-v"
$git {{command}} {{get_option(command)}}

display_type = "labels"
$kubectl get pods --show-{{display_type}}=true

## BAD
option = "-s -v"
$git status {{option}}

options = ['-s', '-v']
$git status {{" ".join(options)}}

# use dynamic interpolation
options = {'version': '-v'}
$git status {{options['version']}}
```

### 11. f-string interpolation
Denoted by f{ any python variable, function call, or expression here }. Interpolated as f-string. The output of the variable, function call, or the expression must still not include spaces.

```python
## GOOD

# git -h
options = {'version': '-v', 'help': '-h'}
$git f{options['h']}

# kubectl get pods --show-labels -n coffee
namespace = "coffee"
$kubectl get pods f{"--" + "-".join(['show', 'labels'])} -n f{namespace}

## BAD
option = "-s -v"
$git status f{option}
```

#### Also works inside methods!
```python
# PYBASH DEMO #
def cp_test():
    $cp test.txt test_copy.txt

cp_test()
```

# Dev

#### Demo
`python -m pybash examples/hello.py`
`python -m pybash demo`

#### Debug
`make debug` to view the transformed source code


            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "PyBash",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Jay",
    "author_email": "jay.github0@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/65/01/4f671d14b06de2925b1159bee98a76373b08ba5cba98d6b49718110b0053/pybash-0.3.5.tar.gz",
    "platform": null,
    "description": "# PyBash\n\n![GitHub Workflow Status (with branch)](https://img.shields.io/github/actions/workflow/status/jaykv/pybash/python-app.yml?branch=main)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/pybash)\n![PyPI](https://img.shields.io/pypi/v/pybash)\n![GitHub](https://img.shields.io/github/license/jaykv/pybash)\n\nStreamline bash-command execution from python with a new syntax. It combines the simplicity of writing bash scripts with the flexibility of python. Under the hood, any line or variable assignment starting with `$` or surrounded by parentheses is transformed to python `subprocess` calls and then injected into `sys.meta_path` as an import hook. All possible thanks to the wonderful [ideas](https://github.com/aroberge/ideas) project!\n\nFor security and performance reasons, PyBash will NOT execute as shell, unless explicitly specified with a `>` instead of a single `$` before the command. While running commands as shell can be convenient, it can also spawn security risks if you're not too careful. If you're curious about the transformations, look at the [unit tests](test_pybash.py) for some quick examples.\n\nNote: this is a mainly experimental library.\n\n# Setup\n\n## As standalone transformer\n`pip install pybash`\n\n\n```python\nfrom pybash.transformer import transform\n\ntransform(\"$echo hello world\") # returns the python code for the bash command as string\n```\n\n## As script runner\n`pip install \"pybash[script]\"`\n\n\n### Example \n```py\ntext = \"HELLO WORLD\"\n$echo f{text}\n```\n\n### Run script:\n```bash\npython -m pybash hello.py\n```\n\n# Supported transforms\n\n### 1. Simple execution with output\n```python\n$python --version\n$echo \\\\nthis is an echo\n```\noutputs:\n```\nPython 3.9.15\n\nthis is an echo\n```\n\n### 2. Set output to variable and parse\n```python\nout = $cat test.txt\ntest_data = out.decode('utf-8').strip()\nprint(test_data.replace(\"HELLO\", \"HOWDY\"))\n```\noutputs:\n```\nHOWDY WORLD\n```\n\n### 3. Wrapped, in-line execution and parsing\n```python\nprint(($cat test.txt).decode('utf-8').strip())\n```\noutputs:\n```\nHELLO WORLD\n```\n\n### 4. Redirection\n```python\n$echo \"hello\" >> test4.txt\n```\n\n### 5. Pipe chaining\n```python\n$cat test.txt | sed 's/HELLO/HOWDY/g' | sed 's/HOW/WHY/g' | sed 's/WHY/WHEN/g'\n```\noutputs:\n```\nWHENDY WORLD\n```\n\n### 6. Redirection chaining\n```python\n$cat test.txt | sed 's/HELLO/HOWDY\\\\n/g' > test1.txt >> test2.txt > test3.txt\n```\n\n### 7. Chaining pipes and redirection- works in tandem!\n```python\n$cat test.txt | sed 's/HELLO/HOWDY\\\\n/g' > test5.txt\n```\n\n### 8. Input redirection\n```python\n$sort < test.txt >> sorted_test.txt\n```\n\n```python\n$sort < test.txt | sed 's/SORT/TEST\\\\n/g'\n```\n### 9. Glob patterns with shell\n```python\n>ls .github/*\n```\n\n### 10. Direct interpolation\nDenoted by {{code here}}. Interpolated as direct code replace. The value/output of the variable, function call, or the expression must not include spaces.\n\n```python\n## GOOD\ncommand = \"status\"\ndef get_option(command):\n    return \"-s\" if command == \"status\" else \"-v\"\n$git {{command}} {{get_option(command)}}\n\ndisplay_type = \"labels\"\n$kubectl get pods --show-{{display_type}}=true\n\n## BAD\noption = \"-s -v\"\n$git status {{option}}\n\noptions = ['-s', '-v']\n$git status {{\" \".join(options)}}\n\n# use dynamic interpolation\noptions = {'version': '-v'}\n$git status {{options['version']}}\n```\n\n### 11. f-string interpolation\nDenoted by f{ any python variable, function call, or expression here }. Interpolated as f-string. The output of the variable, function call, or the expression must still not include spaces.\n\n```python\n## GOOD\n\n# git -h\noptions = {'version': '-v', 'help': '-h'}\n$git f{options['h']}\n\n# kubectl get pods --show-labels -n coffee\nnamespace = \"coffee\"\n$kubectl get pods f{\"--\" + \"-\".join(['show', 'labels'])} -n f{namespace}\n\n## BAD\noption = \"-s -v\"\n$git status f{option}\n```\n\n#### Also works inside methods!\n```python\n# PYBASH DEMO #\ndef cp_test():\n    $cp test.txt test_copy.txt\n\ncp_test()\n```\n\n# Dev\n\n#### Demo\n`python -m pybash examples/hello.py`\n`python -m pybash demo`\n\n#### Debug\n`make debug` to view the transformed source code\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": ">execute bash commands from python easily",
    "version": "0.3.5",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "20d0fb39527a37aa3409469b063dc4a27e5d648c9e034337e44e0613dd8eb0b3",
                "md5": "f4459fa4d3e1f7e8f3f69b0a89ed7c86",
                "sha256": "d147393a72bc5a3f43548ab9fa960043c6b270b97848f16d5ede785a236395ab"
            },
            "downloads": -1,
            "filename": "pybash-0.3.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f4459fa4d3e1f7e8f3f69b0a89ed7c86",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9,<4.0",
            "size": 8266,
            "upload_time": "2024-02-19T00:35:04",
            "upload_time_iso_8601": "2024-02-19T00:35:04.936059Z",
            "url": "https://files.pythonhosted.org/packages/20/d0/fb39527a37aa3409469b063dc4a27e5d648c9e034337e44e0613dd8eb0b3/pybash-0.3.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "65014f671d14b06de2925b1159bee98a76373b08ba5cba98d6b49718110b0053",
                "md5": "174af7b8ccb7ce0b6c1df2901050e7d7",
                "sha256": "ec50344d9a0382ecd3f5fab23b06158a5629b4f3e6ae4b2f806582532da18464"
            },
            "downloads": -1,
            "filename": "pybash-0.3.5.tar.gz",
            "has_sig": false,
            "md5_digest": "174af7b8ccb7ce0b6c1df2901050e7d7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9,<4.0",
            "size": 7397,
            "upload_time": "2024-02-19T00:35:07",
            "upload_time_iso_8601": "2024-02-19T00:35:07.040105Z",
            "url": "https://files.pythonhosted.org/packages/65/01/4f671d14b06de2925b1159bee98a76373b08ba5cba98d6b49718110b0053/pybash-0.3.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-19 00:35:07",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "pybash"
}
        
Jay
Elapsed time: 0.21933s