/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.78.5 by Ian Clatworthy
move import/export of marks into a module
1
# Copyright (C) 2009 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 reading/writing a marks file."""
18
19
20
import re
21
from bzrlib.trace import warning
22
23
24
def import_marks(filename):
25
    """Read the mapping of marks to revision-ids from a file.
26
27
    :param filename: the file to read from
28
    :return: a dictionary with marks as keys and revision-ids as values,
29
      or None if an error was encountered
30
    """
31
    # Check that the file is readable and in the right format
32
    try:
33
        f = file(filename)
34
    except IOError:
35
        warning("Could not import marks file %s - not importing marks",
36
            filename)
37
        return None
38
    firstline = f.readline()
39
    match = re.match(r'^format=(\d+)$', firstline)
40
    if not match:
41
        warning("%r doesn't look like a marks file - not importing marks",
42
            filename)
43
        return None
44
    elif match.group(1) != '1':
45
        warning('format version in marks file %s not supported - not importing'
46
            'marks', filename)
47
        return None
48
49
    for string in f.readline().rstrip('\n').split('\0'):
50
        if not string:
51
            continue
52
        name, integer = string.rsplit('.', 1)
53
        # We really can't do anything with the branch information, so we
54
        # just skip it
55
        
56
    revision_ids = {}
57
    for line in f:
58
        line = line.rstrip('\n')
59
        mark, revid = line.split(' ', 1)
60
        revision_ids[mark] = revid
61
    f.close()
62
    return revision_ids
63
64
65
def export_marks(filename, revision_ids):
66
    """Save marks to a file.
67
68
    :param filename: filename to save data to
69
    :param revision_ids: dictionary mapping marks -> bzr revision-ids
70
    """
71
    try:
72
        f = file(filename, 'w')
73
    except IOError:
74
        warning("Could not open export-marks file %s - not exporting marks",
75
            filename)
76
        return
77
    f.write('format=1\n')
78
    f.write('\0tmp.0\n')
79
    for mark, revid in revision_ids.iteritems():
80
        f.write('%s %s\n' % (mark, revid))
81
    f.close()