/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/bzr/__init__.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:59:44 UTC
  • mfrom: (7143.15.15 more-cleanups)
  • Revision ID: breezy.the.bot@gmail.com-20181116185944-biefv1sub37qfybm
Sprinkle some PEP8iness.

Merged from https://code.launchpad.net/~jelmer/brz/more-cleanups/+merge/358611

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2017 Breezy Developers
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
from __future__ import absolute_import
 
18
 
 
19
from .. import (
 
20
    config,
 
21
    errors,
 
22
    controldir,
 
23
    pyutils,
 
24
    registry,
 
25
    )
 
26
 
 
27
 
 
28
class BzrProber(controldir.Prober):
 
29
    """Prober for formats that use a .bzr/ control directory."""
 
30
 
 
31
    formats = registry.FormatRegistry(controldir.network_format_registry)
 
32
    """The known .bzr formats."""
 
33
 
 
34
    @classmethod
 
35
    def probe_transport(klass, transport):
 
36
        """Return the .bzrdir style format present in a directory."""
 
37
        try:
 
38
            format_string = transport.get_bytes(".bzr/branch-format")
 
39
        except errors.NoSuchFile:
 
40
            raise errors.NotBranchError(path=transport.base)
 
41
        try:
 
42
            first_line = format_string[:format_string.index(b"\n") + 1]
 
43
        except ValueError:
 
44
            first_line = format_string
 
45
        try:
 
46
            cls = klass.formats.get(first_line)
 
47
        except KeyError:
 
48
            if first_line.endswith(b"\r\n"):
 
49
                raise errors.LineEndingError(file=".bzr/branch-format")
 
50
            else:
 
51
                raise errors.UnknownFormatError(
 
52
                    format=first_line, kind='bzrdir')
 
53
        return cls.from_string(format_string)
 
54
 
 
55
    @classmethod
 
56
    def known_formats(cls):
 
57
        result = []
 
58
        for name, format in cls.formats.items():
 
59
            if callable(format):
 
60
                format = format()
 
61
            result.append(format)
 
62
        return result
 
63
 
 
64
 
 
65
controldir.ControlDirFormat.register_prober(BzrProber)
 
66
 
 
67
 
 
68
class RemoteBzrProber(controldir.Prober):
 
69
    """Prober for remote servers that provide a Bazaar smart server."""
 
70
 
 
71
    @classmethod
 
72
    def probe_transport(klass, transport):
 
73
        """Return a RemoteBzrDirFormat object if it looks possible."""
 
74
        try:
 
75
            medium = transport.get_smart_medium()
 
76
        except (NotImplementedError, AttributeError,
 
77
                errors.TransportNotPossible, errors.NoSmartMedium,
 
78
                errors.SmartProtocolError):
 
79
            # no smart server, so not a branch for this format type.
 
80
            raise errors.NotBranchError(path=transport.base)
 
81
        else:
 
82
            # Decline to open it if the server doesn't support our required
 
83
            # version (3) so that the VFS-based transport will do it.
 
84
            if medium.should_probe():
 
85
                try:
 
86
                    server_version = medium.protocol_version()
 
87
                except errors.SmartProtocolError:
 
88
                    # Apparently there's no usable smart server there, even though
 
89
                    # the medium supports the smart protocol.
 
90
                    raise errors.NotBranchError(path=transport.base)
 
91
                if server_version != '2':
 
92
                    raise errors.NotBranchError(path=transport.base)
 
93
            from .remote import RemoteBzrDirFormat
 
94
            return RemoteBzrDirFormat()
 
95
 
 
96
    @classmethod
 
97
    def known_formats(cls):
 
98
        from .remote import RemoteBzrDirFormat
 
99
        return [RemoteBzrDirFormat()]
 
100
 
 
101
 
 
102
controldir.ControlDirFormat.register_server_prober(RemoteBzrProber)
 
103
 
 
104
# Register bzr formats
 
105
BzrProber.formats.register_lazy(
 
106
    b"Bazaar-NG meta directory, format 1\n",
 
107
    __name__ + '.bzrdir', 'BzrDirMetaFormat1')
 
108
BzrProber.formats.register_lazy(
 
109
    b"Bazaar meta directory, format 1 (with colocated branches)\n",
 
110
    __name__ + '.bzrdir', 'BzrDirMetaFormat1Colo')
 
