1
# Copyright (C) 2008 Canonical Ltd
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.
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.
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
17
"""Processor of import commands.
19
This module provides core processing functionality including an abstract class
20
for basing real processors on. See the processors package for examples.
24
from bzrlib.errors import NotBranchError
28
class ImportProcessor(object):
29
"""Base class for import processors.
31
Subclasses should override the pre_*, post_* and *_handler
32
methods as appropriate.
37
def __init__(self, bzrdir, params=None, verbose=False):
38
self.verbose = verbose
43
self.validate_parameters()
46
# Some 'importers' don't need a repository to write to
47
self.working_tree = None
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
59
self.repo = bzrdir.open_repository()
61
# Handlers can set this to request exiting cleanly without
62
# iterating through the remaining commands
65
def validate_parameters(self):
66
"""Validate that the parameters are correctly specified."""
68
if p not in self.known_params:
69
raise errors.UnknownParameter(p, self.known_params)
71
def process(self, command_iter):
72
"""Import data into Bazaar by processing a stream of commands.
74
:param command_iter: an iterator providing commands
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()
80
elif self.repo is not None:
81
self.repo.lock_write()
83
self._process(command_iter)
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()
89
if self.working_tree is not None:
90
self.working_tree.unlock()
91
elif self.branch is not None:
93
elif self.repo is not None:
96
def _process(self, command_iter):
98
for cmd in command_iter():
100
handler = self.__class__.__dict__[cmd.name + "_handler"]
102
raise errors.MissingHandler(cmd.name)
104
self.pre_handler(cmd)
106
self.post_handler(cmd)
111
def pre_process(self):
112
"""Hook for logic at start of processing."""
115
def post_process(self):
116
"""Hook for logic at end of processing."""
119
def pre_handler(self, cmd):
120
"""Hook for logic before each handler starts."""
123
def post_handler(self, cmd):
124
"""Hook for logic after each handler finishes."""
127
def progress_handler(self, cmd):
128
"""Process a ProgressCommand."""
129
raise NotImplementedError(self.progress_handler)
131
def blob_handler(self, cmd):
132
"""Process a BlobCommand."""
133
raise NotImplementedError(self.blob_handler)
135
def checkpoint_handler(self, cmd):
136
"""Process a CheckpointCommand."""
137
raise NotImplementedError(self.checkpoint_handler)
139
def commit_handler(self, cmd):
140
"""Process a CommitCommand."""
141
raise NotImplementedError(self.commit_handler)
143
def reset_handler(self, cmd):
144
"""Process a ResetCommand."""
145
raise NotImplementedError(self.reset_handler)
147
def tag_handler(self, cmd):
148
"""Process a TagCommand."""
149
raise NotImplementedError(self.tag_handler)
152
class CommitHandler(object):
153
"""Base class for commit handling.
155
Subclasses should override the pre_*, post_* and *_handler
156
methods as appropriate.
159
def __init__(self, command):
160
self.command = command
163
self.pre_process_files()
164
for fc in self.command.file_iter():
166
handler = self.__class__.__dict__[fc.name[4:] + "_handler"]
168
raise errors.MissingHandler(fc.name)
171
self.post_process_files()
173
def pre_process_files(self):
174
"""Prepare for committing."""
177
def post_process_files(self):
178
"""Save the revision."""
181
def modify_handler(self, filecmd):
182
"""Handle a filemodify command."""
183
raise NotImplementedError(self.modify_handler)
185
def delete_handler(self, filecmd):
186
"""Handle a filedelete command."""
187
raise NotImplementedError(self.delete_handler)
189
def copy_handler(self, filecmd):
190
"""Handle a filecopy command."""
191
raise NotImplementedError(self.copy_handler)
193
def rename_handler(self, filecmd):
194
"""Handle a filerename command."""
195
raise NotImplementedError(self.rename_handler)
197
def deleteall_handler(self, filecmd):
198
"""Handle a filedeleteall command."""
199
raise NotImplementedError(self.deleteall_handler)