| 1 | #! /usr/bin/python |
|---|
| 2 | # -*- mode: python; coding: utf-8 -*- |
|---|
| 3 | # :Progetto: vcpx -- Subversion details |
|---|
| 4 | # :Creato: ven 18 giu 2004 15:00:52 CEST |
|---|
| 5 | # :Autore: Lele Gaifax <lele@nautilus.homeip.net> |
|---|
| 6 | # |
|---|
| 7 | |
|---|
| 8 | __docformat__ = 'reStructuredText' |
|---|
| 9 | |
|---|
| 10 | from cvsync.shwrap import SystemCommand |
|---|
| 11 | from source import UpdatableSourceWorkingDir |
|---|
| 12 | from target import SyncronizableTargetWorkingDir |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | class SvnUpdate(SystemCommand): |
|---|
| 16 | COMMAND = "svn update --revision %(revision)s %(entry)s" |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | class SvnInfo(SystemCommand): |
|---|
| 20 | COMMAND = "svn info %(entry)s" |
|---|
| 21 | |
|---|
| 22 | def __call__(self, output=None, dry_run=False, **kwargs): |
|---|
| 23 | output = SystemCommand.__call__(self, output=True, |
|---|
| 24 | dry_run=dry_run, |
|---|
| 25 | entry=entry) |
|---|
| 26 | res = {} |
|---|
| 27 | for l in output: |
|---|
| 28 | l = l[:-1] |
|---|
| 29 | if l: |
|---|
| 30 | key, value = l.split(':', 1) |
|---|
| 31 | res[key] = value[1:] |
|---|
| 32 | return res |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | class SvnPropGet(SystemCommand): |
|---|
| 36 | COMMAND = "svn propget %(property)s %(entry)s" |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | class SvnPropSet(SystemCommand): |
|---|
| 40 | COMMAND = "svn propset --quiet %(property)s %(value)s %(entry)s" |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | class SvnLog(SystemCommand): |
|---|
| 44 | COMMAND = "svn log %(quiet)s %(xml)s --revision %(startrev)s:%(endrev)s %(entry)s" |
|---|
| 45 | |
|---|
| 46 | def __call__(self, output=None, dry_run=False, **kwargs): |
|---|
| 47 | quiet = kwargs.get('quiet', True) |
|---|
| 48 | if quiet == True: |
|---|
| 49 | kwargs['quiet'] = '--quiet' |
|---|
| 50 | elif quiet == False: |
|---|
| 51 | kwargs['quiet'] = '' |
|---|
| 52 | |
|---|
| 53 | xml = kwargs.get('xml', False) |
|---|
| 54 | if xml: |
|---|
| 55 | kwargs['xml'] = '--xml' |
|---|
| 56 | output = True |
|---|
| 57 | else: |
|---|
| 58 | kwargs['xml'] = '' |
|---|
| 59 | |
|---|
| 60 | startrev = kwargs.get('startrev') |
|---|
| 61 | if not startrev: |
|---|
| 62 | kwargs['startrev'] = 'BASE' |
|---|
| 63 | |
|---|
| 64 | endrev = kwargs.get('endrev') |
|---|
| 65 | if not endrev: |
|---|
| 66 | kwargs['endrev'] = 'HEAD' |
|---|
| 67 | |
|---|
| 68 | output = SystemCommand.__call__(self, output=output, |
|---|
| 69 | dry_run=dry_run, **kwargs) |
|---|
| 70 | |
|---|
| 71 | if xml: |
|---|
| 72 | # parse the output and return the result |
|---|
| 73 | pass |
|---|
| 74 | |
|---|
| 75 | return output |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | class SvnCheckout(SystemCommand): |
|---|
| 79 | COMMAND = "svn co --quiet --revision %(revision)s %(repository)s %(wc)s" |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | class SvnCommit(SystemCommand): |
|---|
| 83 | COMMAND = "svn commit --quiet %(logfile)s %(entries)s" |
|---|
| 84 | |
|---|
| 85 | def __call__(self, output=None, dry_run=False, **kwargs): |
|---|
| 86 | logfile = kwargs.get('logfile') |
|---|
| 87 | if not logfile: |
|---|
| 88 | from tempfile import NamedTemporaryFile |
|---|
| 89 | |
|---|
| 90 | log = NamedTemporaryFile(bufsize=0) |
|---|
| 91 | logmessage = kwargs.get('logmessage') |
|---|
| 92 | if logmessage: |
|---|
| 93 | print >>log, logmessage |
|---|
| 94 | |
|---|
| 95 | kwargs['logfile'] = log.name |
|---|
| 96 | |
|---|
| 97 | return SystemCommand.__call__(self, output=output, |
|---|
| 98 | dry_run=dry_run, **kwargs) |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | class SvnAdd(SystemCommand): |
|---|
| 102 | COMMAND = "svn add --quiet --non-recursive --no-auto-props %(entry)s" |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | class SvnRemove(SystemCommand): |
|---|
| 106 | COMMAND = "svn remove --quiet --force %(entry)s" |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | class SvnMv(SystemCommand): |
|---|
| 110 | COMMAND = "svn mv --quiet %(old)s %(new)s" |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | class SvnWorkingDir(UpdatableSourceWorkingDir, SyncronizableTargetWorkingDir): |
|---|
| 114 | |
|---|
| 115 | ## UpdatableSourceWorkingDir |
|---|
| 116 | |
|---|
| 117 | def _getUpstreamChangesets(self, root, startfrom_rev=None): |
|---|
| 118 | svnlog = SvnLog(working_dir=root) |
|---|
| 119 | log = svnlog(quiet='--verbose', output=True, xml=True, entry='.') |
|---|
| 120 | |
|---|
| 121 | self.__parseSvnLog(log) |
|---|
| 122 | |
|---|
| 123 | def __parseSvnLog(self, log): |
|---|
| 124 | """Return an object representation of the ``svn log`` thru HEAD.""" |
|---|
| 125 | |
|---|
| 126 | from xml.sax import parseString |
|---|
| 127 | from xml.sax.handler import ContentHandler |
|---|
| 128 | |
|---|
| 129 | class SvnXMLLogHandler(ContentHandler): |
|---|
| 130 | def __init__(self, changesets): |
|---|
| 131 | self.changesets = changesets |
|---|
| 132 | self.current = None |
|---|
| 133 | self.current_field = [] |
|---|
| 134 | |
|---|
| 135 | def startElement(self, name, attributes): |
|---|
| 136 | if name == 'logentry': |
|---|
| 137 | self.current = {} |
|---|
| 138 | self.current['revision'] = attributes['revision'] |
|---|
| 139 | self.current['entries'] = [] |
|---|
| 140 | elif name in ['author', 'date', 'msg']: |
|---|
| 141 | self.current_field = [] |
|---|
| 142 | elif name == 'path': |
|---|
| 143 | self.current_field = [] |
|---|
| 144 | if attributes.has_key('copyfrom-path'): |
|---|
| 145 | self.current_path_action = ( |
|---|
| 146 | attributes['action'], |
|---|
| 147 | attributes['copyfrom-path'][1:], # make it relative |
|---|
| 148 | attributes['copyfrom-rev']) |
|---|
| 149 | else: |
|---|
| 150 | self.current_path_action = attributes['action'] |
|---|
| 151 | |
|---|
| 152 | def endElement(self, name): |
|---|
| 153 | if name == 'logentry': |
|---|
| 154 | # Sort the paths to make tests easier |
|---|
| 155 | self.current['entries'].sort() |
|---|
| 156 | self.changesets.append(ChangeSet(**self.current)) |
|---|
| 157 | self.current = None |
|---|
| 158 | elif name in ['author', 'date', 'msg']: |
|---|
| 159 | self.current[name] = ''.join(self.current_field)) |
|---|
| 160 | elif name == 'path': |
|---|
| 161 | entry = ChangesetEntry(''.join(self.current_field)[1:]) |
|---|
| 162 | if type(self.current_path_action) == type(''): |
|---|
| 163 | entry.action_kind = self.current_path_action |
|---|
| 164 | else: |
|---|
| 165 | entry.action_kind = entry.RENAMED |
|---|
| 166 | entry.old_name = self.current_path_action[1] |
|---|
| 167 | |
|---|
| 168 | self.current['entries'].append(entry) |
|---|
| 169 | |
|---|
| 170 | def characters(self, data): |
|---|
| 171 | self.current_field.append(data) |
|---|
| 172 | |
|---|
| 173 | handler = SvnXMLLogHandler(self.changesets) |
|---|
| 174 | parseString(log.getvalue(), handler) |
|---|
| 175 | |
|---|
| 176 | def _applyChangeset(self, root, changeset): |
|---|
| 177 | svnup = SvnUpdate(working_dir=root) |
|---|
| 178 | svnup(entry='.', revision=changeset.revision) |
|---|
| 179 | |
|---|
| 180 | ## SyncronizableTargetWorkingDir |
|---|
| 181 | |
|---|
| 182 | def _addEntry(self, root, entry): |
|---|
| 183 | """ |
|---|
| 184 | Add a new entry, maybe registering the directory as well. |
|---|
| 185 | """ |
|---|
| 186 | |
|---|
| 187 | from os.path import split, join, exists |
|---|
| 188 | |
|---|
| 189 | basedir = split(entry)[0] |
|---|
| 190 | if basedir and not exists(join(basedir, '.svn')): |
|---|
| 191 | self._addEntry(root, basedir) |
|---|
| 192 | |
|---|
| 193 | c = SvnAdd(working_dir=root) |
|---|
| 194 | c(entry=entry) |
|---|
| 195 | |
|---|
| 196 | def _commit(self, root, author, remark, changelog=None, entries=None): |
|---|
| 197 | """ |
|---|
| 198 | Commit the changeset. |
|---|
| 199 | """ |
|---|
| 200 | |
|---|
| 201 | c = SvnCommit(working_dir=root) |
|---|
| 202 | |
|---|
| 203 | logmessage = remark + '\n' |
|---|
| 204 | if changelog: |
|---|
| 205 | logmessage = logmessage + changelog + '\n' |
|---|
| 206 | |
|---|
| 207 | if entries: |
|---|
| 208 | entries = ' '.join(entries) |
|---|
| 209 | else: |
|---|
| 210 | entries = '.' |
|---|
| 211 | |
|---|
| 212 | c(logmessage=logmessage, entries=entries) |
|---|
| 213 | |
|---|
| 214 | def _removeEntry(self, root, entry): |
|---|
| 215 | """ |
|---|
| 216 | Remove an entry. |
|---|
| 217 | """ |
|---|
| 218 | |
|---|
| 219 | c = SvnRemove(working_dir=root) |
|---|
| 220 | c(entry=entry) |
|---|
| 221 | |
|---|
| 222 | def _renameEntry(self, root, oldentry, newentry): |
|---|
| 223 | """ |
|---|
| 224 | Rename an entry. |
|---|
| 225 | """ |
|---|
| 226 | |
|---|
| 227 | c = SvnMv(working_dir=root) |
|---|
| 228 | c(old=oldentry, new=newentry) |
|---|
| 229 | |
|---|