Exemplo n.º 1
0
def run_parallel_context():
    print('Running run_parallel_context')
    import sys
    import time
    from shell_proc import Shell, python_args

    if Shell.is_linux():
        python_args.PYTHON = 'python3'

    with Shell(stdout=sys.stdout, stderr=sys.stderr, shell=True) as sh:
        # if sh.is_linux():  # This wont work with parallel due to a new shell being created.
        #     sh('alias python=python3')

        with sh.parallel() as p:
            for i in range(10):
                if i == 3:
                    p.python(
                        '-c', 'import os', 'import time', 'time.sleep(1)',
                        "print('My ID:', {id}, 'My PID:', os.getpid(), time.time())"
                        .format(id=i))
                else:
                    p.python(
                        '-c', 'import os', 'import time',
                        "print('My ID:', {id}, 'My PID:', os.getpid(), time.time())"
                        .format(id=i))
            # p.wait() on exit context
        print('finished parallel')
Exemplo n.º 2
0
def run_module():
    import sys
    from shell_proc import Shell, python_args

    if Shell.is_linux():
        python_args.PYTHON = 'python3'

    with Shell(stdout=sys.stdout, stderr=sys.stderr) as sh:
        if sh.is_linux():
            sh('alias python=python3')

        sh.python('-m', 'pip', '-V')
Exemplo n.º 3
0
def run_python_file():
    import sys
    from shell_proc import Shell, python_args

    if Shell.is_linux():
        python_args.PYTHON = 'python3'

    with Shell(stdout=sys.stdout, stderr=sys.stderr) as sh:
        if sh.is_linux():
            sh('alias python=python3')

        sh.python('hello_world.py')
Exemplo n.º 4
0
def run_command():
    import sys
    from shell_proc import Shell, python_args

    if Shell.is_linux():
        python_args.PYTHON = 'python3'

    with Shell(stdout=sys.stdout, stderr=sys.stderr) as sh:
        if sh.is_linux():
            sh('alias python=python3')

        sh.python('-c', 'import os', 'print("My PID:", os.getpid())')

        # Try different quotes
        sh.python('-c', 'import os', "print('My PID:', os.getpid())")
Exemplo n.º 5
0
def run_subprocess():
    # Create the continuous terminal process
    if Shell.is_windows():
        proc = Popen('cmd.exe',
                     stdin=PIPE,
                     stdout=PIPE,
                     stderr=PIPE,
                     shell=False)
    else:
        proc = Popen('/bin/bash',
                     stdin=PIPE,
                     stdout=PIPE,
                     stderr=PIPE,
                     shell=False)

    # Cannot communicate multiple times
    # out, err = proc.communicate(input=b'cd ./storage\n')
    # print(out, err, proc.poll())
    # out, err = proc.communicate(input=b'dir\n')
    # print(out, err)

    proc.stdin.write(b'cd ./storage\n')
    proc.stdin.flush()
    print(proc.poll(), proc.returncode)
    proc.stdin.write(b'dir\n')
    proc.stdin.flush()
    print(proc.poll(), proc.returncode)
Exemplo n.º 6
0
def run_context_manager():
    import sys
    from shell_proc import Shell

    with Shell(stdout=sys.stdout, stderr=sys.stderr) as sh:
        sh.run('mkdir storage')
        sh('cd storage')  # Same as sh.run()
        sh('echo Hello World! > hello.txt')

        if sh.is_windows():
            sh('python -m venv ./winvenv')
            sh('call ./winvenv/Scripts/activate.bat')
        else:
            pwd = sh('pwd')
            sh('cd ~')
            sh('python3 -m venv ./lxvenv')
            sh('source ./lxvenv/bin/activate')
            sh('cd {}'.format(pwd.stdout.strip()))
        sh('pip install requests')
        sh('pip list')

    table = '|{:_<20}|{:_<20}|{:_<20}|{:_<50}|'
    print(table.format('', '', '', '').replace('|', '_'))
    print(table.format("Exit Code", "Has Error", "Has Ouput", "Command").replace('_', ' '))
    print(table.format('', '', '', ''))
    for cmd in sh.history:
        print(table.format(cmd.exit_code, cmd.has_error(), cmd.has_output(), cmd.cmd).replace('_', ' '))
        x = 1
    print(table.format('', '', '', '').replace('|', '_'))
