Exemplo n.º 1
0
def prep_subprocess(args=[], pipe_read=None, pipe_write=None):
    ''' Return a process object that read can execute.'''
    p = Process()
    new_in_fpath = None
    new_out_fpath = None
    
    try: # check for < > file redirection
        index = 0
        while index < len(args):
            arg = args[index]
            if arg == '<':
                if pipe_read:
                    raise IndexError # TODO Multiple redirection error
                new_in_fpath = args[index + 1]
                index += 2
            elif arg == '>':
                if pipe_write:
                    raise IndexError # TODO Multiple redirection error
                new_out_fpath = args[index + 1]
                index += 2
            else:
                p.args.append(arg)
                index += 1
 
    except IndexError:
        os.write(2, ("Shelly: Unexpected redirection %s \n" % args).encode())
        return None

    if new_in_fpath:
        p.input= os.open(new_in_fpath, os.O_RDONLY)
        os.set_inheritable(p.input, True)
    else:
        p.input = pipe_read

    if new_out_fpath:
        p.output = os.open(new_out_fpath, os.O_WRONLY | os.O_CREAT)
        os.set_inheritable(p.output, True)
    else:
        p.output = pipe_write

    return p