git_remote_helpers: add fastimport library
[git/dscho.git] / git_remote_helpers / fastimport / idmapfile.py
blob7b4ccf4afe185a90188c06d4f3ed523afa80d684
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, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 """Routines for saving and loading the id-map file."""
19 import os
22 def save_id_map(filename, revision_ids):
23 """Save the mapping of commit ids to revision ids to a file.
25 Throws the usual exceptions if the file cannot be opened,
26 written to or closed.
28 :param filename: name of the file to save the data to
29 :param revision_ids: a dictionary of commit ids to revision ids.
30 """
31 f = open(filename, 'wb')
32 try:
33 for commit_id, rev_id in revision_ids.iteritems():
34 f.write("%s %s\n" % (commit_id, rev_id))
35 f.flush()
36 finally:
37 f.close()
40 def load_id_map(filename):
41 """Load the mapping of commit ids to revision ids from a file.
43 If the file does not exist, an empty result is returned.
44 If the file does exists but cannot be opened, read or closed,
45 the normal exceptions are thrown.
47 NOTE: It is assumed that commit-ids do not have embedded spaces.
49 :param filename: name of the file to save the data to
50 :result: map, count where:
51 map = a dictionary of commit ids to revision ids;
52 count = the number of keys in map
53 """
54 result = {}
55 count = 0
56 if os.path.exists(filename):
57 f = open(filename)
58 try:
59 for line in f:
60 parts = line[:-1].split(' ', 1)
61 result[parts[0]] = parts[1]
62 count += 1
63 finally:
64 f.close()
65 return result, count