~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/codehosting/codeimport/worker.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-09-16 08:57:11 UTC
  • mfrom: (13958.3.4 follow-http-redirects)
  • Revision ID: launchpad@pqm.canonical.com-20110916085711-7mu22op9x06v34vt
[r=jcsackett][bug=804218] Make the code imports follow HTTP redirects.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
    NoRepositoryPresent,
38
38
    NoSuchFile,
39
39
    NotBranchError,
40
 
    )
41
 
from bzrlib.transport import get_transport
 
40
    TooManyRedirections,
 
41
    )
 
42
from bzrlib.transport import (
 
43
    do_catching_redirections,
 
44
    get_transport,
 
45
    )
42
46
from bzrlib.upgrade import upgrade
43
47
import bzrlib.ui
44
48
from bzrlib.urlutils import (
675
679
        :param url: URL to open
676
680
        :return: ControlDir instance
677
681
        """
 
682
        def redirected(transport, e, redirection_notice):
 
683
            self._opener_policy.checkOneURL(e.target)
 
684
            redirected_transport = transport._redirected_to(e.source, e.target)
 
685
            if redirected_transport is None:
 
686
                raise NotBranchError(e.source)
 
687
            self._logger.info('%s is%s redirected to %s',
 
688
                 transport.base, e.permanently, redirected_transport.base)
 
689
            return redirected_transport
 
690
        def find_format(transport):
 
691
            last_error = None
 
692
            for prober_kls in self.probers:
 
693
                prober = prober_kls()
 
694
                try:
 
695
                    return transport, prober.probe_transport(transport)
 
696
                except NotBranchError, e:
 
697
                    last_error = e
 
698
            else:
 
699
                raise last_error
678
700
        transport = get_transport(url)
679
 
        for prober_kls in self.probers:
680
 
            prober = prober_kls()
681
 
            try:
682
 
                format = prober.probe_transport(transport)
683
 
            except NotBranchError:
684
 
                pass
685
 
            else:
686
 
                return format.open(transport)
687
 
        else:
688
 
            raise NotBranchError("Not a valid branch")
 
701
        transport, format = do_catching_redirections(find_format, transport,
 
702
            redirected)
 
703
        return format.open(transport)
689
704
 
690
705
    def _doImport(self):
691
706
        self._logger.info("Starting job.")
699
714
            try:
700
715
                remote_branch = opener.open(
701
716
                    self.source_details.url, self._open_dir)
 
717
            except TooManyRedirections:
 
718
                self._logger.info("Too many redirections.")
 
719
                return CodeImportWorkerExitCode.FAILURE_INVALID
702
720
            except NotBranchError:
703
721
                self._logger.info("No branch found at remote location.")
704
722
                return CodeImportWorkerExitCode.FAILURE_INVALID