Fix compatibility with bzr 2.5.
[bzr-fastimport.git] / marks_file.py
blob78282a9822f168d9b6bebb4300baebd7713a8443
1 # Copyright (C) 2009 Canonical Ltd
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 """Routines for reading/writing a marks file."""
19 from bzrlib.trace import warning
22 def import_marks(filename):
23 """Read the mapping of marks to revision-ids from a file.
25 :param filename: the file to read from
26 :return: None if an error is encountered or a dictionary with marks
27 as keys and revision-ids as values
28 """
29 # Check that the file is readable and in the right format
30 try:
31 f = file(filename)
32 except IOError:
33 warning("Could not import marks file %s - not importing marks",
34 filename)
35 return None
37 # Read the revision info
38 revision_ids = {}
40 line = f.readline()
41 if line == 'format=1\n':
42 # Cope with old-style marks files
43 # Read the branch info
44 branch_names = {}
45 for string in f.readline().rstrip('\n').split('\0'):
46 if not string:
47 continue
48 name, integer = string.rsplit('.', 1)
49 branch_names[name] = int(integer)
50 line = f.readline()
52 while line:
53 line = line.rstrip('\n')
54 mark, revid = line.split(' ', 1)
55 mark = mark.lstrip(':')
56 revision_ids[mark] = revid
57 line = f.readline()
58 f.close()
59 return revision_ids
62 def export_marks(filename, revision_ids):
63 """Save marks to a file.
65 :param filename: filename to save data to
66 :param revision_ids: dictionary mapping marks -> bzr revision-ids
67 """
68 try:
69 f = file(filename, 'w')
70 except IOError:
71 warning("Could not open export-marks file %s - not exporting marks",
72 filename)
73 return
75 # Write the revision info
76 for mark, revid in revision_ids.iteritems():
77 f.write(':%s %s\n' % (str(mark).lstrip(':'), revid))
78 f.close()