Exemplo n.º 7
0
def test_has_err():
    from shell_proc import Shell

    with Shell() as sh:
        print('\n========== Begin Shell has_err ==========')
        # Create a directory storage and make a virtual environment in storage with the requests library installed
        sh('mkdir storage')
        print('mkdir storage', '>>',
              sh.last_command.stderr)  # Directory may exist
        sh('cd storage')
        print('cd storage', '>>', sh.last_command.stderr)
        sh('echo Hello World! > hello.txt')
        print('echo Hello World! > hello.txt', '>>', sh.last_command.stderr)

        if sh.is_windows():
            sh('python -m venv ./winvenv')
            print('python -m venv ./winvenv', '>>', sh.last_command.stderr)
            sh('call ./winvenv/Scripts/activate.bat')
        else:
            pwd = sh('pwd')
            sh('cd ~')
            sh('python3 -m venv ./lxvenv')
            print('python3 -m venv ./lxvenv', '>>', sh.last_command.stderr)
            sh('source ./lxvenv/bin/activate')
            sh('cd {}'.format(pwd.stdout))
        sh('pip -V')
        print('pip -V', '>>', sh.last_command.stderr)
        sh('pip install requests')
        print('pip install requests', '>>',
              sh.last_command.stderr)  # Possible stderr message to update pip
        sh('pip list')
        print('pip list', '>>',
              sh.last_command.stderr)  # Possible stderr message to update pip
    print('========== End Shell ==========')
Exemplo n.º 8
0
def run_non_blocking():
    import sys
    import time
    from shell_proc import Shell

    with Shell(stdout=sys.stdout, stderr=sys.stderr, blocking=False, wait_on_exit=True) as sh:
        sh.run('mkdir storage')
        sh('cd storage')  # Same as sh.run()
        sh('echo Hello World! > hello.txt')

        if sh.is_windows():
            sh('python -m venv ./winvenv')
            sh('call ./winvenv/Scripts/activate.bat')
        else:
            pwd = sh('pwd')
            sh('cd ~')
            sh('python3 -m venv ./lxvenv')
            sh('source ./lxvenv/bin/activate')
            sh('cd {}'.format(pwd.stdout))
        sh('pip install requests')
        sh('pip list')
        print('---------- At exit (shows non-blocking until exit) ----------')

    table = '|{:_<20}|{:_<20}|{:_<20}|{:_<50}|'
    print(table.format('', '', '', '').replace('|', '_'))
    print(table.format("Exit Code", "Has Error", "Has Ouput", "Command").replace('_', ' '))
    print(table.format('', '', '', ''))
    for cmd in sh.history:
        print(table.format(cmd.exit_code, cmd.has_error(), cmd.has_output(), cmd.cmd).replace('_', ' '))
    print(table.format('', '', '', '').replace('|', '_'))
Exemplo n.º 9
0
def run_python():
    import sys
    from shell_proc import Shell

    with Shell(stdout=sys.stdout, stderr=sys.stderr, python_call='python3') as sh:
        sh.python('-c',
                  'import os',
                  'print("My PID:", os.getpid())')
Exemplo n.º 10
0
def run_parallel():
    print('Running run_parallel')
    import sys
    import time
    from shell_proc import Shell, python_args

    if Shell.is_linux():
        python_args.PYTHON = 'python3'

    with Shell(stdout=sys.stdout, stderr=sys.stderr) as sh:
        # if sh.is_linux():  # This wont work with parallel due to a new shell being created.
        #     sh('alias python=python3')

        start = time.time()
        sh.parallel(*(python_args(
            '-c', 'import os', 'import time',
            'print("My ID:", {id}, "My PID:", os.getpid(), time.time() - {s})'.
            format(s=start, id=i)) for i in range(10)))
Exemplo n.º 11
0
def run_interactive():
    """WIP: I dont know how to get this to work with a subshell."""
    import sys
    import time
    from shell_proc import Shell, python_args

    if Shell.is_linux():
        python_args.PYTHON = 'python3'

    with Shell(stdout=sys.stdout, stderr=sys.stderr, shell=True) as sh:
        if sh.is_linux():
            sh('alias python=python3')

        sh.end_command = ''  # Terminator to help determine when a task finished
        sh.set_blocking(False)  # Must have non blocking commands
        sh.python()
        time.sleep(0.5)
        while sh.is_proc_running():
            sh(input('>>> '))
