| 1 | # -*- mode: python; coding: utf-8 -*- |
|---|
| 2 | # :Progetto: vcpx -- Mercurial stuff |
|---|
| 3 | # :Creato: ven 24 giu 2005 20:42:46 CEST |
|---|
| 4 | # :Autore: Lele Gaifax <lele@nautilus.homeip.net> |
|---|
| 5 | # :Licenza: GNU General Public License |
|---|
| 6 | # |
|---|
| 7 | |
|---|
| 8 | """ |
|---|
| 9 | This module implements the backends for Mercurial. |
|---|
| 10 | """ |
|---|
| 11 | |
|---|
| 12 | __docformat__ = 'reStructuredText' |
|---|
| 13 | |
|---|
| 14 | from shwrap import ExternalCommand, PIPE, ReopenableNamedTemporaryFile |
|---|
| 15 | from target import SyncronizableTargetWorkingDir, TargetInitializationFailure |
|---|
| 16 | |
|---|
| 17 | HG_CMD = "hg" |
|---|
| 18 | |
|---|
| 19 | class HgWorkingDir(SyncronizableTargetWorkingDir): |
|---|
| 20 | |
|---|
| 21 | ## SyncronizableTargetWorkingDir |
|---|
| 22 | |
|---|
| 23 | def _addPathnames(self, root, names): |
|---|
| 24 | """ |
|---|
| 25 | Add some new filesystem objects. |
|---|
| 26 | """ |
|---|
| 27 | |
|---|
| 28 | from os.path import join, isdir |
|---|
| 29 | |
|---|
| 30 | # Currently hg does not handle directories at all, so filter |
|---|
| 31 | # them out. |
|---|
| 32 | |
|---|
| 33 | notdirs = [n for n in names if not isdir(join(root, n))] |
|---|
| 34 | if notdirs: |
|---|
| 35 | cmd = [HG_CMD, "add"] |
|---|
| 36 | ExternalCommand(cwd=root, command=cmd).execute(notdirs) |
|---|
| 37 | |
|---|
| 38 | def _commit(self,root, date, author, remark, changelog=None, entries=None): |
|---|
| 39 | """ |
|---|
| 40 | Commit the changeset. |
|---|
| 41 | """ |
|---|
| 42 | |
|---|
| 43 | from time import mktime |
|---|
| 44 | from sys import getdefaultencoding |
|---|
| 45 | |
|---|
| 46 | encoding = ExternalCommand.FORCE_ENCODING or getdefaultencoding() |
|---|
| 47 | |
|---|
| 48 | logmessage = [] |
|---|
| 49 | if remark: |
|---|
| 50 | logmessage.append(remark.encode(encoding)) |
|---|
| 51 | if changelog: |
|---|
| 52 | logmessage.append('') |
|---|
| 53 | logmessage.append(changelog.encode(encoding)) |
|---|
| 54 | logmessage.append('') |
|---|
| 55 | |
|---|
| 56 | cmd = [HG_CMD, "commit", "-u", author, "-l", "%(logfile)s", |
|---|
| 57 | "-d", "%(time)s UTC"] |
|---|
| 58 | c = ExternalCommand(cwd=root, command=cmd) |
|---|
| 59 | |
|---|
| 60 | rontf = ReopenableNamedTemporaryFile('hg', 'tailor') |
|---|
| 61 | log = open(rontf.name, "w") |
|---|
| 62 | log.write('\n'.join(logmessage)) |
|---|
| 63 | log.close() |
|---|
| 64 | |
|---|
| 65 | c.execute(logfile=rontf.name, time=mktime(date.timetuple())) |
|---|
| 66 | |
|---|
| 67 | def _removePathnames(self, root, names): |
|---|
| 68 | """ |
|---|
| 69 | Remove some filesystem object. |
|---|
| 70 | """ |
|---|
| 71 | |
|---|
| 72 | from os.path import join, isdir |
|---|
| 73 | |
|---|
| 74 | # Currently hg does not handle directories at all, so filter |
|---|
| 75 | # them out. |
|---|
| 76 | |
|---|
| 77 | notdirs = [n for n in names if not isdir(join(root, n))] |
|---|
| 78 | if notdirs: |
|---|
| 79 | cmd = [HG_CMD, "remove"] |
|---|
| 80 | ExternalCommand(cwd=root, command=cmd).execute(notdirs) |
|---|
| 81 | |
|---|
| 82 | def _renamePathname(self, root, oldname, newname): |
|---|
| 83 | """ |
|---|
| 84 | Rename a filesystem object. |
|---|
| 85 | """ |
|---|
| 86 | |
|---|
| 87 | from os.path import join, isdir |
|---|
| 88 | from os import walk |
|---|
| 89 | from dualwd import IGNORED_METADIRS |
|---|
| 90 | |
|---|
| 91 | cmd = [HG_CMD, "copy"] |
|---|
| 92 | copy = ExternalCommand(cwd=root, command=cmd) |
|---|
| 93 | if isdir(join(root, newname)): |
|---|
| 94 | # Given lack of support for directories in current HG, |
|---|
| 95 | # loop over all files under the new directory and |
|---|
| 96 | # do a copy on them. |
|---|
| 97 | skip = len(root)+len(newname)+2 |
|---|
| 98 | for dir, subdirs, files in walk(join(root, newname)): |
|---|
| 99 | prefix = dir[skip:] |
|---|
| 100 | |
|---|
| 101 | for excd in IGNORED_METADIRS: |
|---|
| 102 | if excd in subdirs: |
|---|
| 103 | subdirs.remove(excd) |
|---|
| 104 | |
|---|
| 105 | for f in files: |
|---|
| 106 | copy.execute(join(oldname, prefix, f), |
|---|
| 107 | join(newname, prefix, f)) |
|---|
| 108 | else: |
|---|
| 109 | copy.execute(oldname, newname) |
|---|
| 110 | |
|---|
| 111 | def _initializeWorkingDir(self, root, repository, module, subdir): |
|---|
| 112 | """ |
|---|
| 113 | Execute ``hg init``. |
|---|
| 114 | """ |
|---|
| 115 | |
|---|
| 116 | from os import getenv |
|---|
| 117 | from os.path import join |
|---|
| 118 | from re import escape |
|---|
| 119 | from dualwd import IGNORED_METADIRS |
|---|
| 120 | |
|---|
| 121 | init = ExternalCommand(cwd=root, command=[HG_CMD, "init"]) |
|---|
| 122 | init.execute() |
|---|
| 123 | |
|---|
| 124 | if init.exit_status: |
|---|
| 125 | raise TargetInitializationFailure( |
|---|
| 126 | "%s returned status %s" % (str(init), init.exit_status)) |
|---|
| 127 | |
|---|
| 128 | # Create the .hgignore file, that contains a regexp per line |
|---|
| 129 | # with all known VCs metadirs to be skipped. |
|---|
| 130 | ignore = open(join(root, '.hgignore'), 'w') |
|---|
| 131 | ignore.write('\n'.join(['(^|/)%s($|/)' % escape(md) |
|---|
| 132 | for md in IGNORED_METADIRS])) |
|---|
| 133 | ignore.write('\n^tailor.log$\n^tailor.info$\n') |
|---|
| 134 | ignore.close() |
|---|
| 135 | |
|---|
| 136 | ExternalCommand(cwd=root, command=[HG_CMD, "addremove"]).execute() |
|---|