Ejemplo n.º 1
0
def getout(prog, arg=None, stdin=None, verb=False, raiseIfNoneOut=False):
    '''Replacement for commands.getoutput. Arguments:
    - prog. Executable to be run. STRING. The only non-optional argument.
    - arg. List of strings with program arguments, or just a string
    - stdin. Filename STRING.
    - verb: whether to display command before executing it. BOOL
    Returned value: stdout of command, with stripped newlines'''

    assert type(prog) == str
    assert type(verb) == bool
    if stdin != None:
        stdin = stdin.strip()

    def cat_cmd(cmdlist, stdin):
        cmd = ' '.join(cmdlist)
        if stdin != None:
            cmd += ' <' + stdin
        return cmd

    prog = os.path.join(rsfprog.RSFROOT,'bin',prog)

    #if which(prog) == None:
    #   raise m8rex.MissingProgram(prog)

    # Build the [prog, args] list
    cmdlist = [prog]
    if arg != None:
        cmdlist += mklist(arg)

    # Build command string for printing or Python < 2.4
    if verb or not have_subprocess:
        cmd = cat_cmd(cmdlist, stdin)
        msg(cmd, verb)

    if have_subprocess:
        if stdin == None:
            finp = None
        else:
            if os.path.isfile(stdin):
                finp = open(stdin,'r')
            else:
                raise m8rex.NotAValidFile(stdin)
        try:
            s = subprocess.Popen(cmdlist,stdin=finp,stdout=subprocess.PIPE)
            output = s.communicate()[0]
        except:
            raise m8rex.FailedExtCall(cat_cmd(cmdlist, stdin))
    else: # no subprocess module present
        try:
            output = getoutput(cmd)
        except:
            raise m8rex.FailedExtCall(cmd)

    if output == None:
        if raiseIfNoneOut:
            raise m8rex.NoReturnFromExtProgram(prog)
    else:
        output = output.rstrip('\n')

    return output
Ejemplo n.º 2
0
def chk_file_r(filename):
    'Checks if a file exists, is a regular file and is readable'

    if not os.path.exists(filename):
        raise m8rex.WrongPath(filename)

    if not os.path.isfile(filename):
        raise m8rex.NotAValidFile(filename)

    if not os.access(filename, os.R_OK):
        raise m8rex.NoReadPermissions(filename)
Ejemplo n.º 3
0
def getppout(proglist, arglist=[[]], stdin=None):
    'Get pipe output'

    if stdin == None:
        finp = None
    else:
        if os.path.isfile(stdin):
            finp = open(stdin,'r')
        else:
            raise m8rex.NotAValidFile(stdin)

    c = [proglist[0]] + arglist[0]
    clist = [subprocess.Popen(c, stdin=finp, stdout=subprocess.PIPE)]

    for i in range(1,len(proglist)):
        c = [proglist[i]]+arglist[i]
        clist.append(subprocess.Popen(c,
                                      stdin=clist[i-1].stdout,
                                      stdout=subprocess.PIPE))

    return clist[-1].communicate()[0]