/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 idmapfile.py

  • Committer: Ian Clatworthy
  • Date: 2009-02-17 23:37:24 UTC
  • mto: (0.64.114 trunk)
  • mto: This revision was merged to the branch mainline in revision 6631.
  • Revision ID: ian.clatworthy@canonical.com-20090217233724-y6q12cyoyln6vkh6
code & tests for file copying

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, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Routines for saving and loading the id-map file."""
 
18
 
 
19
import os
 
20
 
 
21
 
 
22
def save_id_map(filename, revision_ids):
 
23
    """Save the mapping of commit ids to revision ids to a file.
 
24
 
 
25
    Throws the usual exceptions if the file cannot be opened,
 
26
    written to or closed.
 
27
 
 
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()
 
38
 
 
39
 
 
40
def load_id_map(filename):
 
41
    """Load the mapping of commit ids to revision ids from a file.
 
42
 
 
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.
 
46
 
 
47
    NOTE: It is assumed that commit-ids do not have embedded spaces.
 
48
 
 
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