fix timezones in darcs-fast-export, take 2
[bzr-fastimport/rorcz.git] / marks_file.py
blob96e3caba8c07a9d031a94ddcbdd91eee668879c3
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 import re
21 from bzrlib.trace import warning
24 def import_marks(filename):
25 """Read the mapping of marks to revision-ids from a file.
27 :param filename: the file to read from
28 :return: None if an error is encountered or (revision_ids, branch_names)
29 where
30 * revision_ids is a dictionary with marks as keys and revision-ids
31 as values
32 * branch_names is a dictionary mapping branch names to some magic #
33 """
34 # Check that the file is readable and in the right format
35 try:
36 f = file(filename)
37 except IOError:
38 warning("Could not import marks file %s - not importing marks",
39 filename)
40 return None
41 firstline = f.readline()
42 match = re.match(r'^format=(\d+)$', firstline)
43 if not match:
44 warning("%r doesn't look like a marks file - not importing marks",
45 filename)
46 return None
47 elif match.group(1) != '1':
48 warning('format version in marks file %s not supported - not importing'
49 'marks', filename)
50 return None
52 # Read the branch info
53 branch_names = {}
54 for string in f.readline().rstrip('\n').split('\0'):
55 if not string:
56 continue
57 name, integer = string.rsplit('.', 1)
58 branch_names[name] = int(integer)
60 # Read the revision info
61 revision_ids = {}
62 for line in f:
63 line = line.rstrip('\n')
64 mark, revid = line.split(' ', 1)
65 revision_ids[mark] = revid
66 f.close()
67 return (revision_ids, branch_names)
70 def export_marks(filename, revision_ids, branch_names=None):
71 """Save marks to a file.
73 :param filename: filename to save data to
74 :param revision_ids: dictionary mapping marks -> bzr revision-ids
75 :param branch_names: dictionary mapping branch names to some magic #
76 """
77 try:
78 f = file(filename, 'w')
79 except IOError:
80 warning("Could not open export-marks file %s - not exporting marks",
81 filename)
82 return
83 f.write('format=1\n')
85 # Write the branch names line
86 if branch_names:
87 branch_names = [ '%s.%d' % x for x in branch_names.iteritems() ]
88 f.write('\0'.join(branch_names) + '\n')
89 else:
90 f.write('\0tmp.0\n')
92 # Write the revision info
93 for mark, revid in revision_ids.iteritems():
94 f.write('%s %s\n' % (mark, revid))
95 f.close()