111
 
 
112
 
 
113
def register_metadir(registry, key,
 
114
                     repository_format, help, native=True, deprecated=False,
 
115
                     branch_format=None,
 
116
                     tree_format=None,
 
117
                     hidden=False,
 
118
                     experimental=False,
 
119
                     bzrdir_format=None):
 
120
    """Register a metadir subformat.
 
121
 
 
122
    These all use a meta bzrdir, but can be parameterized by the
 
123
    Repository/Branch/WorkingTreeformats.
 
124
 
 
125
    :param repository_format: The fully-qualified repository format class
 
126
        name as a string.
 
127
    :param branch_format: Fully-qualified branch format class name as
 
128
        a string.
 
129
    :param tree_format: Fully-qualified tree format class name as
 
130
        a string.
 
131
    """
 
132
    if bzrdir_format is None:
 
133
        bzrdir_format = 'breezy.bzr.bzrdir.BzrDirMetaFormat1'
 
134
    # This should be expanded to support setting WorkingTree and Branch
 
135
    # formats, once the API supports that.
 
136
 
 
137
    def _load(full_name):
 
138
        mod_name, factory_name = full_name.rsplit('.', 1)
 
139
        try:
 
140
            factory = pyutils.get_named_object(mod_name, factory_name)
 
141
        except ImportError as e:
 
142
            raise ImportError('failed to load %s: %s' % (full_name, e))
 
143
        except AttributeError:
 
144
            raise AttributeError('no factory %s in module %r'
 
145
                                 % (full_name, sys.modules[mod_name]))
 
146
        return factory()
 
147
 
 
148
    def helper():
 
149
        bd = _load(bzrdir_format)
 
150
        if branch_format is not None:
 
151
            bd.set_branch_format(_load(branch_format))
 
152
        if tree_format is not None:
 
153
            bd.workingtree_format = _load(tree_format)
 
154
        if repository_format is not None:
 
155
            bd.repository_format = _load(repository_format)
 
156
        return bd
 
157
    registry.register(key, helper, help, native, deprecated, hidden,
 
158
                      experimental)
 
159
 
 
160
 
 
161
register_metadir(
 
162
    controldir.format_registry, 'knit',
 
163
    'breezy.bzr.knitrepo.RepositoryFormatKnit1',
 
164
    'Format using knits.  Recommended for interoperation with bzr <= 0.14.',
 
165
    branch_format='breezy.bzr.fullhistory.BzrBranchFormat5',
 
166
    tree_format='breezy.bzr.workingtree_3.WorkingTreeFormat3',
 
167
    hidden=True,
 
168
    deprecated=True)
 
169
register_metadir(
 
170
    controldir.format_registry, 'dirstate',
 
171
    'breezy.bzr.knitrepo.RepositoryFormatKnit1',
 
172
    help='Format using dirstate for working trees. '
 
173
    'Compatible with bzr 0.8 and '
 
174
    'above when accessed over the network. Introduced in bzr 0.15.',
 
175
    branch_format='breezy.bzr.fullhistory.BzrBranchFormat5',
 
176
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat4',
 
177
    hidden=True,
 
178
    deprecated=True)
 
179
register_metadir(
 
180
    controldir.format_registry, 'dirstate-tags',
 
181
    'breezy.bzr.knitrepo.RepositoryFormatKnit1',
 
182
    help='Variant of dirstate with support for tags. '
 
183
    'Introduced in bzr 0.15.',
 
184
    branch_format='breezy.bzr.branch.BzrBranchFormat6',
 
185
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat4',
 
186
    hidden=True,
 
187
    deprecated=True)
 
188
register_metadir(
 
189
    controldir.format_registry, 'rich-root',
 
190
    'breezy.bzr.knitrepo.RepositoryFormatKnit4',
 
191
    help='Variant of dirstate with better handling of tree roots. '
 
192
    'Introduced in bzr 1.0',
 
193
    branch_format='breezy.bzr.branch.BzrBranchFormat6',
 
194
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat4',
 
195
    hidden=True,
 
196
    deprecated=True)
 
