Mercurial > hg > Members > shinya > pyrect
view pyrect/jitgrep.py @ 63:020ba001c58a
modify I/O routine. use mmap. it's really faster than fgets ;-)
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 05 Nov 2010 01:39:42 +0900 |
parents | fd3d0b8326fe |
children | 4de11d799dee |
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 * from pyrect.regexp import Regexp def main(argv): myusage = """%prog [--buf-size=size] [--dump] [--time] [--debug] [--cc=compiler] [-c] [-Olevel] regexp [file..] [--out=file] [--thread=thread_num] [--disable-filter]""" psr = OptionParser(usage=myusage) redirect = "" srcpath = "/tmp/jitgrep_dfa.c" binpath = "/tmp/jitgrep" psr.add_option("--cc", action="store", type="string", dest="cc", default="gcc", metavar="FILE", help="Choose compiler (default is gcc).") psr.add_option("-c", action="store_true", dest="compile", default=False , help="compile only.") psr.add_option("--buf-size=size", action="store", type="string", dest="bufsize", default="1M" , help="Set read-buffer size (e.x. 1024, 1024K, 2M)") psr.add_option("--cflags", action="store", type="string", dest="cflags", default="-O3", help="Print compile/matching time.") psr.add_option("--time", action="store_true", dest="time", default=False, help="Print compile/matching time.") psr.add_option("--thread", action="store", type="string", dest="thread", default="0", metavar="FILE", help="number of thread.") psr.add_option("--disable-filter", action="store_true", dest="no_filter", default=False, help="disable fixed-string filter(Boyer-Moore).") psr.add_option("--debug", action="store_true", dest="debug", default=False, help="Dump commands, not evalute matching (except interactive mode).") psr.add_option("--label", action="store_true", dest="label", default=False, help="label implimentation in C.") 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) < 2: psr.print_usage() return if opts.cc == "cbc": cbc = True opts.cc = "$CBCROOT/INSTALL_DIR/bin/gcc" opts.cflags += " -L$CBCROOT/gcc -w" else: cbc = False if opts.debug: print("option", opts) if opts.debug: print("args", args) string = args[1] try: if opts.bufsize[-1] == 'K': bufsize = int(opts.bufsize[:-1]) * 2**10 elif opts.bufsize[-1] == 'M': bufsize = int(opts.bufsize[:-1]) * 2**20 else: bufsize = int(opts.bufsize) except ValueError: psr.print_usage() return if opts.time : start_time = time.time() reg = Regexp(".*"+string) if cbc: grept = CbCGREPTranslator(reg) elif opts.label: grept = GoToGREPTranslator(reg) else: grept = GREPTranslator(reg) grept.filter = not opts.no_filter grept.thread_line = int(opts.thread) grept.bufsize = bufsize if opts.dump: grept.translate() return else: tmpsrc = open(srcpath, 'w') grept.translate(tmpsrc) tmpsrc.close() if (opts.time): end_time = time.time() print("Translation: " + str(end_time - start_time) + " Sec.") cmd = " ".join([opts.cc, opts.cflags, srcpath, "-o", binpath]) if opts.debug: print("compile command", cmd) else: if (opts.time): start_time = time.time() os.system(cmd) if (opts.time): end_time = time.time() print("Compiling : " + str(end_time - start_time) + " Sec.") if opts.debug: print("argv=", argv) print("args=", args) print("opts=", opts) if opts.compile: return if len(args) == 2 and not opts.debug: while True: try: os.system(binpath + ' ' + raw_input()) except (KeyboardInterrupt, EOFError): break else: if (opts.time): redirect = "> /dev/null" if (opts.out): redirect = ">" + opts.out cmd = ' '.join([binpath, "dummy"] + args[2:] + [redirect]) if opts.debug: print("exec command", cmd) else: if (opts.time): start_time = time.time() os.system(cmd) if (opts.time): end_time = time.time() print("Matching : " + str(end_time - start_time) + " Sec.") if not opts.debug: #os.remove(srcpath) #os.remove(binpath) pass if __name__ == '__main__': main(sys.argv)