| Fred Drake | 693a2c6 | 1999-04-22 13:08:09 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python | 
 | 2 | #  -*- Python -*- | 
 | 3 |  | 
 | 4 | import fileinput | 
| Fred Drake | c4a623e | 1999-04-22 20:32:21 +0000 | [diff] [blame] | 5 | import getopt | 
| Fred Drake | 693a2c6 | 1999-04-22 13:08:09 +0000 | [diff] [blame] | 6 | import glob | 
 | 7 | import os | 
 | 8 | import re | 
 | 9 | import sys | 
 | 10 |  | 
 | 11 |  | 
 | 12 | declare_rx = re.compile( | 
 | 13 |     r"\\declaremodule(?:\[[a-zA-Z0-9]*\]*)?{[a-zA-Z_0-9]+}{([a-zA-Z_0-9]+)}") | 
 | 14 |  | 
 | 15 | module_rx = re.compile(r"\\module{([a-zA-Z_0-9]+)}") | 
 | 16 |  | 
 | 17 | def main(): | 
 | 18 |     try: | 
| Fred Drake | c4a623e | 1999-04-22 20:32:21 +0000 | [diff] [blame] | 19 |         just_list = 0 | 
 | 20 |         print_lineno = 0 | 
 | 21 |         opts, args = getopt.getopt(sys.argv[1:], "ln", ["list", "number"]) | 
 | 22 |         for opt, arg in opts: | 
 | 23 |             if opt in ("-l", "--list"): | 
 | 24 |                 just_list = 1 | 
 | 25 |             elif opt in ("-n", "--number"): | 
 | 26 |                 print_lineno = 1 | 
 | 27 |         files = args | 
| Fred Drake | 693a2c6 | 1999-04-22 13:08:09 +0000 | [diff] [blame] | 28 |         if not files: | 
 | 29 |             files = glob.glob("*.tex") | 
 | 30 |             files.sort() | 
 | 31 |         modulename = None | 
 | 32 |         for line in fileinput.input(files): | 
 | 33 |             if line[:9] == r"\section{": | 
 | 34 |                 modulename = None | 
 | 35 |                 continue | 
 | 36 |             if line[:16] == r"\modulesynopsys{": | 
 | 37 |                 continue | 
 | 38 |             m = declare_rx.match(line) | 
 | 39 |             if m: | 
 | 40 |                 modulename = m.group(1) | 
 | 41 |                 continue | 
 | 42 |             if not modulename: | 
 | 43 |                 continue | 
 | 44 |             m = module_rx.search(line) | 
 | 45 |             if m: | 
 | 46 |                 name = m.group(1) | 
 | 47 |                 if name != modulename: | 
| Fred Drake | c4a623e | 1999-04-22 20:32:21 +0000 | [diff] [blame] | 48 |                     filename = fileinput.filename() | 
 | 49 |                     if just_list: | 
 | 50 |                         print filename | 
 | 51 |                         fileinput.nextfile() | 
 | 52 |                         modulename = None | 
 | 53 |                     elif print_lineno: | 
 | 54 |                         print "%s(%d):%s" \ | 
 | 55 |                               % (filename, fileinput.filelineno(), line[:-1]) | 
 | 56 |                     else: | 
 | 57 |                         print "%s:%s" % (filename, line[:-1]) | 
| Fred Drake | 693a2c6 | 1999-04-22 13:08:09 +0000 | [diff] [blame] | 58 |     except KeyboardInterrupt: | 
 | 59 |         sys.exit(1) | 
 | 60 |  | 
 | 61 |  | 
 | 62 | if __name__ == "__main__": | 
 | 63 |     main() |