def _download()

in mozregression/download_manager.py [0:0]


    def _download(self, url, dest, finished_callback, chunk_size, session):
        # save the file under a temporary name
        # this allow to not use a broken file in case things went really bad
        # while downloading the file (ie the python interpreter is killed
        # abruptly)
        temp = None
        bytes_so_far = 0
        try:
            with closing(session.get(url, stream=True)) as response:
                # GCP storage does not always return a content-length header, check alternates.
                total_size = self.get_total_size(response.headers)

                if total_size:
                    self._update_progress(bytes_so_far, total_size)
                # we use NamedTemporaryFile as raw open() call was causing
                # issues on windows - see:
                # https://bugzilla.mozilla.org/show_bug.cgi?id=1185756
                with tempfile.NamedTemporaryFile(
                    delete=False, mode="wb", suffix=".tmp", dir=os.path.dirname(dest)
                ) as temp:
                    for chunk in response.iter_content(chunk_size):
                        if self.is_canceled():
                            break
                        if chunk:
                            temp.write(chunk)
                        bytes_so_far += len(chunk)
                        if total_size:
                            self._update_progress(bytes_so_far, total_size)
            response.raise_for_status()
        except Exception:
            self.__error = sys.exc_info()
        try:
            if temp is None:
                pass  # not even opened the temp file, nothing to do
            elif self.is_canceled() or self.__error:
                mozfile.remove(temp.name)
            else:
                # if all goes well, then rename the file to the real dest
                mozfile.remove(dest)  # just in case it already existed
                mozfile.move(temp.name, dest)
        finally:
            if finished_callback:
                finished_callback(self)