Commit | Line | Data |
---|---|---|
b2ffb9b7 | 1 | #! @PYTHON@ |
329f8eb8 | 2 | ### -*-python-*- |
bba986ae MW |
3 | |
4 | from cdb import cdbmake | |
329f8eb8 | 5 | from re import compile as r_compile, sub as r_sub |
bba986ae MW |
6 | from sys import argv, stdin, stderr, exit |
7 | ||
8 | ego = r_sub(r'^.*[/\\]', '', argv[0]) | |
9 | def die(msg, prefix = True): | |
841e5aca | 10 | if prefix: msg ='%s: %s' % (ego, msg) |
bba986ae MW |
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 == '-': | |
841e5aca | 20 | yield stdin |
bba986ae | 21 | else: |
841e5aca | 22 | yield open(a, 'r') |
bba986ae MW |
23 | |
24 | if len(argv) < 2: | |
25 | die('usage: %s CDB [INPUT ...]' % ego, False) | |
26 | ||
27 | rx_comment = r_compile(r'^\s*(\#|$)') | |
28 | rx_split = r_compile(r'^\s*([-\w]+)\s*=\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 | m = rx_split.match(line) | |
37 | if not m: | |
38 | die("bad assignment: `%s'" % line) | |
39 | k, v = m.groups([1, 2]) | |
40 | cdb.add(k, v) | |
41 | cdb.finish() |