Exemplo n.º 12
0
def run_shell():
    import sys
    with Shell(stdout=sys.stdout, stderr=sys.stderr) as sh:
        sh('cd ./storage')
        sh('exit()')
        try:
            sh('dir')
            raise AssertionError('Shell already closed!')
        except (ShellExit):
            pass  # Should hit here
Exemplo n.º 13
0
def run_simple_result():
    from shell_proc import Shell

    with Shell() as sh:
        sh('cd ..')
        if sh.is_windows():
            cmd = sh('dir')
        else:
            cmd = sh('ls')

        # cmd (Command) Attributes: cmd, exit_code, stdout, stderr
        print(cmd.stdout)
Exemplo n.º 14
0
def run_parallel():
    import sys
    import time
    from shell_proc import Shell, python_args

    with Shell(stdout=sys.stdout, stderr=sys.stderr, python_call='python3') as sh:
        p = sh.parallel(*(python_args('-c',
                                      'import os',
                                      'import time',
                                      "print('My ID:', {id}, 'My PID:', os.getpid(), time.time())".format(id=i),
                                      python_call='python3') for i in range(10)))
        sh.wait()  # or p.wait()
        print('finished parallel')
        time.sleep(1)

        tasks = []
        for i in range(10):
            if i == 3:
                t = python_args('-c',
                                'import os',
                                'import time',
                                'time.sleep(1)',
                                "print('My ID:', {id}, 'My PID:', os.getpid(), time.time())".format(id=i),
                                python_call='python3')
            else:
                t = python_args('-c',
                                'import os',
                                'import time',
                                "print('My ID:', {id}, 'My PID:', os.getpid(), time.time())".format(id=i),
                                python_call='python3')
            tasks.append(t)
        p = sh.parallel(*tasks)
        p.wait()
        print('finished parallel')
        time.sleep(1)

        with sh.parallel() as p:
            # python3 from shell
            for i in range(10):
                if i == 3:
                    p.python('-c',
                             'import os',
                             'import time',
                             'time.sleep(1)',
                             "print('My ID:', {id}, 'My PID:', os.getpid(), time.time())".format(id=i))
                else:
                    p.python('-c',
                             'import os',
                             'import time',
                             "print('My ID:', {id}, 'My PID:', os.getpid(), time.time())".format(id=i))
            # p.wait() on exit context
        print('finished parallel')
Exemplo n.º 15
0
def run_manual():
    import io
    import sys
    from shell_proc import Shell

    # Initialize and run tasks
    sh = Shell('mkdir storage',
               'cd storage',
               'echo Hello World! > hello.txt',
               stderr=io.StringIO())

    # Manually run tasks
    if sh.is_windows():
        sh('python -m venv ./winvenv')
        sh('call ./winvenv/Scripts/activate.bat')
    else:
        pwd = sh('pwd')
        sh('cd ~')
        sh('python3 -m venv ./lxvenv')
        sh('source ./lxvenv/bin/activate')
        sh('cd {}'.format(pwd.stdout))

    # Not exactly success. If True no output was printed to stderr. Stderr could also be warning like need to update pip
    results = sh.run('pip install requests')
    print("***** Successful install: ", results.exit_code == 0)
    if results.exit_code != 0:
        sh.stderr.seek(0)  # Move to start of io.StringIO()
        err = sh.stderr.read()  # All text collected into stderr from subprocess stderr
        print(err, file=sys.stderr)
        # sh.print_stderr()  # Also available

    sh.stdout = io.StringIO()  # Start saving output for new tasks
    results = sh('pip list')
    print('***** Output Printed\n', results.stdout)

    sh('pip -V')
    print('pip -V =>', sh.last_command.stdout)  # ... for some odd reason PyCharm terminal does not print this "\r\r\n"

    print('All collected stdout')
    sh.stdout.seek(0)  # Move to start of io.StringIO()
    print(sh.stdout.read(), end='', flush=True)  # Print all read data

    # Should close when finished to stop threads from reading stdout and stderr subprocess.PIPE
    # (will close automatically eventually)
    sh.close()
