-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_renumber.py
More file actions
executable file
·47 lines (43 loc) · 1.3 KB
/
db_renumber.py
File metadata and controls
executable file
·47 lines (43 loc) · 1.3 KB
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
#!/usr/bin/env python
"""
This script creates a new storage with the oids of all instances,
(except the root) reassigned so that they are in a minimal range,
which makes FileStorage (when using the Shelf format) more compact
and efficient for some operations.
"""
from durus.connection import Connection
from os.path import exists
from tempfile import TemporaryFile
import sys
if sys.version < "3":
from cPickle import dump, load
else:
from pickle import dump, load
def usage():
print("%s <old-file-storage> <new-file-storage>" % sys.argv[0])
print(__doc__)
raise SystemExit
def main(old_file, new_file):
if old_file.startswith('-'):
usage()
if new_file.startswith('-'):
usage()
assert not exists(new_file)
connection = Connection(sys.argv[1])
tmpfile = TemporaryFile()
print("pickling from " + old_file)
dump(connection.get_root().__getstate__(), tmpfile, 2)
connection = None
tmpfile.seek(0)
connection2 = Connection(sys.argv[2])
print("unpickling")
connection2.get_root().__setstate__(load(tmpfile))
connection2.get_root()._p_note_change()
print("commit to " + new_file)
connection2.commit()
print("pack")
connection2.pack()
if __name__ == '__main__':
if len(sys.argv) != 3:
usage()
main(*sys.argv[1:])