/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/fastimport/idmapfile.py

[merge] robertc's integration, updated tests to check for retcode=3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
2
 
#
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.
7
 
#
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.
12
 
#
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/>.
15
 
 
16
 
"""Routines for saving and loading the id-map file."""
17
 
 
18
 
from __future__ import absolute_import
19
 
 
20
 
import os
21
 
 
22
 
 
23
 
def save_id_map(filename, revision_ids):
24
 
    """Save the mapping of commit ids to revision ids to a file.
25
 
 
26
 
    Throws the usual exceptions if the file cannot be opened,
27
 
    written to or closed.
28
 
 
29
 
    :param filename: name of the file to save the data to
30
 
    :param revision_ids: a dictionary of commit ids to revision ids.
31
 
    """
32
 
    with open(filename, 'wb') as f:
33
 
        for commit_id in revision_ids:
34
 
            f.write(b"%s %s\n" % (commit_id, revision_ids[commit_id]))
35
 
 
36
 
 
37
 
def load_id_map(filename):
38
 
    """Load the mapping of commit ids to revision ids from a file.
39
 
 
40
 
    If the file does not exist, an empty result is returned.
41
 
    If the file does exists but cannot be opened, read or closed,
42
 
    the normal exceptions are thrown.
43
 
 
44
 
    NOTE: It is assumed that commit-ids do not have embedded spaces.
45
 
 
46
 
    :param filename: name of the file to save the data to
47
 
    :result: map, count where:
48
 
      map = a dictionary of commit ids to revision ids;
49
 
      count = the number of keys in map
50
 
    """
51
 
    result = {}
52
 
    count = 0
53
 
    if os.path.exists(filename):
54
 
        with open(filename) as f:
55
 
            for line in f:
56
 
                parts = line[:-1].split(' ', 1)
57
 
                result[parts[0]] = parts[1]
58
 
                count += 1
59
 
    return result, count