subprocess-print-and-capture


Namesubprocess-print-and-capture JSON
Version 0.16 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/subprocess_print_and_capture
SummaryPrint and capture the output of a subprocess simultaneously
upload_time2023-03-14 06:17:22
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords subprocess simultaneously print capture
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Print and capture the output of a subprocess simultaneously





```python



$pip install subprocess-print-and-capture



from subprocess_print_and_capture import execute_subprocess, execute_subprocess_multiple_commands, execute_subprocess_multiple_commands_v2







# -----------------------------------------------------------------------------------------------------------------------------



# Update: you can now pass multiple "subcommands", you will see the output on screen, get the printed text as a list, and can kill the process with a hotkey (and still getting the output)  

adboutput = execute_subprocess_multiple_commands(

    "adb shell", ["ls", "sleep 1", "ls -R -s -i -n -H"], exit_keys="ctrl+x"

)

adboutput = execute_subprocess_multiple_commands_v2(

    "adb shell", ["ls", "sleep 1", "ls -R -s -i -n -H"], exit_keys="ctrl+x"

) #alternative, works sometimes better





# output screen

acct

boot

bugreports

cache

charger

config

d

data

default.prop

dev

etc

file_contexts.bin

fstab.nougat

fstab_sdcard.nougat

init

init.environ.rc

init.nougat.rc

init.rc

init.usb.configfs.rc

init.usb.rc

init.zygote32.rc

lib

mnt

oem

proc

property_contexts

root

sbin

sdcard

seapp_contexts

selinux_version

sepolicy

service_contexts

storage

sys

system

ueventd.nougat.rc

ueventd.rc

vendor





adboutput

Out[3]: 

['acct\n',

 'boot\n',

 'bugreports\n',

 'cache\n',

 'charger\n',

 'config\n',

 'd\n',

 'data\n',

 'default.prop\n',

 'dev\n',

 'etc\n',

 'file_contexts.bin\n',

 'fstab.nougat\n',

 'fstab_sdcard.nougat\n',

 'init\n',

 'init.environ.rc\n',

 'init.nougat.rc\n',

 'init.rc\n',

 'init.usb.configfs.rc\n',

 'init.usb.rc\n',

 'init.zygote32.rc\n',

 'lib\n',

 'mnt\n',

 'oem\n',

 'proc\n',

 'property_contexts\n',

 'root\n',

 'sbin\n',

 'sdcard\n',

 'seapp_contexts\n',

 'selinux_version\n',

 'sepolicy\n',

 'service_contexts\n',

 'storage\n',

 'sys\n',

 'system\n',

 'ueventd.nougat.rc\n',

 'ueventd.rc\n',

 'vendor\n',

 '.:\n',

 'total 1680\n',

 '     1    0 dr-xr-xr-x  68 0    0          0 2022-11-19 15:28 acct\n',

 '  3524    0 drwxrwxr-x   6 1000 1000     240 2022-11-19 15:28 boot\n',

 '  4108    0 lrwxrwxrwx   1 0    0         50 2022-11-19 15:28 bugreports -> /data/user_de/0/com.android.shell/files/bugreports\n',

 '  6103    0 drwxrwx---   6 1000 2001     120 2022-11-19 15:28 cache\n',

 '  4110    0 lrwxrwxrwx   1 0    0         13 2022-11-19 15:28 charger -> /sbin/healthd\n',

 '  4111    0 dr-x------   2 0    0         40 2022-11-19 15:28 config\n',

 '  4112    0 lrwxrwxrwx   1 0    0         17 2022-11-19 15:28 d -> /sys/kernel/debug\n',

 '     2    8 drwxrwx--x  38 1000 1000    4096 2022-11-19 15:28 data\n',

 '  4114    4 -rw-r--r--   1 0    0        958 2022-11-19 15:28 default.prop\n',

 '  4387    0 drwxr-xr-x  13 0    0       3840 2022-11-19 15:28 dev\n',

 '  4115    0 lrwxrwxrwx   1 0    0         11 2022-11-19 15:28 etc -> /system/etc\n',

 '  4116   76 -rw-r--r--   1 0    0      77090 2022-11-19 15:28 file_contexts.bin\n',

 '  4117    4 -rw-r-----   1 0    0        564 2022-11-19 15:28 fstab.nougat\n',

 '  4118    4 -rw-r-----   1 0    0        354 2022-11-19 15:28 fstab_sdcard.nougat\n',

 '  4119 1304 -rwxr-x---   1 0    0    1334780 2022-11-19 15:28 init\n',

 '  4120    4 -rwxr-x---   1 0    0        887 2022-11-19 15:28 init.environ.rc\n',

 '  4121    4 -rwxr-x---   1 0    0       3690 2022-11-19 15:28 init.nougat.rc\n',

 '  4122   28 -rwxr-x---   1 0    0      27803 2022-11-19 15:28 init.rc\n',

 '  4123   12 -rwxr-x---   1 0    0       9283 2022-11-19 15:28 init.usb.configfs.rc\n',

 '  4124    8 -rwxr-x---   1 0    0       5694 2022-11-19 15:28 init.usb.rc\n',

 '  4125    4 -rwxr-x---   1 0    0        411 2022-11-19 15:28 init.zygote32.rc\n',

 '  4443    0 lrwxrwxrwx   1 0    0         10 2022-11-19 15:28 lib -> system/lib\n',

 '  4457    0 drwxr-xr-x  11 0    1000     240 2022-11-19 15:28 mnt\n',

 '  4127    0 drwxr-xr-x   2 0    0         40 2022-11-19 15:28 oem\n',





# -----------------------------------------------------------------------------------------------------------------------------



cmd = r"""adb shell getevent -l"""

results = execute_subprocess(cmd, exit_keys="ctrl+e", end_of_printline="")



#output on screen

add device 1: /dev/input/event3

  name:     "fts"

add device 2: /dev/input/event2

  name:     "STM VL53L1 proximity sensor"

add device 3: /dev/input/event1

  name:     "qwerty"

add device 4: /dev/input/event0

  name:     "gpio_keys"

/dev/input/event3: EV_KEY       BTN_TOUCH            DOWN                

/dev/input/event3: EV_ABS       ABS_MT_TRACKING_ID   00001830            

/dev/input/event3: EV_ABS       ABS_MT_POSITION_X    00000299            

/dev/input/event3: EV_ABS       ABS_MT_POSITION_Y    000006f5            

/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MAJOR   00000040            

/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000030            

/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      0000000d            

/dev/input/event3: EV_SYN       SYN_REPORT           00000000            

/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MAJOR   00000080            

/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000080            

/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      00000022            

/dev/input/event3: EV_SYN       SYN_REPORT           00000000            

/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000070            

/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      00000028            

/dev/input/event3: EV_SYN       SYN_REPORT           00000000            

/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      0000002a      

....





results

Out[3]: 

['add device 1: /dev/input/event3\n',

 '  name:     "fts"\n',

 'add device 2: /dev/input/event2\n',

 '  name:     "STM VL53L1 proximity sensor"\n',

 'add device 3: /dev/input/event1\n',

 '  name:     "qwerty"\n',

 'add device 4: /dev/input/event0\n',

 '  name:     "gpio_keys"\n',

 '/dev/input/event3: EV_KEY       BTN_TOUCH            DOWN                \n',

 '/dev/input/event3: EV_ABS       ABS_MT_TRACKING_ID   00001830            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_POSITION_X    00000299            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_POSITION_Y    000006f5            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MAJOR   00000040            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000030            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      0000000d            \n',

 '/dev/input/event3: EV_SYN       SYN_REPORT           00000000            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MAJOR   00000080            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000080            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      00000022            \n',

 '/dev/input/event3: EV_SYN       SYN_REPORT           00000000            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000070            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      00000028            \n',

 '/dev/input/event3: EV_SYN       SYN_REPORT           00000000            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      0000002a            \n',

 '/dev/input/event3: EV_SYN       SYN_REPORT           00000000            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_POSITION_X    0000029a            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_POSITION_Y    000006f4            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000080            \n',

 '/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      0000002c            \n',

 ....

 

 

 # -----------------------------------------------------------------------------------------------------------------------------

# Update 2022-12-01: added timeout / new functions

cmd = r"ls -R -s -i -n -H"

results = execute_subprocess(cmd, exit_keys="ctrl+e", end_of_printline="",timeout=2) # Useful if the process is frozen, will return the captured stdout



from subprocess_print_and_capture import execute_subprocess, execute_subprocess_multiple_commands, execute_subprocess_multiple_commands_v2,execute_subprocess_multiple_commands_with_timeout_bin ,execute_subprocess_multiple_commands_with_timeout_str  



adb_path = "C:\\Users\\Gamer\\AppData\\Local\\Android\\Sdk\\platform-tools\\adb.exe"

deviceserial = "localhost:5745"

cmd = rf"{adb_path} -s {deviceserial} shell"

subcommands=["ls", "sleep 1", "ls -R -s -i -n -H"]

results1 = execute_subprocess_multiple_commands_with_timeout_bin(cmd=cmd, subcommands=subcommands, exit_keys="ctrl+e", end_of_printline="",print_output=True,timeout=2) # returns a list containing bin (to avoid encoding problems)

results2 = execute_subprocess_multiple_commands_with_timeout_str(cmd=cmd, subcommands=subcommands, exit_keys="ctrl+e", end_of_printline="",print_output=True,timeout=2) # returns a list containing str

 

  # -----------------------------------------------------------------------------------------------------------------------------



 

# Update 2023-03-14:  Added some new stuff:

execute_subprocess_multiple_commands_with_timeout_bin2

    Executes a subprocess and runs one or more commands in it, with the option to add a timeout and exit keys.



    :param cmd: The command to be executed in the subprocess.

    :type cmd: str



    :param subcommands: Additional commands to run in the subprocess, as a list or tuple of strings or a single string. Defaults to None.

    :type subcommands: Union[list, tuple, None, str]



    :param exit_keys: If set, the process can be terminated by pressing the specified key combination (e.g. "ctrl+alt+x"). Defaults to None.

    :type exit_keys: Union[str, None]



    :param end_of_printline: The string to be printed at the end of each line of output. Defaults to "".

    :type end_of_printline: str



    :param print_output: Whether to print the output of the subprocess to the console. Defaults to True.

    :type print_output: bool



    :param timeout: The maximum amount of time to allow the subprocess to run before terminating it. Defaults to None.

    :type timeout: Optional[float]



    :param cwd: The working directory for the subprocess. Defaults to the current working directory.

    :type cwd: str



    :param decodestdout: The character encoding to use for decoding the output of the subprocess. Defaults to None.

    :type decodestdout: Optional[str]



    :param decodestdouterrors: The error handling mode to use for decoding the output of the subprocess. Defaults to "ignore".

    :type decodestdouterrors: str



    :param stderrfile: The file path to write standard error output to. Defaults to None.

    :type stderrfile: Optional[str]



    :param stdoutfile: The file path to write standard output to. Defaults to None.

    :type stdoutfile: Optional[str]



    :param create_no_window: Whether to create a new console window for the subprocess. Defaults to True.

    :type create_no_window: bool



    :param use_shlex: Whether to use the shlex module to split the command string into arguments. Defaults to False.

    :type use_shlex: bool



    :param pyinstaller_module_name: The name of the PyInstaller module to run in the subprocess. Defaults to None.

    :type pyinstaller_module_name: Optional[str]



    :param pyinstaller_entry: The name of the PyInstaller entry point to run in the subprocess. Defaults to None.

    :type pyinstaller_entry: Optional[str]



    :param argsforpyinstaller: Additional arguments to pass to the PyInstaller subprocess. Defaults to ().

    :type argsforpyinstaller: Tuple



    :param kwargs: Additional keyword arguments to pass to the subprocess.Popen() constructor.

    :type kwargs: Any



    :return: A list 



execute_as_mainprocess

    Starts a new process using the `start` command on Windows.



    Args:

        cmd (Union[str, List[str]]): The command to execute. Can be a string or a list of strings.

        nameofexe (Optional[str]): The name of the executable file to look for after the process has started. Defaults to None.

        returnpid (bool): Whether to return the PID of the new process or not. Defaults to True.

        timeout_get_pid (int): The maximum amount of time (in seconds) to wait for the new process to start. Defaults to 5.

        creationtimebreak (int): The maximum amount of time (in seconds) to wait for the new process to start after its creation time - if found, returns immediately. If not, the function returns the process after timeout_get_pid Defaults to 1.



    Returns:

        Union[subprocess.Popen, psutil.Process]: If `returnpid` is True, returns the `psutil.Process` object corresponding to the new process. Otherwise, returns the `subprocess.Popen` object that was used to start the new process.



# Examples

adb_path = "C:\\Users\\Gamer\\AppData\\Local\\Android\\Sdk\\platform-tools\\adb.exe"

deviceserial = "localhost:62037"

cmd2 = f"adb -s {deviceserial} shell su -c 'ls -la'"

for q in range(2):

    pap = execute_subprocess_multiple_commands_with_timeout_bin2(cmd=cmd2)

    with open("c:\\asdfasdfasdfasdfasdfasdf.txt", mode="a", encoding="utf-8") as f:

        f.write(b"".join(pap).decode("utf-8", "ignore"))



for q in range(2):

    pap = execute_subprocess_multiple_commands_with_timeout_bin2(

        cmd=cmd2,

        decodestdout="utf-8",

        decodestdouterrors="ignore",

        stdoutfile="f:\\lologo.txt",

    )

    print(pap)

    with open("c:\\asdfasdfasdfasdfasdfasdf.txt", mode="a", encoding="utf-8") as f:

        f.write("".join(pap))



for q in range(2):

    pap = execute_subprocess_multiple_commands_with_timeout_bin2(

        cmd='dir /bvabds /dsxds ""', stderrfile="f:\\testtestloglog.txt"

    )

    with open("c:\\asdfasdfasdfasdfasdfasdf.txt", mode="a", encoding="utf-8") as f:

        f.write(b"".join(pap).decode("utf-8", "ignore"))



for q in range(2):

    pap = execute_subprocess_multiple_commands_with_timeout_bin2(

        cmd='dir /b ""',

        stderrfile="f:\\testtestloglog.txt",

        stdoutfile="f:\\lologo.txt",

    )

    with open("c:\\asdfasdfasdfasdfasdfasdf.txt", mode="a", encoding="utf-8") as f:

        f.write(b"".join(pap).decode("utf-8", "ignore"))





pap = execute_subprocess_multiple_commands_with_timeout_bin2(

    cmd="notepad.exe", subcommands=None, timeout=2

)



for q in range(2):

    pap = execute_subprocess_multiple_commands_with_timeout_bin2(

        cmd=["ls"],

        stderrfile="f:\\testtestloglog.txt",

        stdoutfile="c:\\locccclogo.txt",

    )



mainp = execute_as_mainprocess(

    cmd="notepad.exe",

    nameofexe="notepad.exe",

    returnpid=True,

    timeout_get_pid=5,

    creationtimebreak=1,

)

print(mainp)



pap = execute_subprocess_multiple_commands_with_timeout_bin2(

    cmd='notepad.exe "C:\locccclogo.txt"',

    stdoutfile="c:\\locccclogo.txt2",

    stderrfile="f:\\testtestloglog2.txt",

    subcommands=None,

    timeout=2,

    use_shlex=True,

)



bab = execute_subprocess_multiple_commands_with_timeout_bin2(

    cmd="notepad.exe",

    subcommands=None,

    exit_keys=None,

    end_of_printline="",

    print_output=True,

    timeout=None,

    cwd=os.getcwd(),

    decodestdout=None,

    decodestdouterrors="ignore",

    stdoutfile="c:\\locccclogo3.txt",

    stderrfile="f:\\testtestloglog3.txt",

    create_no_window=False,

    use_shlex=False,

    pyinstaller_module_name="tes",

    pyinstaller_entry="tete",

    argsforpyinstaller=(),

)





bab2 = execute_subprocess_multiple_commands_with_timeout_bin2_thread(

    cmd="dir",

    subcommands=None,

    exit_keys=None,

    end_of_printline="",

    print_output=True,

    timeout=None,

    cwd=os.getcwd(),

    decodestdout=None,

    decodestdouterrors="ignore",

    stdoutfile="c:\\locccclogo3.txt",

    stderrfile="f:\\testtestloglog3.txt",

    CREATE_NO_WINDOW=False,

    use_shlex=False,

    pyinstaller_module_name="tes",

    pyinstaller_entry="tete",

    argsforpyinstaller=(),

)



```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/subprocess_print_and_capture",
    "name": "subprocess-print-and-capture",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "subprocess,simultaneously,print,capture",
    "author": "Johannes Fischer",
    "author_email": "<aulasparticularesdealemaosp@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/72/3f/71161315a53cd49c708bd69775bc2fc0227dfaecce719086e0d43d62696c/subprocess_print_and_capture-0.16.tar.gz",
    "platform": null,
    "description": "\n# Print and capture the output of a subprocess simultaneously\n\n\n\n\n\n```python\n\n\n\n$pip install subprocess-print-and-capture\n\n\n\nfrom subprocess_print_and_capture import execute_subprocess, execute_subprocess_multiple_commands, execute_subprocess_multiple_commands_v2\n\n\n\n\n\n\n\n# -----------------------------------------------------------------------------------------------------------------------------\n\n\n\n# Update: you can now pass multiple \"subcommands\", you will see the output on screen, get the printed text as a list, and can kill the process with a hotkey (and still getting the output)  \n\nadboutput = execute_subprocess_multiple_commands(\n\n    \"adb shell\", [\"ls\", \"sleep 1\", \"ls -R -s -i -n -H\"], exit_keys=\"ctrl+x\"\n\n)\n\nadboutput = execute_subprocess_multiple_commands_v2(\n\n    \"adb shell\", [\"ls\", \"sleep 1\", \"ls -R -s -i -n -H\"], exit_keys=\"ctrl+x\"\n\n) #alternative, works sometimes better\n\n\n\n\n\n# output screen\n\nacct\n\nboot\n\nbugreports\n\ncache\n\ncharger\n\nconfig\n\nd\n\ndata\n\ndefault.prop\n\ndev\n\netc\n\nfile_contexts.bin\n\nfstab.nougat\n\nfstab_sdcard.nougat\n\ninit\n\ninit.environ.rc\n\ninit.nougat.rc\n\ninit.rc\n\ninit.usb.configfs.rc\n\ninit.usb.rc\n\ninit.zygote32.rc\n\nlib\n\nmnt\n\noem\n\nproc\n\nproperty_contexts\n\nroot\n\nsbin\n\nsdcard\n\nseapp_contexts\n\nselinux_version\n\nsepolicy\n\nservice_contexts\n\nstorage\n\nsys\n\nsystem\n\nueventd.nougat.rc\n\nueventd.rc\n\nvendor\n\n\n\n\n\nadboutput\n\nOut[3]: \n\n['acct\\n',\n\n 'boot\\n',\n\n 'bugreports\\n',\n\n 'cache\\n',\n\n 'charger\\n',\n\n 'config\\n',\n\n 'd\\n',\n\n 'data\\n',\n\n 'default.prop\\n',\n\n 'dev\\n',\n\n 'etc\\n',\n\n 'file_contexts.bin\\n',\n\n 'fstab.nougat\\n',\n\n 'fstab_sdcard.nougat\\n',\n\n 'init\\n',\n\n 'init.environ.rc\\n',\n\n 'init.nougat.rc\\n',\n\n 'init.rc\\n',\n\n 'init.usb.configfs.rc\\n',\n\n 'init.usb.rc\\n',\n\n 'init.zygote32.rc\\n',\n\n 'lib\\n',\n\n 'mnt\\n',\n\n 'oem\\n',\n\n 'proc\\n',\n\n 'property_contexts\\n',\n\n 'root\\n',\n\n 'sbin\\n',\n\n 'sdcard\\n',\n\n 'seapp_contexts\\n',\n\n 'selinux_version\\n',\n\n 'sepolicy\\n',\n\n 'service_contexts\\n',\n\n 'storage\\n',\n\n 'sys\\n',\n\n 'system\\n',\n\n 'ueventd.nougat.rc\\n',\n\n 'ueventd.rc\\n',\n\n 'vendor\\n',\n\n '.:\\n',\n\n 'total 1680\\n',\n\n '     1    0 dr-xr-xr-x  68 0    0          0 2022-11-19 15:28 acct\\n',\n\n '  3524    0 drwxrwxr-x   6 1000 1000     240 2022-11-19 15:28 boot\\n',\n\n '  4108    0 lrwxrwxrwx   1 0    0         50 2022-11-19 15:28 bugreports -> /data/user_de/0/com.android.shell/files/bugreports\\n',\n\n '  6103    0 drwxrwx---   6 1000 2001     120 2022-11-19 15:28 cache\\n',\n\n '  4110    0 lrwxrwxrwx   1 0    0         13 2022-11-19 15:28 charger -> /sbin/healthd\\n',\n\n '  4111    0 dr-x------   2 0    0         40 2022-11-19 15:28 config\\n',\n\n '  4112    0 lrwxrwxrwx   1 0    0         17 2022-11-19 15:28 d -> /sys/kernel/debug\\n',\n\n '     2    8 drwxrwx--x  38 1000 1000    4096 2022-11-19 15:28 data\\n',\n\n '  4114    4 -rw-r--r--   1 0    0        958 2022-11-19 15:28 default.prop\\n',\n\n '  4387    0 drwxr-xr-x  13 0    0       3840 2022-11-19 15:28 dev\\n',\n\n '  4115    0 lrwxrwxrwx   1 0    0         11 2022-11-19 15:28 etc -> /system/etc\\n',\n\n '  4116   76 -rw-r--r--   1 0    0      77090 2022-11-19 15:28 file_contexts.bin\\n',\n\n '  4117    4 -rw-r-----   1 0    0        564 2022-11-19 15:28 fstab.nougat\\n',\n\n '  4118    4 -rw-r-----   1 0    0        354 2022-11-19 15:28 fstab_sdcard.nougat\\n',\n\n '  4119 1304 -rwxr-x---   1 0    0    1334780 2022-11-19 15:28 init\\n',\n\n '  4120    4 -rwxr-x---   1 0    0        887 2022-11-19 15:28 init.environ.rc\\n',\n\n '  4121    4 -rwxr-x---   1 0    0       3690 2022-11-19 15:28 init.nougat.rc\\n',\n\n '  4122   28 -rwxr-x---   1 0    0      27803 2022-11-19 15:28 init.rc\\n',\n\n '  4123   12 -rwxr-x---   1 0    0       9283 2022-11-19 15:28 init.usb.configfs.rc\\n',\n\n '  4124    8 -rwxr-x---   1 0    0       5694 2022-11-19 15:28 init.usb.rc\\n',\n\n '  4125    4 -rwxr-x---   1 0    0        411 2022-11-19 15:28 init.zygote32.rc\\n',\n\n '  4443    0 lrwxrwxrwx   1 0    0         10 2022-11-19 15:28 lib -> system/lib\\n',\n\n '  4457    0 drwxr-xr-x  11 0    1000     240 2022-11-19 15:28 mnt\\n',\n\n '  4127    0 drwxr-xr-x   2 0    0         40 2022-11-19 15:28 oem\\n',\n\n\n\n\n\n# -----------------------------------------------------------------------------------------------------------------------------\n\n\n\ncmd = r\"\"\"adb shell getevent -l\"\"\"\n\nresults = execute_subprocess(cmd, exit_keys=\"ctrl+e\", end_of_printline=\"\")\n\n\n\n#output on screen\n\nadd device 1: /dev/input/event3\n\n  name:     \"fts\"\n\nadd device 2: /dev/input/event2\n\n  name:     \"STM VL53L1 proximity sensor\"\n\nadd device 3: /dev/input/event1\n\n  name:     \"qwerty\"\n\nadd device 4: /dev/input/event0\n\n  name:     \"gpio_keys\"\n\n/dev/input/event3: EV_KEY       BTN_TOUCH            DOWN                \n\n/dev/input/event3: EV_ABS       ABS_MT_TRACKING_ID   00001830            \n\n/dev/input/event3: EV_ABS       ABS_MT_POSITION_X    00000299            \n\n/dev/input/event3: EV_ABS       ABS_MT_POSITION_Y    000006f5            \n\n/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MAJOR   00000040            \n\n/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000030            \n\n/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      0000000d            \n\n/dev/input/event3: EV_SYN       SYN_REPORT           00000000            \n\n/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MAJOR   00000080            \n\n/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000080            \n\n/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      00000022            \n\n/dev/input/event3: EV_SYN       SYN_REPORT           00000000            \n\n/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000070            \n\n/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      00000028            \n\n/dev/input/event3: EV_SYN       SYN_REPORT           00000000            \n\n/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      0000002a      \n\n....\n\n\n\n\n\nresults\n\nOut[3]: \n\n['add device 1: /dev/input/event3\\n',\n\n '  name:     \"fts\"\\n',\n\n 'add device 2: /dev/input/event2\\n',\n\n '  name:     \"STM VL53L1 proximity sensor\"\\n',\n\n 'add device 3: /dev/input/event1\\n',\n\n '  name:     \"qwerty\"\\n',\n\n 'add device 4: /dev/input/event0\\n',\n\n '  name:     \"gpio_keys\"\\n',\n\n '/dev/input/event3: EV_KEY       BTN_TOUCH            DOWN                \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_TRACKING_ID   00001830            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_POSITION_X    00000299            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_POSITION_Y    000006f5            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MAJOR   00000040            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000030            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      0000000d            \\n',\n\n '/dev/input/event3: EV_SYN       SYN_REPORT           00000000            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MAJOR   00000080            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000080            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      00000022            \\n',\n\n '/dev/input/event3: EV_SYN       SYN_REPORT           00000000            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000070            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      00000028            \\n',\n\n '/dev/input/event3: EV_SYN       SYN_REPORT           00000000            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      0000002a            \\n',\n\n '/dev/input/event3: EV_SYN       SYN_REPORT           00000000            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_POSITION_X    0000029a            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_POSITION_Y    000006f4            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_TOUCH_MINOR   00000080            \\n',\n\n '/dev/input/event3: EV_ABS       ABS_MT_PRESSURE      0000002c            \\n',\n\n ....\n\n \n\n \n\n # -----------------------------------------------------------------------------------------------------------------------------\n\n# Update 2022-12-01: added timeout / new functions\n\ncmd = r\"ls -R -s -i -n -H\"\n\nresults = execute_subprocess(cmd, exit_keys=\"ctrl+e\", end_of_printline=\"\",timeout=2) # Useful if the process is frozen, will return the captured stdout\n\n\n\nfrom subprocess_print_and_capture import execute_subprocess, execute_subprocess_multiple_commands, execute_subprocess_multiple_commands_v2,execute_subprocess_multiple_commands_with_timeout_bin ,execute_subprocess_multiple_commands_with_timeout_str  \n\n\n\nadb_path = \"C:\\\\Users\\\\Gamer\\\\AppData\\\\Local\\\\Android\\\\Sdk\\\\platform-tools\\\\adb.exe\"\n\ndeviceserial = \"localhost:5745\"\n\ncmd = rf\"{adb_path} -s {deviceserial} shell\"\n\nsubcommands=[\"ls\", \"sleep 1\", \"ls -R -s -i -n -H\"]\n\nresults1 = execute_subprocess_multiple_commands_with_timeout_bin(cmd=cmd, subcommands=subcommands, exit_keys=\"ctrl+e\", end_of_printline=\"\",print_output=True,timeout=2) # returns a list containing bin (to avoid encoding problems)\n\nresults2 = execute_subprocess_multiple_commands_with_timeout_str(cmd=cmd, subcommands=subcommands, exit_keys=\"ctrl+e\", end_of_printline=\"\",print_output=True,timeout=2) # returns a list containing str\n\n \n\n  # -----------------------------------------------------------------------------------------------------------------------------\n\n\n\n \n\n# Update 2023-03-14:  Added some new stuff:\n\nexecute_subprocess_multiple_commands_with_timeout_bin2\n\n    Executes a subprocess and runs one or more commands in it, with the option to add a timeout and exit keys.\n\n\n\n    :param cmd: The command to be executed in the subprocess.\n\n    :type cmd: str\n\n\n\n    :param subcommands: Additional commands to run in the subprocess, as a list or tuple of strings or a single string. Defaults to None.\n\n    :type subcommands: Union[list, tuple, None, str]\n\n\n\n    :param exit_keys: If set, the process can be terminated by pressing the specified key combination (e.g. \"ctrl+alt+x\"). Defaults to None.\n\n    :type exit_keys: Union[str, None]\n\n\n\n    :param end_of_printline: The string to be printed at the end of each line of output. Defaults to \"\".\n\n    :type end_of_printline: str\n\n\n\n    :param print_output: Whether to print the output of the subprocess to the console. Defaults to True.\n\n    :type print_output: bool\n\n\n\n    :param timeout: The maximum amount of time to allow the subprocess to run before terminating it. Defaults to None.\n\n    :type timeout: Optional[float]\n\n\n\n    :param cwd: The working directory for the subprocess. Defaults to the current working directory.\n\n    :type cwd: str\n\n\n\n    :param decodestdout: The character encoding to use for decoding the output of the subprocess. Defaults to None.\n\n    :type decodestdout: Optional[str]\n\n\n\n    :param decodestdouterrors: The error handling mode to use for decoding the output of the subprocess. Defaults to \"ignore\".\n\n    :type decodestdouterrors: str\n\n\n\n    :param stderrfile: The file path to write standard error output to. Defaults to None.\n\n    :type stderrfile: Optional[str]\n\n\n\n    :param stdoutfile: The file path to write standard output to. Defaults to None.\n\n    :type stdoutfile: Optional[str]\n\n\n\n    :param create_no_window: Whether to create a new console window for the subprocess. Defaults to True.\n\n    :type create_no_window: bool\n\n\n\n    :param use_shlex: Whether to use the shlex module to split the command string into arguments. Defaults to False.\n\n    :type use_shlex: bool\n\n\n\n    :param pyinstaller_module_name: The name of the PyInstaller module to run in the subprocess. Defaults to None.\n\n    :type pyinstaller_module_name: Optional[str]\n\n\n\n    :param pyinstaller_entry: The name of the PyInstaller entry point to run in the subprocess. Defaults to None.\n\n    :type pyinstaller_entry: Optional[str]\n\n\n\n    :param argsforpyinstaller: Additional arguments to pass to the PyInstaller subprocess. Defaults to ().\n\n    :type argsforpyinstaller: Tuple\n\n\n\n    :param kwargs: Additional keyword arguments to pass to the subprocess.Popen() constructor.\n\n    :type kwargs: Any\n\n\n\n    :return: A list \n\n\n\nexecute_as_mainprocess\n\n    Starts a new process using the `start` command on Windows.\n\n\n\n    Args:\n\n        cmd (Union[str, List[str]]): The command to execute. Can be a string or a list of strings.\n\n        nameofexe (Optional[str]): The name of the executable file to look for after the process has started. Defaults to None.\n\n        returnpid (bool): Whether to return the PID of the new process or not. Defaults to True.\n\n        timeout_get_pid (int): The maximum amount of time (in seconds) to wait for the new process to start. Defaults to 5.\n\n        creationtimebreak (int): The maximum amount of time (in seconds) to wait for the new process to start after its creation time - if found, returns immediately. If not, the function returns the process after timeout_get_pid Defaults to 1.\n\n\n\n    Returns:\n\n        Union[subprocess.Popen, psutil.Process]: If `returnpid` is True, returns the `psutil.Process` object corresponding to the new process. Otherwise, returns the `subprocess.Popen` object that was used to start the new process.\n\n\n\n# Examples\n\nadb_path = \"C:\\\\Users\\\\Gamer\\\\AppData\\\\Local\\\\Android\\\\Sdk\\\\platform-tools\\\\adb.exe\"\n\ndeviceserial = \"localhost:62037\"\n\ncmd2 = f\"adb -s {deviceserial} shell su -c 'ls -la'\"\n\nfor q in range(2):\n\n    pap = execute_subprocess_multiple_commands_with_timeout_bin2(cmd=cmd2)\n\n    with open(\"c:\\\\asdfasdfasdfasdfasdfasdf.txt\", mode=\"a\", encoding=\"utf-8\") as f:\n\n        f.write(b\"\".join(pap).decode(\"utf-8\", \"ignore\"))\n\n\n\nfor q in range(2):\n\n    pap = execute_subprocess_multiple_commands_with_timeout_bin2(\n\n        cmd=cmd2,\n\n        decodestdout=\"utf-8\",\n\n        decodestdouterrors=\"ignore\",\n\n        stdoutfile=\"f:\\\\lologo.txt\",\n\n    )\n\n    print(pap)\n\n    with open(\"c:\\\\asdfasdfasdfasdfasdfasdf.txt\", mode=\"a\", encoding=\"utf-8\") as f:\n\n        f.write(\"\".join(pap))\n\n\n\nfor q in range(2):\n\n    pap = execute_subprocess_multiple_commands_with_timeout_bin2(\n\n        cmd='dir /bvabds /dsxds \"\"', stderrfile=\"f:\\\\testtestloglog.txt\"\n\n    )\n\n    with open(\"c:\\\\asdfasdfasdfasdfasdfasdf.txt\", mode=\"a\", encoding=\"utf-8\") as f:\n\n        f.write(b\"\".join(pap).decode(\"utf-8\", \"ignore\"))\n\n\n\nfor q in range(2):\n\n    pap = execute_subprocess_multiple_commands_with_timeout_bin2(\n\n        cmd='dir /b \"\"',\n\n        stderrfile=\"f:\\\\testtestloglog.txt\",\n\n        stdoutfile=\"f:\\\\lologo.txt\",\n\n    )\n\n    with open(\"c:\\\\asdfasdfasdfasdfasdfasdf.txt\", mode=\"a\", encoding=\"utf-8\") as f:\n\n        f.write(b\"\".join(pap).decode(\"utf-8\", \"ignore\"))\n\n\n\n\n\npap = execute_subprocess_multiple_commands_with_timeout_bin2(\n\n    cmd=\"notepad.exe\", subcommands=None, timeout=2\n\n)\n\n\n\nfor q in range(2):\n\n    pap = execute_subprocess_multiple_commands_with_timeout_bin2(\n\n        cmd=[\"ls\"],\n\n        stderrfile=\"f:\\\\testtestloglog.txt\",\n\n        stdoutfile=\"c:\\\\locccclogo.txt\",\n\n    )\n\n\n\nmainp = execute_as_mainprocess(\n\n    cmd=\"notepad.exe\",\n\n    nameofexe=\"notepad.exe\",\n\n    returnpid=True,\n\n    timeout_get_pid=5,\n\n    creationtimebreak=1,\n\n)\n\nprint(mainp)\n\n\n\npap = execute_subprocess_multiple_commands_with_timeout_bin2(\n\n    cmd='notepad.exe \"C:\\locccclogo.txt\"',\n\n    stdoutfile=\"c:\\\\locccclogo.txt2\",\n\n    stderrfile=\"f:\\\\testtestloglog2.txt\",\n\n    subcommands=None,\n\n    timeout=2,\n\n    use_shlex=True,\n\n)\n\n\n\nbab = execute_subprocess_multiple_commands_with_timeout_bin2(\n\n    cmd=\"notepad.exe\",\n\n    subcommands=None,\n\n    exit_keys=None,\n\n    end_of_printline=\"\",\n\n    print_output=True,\n\n    timeout=None,\n\n    cwd=os.getcwd(),\n\n    decodestdout=None,\n\n    decodestdouterrors=\"ignore\",\n\n    stdoutfile=\"c:\\\\locccclogo3.txt\",\n\n    stderrfile=\"f:\\\\testtestloglog3.txt\",\n\n    create_no_window=False,\n\n    use_shlex=False,\n\n    pyinstaller_module_name=\"tes\",\n\n    pyinstaller_entry=\"tete\",\n\n    argsforpyinstaller=(),\n\n)\n\n\n\n\n\nbab2 = execute_subprocess_multiple_commands_with_timeout_bin2_thread(\n\n    cmd=\"dir\",\n\n    subcommands=None,\n\n    exit_keys=None,\n\n    end_of_printline=\"\",\n\n    print_output=True,\n\n    timeout=None,\n\n    cwd=os.getcwd(),\n\n    decodestdout=None,\n\n    decodestdouterrors=\"ignore\",\n\n    stdoutfile=\"c:\\\\locccclogo3.txt\",\n\n    stderrfile=\"f:\\\\testtestloglog3.txt\",\n\n    CREATE_NO_WINDOW=False,\n\n    use_shlex=False,\n\n    pyinstaller_module_name=\"tes\",\n\n    pyinstaller_entry=\"tete\",\n\n    argsforpyinstaller=(),\n\n)\n\n\n\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Print and capture the output of a subprocess simultaneously",
    "version": "0.16",
    "project_urls": {
        "Homepage": "https://github.com/hansalemaos/subprocess_print_and_capture"
    },
    "split_keywords": [
        "subprocess",
        "simultaneously",
        "print",
        "capture"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "82179340cef6cf7a3e6ef6a2a7128f51b61340154076a7214cb6b5b8a0324265",
                "md5": "588ca27853aae2607a6e7945956b33d6",
                "sha256": "8556c9cfdceffb50ca195e6a6e6120b4f8621e2d612e0bbb45d72b477c405dd8"
            },
            "downloads": -1,
            "filename": "subprocess_print_and_capture-0.16-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "588ca27853aae2607a6e7945956b33d6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 15676,
            "upload_time": "2023-03-14T06:17:20",
            "upload_time_iso_8601": "2023-03-14T06:17:20.895700Z",
            "url": "https://files.pythonhosted.org/packages/82/17/9340cef6cf7a3e6ef6a2a7128f51b61340154076a7214cb6b5b8a0324265/subprocess_print_and_capture-0.16-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "723f71161315a53cd49c708bd69775bc2fc0227dfaecce719086e0d43d62696c",
                "md5": "0c185ec75f5a370bca5af90e4ff32b5f",
                "sha256": "1e00f4e63ae77fb0b2def94a54bac7ea72cecbcc73d079bf6aa8cfd08574f40c"
            },
            "downloads": -1,
            "filename": "subprocess_print_and_capture-0.16.tar.gz",
            "has_sig": false,
            "md5_digest": "0c185ec75f5a370bca5af90e4ff32b5f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 14927,
            "upload_time": "2023-03-14T06:17:22",
            "upload_time_iso_8601": "2023-03-14T06:17:22.407053Z",
            "url": "https://files.pythonhosted.org/packages/72/3f/71161315a53cd49c708bd69775bc2fc0227dfaecce719086e0d43d62696c/subprocess_print_and_capture-0.16.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-14 06:17:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hansalemaos",
    "github_project": "subprocess_print_and_capture",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "subprocess-print-and-capture"
}
        
Elapsed time: 1.78149s