~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to fai/command/replay.py

  • Committer: Robert Collins
  • Date: 2005-09-13 13:07:03 UTC
  • mto: (147.2.6) (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: robertc@robertcollins.net-20050913130702-f471ae9f2833a484
create the output directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004 Aaron Bentley
 
2
# <aaron.bentley@utoronto.ca>
 
3
#
 
4
#    This program is free software; you can redistribute it and/or modify
 
5
#    it under the terms of the GNU General Public License as published by
 
6
#    the Free Software Foundation; either version 2 of the License, or
 
7
#    (at your option) any later version.
 
8
#
 
9
#    This program is distributed in the hope that it will be useful,
 
10
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
#    GNU General Public License for more details.
 
13
#
 
14
#    You should have received a copy of the GNU General Public License
 
15
#    along with this program; if not, write to the Free Software
 
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
import sys
 
19
import os
 
20
import shutil
 
21
sys.path=[os.path.realpath(os.path.dirname(__file__)+"/..")]+sys.path
 
22
import arch
 
23
 
 
24
import pylon
 
25
import pylon.util
 
26
from pylon import errors
 
27
 
 
28
import cmdutil
 
29
import commands
 
30
 
 
31
__docformat__ = "restructuredtext"
 
32
__doc__ = "Native replay command"
 
33
 
 
34
def read_changeset(revision):
 
35
    my_dir = pylon.util.tmpdir(os.getcwd())
 
36
    changeset = revision.get_patch(my_dir)
 
37
    real = pylon.read_arch_changeset(changeset)
 
38
    shutil.rmtree(my_dir)
 
39
    return real
 
40
 
 
41
class ComposeHandler:
 
42
    def __init__(self, tree, reverse, munge_opts):
 
43
        self.tree = tree
 
44
        self.reverse = reverse
 
45
        self.munge_opts = munge_opts
 
46
        self.last_changeset = None
 
47
 
 
48
    def handle_revision(self, revision):
 
49
        if self.last_changeset is None:
 
50
            cmdutil.chatter("Reading %s" % str(revision))
 
51
            self.last_changeset = read_changeset(revision)
 
52
        else:
 
53
            cmdutil.chatter("Combining with %s" % str(revision))
 
54
            next_changeset = read_changeset(revision)
 
55
            self.last_changeset = pylon.compose_changesets(self.last_changeset,
 
56
                                                           next_changeset)
 
57
        return 0
 
58
 
 
59
 
 
60
    def finalize(self):
 
61
        if self.last_changeset is None:
 
62
            return False
 
63
        cmdutil.chatter("Applying changeset")
 
64
        inventory = pylon.get_id_filename_map(self.tree)
 
65
        pylon.add_inventory_missing(inventory, self.last_changeset, 
 
66
                                    self.reverse)
 
67
        pylon.apply_changeset(self.last_changeset, inventory, self.tree, 
 
68
                              pylon.ArchConflictHandler(self.tree), 
 
69
                              self.reverse)
 
70
        return True
 
71
 
 
72
class ApplyNativeHandler:
 
73
    def __init__(self, tree, reverse, munge_opts):
 
74
        self.tree = tree
 
75
        self.reverse = reverse
 
76
        self.did_nothing = True
 
77
        self.munge_opts = munge_opts
 
78
 
 
79
    def handle_revision(self, revision):
 
80
        self.did_nothing = False
 
81
        cmdutil.chatter("Applying %s" % str(revision))
 
82
        self.last_changeset = read_changeset(revision)
 
83
        inventory = pylon.get_id_filename_map(self.tree)
 
84
        pylon.add_inventory_missing(inventory, self.last_changeset, 
 
85
                                    self.reverse)
 
86
        conflict_handler = pylon.ArchConflictHandler(self.tree)
 
87
        pylon.apply_changeset(self.last_changeset, inventory, self.tree, 
 
88
                              conflict_handler, self.reverse)
 
89
        if conflict_handler.stop_conflicts:
 
90
            return 1
 
91
        else:
 
92
            return 0
 
93
 
 
94
    def finalize(self):
 
95
        return not self.did_nothing
 
96
 
 
97
class ApplyHandler:
 
98
    def __init__(self, tree, reverse, munge_opts):
 
99
        self.tree = tree
 
100
        self.reverse = reverse
 
101
        self.did_nothing = True
 
102
        self.munge_opts = munge_opts
 
103
 
 
104
    def handle_revision(self, revision):
 
105
        self.did_nothing = False
 
106
        cmdutil.chatter("Applying %s" % str(revision))
 
107
        result = pylon.replay(self.tree, revision, cmdutil.colorize,
 
108
                              reverse=self.reverse, munge_opts=self.munge_opts)
 
109
        return result
 
110
 
 
111
    def finalize(self):
 
112
        return not self.did_nothing
 
113
 
 
114
class Replay(commands.BaseCommand):
 
115
    """Apply a revision's changeset to the current tree"""
 
116
    def __init__(self):
 
117
        self.description = self.__doc__
 
118
 
 
119
    def get_completer(self, arg, index):
 
120
        return cmdutil.merge_completions(arch.tree_root("."), arg, index)
 
121
 
 
122
    def do_command(self, cmdargs):
 
123
        try:
 
124
            tree = arch.tree_root(".")
 
125
        except arch.errors.TreeRootError, e:
 
126
            raise errors.CommandFailedWrapper(e)
 
127
            
 
128
        parser = self.get_parser()
 
129
        (options, args) = parser.parse_args(cmdargs)
 
130
        action = options.type
 
131
        if action == "default":
 
132
            action = "skip-present"
 
133
 
 
134
        if options.logs_only:
 
135
            munge_opts = pylon.MungeOpts()
 
136
            munge_opts.all_types(True)
 
137
            munge_opts.add_keep_pattern('^\./\{arch\}/[^=].*')
 
138
 
 
139
        else:
 
140
            munge_opts = None
 
141
 
 
142
        if len(args) == 0:
 
143
            args.append(None)
 
144
        if munge_opts is not None or action == "skip-conflicts":
 
145
            revision_handler = ApplyHandler(tree, options.reverse, munge_opts)
 
146
        else:
 
147
            revision_handler = options.method(tree, options.reverse, munge_opts)
 
148
        if isinstance(revision_handler, ComposeHandler):
 
149
            if options.reverse:
 
150
                options.reverse_list = not options.reverse_list
 
151
 
 
152
        for arg in args:
 
153
            revisions = None
 
154
            if options.type == "default" and arg is not None:
 
155
                result = cmdutil.determine_version_revision_tree(arg, tree)
 
156
                if isinstance(result, arch.Revision):
 
157
                    revisions = [result]
 
158
            if revisions == None:
 
159
                revisions = cmdutil.revision_iterator(tree, action, [arg],
 
160
                                                      options.reverse_list, 
 
161
                                                      options.modified,
 
162
                                                      options.shallow)
 
163
            for revision in revisions:
 
164
                if isinstance(revision, arch.Patchlog):
 
165
                    revision = revision.revision
 
166
                cmdutil.ensure_archive_registered(revision.archive)
 
167
                status = revision_handler.handle_revision(revision)
 
168
                if status != 0:
 
169
                    return status
 
170
            if not revision_handler.finalize():
 
171
                cmdutil.chatter("Tree is already up to date")
 
172
        return 0
 
173
 
 
174
    def get_parser(self):
 
175
        parser = cmdutil.CmdOptionParser("fai revisions [revision]")
 
176
        select = cmdutil.OptionGroup(parser, "Selection options",
 
177
                          "Control which revisions are applied.  These options"
 
178
                          " are mutually exclusive.  If more than one is"
 
179
                          " specified, the last is used.")
 
180
        cmdutil.add_revision_iter_options(select)
 
181
        select.add_option("--skip-conflicts", action="store_const", 
 
182
                          const="skip-conflicts", dest="type",
 
183
                          help="Revisions that can be applied without"
 
184
                          "producing conflicts")
 
185
 
 
186
        parser.add_option_group(select)
 
187
        parser.add_option("-r", "--reverse", action="store_true", 
 
188
                          dest="reverse", help="Apply changeset(s) in reverse")
 
189
        parser.add_option("--reverse-list", action="store_true", 
 
190
                          dest="reverse_list", 
 
191
                          help="Apply revisions in reverse order")
 
192
        parser.add_option("--patch-logs-only", action="store_true", 
 
193
                          dest="logs_only", 
 
194
                          help="Apply log changes only")
 
195
        native = "read_arch_changeset" in dir(pylon)
 
196
        parser.set_defaults(method=ApplyHandler)
 
197
        if native:
 
198
            parser.add_option("--native", action="store_const", dest="method",
 
199
                              help="Use native changeset method", 
 
200
                              const=ApplyNativeHandler)
 
201
            parser.add_option("--combine", action="store_const", dest="method",
 
202
                              help="Combine changesets before applying",
 
203
                              const=ComposeHandler)
 
204
        return parser
 
205
    def help(self, parser=None):
 
206
        """
 
207
        Prints a help message.
 
208
 
 
209
        :param parser: If supplied, the parser to use for generating help.  If \
 
210
        not supplied, it is retrieved.
 
211
        :type parser: cmdutil.CmdOptionParser
 
212
        """
 
213
        if parser==None:
 
214
            parser=self.get_parser()
 
215
        parser.print_help()
 
216
 
 
217
 
 
218
def add_command(commands):
 
219
    commands["replay"] = Replay
 
220
 
 
221
# arch-tag: cd6b5159-30ee-410b-8ad4-7437b75f98cd