-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbutil.py
executable file
·79 lines (71 loc) · 2.32 KB
/
dbutil.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
"""
database utility for cache-manager databases
"""
import sys
from cachemanager import *
from cmserver import *
__version__ = "$Rev: 821 $"
__author__ = "[email protected] (David Rybach)"
__copyright__ = "Copyright 2012, RWTH Aachen University"
def main(argv):
if len(argv) < 3:
print "usage: %s <db-file> <action>" % argv[0]
print "action: stat | filestat | dump | convert <file> | clean <max-age> | delete <prefix>"
return 1
enableDebug()
db = FileDatabase()
if not db.load(argv[1]):
print "cannot load database from " + argv[1]
action = argv[2]
if action == "stat":
numFiles, numLoc = db.getStat()
print "numFiles: ", numFiles
print "numLoc: ", numLoc
elif action == "filestat":
for f in db.files:
print f, len(db.files[f])
elif action == "dump":
for f in db.files:
for l in db.files[f]:
print f, l
elif action == "convert":
db.write(sys.argv[3])
elif action == "clean":
# db.getLocation( db.files.keys()[1] )
db.removeOldRecords(int(time.time()) - int(sys.argv[3]))
db.write(sys.argv[1])
elif action == "delete":
prefix = sys.argv[3]
deleted = 0
scanned = 0
for f in db.files.keys():
scanned += 1
if f.startswith(prefix):
del db.files[f]
deleted += 1
print "deleted %d / %d records" % (deleted, scanned)
db.write(sys.argv[1])
elif action == "fill":
nFiles = int(sys.argv[3])
nLoc = int(sys.argv[4])
for f in range(nFiles):
length = random.randint(4, 20)
name = []
for l in range(length):
name.append( "DIR_%d" % random.randint(0,20) )
filename = "FILE_%d" % random.randint(0,500)
filepath = "/" + "/".join(name) + "/" + filename
for l in range(nLoc):
host = "HOST_%d" % random.randint(0, 150)
user = "USER_%d" % random.randint(0, 100)
path = "/var/tmp/%s%s" % (user, filepath)
loc = Location(path, 1, int(time.time()), host)
db.addLocation(filepath, loc)
db.write(sys.argv[1])
else:
print "unknwon action: " + action
return 1
return 0
if __name__ == "__main__":
sys.exit( main(sys.argv) )