| 1 | #! @PYTHON@ |
| 2 | ### -*-python-*- |
| 3 | |
| 4 | from cdb import cdbmake |
| 5 | from re import compile as r_compile, sub as r_sub |
| 6 | from sys import argv, stdin, stderr, exit |
| 7 | |
| 8 | ego = r_sub(r'^.*[/\\]', '', argv[0]) |
| 9 | def die(msg, prefix = True): |
| 10 | if prefix: msg ='%s: %s' % (ego, msg) |
| 11 | print >>stderr, msg |
| 12 | exit(1) |
| 13 | |
| 14 | def files(args): |
| 15 | if len(args) == 0: |
| 16 | yield stdin |
| 17 | else: |
| 18 | for a in args: |
| 19 | if a == '-': |
| 20 | yield stdin |
| 21 | else: |
| 22 | yield open(a, 'r') |
| 23 | |
| 24 | if len(argv) < 2: |
| 25 | die('usage: %s CDB [INPUT ...]' % ego, False) |
| 26 | |
| 27 | rx_comment = r_compile(r'^\s*(\#|$)') |
| 28 | rx_shave = r_compile(r'\s*(.*\S|)\s*$') |
| 29 | |
| 30 | cdb = cdbmake(argv[1], argv[1] + '.new') |
| 31 | for f in files(argv[2:]): |
| 32 | for line in f: |
| 33 | if len(line) and line[-1] == '\n': line = line[:-1] |
| 34 | if rx_comment.match(line): |
| 35 | continue |
| 36 | line = rx_shave.sub(r'\1', line) |
| 37 | cdb.add(line, '') |
| 38 | cdb.finish() |