/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/smart/repository.py

  • Committer: Andrew Bennetts
  • Date: 2008-09-08 12:59:00 UTC
  • mfrom: (3695 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3756.
  • Revision ID: andrew.bennetts@canonical.com-20080908125900-8ywtsr7jqyyatjz0
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
import tempfile
24
24
import tarfile
25
25
 
26
 
from bzrlib import errors
 
26
from bzrlib import (
 
27
    errors,
 
28
    osutils,
 
29
    )
27
30
from bzrlib.bzrdir import BzrDir
28
31
from bzrlib.pack import ContainerSerialiser
29
32
from bzrlib.smart.request import (
257
260
              firstrev: 1234.230 0
258
261
              latestrev: 345.700 3600
259
262
              revisions: 2
260
 
              size:45
261
263
 
262
264
              But containing only fields returned by the gather_stats() call
263
265
        """
350
352
 
351
353
    def do_repository_request(self, repository, compression):
352
354
        from bzrlib import osutils
353
 
        repo_transport = repository.control_files._transport
354
355
        tmp_dirname, tmp_repo = self._copy_to_tempdir(repository)
355
356
        try:
356
357
            controldir_name = tmp_dirname + '/.bzr'
359
360
            osutils.rmtree(tmp_dirname)
360
361
 
361
362
    def _copy_to_tempdir(self, from_repo):
362
 
        tmp_dirname = tempfile.mkdtemp(prefix='tmpbzrclone')
 
363
        tmp_dirname = osutils.mkdtemp(prefix='tmpbzrclone')
363
364
        tmp_bzrdir = from_repo.bzrdir._format.initialize(tmp_dirname)
364
365
        tmp_repo = from_repo._format.initialize(tmp_bzrdir)
365
366
        from_repo.copy_content_into(tmp_repo)
372
373
            # all finished; write the tempfile out to the network
373
374
            temp.seek(0)
374
375
            return SuccessfulSmartServerResponse(('ok',), temp.read())
375
 
            # FIXME: Don't read the whole thing into memory here; rather stream it
376
 
            # out from the file onto the network. mbp 20070411
 
376
            # FIXME: Don't read the whole thing into memory here; rather stream
 
377
            # it out from the file onto the network. mbp 20070411
377
378
        finally:
378
379
            temp.close()
379
380
 
389
390
            dirname = dirname.encode(sys.getfilesystemencoding())
390
391
            # python's tarball module includes the whole path by default so
391
392
            # override it
392
 
            assert dirname.endswith('.bzr')
 
393
            if not dirname.endswith('.bzr'):
 
394
                raise ValueError(dirname)
393
395
            tarball.add(dirname, '.bzr') # recursive by default
394
396
        finally:
395
397
            tarball.close()
396
 
 
397
 
 
398
 
class SmartServerRepositoryStreamKnitDataForRevisions(SmartServerRepositoryRequest):
399
 
    """Bzr <= 1.1 streaming pull, buffers all data on server."""
400
 
 
401
 
    def do_repository_request(self, repository, *revision_ids):
402
 
        repository.lock_read()
403
 
        try:
404
 
            return self._do_repository_request(repository, revision_ids)
405
 
        finally:
406
 
            repository.unlock()
407
 
 
408
 
    def _do_repository_request(self, repository, revision_ids):
409
 
        stream = repository.get_data_stream_for_search(
410
 
            repository.revision_ids_to_search_result(set(revision_ids)))
411
 
        buffer = StringIO()
412
 
        pack = ContainerSerialiser()
413
 
        buffer.write(pack.begin())
414
 
        try:
415
 
            try:
416
 
                for name_tuple, bytes in stream:
417
 
                    buffer.write(pack.bytes_record(bytes, [name_tuple]))
418
 
            except:
419
 
                # Undo the lock_read that happens once the iterator from
420
 
                # get_data_stream is started.
421
 
                repository.unlock()
422
 
                raise
423
 
        except errors.RevisionNotPresent, e:
424
 
            return FailedSmartServerResponse(('NoSuchRevision', e.revision_id))
425
 
        buffer.write(pack.end())
426
 
        return SuccessfulSmartServerResponse(('ok',), buffer.getvalue())
427
 
 
428
 
 
429
 
class SmartServerRepositoryStreamRevisionsChunked(SmartServerRepositoryRequest):
430
 
    """Bzr 1.1+ streaming pull."""
431
 
 
432
 
    def do_body(self, body_bytes):
433
 
        repository = self._repository
434
 
        repository.lock_read()
435
 
        try:
436
 
            search, error = self.recreate_search(repository, body_bytes)
437
 
            if error is not None:
438
 
                repository.unlock()
439
 
                return error
440
 
            stream = repository.get_data_stream_for_search(search.get_result())
441
 
        except Exception:
442
 
            # On non-error, unlocking is done by the body stream handler.
443
 
            repository.unlock()
444
 
            raise
445
 
        return SuccessfulSmartServerResponse(('ok',),
446
 
            body_stream=self.body_stream(stream, repository))
447
 
 
448
 
    def body_stream(self, stream, repository):
449
 
        pack = ContainerSerialiser()
450
 
        yield pack.begin()
451
 
        try:
452
 
            try:
453
 
                for name_tuple, bytes in stream:
454
 
                    yield pack.bytes_record(bytes, [name_tuple])
455
 
            except:
456
 
                # Undo the lock_read that that happens once the iterator from
457
 
                # get_data_stream is started.
458
 
                repository.unlock()
459
 
                raise
460
 
        except errors.RevisionNotPresent, e:
461
 
            # This shouldn't be able to happen, but as we don't buffer
462
 
            # everything it can in theory happen.
463
 
            repository.unlock()
464
 
            yield FailedSmartServerResponse(('NoSuchRevision', e.revision_id))
465
 
        else:
466
 
            repository.unlock()
467
 
            pack.end()
468