| 1 | # -*- mode: python; coding: utf-8 -*- |
|---|
| 2 | # :Progetto: vcpx -- bazaar-ng support |
|---|
| 3 | # :Creato: ven 20 mag 2005 08:15:02 CEST |
|---|
| 4 | # :Autore: Johan Rydberg <jrydberg@gnu.org> |
|---|
| 5 | # :Licenza: GNU General Public License |
|---|
| 6 | # |
|---|
| 7 | |
|---|
| 8 | """ |
|---|
| 9 | This module implements the backends for Bazaar-NG. |
|---|
| 10 | """ |
|---|
| 11 | |
|---|
| 12 | __docformat__ = 'reStructuredText' |
|---|
| 13 | |
|---|
| 14 | from target import SyncronizableTargetWorkingDir, TargetInitializationFailure |
|---|
| 15 | from source import ChangesetApplicationFailure |
|---|
| 16 | from shwrap import ExternalCommand |
|---|
| 17 | |
|---|
| 18 | class BzrWorkingDir(SyncronizableTargetWorkingDir): |
|---|
| 19 | |
|---|
| 20 | ## SyncronizableTargetWorkingDir |
|---|
| 21 | |
|---|
| 22 | def _addPathnames(self, names): |
|---|
| 23 | """ |
|---|
| 24 | Add some new filesystem objects. |
|---|
| 25 | """ |
|---|
| 26 | |
|---|
| 27 | cmd = [self.repository.BZR_CMD, "add"] |
|---|
| 28 | ExternalCommand(cwd=self.basedir, command=cmd).execute(names) |
|---|
| 29 | |
|---|
| 30 | def _commit(self, date, author, patchname, changelog=None, entries=None): |
|---|
| 31 | """ |
|---|
| 32 | Commit the changeset. |
|---|
| 33 | """ |
|---|
| 34 | |
|---|
| 35 | from sys import getdefaultencoding |
|---|
| 36 | |
|---|
| 37 | encoding = ExternalCommand.FORCE_ENCODING or getdefaultencoding() |
|---|
| 38 | |
|---|
| 39 | logmessage = [] |
|---|
| 40 | if patchname: |
|---|
| 41 | logmessage.append(patchname.encode(encoding)) |
|---|
| 42 | if changelog: |
|---|
| 43 | logmessage.append(changelog.replace('%', '%%').encode(encoding)) |
|---|
| 44 | logmessage.append('') |
|---|
| 45 | logmessage.append('Original author: %s' % author.encode(encoding)) |
|---|
| 46 | logmessage.append('Date: %s' % date) |
|---|
| 47 | logmessage.append('') |
|---|
| 48 | |
|---|
| 49 | cmd = [self.repository.BZR_CMD, "commit", "--unchanged", |
|---|
| 50 | "-m", '\n'.join(logmessage)] |
|---|
| 51 | if not entries: |
|---|
| 52 | entries = ['.'] |
|---|
| 53 | |
|---|
| 54 | c = ExternalCommand(cwd=self.basedir, command=cmd) |
|---|
| 55 | c.execute(entries) |
|---|
| 56 | |
|---|
| 57 | if c.exit_status: |
|---|
| 58 | raise ChangesetApplicationFailure("%s returned status %d" % |
|---|
| 59 | (str(c), c.exit_status)) |
|---|
| 60 | |
|---|
| 61 | def _removePathnames(self, names): |
|---|
| 62 | """ |
|---|
| 63 | Remove some filesystem objects. |
|---|
| 64 | """ |
|---|
| 65 | |
|---|
| 66 | cmd = [self.repository.BZR_CMD, "remove"] |
|---|
| 67 | ExternalCommand(cwd=self.basedir, command=cmd).execute(names) |
|---|
| 68 | |
|---|
| 69 | def _renamePathname(self, oldname, newname): |
|---|
| 70 | """ |
|---|
| 71 | Rename a filesystem object to some other name/location. |
|---|
| 72 | """ |
|---|
| 73 | |
|---|
| 74 | cmd = [self.repository.BZR_CMD, "rename"] |
|---|
| 75 | ExternalCommand(cwd=self.basedir, command=cmd).execute(oldname, newname) |
|---|
| 76 | |
|---|
| 77 | def _prepareTargetRepository(self, source_repo): |
|---|
| 78 | """ |
|---|
| 79 | Execute ``bzr init``. |
|---|
| 80 | """ |
|---|
| 81 | |
|---|
| 82 | from os.path import join, exists |
|---|
| 83 | from os import makedirs |
|---|
| 84 | from os.path import join, exists |
|---|
| 85 | |
|---|
| 86 | if not exists(self.basedir): |
|---|
| 87 | makedirs(self.basedir) |
|---|
| 88 | elif exists(join(self.basedir, self.repository.METADIR)): |
|---|
| 89 | return |
|---|
| 90 | |
|---|
| 91 | cmd = [self.repository.BZR_CMD, "init"] |
|---|
| 92 | init = ExternalCommand(cwd=self.basedir, command=cmd) |
|---|
| 93 | init.execute() |
|---|
| 94 | |
|---|
| 95 | if init.exit_status: |
|---|
| 96 | raise TargetInitializationFailure( |
|---|
| 97 | "%s returned status %s" % (str(init), init.exit_status)) |
|---|
| 98 | |
|---|
| 99 | def _prepareWorkingDirectory(self, source_repo): |
|---|
| 100 | """ |
|---|
| 101 | Create the .bzrignore. |
|---|
| 102 | """ |
|---|
| 103 | |
|---|
| 104 | from os.path import join |
|---|
| 105 | from dualwd import IGNORED_METADIRS |
|---|
| 106 | |
|---|
| 107 | # Create the .bzrignore file, that contains a glob per line, |
|---|
| 108 | # with all known VCs metadirs to be skipped. |
|---|
| 109 | ignore = open(join(self.basedir, '.bzrignore'), 'w') |
|---|
| 110 | ignore.write('\n'.join(['(^|/)%s($|/)' % md |
|---|
| 111 | for md in IGNORED_METADIRS])) |
|---|
| 112 | ignore.write('\n') |
|---|
| 113 | if self.logfile.startswith(self.basedir): |
|---|
| 114 | ignore.write('^') |
|---|
| 115 | ignore.write(self.logfile[len(self.basedir)+1:]) |
|---|
| 116 | ignore.write('$\n') |
|---|
| 117 | if self.state_file.filename.startswith(self.basedir): |
|---|
| 118 | ignore.write('^') |
|---|
| 119 | ignore.write(self.state_file.filename[len(self.basedir)+1:]) |
|---|
| 120 | ignore.write('\n') |
|---|
| 121 | ignore.close() |
|---|