interactiveshellsubproc


Nameinteractiveshellsubproc JSON
Version 0.13 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/interactiveshellsubproc
SummaryInteractive subprocess Instances for cmd.exe, powershell.exe, adb.exe, bash.exe without deadlocks - pure Python, no dependencies
upload_time2024-03-27 02:03:00
maintainerNone
docs_urlNone
authorJohannes Fischer
requires_pythonNone
licenseMIT
keywords shell deadlock interactive bidirectional
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Interactive subprocess Instances for cmd.exe, powershell.exe, adb.exe, bash.exe without deadlocks

## Easy, bidirectional communication with various subprocesses such as PowerShell, ADB, Command Prompt (Cmd), and Bash.

### pip install interactiveshellsubproc

### Tested against MSYS2 / powershell/cmd.dexe -> Windows 10 / Bluestacks / Python 3.11 / Windows 10

## Advantages

### Interactivity:
Allows bidirectional communication with subprocesses, enabling command execution and retrieval of outputs.

### Versatility:
Supports various subprocess types, including PowerShell, ADB, Cmd, and Bash, catering to different use cases and environments. New classes for any other shell can be easily derived from the base class.

### Error Handling:
Implements error handling mechanisms such as automatic process restart when stdin breaks (BrokenPipe) and configurable restart policies, enhancing robustness.

### configurability:
Offers a range of configurable options such as daemon mode, output printing settings, and restart behavior, providing flexibility in usage.

### Ease of Use:
Provides a straightforward interface for sending commands to subprocesses and retrieving outputs, simplifying interaction with external processes.

### Very low (almost no) deadlock danger: It very rarely deadlocks.
If you are sending normal shell commands, your instance will probably never deadlock. However, when you write, for example, a shell script and forget a curly brace or a bracket at the end and execute the half-backed command, or you open AWK or cat, and they start reading from stdin and not from a file or a PIPE because you didn't pass it, then chances are very high that the console will deadlock.

