source: tailor/vcpx/hg.py @ 527

Revision 527, 4.4 KB checked in by lele@…, 8 years ago (diff)

Big API change, reducing arguments in favour of instance attributes
This is a big and subtle change that brings nothing in term of
functionality but make it a lot easier maintaining and extending
tailor as a whole.

Basically, 'root' and 'subdir' arguments are gone replaced by a
self.basedir, computed from the configuration; instead of 'logger',
the code uses two new methods, log_info() and log_error() on most
objects. Other arguments are derived from the configuration objects
that hang around.

Line 
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"""
9This module implements the backends for Mercurial.
10"""
11
12__docformat__ = 'reStructuredText'
13
14from shwrap import ExternalCommand, PIPE, ReopenableNamedTemporaryFile
15from target import SyncronizableTargetWorkingDir, TargetInitializationFailure
16
17class HgWorkingDir(SyncronizableTargetWorkingDir):
18
19    ## SyncronizableTargetWorkingDir
20
21    def _addPathnames(self, names):
22        """
23        Add some new filesystem objects.
24        """
25
26        from os.path import join, isdir
27
28        # Currently hg does not handle directories at all, so filter
29        # them out.
30
31        notdirs = [n for n in names if not isdir(join(self.basedir, n))]
32        if notdirs:
33            cmd = [self.repository.HG_CMD, "add"]
34            ExternalCommand(cwd=self.basedir, command=cmd).execute(notdirs)
35
36    def _commit(self, date, author, patchname, changelog=None, entries=None):
37        """
38        Commit the changeset.
39        """
40
41        from time import mktime
42        from sys import getdefaultencoding
43
44        encoding = ExternalCommand.FORCE_ENCODING or getdefaultencoding()
45
46        logmessage = []
47        if patchname:
48            logmessage.append(patchname.encode(encoding))
49        if changelog:
50            logmessage.append('')
51            logmessage.append(changelog.encode(encoding))
52        logmessage.append('')
53
54        cmd = [self.repository.HG_CMD, "commit", "-u", author,
55               "-l", "%(logfile)s",
56               "-d", "%(time)s UTC"]
57        c = ExternalCommand(cwd=self.basedir, command=cmd)
58
59        rontf = ReopenableNamedTemporaryFile('hg', 'tailor')
60        log = open(rontf.name, "w")
61        log.write('\n'.join(logmessage))
62        log.close()
63
64        c.execute(logfile=rontf.name, time=mktime(date.timetuple()))
65
66    def _removePathnames(self, names):
67        """
68        Remove some filesystem object.
69        """
70
71        from os.path import join, isdir
72
73        # Currently hg does not handle directories at all, so filter
74        # them out.
75
76        notdirs = [n for n in names if not isdir(join(self.basedir, n))]
77        if notdirs:
78            cmd = [self.repository.HG_CMD, "remove"]
79            ExternalCommand(cwd=self.basedir, command=cmd).execute(notdirs)
80
81    def _renamePathname(self, oldname, newname):
82        """
83        Rename a filesystem object.
84        """
85
86        from os.path import join, isdir
87        from os import walk
88        from dualwd import IGNORED_METADIRS
89
90        cmd = [self.repository.HG_CMD, "copy"]
91        copy = ExternalCommand(cwd=self.basedir, command=cmd)
92        if isdir(join(self.basedir, newname)):
93            # Given lack of support for directories in current HG,
94            # loop over all files under the new directory and
95            # do a copy on them.
96            skip = len(self.basedir)+len(newname)+2
97            for dir, subdirs, files in walk(join(self.basedir, newname)):
98                prefix = dir[skip:]
99
100                for excd in IGNORED_METADIRS:
101                    if excd in subdirs:
102                        subdirs.remove(excd)
103
104                for f in files:
105                    copy.execute(join(oldname, prefix, f),
106                                 join(newname, prefix, f))
107        else:
108            copy.execute(oldname, newname)
109
110    def _initializeWorkingDir(self):
111        """
112        Execute ``hg init``.
113        """
114
115        from os import getenv
116        from os.path import join
117        from re import escape
118        from dualwd import IGNORED_METADIRS
119
120        init = ExternalCommand(cwd=self.basedir,
121                               command=[self.repository.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(self.basedir, '.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=self.basedir,
137                        command=[self.repository.HG_CMD, "addremove"]).execute()
Note: See TracBrowser for help on using the repository browser.