197
register_metadir(
 
198
    controldir.format_registry, 'dirstate-with-subtree',
 
199
    'breezy.bzr.knitrepo.RepositoryFormatKnit3',
 
200
    help='Variant of dirstate with support for nested trees. '
 
201
    'Introduced in 0.15.',
 
202
    branch_format='breezy.bzr.branch.BzrBranchFormat6',
 
203
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat4',
 
204
    experimental=True,
 
205
    hidden=True,
 
206
    )
 
207
register_metadir(
 
208
    controldir.format_registry, 'pack-0.92',
 
209
    'breezy.bzr.knitpack_repo.RepositoryFormatKnitPack1',
 
210
    help='Pack-based format used in 1.x series. Introduced in 0.92. '
 
211
    'Interoperates with bzr repositories before 0.92 but cannot be '
 
212
    'read by bzr < 0.92. ',
 
213
    branch_format='breezy.bzr.branch.BzrBranchFormat6',
 
214
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat4',
 
215
    deprecated=True,
 
216
    hidden=True,
 
217
    )
 
218
register_metadir(
 
219
    controldir.format_registry, 'pack-0.92-subtree',
 
220
    'breezy.bzr.knitpack_repo.RepositoryFormatKnitPack3',
 
221
    help='Pack-based format used in 1.x series, with subtree support. '
 
222
    'Introduced in 0.92. Interoperates with '
 
223
    'bzr repositories before 0.92 but cannot be read by bzr < 0.92. ',
 
224
    branch_format='breezy.bzr.branch.BzrBranchFormat6',
 
225
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat4',
 
226
    hidden=True,
 
227
    deprecated=True,
 
228
    experimental=True,
 
229
    )
 
230
register_metadir(
 
231
    controldir.format_registry, 'rich-root-pack',
 
232
    'breezy.bzr.knitpack_repo.RepositoryFormatKnitPack4',
 
233
    help='A variant of pack-0.92 that supports rich-root data '
 
234
    '(needed for bzr-svn and bzr-git). Introduced in 1.0.',
 
235
    branch_format='breezy.bzr.branch.BzrBranchFormat6',
 
236
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat4',
 
237
    hidden=True,
 
238
    deprecated=True,
 
239
    )
 
240
register_metadir(
 
241
    controldir.format_registry, '1.6',
 
242
    'breezy.bzr.knitpack_repo.RepositoryFormatKnitPack5',
 
243
    help='A format that allows a branch to indicate that there is another '
 
244
    '(stacked) repository that should be used to access data that is '
 
245
    'not present locally.',
 
246
    branch_format='breezy.bzr.branch.BzrBranchFormat7',
 
247
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat4',
 
248
    hidden=True,
 
249
    deprecated=True,
 
250
    )
 
251
register_metadir(
 
252
    controldir.format_registry, '1.6.1-rich-root',
 
253
    'breezy.bzr.knitpack_repo.RepositoryFormatKnitPack5RichRoot',
 
254
    help='A variant of 1.6 that supports rich-root data '
 
255
    '(needed for bzr-svn and bzr-git).',
 
256
    branch_format='breezy.bzr.branch.BzrBranchFormat7',
 
257
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat4',
 
258
    hidden=True,
 
259
    deprecated=True,
 
260
    )
 
261
register_metadir(
 
262
    controldir.format_registry, '1.9',
 
263
    'breezy.bzr.knitpack_repo.RepositoryFormatKnitPack6',
 
264
    help='A repository format using B+tree indexes. These indexes '
 
265
    'are smaller in size, have smarter caching and provide faster '
 
266
    'performance for most operations.',
 
267
    branch_format='breezy.bzr.branch.BzrBranchFormat7',
 
268
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat4',
 
269
    hidden=True,
 
270
    deprecated=True,
 
271
    )
 
272
register_metadir(
 
273
    controldir.format_registry, '1.9-rich-root',
 
274
    'breezy.bzr.knitpack_repo.RepositoryFormatKnitPack6RichRoot',
 
275
    help='A variant of 1.9 that supports rich-root data '
 
276
    '(needed for bzr-svn and bzr-git).',
 
277
    branch_format='breezy.bzr.branch.BzrBranchFormat7',
 
278
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat4',
 
279
    hidden=True,
 
280
    deprecated=True,
 
281
    )
 
