/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
0.79.2 by Ian Clatworthy
extend & use marks_file API
28
    :return: None if an error is encountered or (revision_ids, branch_names)
29
      where
30
      * revision_ids is a dictionary with marks as keys and revision-ids
31
        as values
32
      * branch_names is a dictionary mapping branch names to some magic #
0.78.5 by Ian Clatworthy
move import/export of marks into a module
33
    """
34
    # Check that the file is readable and in the right format
35
    try:
36
        f = file(filename)
37
    except IOError:
38
        warning("Could not import marks file %s - not importing marks",
39
            filename)
40
        return None
41
    firstline = f.readline()
42
    match = re.match(r'^format=(\d+)$', firstline)
43
    if not match:
44
        warning("%r doesn't look like a marks file - not importing marks",
45
            filename)
46
        return None
47
    elif match.group(1) != '1':
48
        warning('format version in marks file %s not supported - not importing'
49
            'marks', filename)
50
        return None
51
0.79.2 by Ian Clatworthy
extend & use marks_file API
52
    # Read the branch info
53
    branch_names = {}
0.78.5 by Ian Clatworthy
move import/export of marks into a module
54
    for string in f.readline().rstrip('\n').split('\0'):
55
        if not string:
56
            continue
57
        name, integer = string.rsplit('.', 1)
0.79.2 by Ian Clatworthy
extend & use marks_file API
58
        branch_names[name] = int(integer)
59
 
60
    # Read the revision info
0.78.5 by Ian Clatworthy
move import/export of marks into a module
61
    revision_ids = {}
62
    for line in f:
63
        line = line.rstrip('\n')
64
        mark, revid = line.split(' ', 1)
65
        revision_ids[mark] = revid
66
    f.close()
0.79.2 by Ian Clatworthy
extend & use marks_file API
67
    return (revision_ids, branch_names)
68
69
70
def export_marks(filename, revision_ids, branch_names=None):
0.78.5 by Ian Clatworthy
move import/export of marks into a module
71
    """Save marks to a file.
72
73
    :param filename: filename to save data to
74
    :param revision_ids: dictionary mapping marks -> bzr revision-ids
0.79.2 by Ian Clatworthy
extend & use marks_file API
75
    :param branch_names: dictionary mapping branch names to some magic #
0.78.5 by Ian Clatworthy
move import/export of marks into a module
76
    """
77
    try:
78
        f = file(filename, 'w')
79
    except IOError:
80
        warning("Could not open export-marks file %s - not exporting marks",
81
            filename)
82
        return
83
    f.write('format=1\n')
0.79.2 by Ian Clatworthy
extend & use marks_file API
84
85
    # Write the branch names line
86
    if branch_names:
87
        branch_names = [ '%s.%d' % x for x in branch_names.iteritems() ]
88
        f.write('\0'.join(branch_names) + '\n')
89
    else:
90
        f.write('\0tmp.0\n')
91
92
    # Write the revision info
0.78.5 by Ian Clatworthy
move import/export of marks into a module
93
    for mark, revid in revision_ids.iteritems():
94
        f.write('%s %s\n' % (mark, revid))
95
    f.close()