/
db_dump.py
executable file
·58 lines (47 loc) · 1.14 KB
/
db_dump.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
#
# arch-tag: E543AADA-12B1-11D9-89EB-000A957659CC
from __future__ import generators
import getopt
import anydbm
import sys
def walkDbm(dbm):
finished=None
k,v=dbm.first()
while 1:
yield k,v
try:
k,v=dbm.next()
except KeyError:
raise StopIteration
def usage():
print "Usage: %s [-ag]" % sys.argv[0]
print " -a dumps articles"
print " -g dumps groups"
print " Given no option, both articles and groups will be dumped"
sys.exit(1)
opts, args=getopt.getopt(sys.argv[1:], 'ag')
copyGroup=True
copyArticle=True
if len(opts) > 0:
copyGroup=False
copyArticle=False
for opt in opts:
if opt[0] == '-a':
copyArticle=True
elif opt[0] == '-g':
copyGroup=True
else:
usage()
if len(args) != 1:
usage()
db=anydbm.open(args[0])
for k,v in walkDbm(db):
copyLine=False
# Figure out whether we want to copy this kind of stuff
if k[0] == 'l' and copyGroup:
copyLine=True
elif k[0] == 'a' and copyArticle:
copyLine=True
if copyLine:
print k + "\t" + v