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