| 1 | # -*- mode: python; coding: utf-8 -*- |
|---|
| 2 | # :Progetto: vcpx -- Updatable VC working directory |
|---|
| 3 | # :Creato: mer 09 giu 2004 13:55:35 CEST |
|---|
| 4 | # :Autore: Lele Gaifax <lele@nautilus.homeip.net> |
|---|
| 5 | # :Licenza: GNU General Public License |
|---|
| 6 | # |
|---|
| 7 | |
|---|
| 8 | """ |
|---|
| 9 | Updatable sources are the simplest abstract wrappers around a working |
|---|
| 10 | directory under some kind of version control system. |
|---|
| 11 | """ |
|---|
| 12 | |
|---|
| 13 | __docformat__ = 'reStructuredText' |
|---|
| 14 | |
|---|
| 15 | from workdir import WorkingDir |
|---|
| 16 | |
|---|
| 17 | CONFLICTS_PROMPT = """ |
|---|
| 18 | The changeset |
|---|
| 19 | |
|---|
| 20 | %s |
|---|
| 21 | caused conflicts on the following files: |
|---|
| 22 | |
|---|
| 23 | * %s |
|---|
| 24 | |
|---|
| 25 | Either abort the session with Ctrl-C, or manually correct the situation |
|---|
| 26 | with a Ctrl-Z and a few "svn resolved". What would you like to do? |
|---|
| 27 | """ |
|---|
| 28 | |
|---|
| 29 | class GetUpstreamChangesetsFailure(Exception): |
|---|
| 30 | "Failure getting upstream changes" |
|---|
| 31 | |
|---|
| 32 | class ChangesetApplicationFailure(Exception): |
|---|
| 33 | "Failure applying upstream changes" |
|---|
| 34 | |
|---|
| 35 | class InvocationError(Exception): |
|---|
| 36 | "Bad invocation, use --help for details" |
|---|
| 37 | |
|---|
| 38 | class UpdatableSourceWorkingDir(WorkingDir): |
|---|
| 39 | """ |
|---|
| 40 | This is an abstract working dir able to follow an upstream |
|---|
| 41 | source of ``changesets``. |
|---|
| 42 | |
|---|
| 43 | It has three main functionalities: |
|---|
| 44 | |
|---|
| 45 | getPendingChangesets |
|---|
| 46 | to query the upstream server about new changesets |
|---|
| 47 | |
|---|
| 48 | applyPendingChangesets |
|---|
| 49 | to apply them to the working directory |
|---|
| 50 | |
|---|
| 51 | checkoutUpstreamRevision |
|---|
| 52 | to extract a new copy of the sources, actually initializing |
|---|
| 53 | the mechanism. |
|---|
| 54 | |
|---|
| 55 | Subclasses MUST override at least the _underscoredMethods. |
|---|
| 56 | """ |
|---|
| 57 | |
|---|
| 58 | def setStateFile(self, state_file): |
|---|
| 59 | """ |
|---|
| 60 | Set the state file used to store the revision and pending changesets. |
|---|
| 61 | """ |
|---|
| 62 | |
|---|
| 63 | self.state_file = state_file |
|---|
| 64 | |
|---|
| 65 | def applyPendingChangesets(self, applyable=None, replayable=None, |
|---|
| 66 | replay=None, applied=None): |
|---|
| 67 | """ |
|---|
| 68 | Apply the collected upstream changes. |
|---|
| 69 | |
|---|
| 70 | Loop over the collected changesets, doing whatever is needed |
|---|
| 71 | to apply each one to the working dir and if the changes do |
|---|
| 72 | not raise conflicts call the `replay` function to mirror the |
|---|
| 73 | changes on the target. |
|---|
| 74 | |
|---|
| 75 | Return a tuple of two elements: |
|---|
| 76 | |
|---|
| 77 | - the last applied changeset, if any |
|---|
| 78 | - the sequence (potentially empty!) of conflicts. |
|---|
| 79 | """ |
|---|
| 80 | |
|---|
| 81 | c = None |
|---|
| 82 | last = None |
|---|
| 83 | conflicts = [] |
|---|
| 84 | |
|---|
| 85 | if not self.pending: |
|---|
| 86 | return last, conflicts |
|---|
| 87 | |
|---|
| 88 | remaining = self.pending[:] |
|---|
| 89 | for c in self.pending: |
|---|
| 90 | if not self._willApplyChangeset(c, applyable): |
|---|
| 91 | break |
|---|
| 92 | |
|---|
| 93 | self.log_info("Applying changeset %s" % c.revision) |
|---|
| 94 | |
|---|
| 95 | try: |
|---|
| 96 | res = self._applyChangeset(c) |
|---|
| 97 | except: |
|---|
| 98 | self.log_error("Couldn't apply changeset %s" % c.revision, |
|---|
| 99 | exc=True) |
|---|
| 100 | raise |
|---|
| 101 | |
|---|
| 102 | if res: |
|---|
| 103 | conflicts.append((c, res)) |
|---|
| 104 | try: |
|---|
| 105 | raw_input(CONFLICTS_PROMPT % (str(c), '\n * '.join(res))) |
|---|
| 106 | except KeyboardInterrupt: |
|---|
| 107 | self.log_info("INTERRUPTED BY THE USER!") |
|---|
| 108 | return last, conflicts |
|---|
| 109 | |
|---|
| 110 | if not self._didApplyChangeset(c, replayable): |
|---|
| 111 | continue |
|---|
| 112 | |
|---|
| 113 | if replay: |
|---|
| 114 | replay(c) |
|---|
| 115 | |
|---|
| 116 | remaining.remove(c) |
|---|
| 117 | self.state_file.write(c.revision, remaining) |
|---|
| 118 | |
|---|
| 119 | if applied: |
|---|
| 120 | applied(c) |
|---|
| 121 | |
|---|
| 122 | last = c |
|---|
| 123 | |
|---|
| 124 | self.pending = remaining |
|---|
| 125 | return last, conflicts |
|---|
| 126 | |
|---|
| 127 | def _willApplyChangeset(self, changeset, applyable=None): |
|---|
| 128 | """ |
|---|
| 129 | This gets called just before applying each changeset. The whole |
|---|
| 130 | process will be stopped if this returns False. |
|---|
| 131 | |
|---|
| 132 | Subclasses may use this to stop the process on some conditions, |
|---|
| 133 | or to do whatever before application. |
|---|
| 134 | """ |
|---|
| 135 | |
|---|
| 136 | if applyable: |
|---|
| 137 | return applyable(changeset) |
|---|
| 138 | else: |
|---|
| 139 | return True |
|---|
| 140 | |
|---|
| 141 | def _didApplyChangeset(self, changeset, replayable=None): |
|---|
| 142 | """ |
|---|
| 143 | This gets called right after changeset application. The final |
|---|
| 144 | commit on the target system won't be carried out if this |
|---|
| 145 | returns False. |
|---|
| 146 | |
|---|
| 147 | Subclasses may use this to alter the changeset in any way, before |
|---|
| 148 | committing its changes to the target system. |
|---|
| 149 | """ |
|---|
| 150 | |
|---|
| 151 | if replayable: |
|---|
| 152 | return replayable(changeset) |
|---|
| 153 | else: |
|---|
| 154 | return True |
|---|
| 155 | |
|---|
| 156 | def getPendingChangesets(self): |
|---|
| 157 | """ |
|---|
| 158 | Load the pending changesets from the state file, or query the |
|---|
| 159 | upstream repository if there's none. |
|---|
| 160 | """ |
|---|
| 161 | |
|---|
| 162 | revision, self.pending = self.state_file.load() |
|---|
| 163 | if not self.pending: |
|---|
| 164 | self.pending = self._getUpstreamChangesets(revision) |
|---|
| 165 | return self.pending |
|---|
| 166 | |
|---|
| 167 | def _getUpstreamChangesets(self, sincerev): |
|---|
| 168 | """ |
|---|
| 169 | Query the upstream repository about what happened on the |
|---|
| 170 | sources since last sync, returning a sequence of Changesets |
|---|
| 171 | instances. |
|---|
| 172 | |
|---|
| 173 | This method must be overridden by subclasses. |
|---|
| 174 | """ |
|---|
| 175 | |
|---|
| 176 | raise "%s should override this method" % self.__class__ |
|---|
| 177 | |
|---|
| 178 | def _applyChangeset(self, changeset): |
|---|
| 179 | """ |
|---|
| 180 | Do the actual work of applying the changeset to the working copy. |
|---|
| 181 | |
|---|
| 182 | Subclasses should reimplement this method performing the |
|---|
| 183 | necessary steps to *merge* given `changeset`, returning a list |
|---|
| 184 | with the conflicts, if any. |
|---|
| 185 | """ |
|---|
| 186 | |
|---|
| 187 | raise "%s should override this method" % self.__class__ |
|---|
| 188 | |
|---|
| 189 | def checkoutUpstreamRevision(self, revision): |
|---|
| 190 | """ |
|---|
| 191 | Extract a working copy of the given revision from a repository. |
|---|
| 192 | |
|---|
| 193 | Return the last applied changeset. |
|---|
| 194 | """ |
|---|
| 195 | |
|---|
| 196 | last = self._checkoutUpstreamRevision(revision) |
|---|
| 197 | self.state_file.write(last.revision, None) |
|---|
| 198 | |
|---|
| 199 | return last |
|---|
| 200 | |
|---|
| 201 | def _checkoutUpstreamRevision(self, revision): |
|---|
| 202 | """ |
|---|
| 203 | Concretely do the checkout of the upstream revision. |
|---|
| 204 | """ |
|---|
| 205 | |
|---|
| 206 | raise "%s should override this method" % self.__class__ |
|---|