def pipeline(tmp_dir, fn, cmds, debug=False):
    counter = 0
    input_fn = fn
    for cmd in cmds:
        counter += 1
        output_fn = pjoin(tmp_dir, "%05d" % counter)

        if IN_FILE not in cmd:
            input = file(input_fn, "rb")
        else:
            input = None
        if OUT_FILE not in cmd:
            output = file(output_fn, "wb")
        else:
            output = None

        in_out = {"i": input_fn, "o": output_fn}
        cmd = [i % in_out for i in cmd]

        # print 'cmd=%r, input_fn=%r, output_fn=%r' % (cmd, input_fn, output_fn)
        p = Popen(cmd, stdin=input, stdout=output)
        p.wait()

        if input is not None:
            input.close()
        if output is not None:
            output.close()

        if not debug:
            os.remove(input_fn)
        input_fn = output_fn
    return output_fn
def pipeline(tmp_dir, fn, cmds, debug=False):
    counter = 0
    input_fn = fn
    for cmd in cmds:
        counter += 1
        output_fn = pjoin(tmp_dir, '%05d' % counter)

        if IN_FILE not in cmd:
            input = file(input_fn, 'rb')
        else:
            input = None
        if OUT_FILE not in cmd:
            output = file(output_fn, 'wb')
        else:
            output = None

        in_out = {'i': input_fn, 'o': output_fn}
        cmd = [i % in_out for i in cmd]

        #print 'cmd=%r, input_fn=%r, output_fn=%r' % (cmd, input_fn, output_fn)
        p = Popen(cmd, stdin=input, stdout=output)
        p.wait()

        if input is not None:
            input.close()
        if output is not None:
            output.close()

        if not debug:
            os.remove(input_fn)
        input_fn = output_fn
    return output_fn
def fsmrawdecode(model_dir, lines, debug=False):
    tmp_dir = tempfile.mkdtemp(prefix="hvsparser")
    cmds = [
        ["fsmcompile", "-t", "-F", OUT_FILE],
        ["fsmcompose", "-F", OUT_FILE, IN_FILE, pjoin(model_dir, "hvsparser.fsm")],
        ["fsmbestpath", "-n", "1", "-F", OUT_FILE, IN_FILE],
        ["fsmprint", IN_FILE],
    ]
    input_fn = pjoin(tmp_dir, "input")
    input = file(input_fn, "wb")
    for line in lines:
        print >> input, line,
    input.close()
    output_fn = pipeline(tmp_dir, input_fn, cmds, debug=debug)
    output = file(output_fn, "rb")
    for line in output:
        yield line
    output.close()
    if not debug:
        os.remove(output_fn)
        os.rmdir(tmp_dir)
def fsmrawdecode(model_dir, lines, debug=False):
    tmp_dir = tempfile.mkdtemp(prefix='hvsparser')
    cmds = [
        ['fsmcompile', '-t', '-F', OUT_FILE],
        [
            'fsmcompose', '-F', OUT_FILE, IN_FILE,
            pjoin(model_dir, 'hvsparser.fsm')
        ],
        ['fsmbestpath', '-n', '1', '-F', OUT_FILE, IN_FILE],
        ['fsmprint', IN_FILE],
    ]
    input_fn = pjoin(tmp_dir, 'input')
    input = file(input_fn, 'wb')
    for line in lines:
        print >> input, line,
    input.close()
    output_fn = pipeline(tmp_dir, input_fn, cmds, debug=debug)
    output = file(output_fn, 'rb')
    for line in output:
        yield line
    output.close()
    if not debug:
        os.remove(output_fn)
        os.rmdir(tmp_dir)