Fix bzr-fastimport when used with newer versions of python-fastimport.(Jelmer Vernooij)
[bzr-fastimport.git] / idmapfile.py
blob669dbcecf8eb4f0df7c9eed0a3c48d45ac4bac82
1 # Copyright (C) 2008 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 saving and loading the id-map file."""
18 import os
21 def save_id_map(filename, revision_ids):
22 """Save the mapping of commit ids to revision ids to a file.
24 Throws the usual exceptions if the file cannot be opened,
25 written to or closed.
27 :param filename: name of the file to save the data to
28 :param revision_ids: a dictionary of commit ids to revision ids.
29 """
30 f = open(filename, 'wb')
31 try:
32 for commit_id, rev_id in revision_ids.iteritems():
33 f.write("%s %s\n" % (commit_id, rev_id))
34 f.flush()
35 finally:
36 f.close()
39 def load_id_map(filename):
40 """Load the mapping of commit ids to revision ids from a file.
42 If the file does not exist, an empty result is returned.
43 If the file does exists but cannot be opened, read or closed,
44 the normal exceptions are thrown.
46 NOTE: It is assumed that commit-ids do not have embedded spaces.
48 :param filename: name of the file to save the data to
49 :result: map, count where:
50 map = a dictionary of commit ids to revision ids;
51 count = the number of keys in map
52 """
53 result = {}
54 count = 0
55 if os.path.exists(filename):
56 f = open(filename)
57 try:
58 for line in f:
59 parts = line[:-1].split(' ', 1)
60 result[parts[0]] = parts[1]
61 count += 1
62 finally:
63 f.close()
64 return result, count