Avoid Inventory.copy, which has disappeared in newer versions of Bazaar.
[bzr-fastimport.git] / marks_file.py
blob7066a1c39c6a83f29f2aea0fa97835d3acc557d0
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, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 """Routines for reading/writing a marks file."""
20 from bzrlib.trace import warning
23 def import_marks(filename):
24 """Read the mapping of marks to revision-ids from a file.
26 :param filename: the file to read from
27 :return: None if an error is encountered or a dictionary with marks
28 as keys and revision-ids as values
29 """
30 # Check that the file is readable and in the right format
31 try:
32 f = file(filename)
33 except IOError:
34 warning("Could not import marks file %s - not importing marks",
35 filename)
36 return None
38 # Read the revision info
39 revision_ids = {}
41 line = f.readline()
42 if line == 'format=1\n':
43 # Cope with old-style marks files
44 # Read the branch info
45 branch_names = {}
46 for string in f.readline().rstrip('\n').split('\0'):
47 if not string:
48 continue
49 name, integer = string.rsplit('.', 1)
50 branch_names[name] = int(integer)
51 line = f.readline()
53 while line:
54 line = line.rstrip('\n')
55 mark, revid = line.split(' ', 1)
56 mark = mark.lstrip(':')
57 revision_ids[mark] = revid
58 line = f.readline()
59 f.close()
60 return revision_ids
63 def export_marks(filename, revision_ids):
64 """Save marks to a file.
66 :param filename: filename to save data to
67 :param revision_ids: dictionary mapping marks -> bzr revision-ids
68 """
69 try:
70 f = file(filename, 'w')
71 except IOError:
72 warning("Could not open export-marks file %s - not exporting marks",
73 filename)
74 return
76 # Write the revision info
77 for mark, revid in revision_ids.iteritems():
78 f.write(':%s %s\n' % (str(mark).lstrip(':'), revid))
79 f.close()