/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
1
# Copyright (C) 2011 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
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
6779.1.3 by Jelmer Vernooij
Add some basic tests for repodebug.
17
from __future__ import absolute_import
18
from __future__ import print_function
19
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
20
from ...controldir import ControlDir
21
from ...commands import Command, Option
7143.11.1 by Jelmer Vernooij
Remove some unused imports.
22
from ... import errors, urlutils
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
23
24
25
class cmd_fetch_all_records(Command):
26
    __doc__ = """Fetch all records from another repository.
6779.1.3 by Jelmer Vernooij
Add some basic tests for repodebug.
27
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
28
    This inserts every key from SOURCE_REPO into the target repository.  Unlike
29
    regular fetches this doesn't assume any relationship between keys (e.g.
30
    that text X may be assumed to be present if inventory Y is present), so it
31
    can be used to repair repositories where invariants about those
32
    relationships have somehow been violated.
33
    """
34
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
35
    hidden = True
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
36
    takes_args = ['source_repo']
37
    takes_options = [
38
        'directory',
39
        Option('dry-run',
40
               help="Show what would be done, but don't actually do anything."),
41
        ]
42
43
    def run(self, source_repo, directory=u'.', dry_run=False):
44
        try:
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
45
            source = ControlDir.open(source_repo).open_repository()
6779.1.3 by Jelmer Vernooij
Add some basic tests for repodebug.
46
        except (errors.NotBranchError, urlutils.InvalidURL):
47
            print(u"Not a branch or invalid URL: %s" % source_repo,
48
                  file=self.outf)
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
49
            return
50
51
        try:
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
52
            target = ControlDir.open(directory).open_repository()
6779.1.3 by Jelmer Vernooij
Add some basic tests for repodebug.
53
        except (errors.NotBranchError, urlutils.InvalidURL):
7143.15.2 by Jelmer Vernooij
Run autopep8.
54
            print(u"Not a branch or invalid URL: %s" %
55
                  directory, file=self.outf)
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
56
            return
57
58
        self.add_cleanup(source.lock_read().unlock)
59
        self.add_cleanup(target.lock_write().unlock)
0.198.9 by John Arbash Meinel
Find the needed keys before starting the stream.
60
61
        # We need to find the keys to insert before we start the stream.
62
        # Otherwise we'll be querying the target repo while we're trying to
63
        # insert into it.
64
        needed = []
65
        for vf_name in ['signatures', 'texts', 'chk_bytes', 'inventories',
66
                        'revisions']:
67
            vf = getattr(source, vf_name)
68
            target_vf = getattr(target, vf_name)
69
            source_keys = vf.keys()
70
            target_keys = target_vf.keys()
71
            keys = source_keys.difference(target_keys)
72
            needed.append((vf_name, keys))
73
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
74
        def source_stream():
0.198.9 by John Arbash Meinel
Find the needed keys before starting the stream.
75
            for vf_name, keys in needed:
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
76
                vf = getattr(source, vf_name)
77
                yield (vf_name, vf.get_record_stream(keys, 'unordered', True))
0.198.9 by John Arbash Meinel
Find the needed keys before starting the stream.
78
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
79
        resume_tokens, missing_keys = target._get_sink().insert_stream(
80
            source_stream(), source._format, [])
81
82
        if not resume_tokens:
6779.1.3 by Jelmer Vernooij
Add some basic tests for repodebug.
83
            print("Done.", file=self.outf)
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
84
        else:
6779.1.3 by Jelmer Vernooij
Add some basic tests for repodebug.
85
            print("Missing keys!", missing_keys, file=self.outf)