| 1 | # -*- mode: python; coding: utf-8 -*- |
|---|
| 2 | # :Progetto: vcpx -- Monotone details |
|---|
| 3 | # :Creato: Tue Apr 12 01:28:10 CEST 2005 |
|---|
| 4 | # :Autore: Markus Schiltknecht <markus@bluegap.ch> |
|---|
| 5 | # :Licenza: GNU General Public License |
|---|
| 6 | # |
|---|
| 7 | |
|---|
| 8 | """ |
|---|
| 9 | This module contains supporting classes for Monotone. |
|---|
| 10 | """ |
|---|
| 11 | |
|---|
| 12 | __docformat__ = 'reStructuredText' |
|---|
| 13 | |
|---|
| 14 | from shwrap import ExternalCommand, PIPE, ReopenableNamedTemporaryFile |
|---|
| 15 | from source import UpdatableSourceWorkingDir, \ |
|---|
| 16 | ChangesetApplicationFailure, GetUpstreamChangesetsFailure |
|---|
| 17 | from target import SyncronizableTargetWorkingDir, TargetInitializationFailure |
|---|
| 18 | from sys import stderr |
|---|
| 19 | |
|---|
| 20 | class MonotoneWorkingDir(SyncronizableTargetWorkingDir): |
|---|
| 21 | |
|---|
| 22 | ## SyncronizableTargetWorkingDir |
|---|
| 23 | |
|---|
| 24 | def _addPathnames(self, names): |
|---|
| 25 | """ |
|---|
| 26 | Add some new filesystem objects. |
|---|
| 27 | """ |
|---|
| 28 | |
|---|
| 29 | cmd = [self.repository.MONOTONE_CMD, "add"] |
|---|
| 30 | add = ExternalCommand(cwd=self.basedir, command=cmd) |
|---|
| 31 | add.execute(names) |
|---|
| 32 | |
|---|
| 33 | def _commit(self, date, author, patchname, changelog=None, entries=None): |
|---|
| 34 | """ |
|---|
| 35 | Commit the changeset. |
|---|
| 36 | """ |
|---|
| 37 | |
|---|
| 38 | from sys import getdefaultencoding |
|---|
| 39 | |
|---|
| 40 | encoding = ExternalCommand.FORCE_ENCODING or getdefaultencoding() |
|---|
| 41 | |
|---|
| 42 | logmessage = [] |
|---|
| 43 | if patchname: |
|---|
| 44 | logmessage.append(patchname.encode(encoding)) |
|---|
| 45 | if changelog: |
|---|
| 46 | logmessage.append('') |
|---|
| 47 | logmessage.append(changelog.encode(encoding)) |
|---|
| 48 | logmessage.append('') |
|---|
| 49 | |
|---|
| 50 | rontf = ReopenableNamedTemporaryFile('mtn', 'tailor') |
|---|
| 51 | log = open(rontf.name, "w") |
|---|
| 52 | log.write('\n'.join(logmessage)) |
|---|
| 53 | log.close() |
|---|
| 54 | |
|---|
| 55 | cmd = [self.repository.MONOTONE_CMD, "commit", "--author", author, |
|---|
| 56 | "--date", date.isoformat(), |
|---|
| 57 | "--message-file", rontf.name] |
|---|
| 58 | commit = ExternalCommit(cwd=self.basedir, command=cmd) |
|---|
| 59 | |
|---|
| 60 | if not entries: |
|---|
| 61 | entries = ['.'] |
|---|
| 62 | |
|---|
| 63 | output = commit.execute(entries, stdout=PIPE) |
|---|
| 64 | |
|---|
| 65 | # monotone complaints if there are no changes from the last commit. |
|---|
| 66 | # we ignore those errors ... |
|---|
| 67 | if commit.exit_status: |
|---|
| 68 | text = output.read() |
|---|
| 69 | if text.find("monotone: misuse: no changes to commit") == -1: |
|---|
| 70 | stderr.write(text) |
|---|
| 71 | raise TargetInitializationFailure( |
|---|
| 72 | "%s returned status %s" % (str(commit),commit.exit_status)) |
|---|
| 73 | else: |
|---|
| 74 | stderr.write("No changes to commit - changeset ignored\n") |
|---|
| 75 | |
|---|
| 76 | def _removePathnames(self, names): |
|---|
| 77 | """ |
|---|
| 78 | Remove some filesystem object. |
|---|
| 79 | """ |
|---|
| 80 | |
|---|
| 81 | cmd = [self.repository.MONOTONE_CMD, "drop"] |
|---|
| 82 | drop = ExternalCommand(cwd=self.basedir, command=cmd) |
|---|
| 83 | drop.execute(names) |
|---|
| 84 | |
|---|
| 85 | def _renamePathname(self, oldname, newname): |
|---|
| 86 | """ |
|---|
| 87 | Rename a filesystem object. |
|---|
| 88 | """ |
|---|
| 89 | |
|---|
| 90 | cmd = [self.repository.MONOTONE_CMD, "rename"] |
|---|
| 91 | rename = ExternalCommand(cwd=self.basedir, command=cmd) |
|---|
| 92 | rename.execute(oldname, newname) |
|---|
| 93 | |
|---|
| 94 | def _initializeWorkingDir(self): |
|---|
| 95 | """ |
|---|
| 96 | Setup the monotone working copy |
|---|
| 97 | |
|---|
| 98 | The user must setup a monotone working directory himself. Then |
|---|
| 99 | we simply use 'monotone commit', without having to specify a database |
|---|
| 100 | file or branch. Monotone looks up the database and branch in it's MT |
|---|
| 101 | directory. |
|---|
| 102 | """ |
|---|
| 103 | |
|---|
| 104 | from os.path import exists, join |
|---|
| 105 | |
|---|
| 106 | if not exists(join(self.basedir, 'MT')): |
|---|
| 107 | raise TargetInitializationFailure("Please setup '%s' as a monotone working directory" % self.basedir) |
|---|
| 108 | |
|---|
| 109 | self._addPathnames([self.repository.subdir]) |
|---|