~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/remote.py

  • Committer: Martin Pool
  • Date: 2007-04-26 07:48:05 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-20070426074805-va53nylsxqt7ur7u
Repository.tarball fixes for python2.4

Use 'r|bz2' to extract since r:bz2 is not supported
Replace extractall, which is not in python2.4
RemoteRepository._get_tarball returns a TemporaryFile
-------------- This line and the following will be ignored --------------

modified:
  bzrlib/remote.py
  bzrlib/tests/test_remote.py
  bzrlib/tests/test_smart.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
429
429
        return self._real_repository.break_lock()
430
430
 
431
431
    def _get_tarball(self, compression):
432
 
        """See Repository.tarball()."""
 
432
        """Return a TemporaryFile containing a repository tarball"""
 
433
        import tempfile
433
434
        path = self.bzrdir._path_for_remote_call(self._client)
434
435
        response, protocol = self._client.call_expecting_body(
435
436
            'Repository.tarball', path, compression)
437
438
            'unexpected response code %s' % (response,)
438
439
        if response[0] == 'ok':
439
440
            # Extract the tarball and return it
440
 
            body = protocol.read_body_bytes()
441
 
            return body
 
441
            t = tempfile.NamedTemporaryFile()
 
442
            # TODO: rpc layer should read directly into it...
 
443
            t.write(protocol.read_body_bytes())
 
444
            t.seek(0)
 
445
            return t
442
446
        else:
443
447
            raise errors.SmartServerError(error_code=response)
444
448
 
597
601
        from StringIO import StringIO
598
602
        # TODO: Maybe a progress bar while streaming the tarball?
599
603
        note("Copying repository content as tarball...")
600
 
        tar_file = StringIO(self._get_tarball('bz2'))
601
 
        tar = tarfile.open('repository', fileobj=tar_file,
602
 
            mode='r:bz2')
603
 
        tmpdir = tempfile.mkdtemp()
 
604
        tar_file = self._get_tarball('bz2')
604
605
        try:
605
 
            tar.extractall(tmpdir)
606
 
            tmp_bzrdir = BzrDir.open(tmpdir)
607
 
            tmp_repo = tmp_bzrdir.open_repository()
608
 
            tmp_repo.copy_content_into(destination, revision_id)
 
606
            tar = tarfile.open('repository', fileobj=tar_file,
 
607
                mode='r|bz2')
 
608
            tmpdir = tempfile.mkdtemp()
 
609
            try:
 
610
                _extract_tar(tar, tmpdir)
 
611
                tmp_bzrdir = BzrDir.open(tmpdir)
 
612
                tmp_repo = tmp_bzrdir.open_repository()
 
613
                tmp_repo.copy_content_into(destination, revision_id)
 
614
            finally:
 
615
                osutils.rmtree(tmpdir)
609
616
        finally:
610
 
            osutils.rmtree(tmpdir)
 
617
            tar_file.close()
611
618
        # TODO: if the server doesn't support this operation, maybe do it the
612
619
        # slow way using the _real_repository?
613
620
        #
1032
1039
            self._branch_data_config = TreeConfig(self.branch._real_branch)
1033
1040
        return self._branch_data_config
1034
1041
 
 
1042
 
 
1043
def _extract_tar(tar, to_dir):
 
1044
    """Extract all the contents of a tarfile object.
 
1045
 
 
1046
    A replacement for extractall, which is not present in python2.4
 
1047
    """
 
1048
    for tarinfo in tar:
 
1049
        tar.extract(tarinfo, to_dir)