/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.64.252 by Ian Clatworthy
Add --user-map option to both fast-import and fast-import-filter
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.64.252 by Ian Clatworthy
Add --user-map option to both fast-import and fast-import-filter
15
6628.1.2 by Jelmer Vernooij
Fix imports, move exporter.py, drop explorer metadata.
16
from __future__ import absolute_import
17
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
18
try:
19
    from email.utils import parseaddr
20
except ImportError:
21
    from email.Utils import parseaddr
0.64.252 by Ian Clatworthy
Add --user-map option to both fast-import and fast-import-filter
22
23
24
class UserMapper(object):
25
26
    def __init__(self, lines):
27
        """Create a user-mapper from a list of lines.
28
29
        Blank lines and comment lines (starting with #) are ignored.
30
        Otherwise lines are of the form:
31
32
          old-id = new-id
33
34
        Each id may be in the following forms:
35
36
          name <email>
37
          name
38
39
        If old-id has the value '@', then new-id is the domain to use
40
        when generating an email from a user-id.
41
        """
42
        self._parse(lines)
43
44
    def _parse(self, lines):
45
        self._user_map = {}
46
        self._default_domain = None
47
        for line in lines:
48
            line = line.strip()
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
49
            if len(line) == 0 or line.startswith(b'#'):
0.64.252 by Ian Clatworthy
Add --user-map option to both fast-import and fast-import-filter
50
                continue
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
51
            old, new = line.split(b'=', 1)
0.64.252 by Ian Clatworthy
Add --user-map option to both fast-import and fast-import-filter
52
            old = old.strip()
53
            new = new.strip()
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
54
            if old == b'@':
0.64.252 by Ian Clatworthy
Add --user-map option to both fast-import and fast-import-filter
55
                self._default_domain = new
56
                continue
57
            # Parse each id into a name and email address
58
            old_name, old_email = self._parse_id(old)
59
            new_name, new_email = self._parse_id(new)
60
            #print "found user map: %s => %s" % ((old_name, old_email), (new_name, new_email))
61
            self._user_map[(old_name, old_email)] = (new_name, new_email)
62
63
    def _parse_id(self, id):
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
64
        if id.find(b'<') == -1:
65
            return id, b""
0.64.252 by Ian Clatworthy
Add --user-map option to both fast-import and fast-import-filter
66
        else:
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
67
            return parseaddr(id)
0.64.252 by Ian Clatworthy
Add --user-map option to both fast-import and fast-import-filter
68
69
    def map_name_and_email(self, name, email):
70
        """Map a name and an email to the preferred name and email.
71
72
        :param name: the current name
73
        :param email: the current email
74
        :result: the preferred name and email
75
        """
76
        try:
77
            new_name, new_email = self._user_map[(name, email)]
78
        except KeyError:
79
            new_name = name
80
            if self._default_domain and not email:
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
81
                new_email = b"%s@%s" % (name, self._default_domain)
0.64.252 by Ian Clatworthy
Add --user-map option to both fast-import and fast-import-filter
82
            else:
83
                new_email = email
84
        return new_name, new_email