source: tailor/vcpx/bzr.py @ 850

Revision 850, 4.1 KB checked in by lele@…, 8 years ago (diff)

Use getpreferredencoding() instead of getdefaultencoding()
Accordingly to documentation, the former tells the encoding selected
by the user in his environment LANG variable, while the latter is what
Python uses internally to map non ascii strings to unicode.

Line 
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"""
9This module implements the backends for Bazaar-NG.
10"""
11
12__docformat__ = 'reStructuredText'
13
14from target import SyncronizableTargetWorkingDir, TargetInitializationFailure
15from source import ChangesetApplicationFailure
16from shwrap import ExternalCommand, ReopenableNamedTemporaryFile
17
18class BzrWorkingDir(SyncronizableTargetWorkingDir):
19
20    ## SyncronizableTargetWorkingDir
21
22    def _addPathnames(self, names):
23        """
24        Add some new filesystem objects.
25        """
26
27        cmd = self.repository.command("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 locale import getpreferredencoding
36
37        encoding = ExternalCommand.FORCE_ENCODING or getpreferredencoding()
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
48        rontf = ReopenableNamedTemporaryFile('bzr', 'tailor')
49        log = open(rontf.name, "w")
50        log.write('\n'.join(logmessage))
51        log.close()
52
53        cmd = self.repository.command("commit", "--unchanged",
54                                      "--file", rontf.name)
55        if not entries:
56            entries = ['.']
57
58        c = ExternalCommand(cwd=self.basedir, command=cmd)
59        c.execute(entries)
60
61        if c.exit_status:
62            raise ChangesetApplicationFailure("%s returned status %d" %
63                                              (str(c), c.exit_status))
64
65    def _removePathnames(self, names):
66        """
67        Remove some filesystem objects.
68        """
69
70        cmd = self.repository.command("remove")
71        ExternalCommand(cwd=self.basedir, command=cmd).execute(names)
72
73    def _renamePathname(self, oldname, newname):
74        """
75        Rename a filesystem object to some other name/location.
76        """
77
78        cmd = self.repository.command("rename")
79        ExternalCommand(cwd=self.basedir, command=cmd).execute(oldname, newname)
80
81    def _prepareTargetRepository(self):
82        """
83        Create the base directory if it doesn't exist, and the
84        repository as well in the new working directory, executing
85        ``bzr init``.
86        """
87
88        from os.path import join, exists
89
90        if not exists(join(self.basedir, self.repository.METADIR)):
91            cmd = self.repository.command("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            sfrelname = self.state_file.filename[len(self.basedir)+1:]
119            ignore.write('^')
120            ignore.write(sfrelname)
121            ignore.write('\n')
122            ignore.write('^')
123            ignore.write(sfrelname+'.journal')
124            ignore.write('\n')
125        ignore.close()
Note: See TracBrowser for help on using the repository browser.