Rename CVSFileDatabase to CVSPathDatabase.
[cvs2svn.git] / contrib / show_db.py
blob3f14a940eaf632bb46a6b5e8635b9f300d622394
1 #!/usr/bin/env python
3 import anydbm
4 import marshal
5 import sys
6 import os
7 import getopt
8 import cPickle as pickle
9 from cStringIO import StringIO
11 sys.path.insert(0, os.path.dirname(os.path.dirname(sys.argv[0])))
13 from cvs2svn_lib import config
14 from cvs2svn_lib.context import Ctx
15 from cvs2svn_lib.common import DB_OPEN_READ
16 from cvs2svn_lib.artifact_manager import artifact_manager
19 def usage():
20 cmd = sys.argv[0]
21 sys.stderr.write('Usage: %s OPTION [DIRECTORY]\n\n' % os.path.basename(cmd))
22 sys.stderr.write(
23 'Show the contents of the temporary database files created by cvs2svn\n'
24 'in a structured human-readable way.\n'
25 '\n'
26 'OPTION is one of:\n'
27 ' -R SVNRepositoryMirror revisions table\n'
28 ' -N SVNRepositoryMirror nodes table\n'
29 ' -r rev SVNRepositoryMirror node tree for specific revision\n'
30 ' -m MetadataDatabase\n'
31 ' -f CVSPathDatabase\n'
32 ' -c PersistenceManager SVNCommit table\n'
33 ' -C PersistenceManager cvs-revs-to-svn-revnums table\n'
34 ' -i CVSItemDatabase (normal)\n'
35 ' -I CVSItemDatabase (filtered)\n'
36 ' -p file Show the given file, assuming it contains a pickle.\n'
37 '\n'
38 'DIRECTORY is the directory containing the temporary database files.\n'
39 'If omitted, the current directory is assumed.\n')
40 sys.exit(1)
43 def print_node_tree(db, key="0", name="<rootnode>", prefix=""):
44 print "%s%s (%s)" % (prefix, name, key)
45 if name[:1] != "/":
46 dict = marshal.loads(db[key])
47 items = dict.items()
48 items.sort()
49 for entry in items:
50 print_node_tree(db, entry[1], entry[0], prefix + " ")
53 def show_int2str_db(fname):
54 db = anydbm.open(fname, 'r')
55 k = map(int, db.keys())
56 k.sort()
57 for i in k:
58 print "%6d: %s" % (i, db[str(i)])
60 def show_str2marshal_db(fname):
61 db = anydbm.open(fname, 'r')
62 k = db.keys()
63 k.sort()
64 for i in k:
65 print "%6s: %s" % (i, marshal.loads(db[i]))
67 def show_str2pickle_db(fname):
68 db = anydbm.open(fname, 'r')
69 k = db.keys()
70 k.sort()
71 for i in k:
72 o = pickle.loads(db[i])
73 print "%6s: %r" % (i, o)
74 print " %s" % (o,)
76 def show_str2ppickle_db(fname):
77 db = anydbm.open(fname, 'r')
78 k = db.keys()
79 k.remove('_')
80 k.sort(key=lambda s: int(s, 16))
81 u1 = pickle.Unpickler(StringIO(db['_']))
82 u1.load()
83 for i in k:
84 u2 = pickle.Unpickler(StringIO(db[i]))
85 u2.memo = u1.memo.copy()
86 o = u2.load()
87 print "%6s: %r" % (i, o)
88 print " %s" % (o,)
90 def show_cvsitemstore():
91 for cvs_file_items in Ctx()._cvs_items_db.iter_cvs_file_items():
92 items = cvs_file_items.values()
93 items.sort(key=lambda i: i.id)
94 for item in items:
95 print "%6x: %r" % (item.id, item,)
98 def show_filtered_cvs_item_store():
99 from cvs2svn_lib.cvs_item_database import IndexedCVSItemStore
100 db = IndexedCVSItemStore(
101 artifact_manager.get_temp_file(config.CVS_ITEMS_FILTERED_STORE),
102 artifact_manager.get_temp_file(config.CVS_ITEMS_FILTERED_INDEX_TABLE),
103 DB_OPEN_READ)
105 ids = list(db.iterkeys())
106 ids.sort()
107 for id in ids:
108 cvs_item = db[id]
109 print "%6x: %r" % (cvs_item.id, cvs_item,)
113 class ProjectList:
114 """A mock project-list that can be assigned to Ctx()._projects."""
116 def __init__(self):
117 self.projects = {}
119 def __getitem__(self, i):
120 return self.projects.setdefault(i, 'Project%d' % i)
123 def prime_ctx():
124 def rf(filename):
125 artifact_manager.register_temp_file(filename, None)
127 from cvs2svn_lib.common import DB_OPEN_READ
128 from cvs2svn_lib.symbol_database import SymbolDatabase
129 from cvs2svn_lib.cvs_path_database import CVSPathDatabase
130 rf(config.CVS_PATHS_DB)
131 rf(config.SYMBOL_DB)
132 from cvs2svn_lib.cvs_item_database import OldCVSItemStore
133 from cvs2svn_lib.metadata_database import MetadataDatabase
134 rf(config.METADATA_DB)
135 rf(config.CVS_ITEMS_STORE)
136 rf(config.CVS_ITEMS_FILTERED_STORE)
137 rf(config.CVS_ITEMS_FILTERED_INDEX_TABLE)
138 artifact_manager.pass_started(None)
140 Ctx()._projects = ProjectList()
141 Ctx()._symbol_db = SymbolDatabase()
142 Ctx()._cvs_path_db = CVSPathDatabase(DB_OPEN_READ)
143 Ctx()._cvs_items_db = OldCVSItemStore(
144 artifact_manager.get_temp_file(config.CVS_ITEMS_STORE)
146 Ctx()._metadata_db = MetadataDatabase(DB_OPEN_READ)
148 def main():
149 try:
150 opts, args = getopt.getopt(sys.argv[1:], "RNr:mlfcCiIp:")
151 except getopt.GetoptError:
152 usage()
154 if len(args) > 1 or len(opts) != 1:
155 usage()
157 if len(args) == 1:
158 Ctx().tmpdir = args[0]
160 for o, a in opts:
161 if o == "-R":
162 show_int2str_db(config.SVN_MIRROR_REVISIONS_TABLE)
163 elif o == "-N":
164 show_str2marshal_db(
165 config.SVN_MIRROR_NODES_STORE,
166 config.SVN_MIRROR_NODES_INDEX_TABLE
168 elif o == "-r":
169 try:
170 revnum = int(a)
171 except ValueError:
172 sys.stderr.write('Option -r requires a valid revision number\n')
173 sys.exit(1)
174 db = anydbm.open(config.SVN_MIRROR_REVISIONS_TABLE, 'r')
175 key = db[str(revnum)]
176 db.close()
177 db = anydbm.open(config.SVN_MIRROR_NODES_STORE, 'r')
178 print_node_tree(db, key, "Revision %d" % revnum)
179 elif o == "-m":
180 show_str2marshal_db(config.METADATA_DB)
181 elif o == "-f":
182 prime_ctx()
183 cvs_files = list(Ctx()._cvs_path_db.itervalues())
184 cvs_files.sort()
185 for cvs_file in cvs_files:
186 print '%6x: %s' % (cvs_file.id, cvs_file,)
187 elif o == "-c":
188 prime_ctx()
189 show_str2ppickle_db(
190 config.SVN_COMMITS_INDEX_TABLE, config.SVN_COMMITS_STORE
192 elif o == "-C":
193 show_str2marshal_db(config.CVS_REVS_TO_SVN_REVNUMS)
194 elif o == "-i":
195 prime_ctx()
196 show_cvsitemstore()
197 elif o == "-I":
198 prime_ctx()
199 show_filtered_cvs_item_store()
200 elif o == "-p":
201 obj = pickle.load(open(a))
202 print repr(obj)
203 print obj
204 else:
205 usage()
206 sys.exit(2)
209 if __name__ == '__main__':
210 main()