Changelog update.
[debian_buildbot.git] / master / contrib / fix_changes_pickle_encoding.py
blob07cd36d3e5e6b216e659471cc7d3ce0c8644ac8c
1 #!/usr/bin/python
2 """%prog [options] [changes.pck] old_encoding
4 Re-encodes changes in a pickle file to UTF-8 from the given encoding
5 """
7 if __name__ == '__main__':
8 import sys, os
9 from cPickle import load, dump
10 from optparse import OptionParser
12 parser = OptionParser(__doc__)
14 options, args = parser.parse_args()
16 if len(args) == 2:
17 changes_file = args[0]
18 old_encoding = args[1]
19 elif len(args) == 1:
20 changes_file = "changes.pck"
21 old_encoding = args[0]
22 else:
23 parser.error("Need at least one argument")
25 print "opening %s" % (changes_file,)
26 try:
27 fp = open(changes_file)
28 except IOError, e:
29 parser.error("Couldn't open %s: %s" % (changes_file, str(e)))
31 changemgr = load(fp)
32 fp.close()
34 print "decoding bytestrings in %s using %s" % (changes_file, old_encoding)
35 changemgr.recode_changes(old_encoding)
37 changes_backup = changes_file + ".old"
38 i = 0
39 while os.path.exists(changes_backup):
40 i += 1
41 changes_backup = changes_file + ".old.%i" % i
42 print "backing up %s to %s" % (changes_file, changes_backup)
43 os.rename(changes_file, changes_backup)
45 dump(changemgr, open(changes_file, "w"))