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