282
register_metadir(
 
283
    controldir.format_registry, '1.14',
 
284
    'breezy.bzr.knitpack_repo.RepositoryFormatKnitPack6',
 
285
    help='A working-tree format that supports content filtering.',
 
286
    branch_format='breezy.bzr.branch.BzrBranchFormat7',
 
287
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat5',
 
288
    hidden=True,
 
289
    deprecated=True,
 
290
    )
 
291
register_metadir(
 
292
    controldir.format_registry, '1.14-rich-root',
 
293
    'breezy.bzr.knitpack_repo.RepositoryFormatKnitPack6RichRoot',
 
294
    help='A variant of 1.14 that supports rich-root data '
 
295
    '(needed for bzr-svn and bzr-git).',
 
296
    branch_format='breezy.bzr.branch.BzrBranchFormat7',
 
297
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat5',
 
298
    hidden=True,
 
299
    deprecated=True,
 
300
    )
 
301
# The following un-numbered 'development' formats should always just be aliases.
 
302
register_metadir(
 
303
    controldir.format_registry, 'development-subtree',
 
304
    'breezy.bzr.groupcompress_repo.RepositoryFormat2aSubtree',
 
305
    help='Current development format, subtree variant. Can convert data to and '
 
306
        'from pack-0.92-subtree (and anything compatible with '
 
307
        'pack-0.92-subtree) format repositories. Repositories and branches in '
 
308
        'this format can only be read by bzr.dev. Please read '
 
309
        'http://doc.bazaar.canonical.com/latest/developers/development-repo.html '
 
310
        'before use.',
 
311
    branch_format='breezy.bzr.branch.BzrBranchFormat8',
 
312
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat6',
 
313
    experimental=True,
 
314
    hidden=True,
 
315
    )
 
316
register_metadir(
 
317
    controldir.format_registry, 'development5-subtree',
 
318
    'breezy.bzr.knitpack_repo.RepositoryFormatPackDevelopment2Subtree',
 
319
    help='Development format, subtree variant. Can convert data to and '
 
320
    'from pack-0.92-subtree (and anything compatible with '
 
321
    'pack-0.92-subtree) format repositories. Repositories and branches in '
 
322
    'this format can only be read by bzr.dev. Please read '
 
323
    'http://doc.bazaar.canonical.com/latest/developers/development-repo.html '
 
324
    'before use.',
 
325
    branch_format='breezy.bzr.branch.BzrBranchFormat7',
 
326
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat6',
 
327
    experimental=True,
 
328
    hidden=True,
 
329
    )
 
330
 
 
331
register_metadir(
 
332
    controldir.format_registry, 'development-colo',
 
333
    'breezy.bzr.groupcompress_repo.RepositoryFormat2a',
 
334
    help='The 2a format with experimental support for colocated branches.\n',
 
335
    branch_format='breezy.bzr.branch.BzrBranchFormat7',
 
336
    tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat6',
 
337
    experimental=True,
 
338
    bzrdir_format='breezy.bzr.bzrdir.BzrDirMetaFormat1Colo',
 
339
    hidden=True,
 
340
    )
 
341
 
 
342
 
 
343
# And the development formats above will have aliased one of the following:
 
344
 
 
345
# Finally, the current format.
 
346
register_metadir(controldir.format_registry, '2a',
 
347
                 'breezy.bzr.groupcompress_repo.RepositoryFormat2a',
 
348
                 help='Format for the bzr 2.0 series.\n',
 
349
                 branch_format='breezy.bzr.branch.BzrBranchFormat7',
 
350
                 tree_format='breezy.bzr.workingtree_4.WorkingTreeFormat6',
 
351
                 experimental=False,
 
352
                 )
 
353
 
 
354
# The following format should be an alias for the rich root equivalent
 
355
# of the default format
 
356
 
 
357
controldir.format_registry.register_alias(
 
358
    'default-rich-root', '2a', hidden=True)
 
359
 
 
360
# The following format should is just an alias for the default bzr format.
 
361
controldir.format_registry.register_alias('bzr', '2a')
 
362
 
 
363
# The current format that is made on 'bzr init'.
 
364
format_name = config.GlobalStack().get('default_format')
 
365
controldir.format_registry.set_default(format_name)