/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
0.64.334 by Jelmer Vernooij
Remove old FSF address. Thanks Dan Callaghan.
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
0.78.5 by Ian Clatworthy
move import/export of marks into a module
15
16
"""Routines for reading/writing a marks file."""
17
6628.1.2 by Jelmer Vernooij
Fix imports, move exporter.py, drop explorer metadata.
18
from ...trace import warning
0.78.5 by Ian Clatworthy
move import/export of marks into a module
19
20
21
def import_marks(filename):
22
    """Read the mapping of marks to revision-ids from a file.
23
24
    :param filename: the file to read from
0.125.1 by Ian Clatworthy
Use the new marks file format (introduced in git 1.6 apparently)
25
    :return: None if an error is encountered or a dictionary with marks
26
        as keys and revision-ids as values
0.78.5 by Ian Clatworthy
move import/export of marks into a module
27
    """
28
    # Check that the file is readable and in the right format
29
    try:
7490.78.1 by Jelmer Vernooij
Fix marks file handling on Python 3.
30
        f = open(filename, 'rb')
0.78.5 by Ian Clatworthy
move import/export of marks into a module
31
    except IOError:
32
        warning("Could not import marks file %s - not importing marks",
7143.15.2 by Jelmer Vernooij
Run autopep8.
33
                filename)
0.78.5 by Ian Clatworthy
move import/export of marks into a module
34
        return None
35
7027.2.7 by Jelmer Vernooij
Review comments.
36
    try:
37
        # Read the revision info
38
        revision_ids = {}
39
40
        line = f.readline()
7490.78.1 by Jelmer Vernooij
Fix marks file handling on Python 3.
41
        if line == b'format=1\n':
7027.2.7 by Jelmer Vernooij
Review comments.
42
            # Cope with old-style marks files
43
            # Read the branch info
44
            branch_names = {}
7490.78.1 by Jelmer Vernooij
Fix marks file handling on Python 3.
45
            for string in f.readline().rstrip(b'\n').split(b'\0'):
7027.2.7 by Jelmer Vernooij
Review comments.
46
                if not string:
47
                    continue
7490.78.1 by Jelmer Vernooij
Fix marks file handling on Python 3.
48
                name, integer = string.rsplit(b'.', 1)
7027.2.7 by Jelmer Vernooij
Review comments.
49
                branch_names[name] = int(integer)
50
            line = f.readline()
51
52
        while line:
7490.78.1 by Jelmer Vernooij
Fix marks file handling on Python 3.
53
            line = line.rstrip(b'\n')
54
            mark, revid = line.split(b' ', 1)
55
            mark = mark.lstrip(b':')
56
            revision_ids[mark] = revid
7027.2.7 by Jelmer Vernooij
Review comments.
57
            line = f.readline()
58
    finally:
59
        f.close()
0.125.1 by Ian Clatworthy
Use the new marks file format (introduced in git 1.6 apparently)
60
    return revision_ids
61
62
63
def export_marks(filename, revision_ids):
0.78.5 by Ian Clatworthy
move import/export of marks into a module
64
    """Save marks to a file.
65
66
    :param filename: filename to save data to
67
    :param revision_ids: dictionary mapping marks -> bzr revision-ids
68
    """
69
    try:
7490.78.2 by Jelmer Vernooij
Make output consistent.
70
        f = open(filename, 'wb')
0.78.5 by Ian Clatworthy
move import/export of marks into a module
71
    except IOError:
72
        warning("Could not open export-marks file %s - not exporting marks",
7143.15.2 by Jelmer Vernooij
Run autopep8.
73
                filename)
0.78.5 by Ian Clatworthy
move import/export of marks into a module
74
        return
0.79.2 by Ian Clatworthy
extend & use marks_file API
75
7027.2.7 by Jelmer Vernooij
Review comments.
76
    try:
77
        # Write the revision info
7490.78.2 by Jelmer Vernooij
Make output consistent.
78
        for mark, revid in sorted(revision_ids.items()):
79
            f.write(b':%s %s\n' % (mark.lstrip(b':'), revid))
7027.2.7 by Jelmer Vernooij
Review comments.
80
    finally:
81
        f.close()