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