chiark / gitweb /
cdb: Remove dependency on freecdb and libfile-cdb-perl.
[misc] / cdb-map
diff --git a/cdb-map b/cdb-map
old mode 100644 (file)
new mode 100755 (executable)
index 71cdde0..f230a42
--- a/cdb-map
+++ b/cdb-map
@@ -1,14 +1,32 @@
-#! /usr/bin/perl
-
-use CDB_File;
-
-@ARGV >= 1 or die "usage: $0 CDB [INPUT ...]\n";
-$f = shift;
-$c = CDB_File->new($f, "$f.new") or die "CDB_File->new: $!\n";
-while (<>) {
-  chomp;
-  m'^([^:]*):(.*)$' or die "bad assignment `$_'\n";
-  $c->insert($1, $2);
-}
-$c->finish() or die "CDB_File->finish: $!\n";
-exit 0;
+#! /usr/bin/python
+
+from cdb import cdbmake
+from sre import sub as r_sub
+from sys import argv, stdin, stderr, exit
+
+ego = r_sub(r'^.*[/\\]', '', argv[0])
+def die(msg, prefix = True):
+  if prefix: msg ='%s: %s' % (ego, msg) 
+  print >>stderr, msg
+  exit(1)
+
+def files(args):
+  if len(args) == 0:
+    yield stdin
+  else:
+    for a in args:
+      if a == '-':
+        yield stdin
+      else:
+        yield open(a, 'r')
+
+if len(argv) < 2:
+  die('usage: %s CDB [INPUT ...]' % ego, False)
+
+cdb = cdbmake(argv[1], argv[1] + '.new')
+for f in files(argv[2:]):
+  for line in f:
+    if len(line) and line[-1] == '\n': line = line[:-1]
+    k, v = line.split(':', 1)
+    cdb.add(k, v)
+cdb.finish()