~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/smart/repository.py

  • Committer: Martin Pool
  • Date: 2007-03-29 07:25:03 UTC
  • mto: (2420.2.2 bzr.http.auth)
  • mto: This revision was merged to the branch mainline in revision 2462.
  • Revision ID: mbp@sourcefrog.net-20070329072503-ir70ask331e138ux
smart method Repository.tarball actually returns the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Server-side repository related request implmentations."""
18
18
 
 
19
import sys
 
20
import tempfile
 
21
import tarfile
19
22
 
20
23
from bzrlib import errors
21
24
from bzrlib.bzrdir import BzrDir
22
25
from bzrlib.smart.request import SmartServerRequest, SmartServerResponse
 
26
from bzrlib.transport.local import LocalTransport
23
27
 
24
28
 
25
29
class SmartServerRepositoryRequest(SmartServerRequest):
175
179
 
176
180
 
177
181
class SmartServerRepositoryTarball(SmartServerRepositoryRequest):
178
 
    """Get the raw repository files as one big tarball
 
182
    """Get the raw repository files as one big tarball.
 
183
 
 
184
    The tarball contains one directory 'repository' which contains all the
 
185
    files found in that directory.
 
186
 
 
187
    At the moment this is only supported when the repository is on a
 
188
    LocalTransport.
179
189
    
180
 
    This takes one parameter, format, which currently must be 
181
 
    "tbz2".
 
190
    This takes one parameter, compression, which currently must be 
 
191
    "", "gz", or "bz2".
182
192
    """
183
193
 
184
 
    def do_repository_request(self, repository, format):
185
 
        return SmartServerResponse(('ok',))
 
194
    def do_repository_request(self, repository, compression):
 
195
        repo_transport = repository.control_files._transport
 
196
        if not isinstance(repo_transport, LocalTransport):
 
197
            raise NotImplementedError(
 
198
                "Repository.tarball is not supported on non-local "
 
199
                "transport %s"
 
200
                % (repo_transport,))
 
201
        repo_dir = repo_transport.local_abspath('.')
 
202
        temp = tempfile.NamedTemporaryFile()
 
203
        try:
 
204
            tarball = tarfile.open(temp.name, mode='w:' + compression)
 
205
            try:
 
206
                # The tarball module only accepts ascii names, and (i guess)
 
207
                # packs them with their 8bit names.  We know all the files
 
208
                # within the repository have ASCII names so the should be safe
 
209
                # to pack in.
 
210
                repo_dir = repo_dir.encode(sys.getfilesystemencoding())
 
211
                tarball.add(repo_dir, 'repository') # recursive by default
 
212
            finally:
 
213
                tarball.close()
 
214
            # all finished; write the tempfile out to the network
 
215
            temp.seek(0)
 
216
            return SmartServerResponse(('ok',), temp.read())
 
217
        finally:
 
218
            temp.close()