/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
1
# Copyright (C) 2008 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Processor of import commands.
18
19
This module provides core processing functionality including an abstract class
20
for basing real processors on. See the processors package for examples.
21
"""
22
0.77.6 by Ian Clatworthy
no filtering tests
23
import sys
0.64.37 by Ian Clatworthy
create branches as required
24
from bzrlib.errors import NotBranchError
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
25
import errors
26
27
28
class ImportProcessor(object):
29
    """Base class for import processors.
30
    
31
    Subclasses should override the pre_*, post_* and *_handler
32
    methods as appropriate.
33
    """
34
0.64.12 by Ian Clatworthy
lightweight tags, filter processor and param validation
35
    known_params = []
36
0.64.8 by Ian Clatworthy
custom parameters for processors
37
    def __init__(self, bzrdir, params=None, verbose=False):
0.77.6 by Ian Clatworthy
no filtering tests
38
        self.outf = sys.stdout
0.64.12 by Ian Clatworthy
lightweight tags, filter processor and param validation
39
        self.verbose = verbose
40
        if params is None:
0.65.3 by James Westby
ImportProcessor.params is a dict, not a list.
41
            self.params = {}
0.64.12 by Ian Clatworthy
lightweight tags, filter processor and param validation
42
        else:
43
            self.params = params
44
            self.validate_parameters()
0.64.6 by Ian Clatworthy
generic processing method working for one revision in one branch
45
        self.bzrdir = bzrdir
46
        if bzrdir is None:
47
            # Some 'importers' don't need a repository to write to
0.64.37 by Ian Clatworthy
create branches as required
48
            self.working_tree = None
0.64.6 by Ian Clatworthy
generic processing method working for one revision in one branch
49
            self.branch = None
50
            self.repo = None
51
        else:
0.64.37 by Ian Clatworthy
create branches as required
52
            try:
53
                # Might be inside a branch
54
                (self.working_tree, self.branch) = bzrdir._get_tree_branch()
55
                self.repo = self.branch.repository
56
            except NotBranchError:
57
                # Must be inside a repository
58
                self.working_tree = None
59
                self.branch = None
60
                self.repo = bzrdir.open_repository()
0.64.9 by Ian Clatworthy
dump parameter for info processor
61
0.64.28 by Ian Clatworthy
checkpoint and count params to generic processor
62
        # Handlers can set this to request exiting cleanly without
63
        # iterating through the remaining commands
64
        self.finished = False
65
0.64.12 by Ian Clatworthy
lightweight tags, filter processor and param validation
66
    def validate_parameters(self):
67
        """Validate that the parameters are correctly specified."""
68
        for p in self.params:
69
            if p not in self.known_params:
70
                raise errors.UnknownParameter(p, self.known_params)
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
71
72
    def process(self, command_iter):
73
        """Import data into Bazaar by processing a stream of commands.
74
75
        :param command_iter: an iterator providing commands
76
        """
0.64.6 by Ian Clatworthy
generic processing method working for one revision in one branch
77
        if self.working_tree is not None:
78
            self.working_tree.lock_write()
79
        elif self.branch is not None:
80
            self.branch.lock_write()
0.64.37 by Ian Clatworthy
create branches as required
81
        elif self.repo is not None:
82
            self.repo.lock_write()
0.64.6 by Ian Clatworthy
generic processing method working for one revision in one branch
83
        try:
84
            self._process(command_iter)
85
        finally:
0.64.46 by Ian Clatworthy
abort write_group when an unexpected exception occurs so we see the latter
86
            # If an unhandled exception occurred, abort the write group
87
            if self.repo is not None and self.repo.is_in_write_group():
88
                self.repo.abort_write_group()
89
            # Release the locks
0.64.6 by Ian Clatworthy
generic processing method working for one revision in one branch
90
            if self.working_tree is not None:
91
                self.working_tree.unlock()
92
            elif self.branch is not None:
93
                self.branch.unlock()
0.64.37 by Ian Clatworthy
create branches as required
94
            elif self.repo is not None:
95
                self.repo.unlock()
0.64.6 by Ian Clatworthy
generic processing method working for one revision in one branch
96
97
    def _process(self, command_iter):
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
98
        self.pre_process()
99
        for cmd in command_iter():
100
            try:
101
                handler = self.__class__.__dict__[cmd.name + "_handler"]
102
            except KeyError:
103
                raise errors.MissingHandler(cmd.name)
104
            else:
0.64.9 by Ian Clatworthy
dump parameter for info processor
105
                self.pre_handler(cmd)
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
106
                handler(self, cmd)
0.64.9 by Ian Clatworthy
dump parameter for info processor
107
                self.post_handler(cmd)
0.64.28 by Ian Clatworthy
checkpoint and count params to generic processor
108
            if self.finished:
109
                break
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
110
        self.post_process()
111
112
    def pre_process(self):
113
        """Hook for logic at start of processing."""
114
        pass
115
116
    def post_process(self):
117
        """Hook for logic at end of processing."""
118
        pass
119
0.64.9 by Ian Clatworthy
dump parameter for info processor
120
    def pre_handler(self, cmd):
121
        """Hook for logic before each handler starts."""
122
        pass
123
124
    def post_handler(self, cmd):
125
        """Hook for logic after each handler finishes."""
126
        pass
127
0.64.1 by Ian Clatworthy
1st cut: gfi parser + --info processing method
128
    def progress_handler(self, cmd):
129
        """Process a ProgressCommand."""
130
        raise NotImplementedError(self.progress_handler)
131
132
    def blob_handler(self, cmd):
133
        """Process a BlobCommand."""
134
        raise NotImplementedError(self.blob_handler)
135
136
    def checkpoint_handler(self, cmd):
137
        """Process a CheckpointCommand."""
138
        raise NotImplementedError(self.checkpoint_handler)
139
140
    def commit_handler(self, cmd):
141
        """Process a CommitCommand."""
142
        raise NotImplementedError(self.commit_handler)
143
144
    def reset_handler(self, cmd):
145
        """Process a ResetCommand."""
146
        raise NotImplementedError(self.reset_handler)
147
148
    def tag_handler(self, cmd):
149
        """Process a TagCommand."""
150
        raise NotImplementedError(self.tag_handler)
0.64.4 by Ian Clatworthy
abstract CommitHandler
151
152
153
class CommitHandler(object):
154
    """Base class for commit handling.
155
    
156
    Subclasses should override the pre_*, post_* and *_handler
157
    methods as appropriate.
158
    """
159
160
    def __init__(self, command):
161
        self.command = command
162
163
    def process(self):
164
        self.pre_process_files()
165
        for fc in self.command.file_iter():
166
            try:
0.64.20 by Ian Clatworthy
clean-up fixes after filtering enhancements
167
                handler = self.__class__.__dict__[fc.name[4:] + "_handler"]
0.64.4 by Ian Clatworthy
abstract CommitHandler
168
            except KeyError:
169
                raise errors.MissingHandler(fc.name)
170
            else:
171
                handler(self, fc)
172
        self.post_process_files()
173
174
    def pre_process_files(self):
175
        """Prepare for committing."""
176
        pass
177
178
    def post_process_files(self):
179
        """Save the revision."""
180
        pass
181
182
    def modify_handler(self, filecmd):
183
        """Handle a filemodify command."""
184
        raise NotImplementedError(self.modify_handler)
185
186
    def delete_handler(self, filecmd):
187
        """Handle a filedelete command."""
188
        raise NotImplementedError(self.delete_handler)
189
190
    def copy_handler(self, filecmd):
191
        """Handle a filecopy command."""
192
        raise NotImplementedError(self.copy_handler)
193
194
    def rename_handler(self, filecmd):
195
        """Handle a filerename command."""
196
        raise NotImplementedError(self.rename_handler)
197
198
    def deleteall_handler(self, filecmd):
199
        """Handle a filedeleteall command."""
200
        raise NotImplementedError(self.deleteall_handler)