view pyrect/pyrect/llgrep.py @ 9:493c96d030c0

add pyrect
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 14 Jun 2011 17:24:03 +0900
parents
children
line wrap: on
line source

#!/usr/bin/env python

import sys
import os
import re
import time
from optparse import OptionParser
from pyrect.translator import LLVMGREPTranslator
from pyrect.regexp import Regexp

def main(argv):
    myusage = """%prog [--time] [--dump]
                  [-O] regexp file.. [--out=file]"""
    psr = OptionParser(usage=myusage)

    redirect = ""

    psr.add_option("-O", action="store_true", dest="optimize", default=False, help="Print compile/matching time.")
    psr.add_option("--time", action="store_true", dest="time", default=False, help="Print compile/matching time.")
    psr.add_option("--dump", action="store_true", dest="dump", default=False, help="Dump generated grep-source.")
    psr.add_option("--out", action="store", type="string", dest="out", default="", metavar="FILE", help="Output file.")

    (opts, args) = psr.parse_args(argv)

    if len(args) < 3:
        psr.print_usage()
        exit(0)

    if opts.time : start_time = time.time()
    reg = Regexp(args[1])
    grept = LLVMGREPTranslator(reg)
    grept.optimize = opts.optimize
    grept.args = args[2:]

    if opts.dump:
        grept.translate()
        exit(0)

    if (opts.time):
        end_time = time.time()
        print("Translation: " + str(end_time - start_time) + " Sec.")

    if (opts.time): start_time = time.time()
    grept.jitcompile()
    if (opts.time):
        end_time = time.time()
        print("Compiling  : " + str(end_time - start_time) + " Sec.")

    if (opts.time): sys.stdout = open("/dev/null", "r")
    if (opts.out):  sys.stdout = open(opts.out, "r")
    sys.stderr = open("/dev/null", "r")
    if (opts.time): start_time = time.time()
    grept.execute()
    sys.stdout = sys.__stdout__
    if (opts.time):
        end_time = time.time()
        print("Matching   : " + str(end_time - start_time) + " Sec.")

if __name__ == '__main__': main(sys.argv)