/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/fastimport/helpers.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-08 23:30:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170608233031-3qavls2o7a1pqllj
Update imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
"""Miscellaneous useful stuff."""
17
17
 
 
18
from __future__ import absolute_import
 
19
 
18
20
import stat
19
21
 
20
 
from ... import (
21
 
    controldir,
22
 
    )
23
 
 
24
22
 
25
23
def escape_commit_message(message):
26
24
    """Replace xml-incompatible control characters."""
51
49
    :return: the BzrDirFormat or None if no matches were found.
52
50
    """
53
51
    # Based on code from breezy/info.py ...
 
52
    from ... import bzrdir
54
53
    repo_format = repo._format
55
 
    candidates = []
 
54
    candidates  = []
56
55
    non_aliases = set(controldir.format_registry.keys())
57
56
    non_aliases.difference_update(controldir.format_registry.aliases())
58
57
    for key in non_aliases:
59
 
        format = controldir.format_registry.make_controldir(key)
 
58
        format = controldir.format_registry.make_bzrdir(key)
60
59
        # LocalGitBzrDirFormat has no repository_format
61
60
        if hasattr(format, "repository_format"):
62
61
            if format.repository_format == repo_format:
94
93
    if os.path.exists(location):
95
94
        contents = os.listdir(location)
96
95
        if contents:
97
 
            errors.CommandError("Destination must have a .bzr directory, "
98
 
                                   " not yet exist or be empty - files found in %s" % (location,))
 
96
            errors.BzrCommandError("Destination must have a .bzr directory, "
 
97
                " not yet exist or be empty - files found in %s" % (location,))
99
98
    else:
100
99
        try:
101
100
            os.mkdir(location)
102
 
        except IOError as ex:
103
 
            raise errors.CommandError(
104
 
                "Unable to create %s: %s" % (location, ex))
 
101
        except IOError, ex:
 
102
            errors.BzrCommandError("Unable to create %s: %s" %
 
103
                (location, ex))
105
104
 
106
105
    # Create a repository for the nominated format.
107
106
    trace.note("Creating destination repository ...")
108
107
    if format is None:
109
 
        format = controldir.format_registry.make_controldir('default')
 
108
        format = controldir.format_registry.make_bzrdir('default')
110
109
    to_transport = transport.get_transport(location)
111
110
    to_transport.ensure_base()
112
111
    control = format.initialize_on_transport(to_transport)
113
112
    repo = control.create_repository(shared=True)
114
113
    if verbose:
115
114
        from ...info import show_bzrdir_info
116
 
        show_bzrdir_info(repo.controldir, verbose=0)
 
115
        show_bzrdir_info(repo.bzrdir, verbose=0)
117
116
    return control
118
117
 
119
118
 
120
119
def kind_to_mode(kind, executable):
121
120
    if kind == "file":
122
 
        if executable is True:
123
 
            return stat.S_IFREG | 0o755
124
 
        elif executable is False:
125
 
            return stat.S_IFREG | 0o644
 
121
        if executable == True:
 
122
            return stat.S_IFREG | 0755
 
123
        elif executable == False:
 
124
            return stat.S_IFREG | 0644
126
125
        else:
127
126
            raise AssertionError("Executable %r invalid" % executable)
128
127
    elif kind == "symlink":
130
129
    elif kind == "directory":
131
130
        return stat.S_IFDIR
132
131
    elif kind == "tree-reference":
133
 
        return 0o160000
 
132
        return 0160000
134
133
    else:
135
134
        raise AssertionError("Unknown file kind '%s'" % kind)
136
135
 
137
136
 
138
137
def mode_to_kind(mode):
139
138
    # Note: Output from git-fast-export slightly different to spec
140
 
    if mode in (0o644, 0o100644):
 
139
    if mode in (0644, 0100644):
141
140
        return 'file', False
142
 
    elif mode in (0o755, 0o100755):
 
141
    elif mode in (0755, 0100755):
143
142
        return 'file', True
144
 
    elif mode == 0o040000:
 
143
    elif mode == 0040000:
145
144
        return 'directory', False
146
 
    elif mode == 0o120000:
 
145
    elif mode == 0120000:
147
146
        return 'symlink', False
148
 
    elif mode == 0o160000:
 
147
    elif mode == 0160000:
149
148
        return 'tree-reference', False
150
149
    else:
151
150
        raise AssertionError("invalid mode %o" % mode)
176
175
        return single
177
176
    else:
178
177
        return plural
 
178
 
 
179
 
 
180
def invert_dictset(d):
 
181
    """Invert a dictionary with keys matching a set of values, turned into lists."""
 
182
    # Based on recipe from ASPN
 
183
    result = {}
 
184
    for k, c in d.items():
 
185
        for v in c:
 
186
            keys = result.setdefault(v, [])
 
187
            keys.append(k)
 
188
    return result
 
189
 
 
190
 
 
191
def invert_dict(d):
 
192
    """Invert a dictionary with keys matching each value turned into a list."""
 
193
    # Based on recipe from ASPN
 
194
    result = {}
 
195
    for k, v in d.items():
 
196
        keys = result.setdefault(v, [])
 
197
        keys.append(k)
 
198
    return result
 
199
 
 
200