```py
from interactiveshellsubproc import (
    SubProcessInteractivePowerShell,
    SubProcessInteractiveAdb,
    SubProcessInteractiveCmd,
    SubProcessInteractiveBash,
)

if __name__ == "__main__":
    powershellinstance = SubProcessInteractivePowerShell(
        daemon=True,
        restart_when_error=True,
        restart_tries=5,
        sleep_after_reconnection_attempt=5,
        print_stderr=False,
        print_stdout=False,
        return_error=True,
    )

    stdout, stderr, exception = powershellinstance.sendcommand("ls")
    # Out[10]: [b"lsbabababa : The term 'lsbabababa' is not recognized as the name of a cmdlet, function,....
    stdout, stderr, exception = powershellinstance.sendcommand("lsbabababa")
    # [b"lsbabababa : The term 'lsbabababa' is not recognized as the name of a cmdlet, function, script

    adbinstance = SubProcessInteractiveAdb(
        adb_path=r"C:\ProgramData\chocolatey\lib\adb\tools\platform-tools\adb.exe",
        device_serial="127.0.0.1:5845",
        convert_to_83=True,
        daemon=True,
        restart_when_error=True,
        restart_tries=5,
        sleep_after_reconnection_attempt=5,
        print_stderr=False,
        print_stdout=False,
        return_error=True,
    )
    # In [1]: adbinstance.sendcommand("ls")
    # Out[1]: [[b'acct\n', b'apex\n', b'bin\n', b'boot\n', b'bugreports\n', b...

    # In [2]: adbinstance.stdin.close() # closing the pipe to simulate a problem

    # In [3]: adbinstance.sendcommand("ls")
    # ------------------------------------------
    # Traceback (most recent call last):
    #   File "C:\ProgramData\anaconda3\envs\a0\checkpowers.py", line 411, in sendcommand
    #     self._proc.stdin.write((cmd + f"\n{self.finish_command}\n").encode())
    # ValueError: write to closed file
    # ------------------------------------------
    # Restarting ...
    # Out[3]: [[], [], None] # the output of the first command after the restart is mostly not complete

    # In [4]: adbinstance.sendcommand("ls") # After the first one, everything is like before
    # Out[4]: [[b'acct\n', b'apex\n', b'bin\n', b'boot\n', b'bugreports\n'...

    cmdinstance = SubProcessInteractiveCmd(
        daemon=True,
        restart_when_error=True,
        restart_tries=5,
        sleep_after_reconnection_attempt=5,
        print_stderr=False,
        print_stdout=False,
        return_error=True,
    )
    # In [6]: cmdinstance.sendcommand("ls")
    # Out[6]: [[b'Code.VisualElementsManifest.xml\n', b'Code.exe\n', b'LICENSES.chromium.html\n',

    # In [7]:  cmdinstance.stdin.close() # provoking a broken pipe

    # All subclasses of SubProcessInteractive reconnect automatically
    # In [8]: cmdinstance.sendcommand("ls")
    # ------------------------------------------
    # Traceback (most recent call last):
    #   File "C:\ProgramData\anaconda3\envs\a0\checkpowers.py", line 411, in sendcommand
    #     self._proc.stdin.write((cmd + f"\n{self.finish_command}\n").encode())
    # ValueError: write to closed file
    # ------------------------------------------
    # Restarting ...
    # Out[8]: [[], [], None]

    # In [9]: cmdinstance.sendcommand("ls")
    # Out[9]: [[b'Code.VisualElementsManifest.xml\n', b'Code.exe\n',

    bashinstance = SubProcessInteractiveBash(
        bash_path=r"C:\msys64\usr\bin\bash.exe",
        convert_to_83=True,
        daemon=True,
        restart_when_error=True,
        restart_tries=5,
        sleep_after_reconnection_attempt=5,
        print_stderr=False,
        print_stdout=False,
        return_error=True,
    )
    # In [13]: bashinstance.sendcommand("exit")
    # ------------------------------------------
    # Traceback (most recent call last):
    #   File "C:\ProgramData\anaconda3\envs\a0\checkpowers.py", line 435, in sendcommand
    #     while True:

    # StopIteration
    # ------------------------------------------
    # Out[13]: [[b'XXXXCOMMANDSTARTXXXX\n'], [], StopIteration()]

    # In [14]: bashinstance.sendcommand("ls")
    # ------------------------------------------
    # Traceback (most recent call last):
    #   File "C:\ProgramData\anaconda3\envs\a0\checkpowers.py", line 414, in sendcommand
    #     self._proc.stdin.write(cmd + b"\n" + self.finish_command_bytes + b"\n")
    #     ^^^^^^^^^^^^^^^^^^^^^^^^
    # OSError: [Errno 22] Invalid argument
    # ------------------------------------------
    # Restarting ...
    # Out[14]: [[], [], None]

    # In [15]: bashinstance.sendcommand("ls")
    # Out[15]: [[b'Code.VisualElementsManifest.xml\n', b'Code.exe\n', b'L
    #
    so1, se1, error = powershellinstance.sendcommand("ls")
    so2, se2, error = adbinstance.sendcommand("ls")
    so3, se3, error = cmdinstance.sendcommand("ls")

    print(f"{so1,so2,so3=}")
    print(f"{se1,se2,se3=}")

    so1, se1, error = powershellinstance.sendcommand("lsxx")
    so2, se2, error = adbinstance.sendcommand("lsxx")
    so3, se3, error = cmdinstance.sendcommand("lsxx")

    print(f"{so1,so2,so3=}")
    print(f"{se1,se2,se3=}")

    so3, se3, error = bashinstance.sendcommand("ls")
    print(f"{so3, se3, error=}")

    psout, pserr, pserror = powershellinstance.sendcommand(
        "Get-Process | Format-Table *"
    )
    print(psout)
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/interactiveshellsubproc",
    "name": "interactiveshellsubproc",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "shell, deadlock, interactive, bidirectional",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/32/dc/4de104316c7255053a679804d9ce27a6364d192a56df072750a622a885f0/interactiveshellsubproc-0.13.tar.gz",
    "platform": null,
    "description": "\r\n# Interactive subprocess Instances for cmd.exe, powershell.exe, adb.exe, bash.exe without deadlocks\r\n\r\n## Easy, bidirectional communication with various subprocesses such as PowerShell, ADB, Command Prompt (Cmd), and Bash.\r\n\r\n### pip install interactiveshellsubproc\r\n\r\n### Tested against MSYS2 / powershell/cmd.dexe -> Windows 10 / Bluestacks / Python 3.11 / Windows 10\r\n\r\n## Advantages\r\n\r\n### Interactivity:\r\nAllows bidirectional communication with subprocesses, enabling command execution and retrieval of outputs.\r\n\r\n### Versatility:\r\nSupports various subprocess types, including PowerShell, ADB, Cmd, and Bash, catering to different use cases and environments. New classes for any other shell can be easily derived from the base class.\r\n\r\n### Error Handling:\r\nImplements error handling mechanisms such as automatic process restart when stdin breaks (BrokenPipe) and configurable restart policies, enhancing robustness.\r\n\r\n### configurability:\r\nOffers a range of configurable options such as daemon mode, output printing settings, and restart behavior, providing flexibility in usage.\r\n\r\n### Ease of Use:\r\nProvides a straightforward interface for sending commands to subprocesses and retrieving outputs, simplifying interaction with external processes.\r\n\r\n### Very low (almost no) deadlock danger: It very rarely deadlocks.\r\nIf you are sending normal shell commands, your instance will probably never deadlock. However, when you write, for example, a shell script and forget a curly brace or a bracket at the end and execute the half-backed command, or you open AWK or cat, and they start reading from stdin and not from a file or a PIPE because you didn't pass it, then chances are very high that the console will deadlock.\r\n\r\n```py\r\nfrom interactiveshellsubproc import (\r\n    SubProcessInteractivePowerShell,\r\n    SubProcessInteractiveAdb,\r\n    SubProcessInteractiveCmd,\r\n    SubProcessInteractiveBash,\r\n)\r\n\r\nif __name__ == \"__main__\":\r\n    powershellinstance = SubProcessInteractivePowerShell(\r\n        daemon=True,\r\n        restart_when_error=True,\r\n        restart_tries=5,\r\n        sleep_after_reconnection_attempt=5,\r\n        print_stderr=False,\r\n        print_stdout=False,\r\n        return_error=True,\r\n    )\r\n\r\n    stdout, stderr, exception = powershellinstance.sendcommand(\"ls\")\r\n    # Out[10]: [b\"lsbabababa : The term 'lsbabababa' is not recognized as the name of a cmdlet, function,....\r\n    stdout, stderr, exception = powershellinstance.sendcommand(\"lsbabababa\")\r\n    # [b\"lsbabababa : The term 'lsbabababa' is not recognized as the name of a cmdlet, function, script\r\n\r\n    adbinstance = SubProcessInteractiveAdb(\r\n        adb_path=r\"C:\\ProgramData\\chocolatey\\lib\\adb\\tools\\platform-tools\\adb.exe\",\r\n        device_serial=\"127.0.0.1:5845\",\r\n        convert_to_83=True,\r\n        daemon=True,\r\n        restart_when_error=True,\r\n        restart_tries=5,\r\n        sleep_after_reconnection_attempt=5,\r\n        print_stderr=False,\r\n        print_stdout=False,\r\n        return_error=True,\r\n    )\r\n    # In [1]: adbinstance.sendcommand(\"ls\")\r\n    # Out[1]: [[b'acct\\n', b'apex\\n', b'bin\\n', b'boot\\n', b'bugreports\\n', b...\r\n\r\n    # In [2]: adbinstance.stdin.close() # closing the pipe to simulate a problem\r\n\r\n    # In [3]: adbinstance.sendcommand(\"ls\")\r\n    # ------------------------------------------\r\n    # Traceback (most recent call last):\r\n    #   File \"C:\\ProgramData\\anaconda3\\envs\\a0\\checkpowers.py\", line 411, in sendcommand\r\n    #     self._proc.stdin.write((cmd + f\"\\n{self.finish_command}\\n\").encode())\r\n    # ValueError: write to closed file\r\n    # ------------------------------------------\r\n    # Restarting ...\r\n    # Out[3]: [[], [], None] # the output of the first command after the restart is mostly not complete\r\n\r\n    # In [4]: adbinstance.sendcommand(\"ls\") # After the first one, everything is like before\r\n    # Out[4]: [[b'acct\\n', b'apex\\n', b'bin\\n', b'boot\\n', b'bugreports\\n'...\r\n\r\n    cmdinstance = SubProcessInteractiveCmd(\r\n        daemon=True,\r\n        restart_when_error=True,\r\n        restart_tries=5,\r\n        sleep_after_reconnection_attempt=5,\r\n        print_stderr=False,\r\n        print_stdout=False,\r\n        return_error=True,\r\n    )\r\n    # In [6]: cmdinstance.sendcommand(\"ls\")\r\n    # Out[6]: [[b'Code.VisualElementsManifest.xml\\n', b'Code.exe\\n', b'LICENSES.chromium.html\\n',\r\n\r\n    # In [7]:  cmdinstance.stdin.close() # provoking a broken pipe\r\n\r\n    # All subclasses of SubProcessInteractive reconnect automatically\r\n    # In [8]: cmdinstance.sendcommand(\"ls\")\r\n    # ------------------------------------------\r\n    # Traceback (most recent call last):\r\n    #   File \"C:\\ProgramData\\anaconda3\\envs\\a0\\checkpowers.py\", line 411, in sendcommand\r\n    #     self._proc.stdin.write((cmd + f\"\\n{self.finish_command}\\n\").encode())\r\n    # ValueError: write to closed file\r\n    # ------------------------------------------\r\n    # Restarting ...\r\n    # Out[8]: [[], [], None]\r\n\r\n    # In [9]: cmdinstance.sendcommand(\"ls\")\r\n    # Out[9]: [[b'Code.VisualElementsManifest.xml\\n', b'Code.exe\\n',\r\n\r\n    bashinstance = SubProcessInteractiveBash(\r\n        bash_path=r\"C:\\msys64\\usr\\bin\\bash.exe\",\r\n        convert_to_83=True,\r\n        daemon=True,\r\n        restart_when_error=True,\r\n        restart_tries=5,\r\n        sleep_after_reconnection_attempt=5,\r\n        print_stderr=False,\r\n        print_stdout=False,\r\n        return_error=True,\r\n    )\r\n    # In [13]: bashinstance.sendcommand(\"exit\")\r\n    # ------------------------------------------\r\n    # Traceback (most recent call last):\r\n    #   File \"C:\\ProgramData\\anaconda3\\envs\\a0\\checkpowers.py\", line 435, in sendcommand\r\n    #     while True:\r\n\r\n    # StopIteration\r\n    # ------------------------------------------\r\n    # Out[13]: [[b'XXXXCOMMANDSTARTXXXX\\n'], [], StopIteration()]\r\n\r\n    # In [14]: bashinstance.sendcommand(\"ls\")\r\n    # ------------------------------------------\r\n    # Traceback (most recent call last):\r\n    #   File \"C:\\ProgramData\\anaconda3\\envs\\a0\\checkpowers.py\", line 414, in sendcommand\r\n    #     self._proc.stdin.write(cmd + b\"\\n\" + self.finish_command_bytes + b\"\\n\")\r\n    #     ^^^^^^^^^^^^^^^^^^^^^^^^\r\n    # OSError: [Errno 22] Invalid argument\r\n    # ------------------------------------------\r\n    # Restarting ...\r\n    # Out[14]: [[], [], None]\r\n\r\n    # In [15]: bashinstance.sendcommand(\"ls\")\r\n    # Out[15]: [[b'Code.VisualElementsManifest.xml\\n', b'Code.exe\\n', b'L\r\n    #\r\n    so1, se1, error = powershellinstance.sendcommand(\"ls\")\r\n    so2, se2, error = adbinstance.sendcommand(\"ls\")\r\n    so3, se3, error = cmdinstance.sendcommand(\"ls\")\r\n\r\n    print(f\"{so1,so2,so3=}\")\r\n    print(f\"{se1,se2,se3=}\")\r\n\r\n    so1, se1, error = powershellinstance.sendcommand(\"lsxx\")\r\n    so2, se2, error = adbinstance.sendcommand(\"lsxx\")\r\n    so3, se3, error = cmdinstance.sendcommand(\"lsxx\")\r\n\r\n    print(f\"{so1,so2,so3=}\")\r\n    print(f\"{se1,se2,se3=}\")\r\n\r\n    so3, se3, error = bashinstance.sendcommand(\"ls\")\r\n    print(f\"{so3, se3, error=}\")\r\n\r\n    psout, pserr, pserror = powershellinstance.sendcommand(\r\n        \"Get-Process | Format-Table *\"\r\n    )\r\n    print(psout)\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Interactive subprocess Instances for cmd.exe, powershell.exe, adb.exe, bash.exe without deadlocks - pure Python, no dependencies",
    "version": "0.13",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/interactiveshellsubproc"
    },
    "split_keywords": [
        "shell",
        " deadlock",
        " interactive",
        " bidirectional"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ab624a3d701bcf6b03e8039b959e14a48ad8fceba2b5b1dabd78b1684ed7f148",
                "md5": "acee21a410e841dccbc4cc0cd7994194",
                "sha256": "e366884e15b918db422be820cd6be0a963bf14386389d5df8e6bca98f9385232"
            },
            "downloads": -1,
            "filename": "interactiveshellsubproc-0.13-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "acee21a410e841dccbc4cc0cd7994194",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 14451,
            "upload_time": "2024-03-27T02:02:59",
            "upload_time_iso_8601": "2024-03-27T02:02:59.573716Z",
            "url": "https://files.pythonhosted.org/packages/ab/62/4a3d701bcf6b03e8039b959e14a48ad8fceba2b5b1dabd78b1684ed7f148/interactiveshellsubproc-0.13-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "32dc4de104316c7255053a679804d9ce27a6364d192a56df072750a622a885f0",
                "md5": "9a01c9bd088c2f0272679e5818c6fd44",
                "sha256": "c1b109b85cfd472febb9d39775de883e137f224daf18a1861106afbad42e9c33"
            },
            "downloads": -1,
            "filename": "interactiveshellsubproc-0.13.tar.gz",
            "has_sig": false,
            "md5_digest": "9a01c9bd088c2f0272679e5818c6fd44",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 14342,
            "upload_time": "2024-03-27T02:03:00",
            "upload_time_iso_8601": "2024-03-27T02:03:00.860965Z",
            "url": "https://files.pythonhosted.org/packages/32/dc/4de104316c7255053a679804d9ce27a6364d192a56df072750a622a885f0/interactiveshellsubproc-0.13.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-27 02:03:00",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "interactiveshellsubproc",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "interactiveshellsubproc"
}
        
Elapsed time: 0.21461s