Back to index

python3.2  3.2.2
Functions
rgrep Namespace Reference

Functions

def main
def usage

Function Documentation

def rgrep.main ( void  )

Definition at line 13 of file rgrep.py.

00013 
00014 def main():
00015     bufsize = 64 * 1024
00016     reflags = 0
00017     opts, args = getopt.getopt(sys.argv[1:], "i")
00018     for o, a in opts:
00019         if o == '-i':
00020             reflags = reflags | re.IGNORECASE
00021     if len(args) < 2:
00022         usage("not enough arguments")
00023     if len(args) > 2:
00024         usage("exactly one file argument required")
00025     pattern, filename = args
00026     try:
00027         prog = re.compile(pattern, reflags)
00028     except re.error as msg:
00029         usage("error in regular expression: %s" % msg)
00030     try:
00031         f = open(filename)
00032     except IOError as msg:
00033         usage("can't open %r: %s" % (filename, msg), 1)
00034     f.seek(0, 2)
00035     pos = f.tell()
00036     leftover = None
00037     while pos > 0:
00038         size = min(pos, bufsize)
00039         pos = pos - size
00040         f.seek(pos)
00041         buffer = f.read(size)
00042         lines = buffer.split("\n")
00043         del buffer
00044         if leftover is None:
00045             if not lines[-1]:
00046                 del lines[-1]
00047         else:
00048             lines[-1] = lines[-1] + leftover
00049         if pos > 0:
00050             leftover = lines[0]
00051             del lines[0]
00052         else:
00053             leftover = None
00054         for line in reversed(lines):
00055             if prog.search(line):
00056                 print(line)
00057 

Here is the call graph for this function:

Here is the caller graph for this function:

def rgrep.usage (   msg,
  code = 2 
)

Definition at line 58 of file rgrep.py.

00058 
00059 def usage(msg, code=2):
00060     sys.stdout = sys.stderr
00061     print(msg)
00062     print(__doc__)
00063     sys.exit(code)
00064 

Here is the call graph for this function:

Here is the caller graph for this function: