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