Exemplo n.º 16
0
def run_command_pipe():
    import sys
    from shell_proc import Shell, ShellExit, shell_args, quote

    with Shell(stdout=sys.stdout, stderr=sys.stderr) as sh:
        # One step
        if sh.is_windows():
            results = sh(
                'dir') | 'find "run"'  # Hard to tell where find output starts
        else:
            results = sh('ls -al') | 'grep "run"'
        assert 'run_subprocess.py' in results.stdout, results.stdout

        # Two Steps
        if sh.is_windows():
            cmd = sh('dir')
            print('\nRUN PIPE')
            results = cmd | 'find "run"'
        else:
            cmd = sh('ls -al')
            print('\nRUN PIPE')
            results = cmd | 'grep "run"'
        assert 'run_subprocess.py' in results.stdout
Exemplo n.º 17
0
def test_has_out():
    import io
    from shell_proc import Shell

    with Shell(stdout=io.StringIO()) as sh:
        print('\n========== Begin Shell has_out ==========')
        # Create a directory storage and make a virtual environment in storage with the requests library installed
        sh('mkdir storage')
        print('mkdir storage', '>>', sh.last_command.stdout)
        sh('cd storage')
        print('cd storage', '>>', sh.last_command.stdout)
        sh('echo Hello World! > hello.txt')
        print('echo Hello World! > hello.txt', '>>', sh.last_command.stdout)

        if sh.is_windows():
            sh('python -m venv ./winvenv')
            print('python -m venv ./winvenv', '>>', sh.last_command.stdout)
            sh('call ./winvenv/Scripts/activate.bat')
        else:
            pwd = sh('pwd')
            sh('cd ~')
            sh('python3 -m venv ./lxvenv')
            print('python3 -m venv ./lxvenv', '>>', sh.last_command.stdout)
            sh('source ./lxvenv/bin/activate')
            sh('cd {}'.format(pwd.stdout))
        sh('pip -V')
        print('pip -V', '>>', sh.last_command.stdout)
        sh('pip install requests')
        print('pip install requests', '>>', sh.last_command.stdout)
        sh('pip list')
        print('pip list', '>>', sh.last_command.stdout)

        sh.stdout.seek(0)
        out = sh.stdout.read(
        )  # All text saved to stdout from the subprocess terminal commands
        assert out != ''
    print('========== End Shell ==========')
Exemplo n.º 18
0
def test_simple():
    import sys
    from shell_proc import Shell

    with Shell(stdout=sys.stdout, stderr=sys.stderr) as sh:
        print('\n========== Begin Shell ==========')
        # Create a directory storage and make a virtual environment in storage with the requests library installed
        sh('mkdir storage')
        sh('cd storage')
        sh('echo Hello World! > hello.txt')

        if sh.is_windows():
            sh('python -m venv ./winvenv')
            sh('call ./winvenv/Scripts/activate.bat')
        else:
            pwd = sh('pwd')
            sh('cd ~')
            sh('python3 -m venv ./lxvenv')
            sh('source ./lxvenv/bin/activate')
            sh('cd {}'.format(pwd.stdout))
        sh('pip -V')
        sh('pip install requests')
        sh('pip list')
    print('========== End Shell ==========')
Exemplo n.º 19
0
import sys
import os
import argparse
from shell_proc import Shell

P = argparse.ArgumentParser('Run shell commands')
P.add_argument('tasks', nargs='*', type=str, help='Shell tasks to run.')
P.add_argument('--stdout',
               type=str,
               default=sys.stdout,
               help='Filename to write stdout to')
P.add_argument('--stderr',
               type=str,
               default=sys.stderr,
               help='Filename to write stderr to')
P.add_argument('--blocking', type=bool, default=True)

ARGS, REMAIN = P.parse_known_args(sys.argv[1:])

OUT = ARGS.stdout
if isinstance(OUT, str):
    OUT = open(OUT, 'w')
ERR = ARGS.stderr
if isinstance(ERR, str):
    ERR = open(ERR, 'w')

with Shell(*ARGS.tasks, stdout=OUT, stderr=ERR, blocking=ARGS.blocking) as sh:
    while sh.is_proc_running():
        sh(input('> '))