source: tailor/vcpx/dualwd.py @ 754

Revision 754, 3.8 KB checked in by lele@…, 8 years ago (diff)

Better public names for the methods

Line 
1# -*- mode: python; coding: utf-8 -*-
2# :Progetto: vcpx -- Dual working directory
3# :Creato:   dom 20 giu 2004 11:02:01 CEST
4# :Autore:   Lele Gaifax <lele@nautilus.homeip.net>
5# :Licenza:  GNU General Public License
6#
7
8"""
9The easiest way to propagate changes from one VC control system to one
10of an another kind is having a single directory containing a live
11working copy shared between the two VC systems.
12
13In a slightly more elaborated way, the source and the target system may
14use separate directories, that gets rsynced when needed.
15
16This module implements `DualWorkingDir`, which instances have a
17`source` and `target` properties offering the right capabilities to do
18the job.
19"""
20
21__docformat__ = 'reStructuredText'
22
23from source import UpdatableSourceWorkingDir, InvocationError
24from target import SyncronizableTargetWorkingDir
25from shwrap import ExternalCommand
26
27IGNORED_METADIRS = []
28
29class DualWorkingDir(UpdatableSourceWorkingDir, SyncronizableTargetWorkingDir):
30    """
31    Dual working directory, one that is under two different VC systems at
32    the same time.
33
34    This class reimplements the two interfaces, dispatching the right method
35    to the right backend.
36    """
37
38    def __init__(self, source_repo, target_repo):
39        global IGNORED_METADIRS
40
41        self.source = source_repo.workingDir()
42        self.target = target_repo.workingDir()
43
44        sbdir = self.source.basedir
45        tbdir = self.target.basedir
46        if sbdir == tbdir:
47            shared = True
48        elif tbdir.startswith(sbdir):
49            raise InvocationError('Target base directory %r cannot be a '
50                                  'subdirectory of source directory %r' %(
51                (tbdir, sbdir)))
52        elif sbdir.startswith(tbdir):
53            shared = True
54        else:
55            shared = False
56        self.shared_basedirs = shared
57        self.source.shared_basedirs = shared
58        self.target.shared_basedirs = shared
59
60        IGNORED_METADIRS = filter(None, [source_repo.METADIR,
61                                         target_repo.METADIR])
62        IGNORED_METADIRS.extend(source_repo.EXTRA_METADIRS)
63        IGNORED_METADIRS.extend(target_repo.EXTRA_METADIRS)
64
65        self.source.prepareSourceRepository()
66        self.target.prepareTargetRepository()
67
68        # UpdatableSourceWorkingDir
69
70        self.getPendingChangesets = self.source.getPendingChangesets
71        self.checkoutUpstreamRevision = self.source.checkoutUpstreamRevision
72
73        # SyncronizableTargetWorkingDir
74
75        self.prepareWorkingDirectory = self.target.prepareWorkingDirectory
76
77    def setStateFile(self, state_file):
78        """
79        Set the state file used to store the revision and pending changesets.
80        """
81
82        self.source.setStateFile(state_file)
83        self.target.setStateFile(state_file)
84
85    def setLogfile(self, logfile):
86        """
87        Set the name of the logfile, just to ignore it.
88        """
89
90        self.target.logfile = logfile
91
92    def applyPendingChangesets(self, applyable=None, replay=None, applied=None):
93        return self.source.applyPendingChangesets(replay=self.replayChangeset,
94                                                  applyable=applyable,
95                                                  applied=applied)
96
97    def importFirstRevision(self, source_repo, changeset, initial):
98        if not self.shared_basedirs:
99            self._syncTargetWithSource()
100        self.target.importFirstRevision(source_repo, changeset, initial)
101
102    def replayChangeset(self, changeset):
103        if not self.shared_basedirs:
104            self._syncTargetWithSource()
105        self.target.replayChangeset(changeset)
106
107    def _syncTargetWithSource(self):
108        cmd = ['rsync', '--delete', '--archive']
109        for M in IGNORED_METADIRS:
110            cmd.extend(['--exclude', M])
111
112        rsync = ExternalCommand(command=cmd)
113        rsync.execute(self.source.basedir+'/', self.target.basedir)
Note: See TracBrowser for help on using the repository browser.