Ejemplo n.º 1
0
def test_regress_below_min(ansi_io):
    bar = ProgressBar(ansi_io, 10, 0)
    bar.set_progress(1)
    bar.advance(-1)
    bar.advance(-1)

    output = [
        "  1/10 [==>-------------------------]  10%",
        "  0/10 [>---------------------------]   0%",
    ]

    expected = generate_output(output)

    assert expected == ansi_io.fetch_error()
Ejemplo n.º 2
0
    def _download_archive(self, operation: Install | Update,
                          link: Link) -> Path:
        response = self._authenticator.request("get",
                                               link.url,
                                               stream=True,
                                               io=self._sections.get(
                                                   id(operation), self._io))
        wheel_size = response.headers.get("content-length")
        operation_message = self.get_operation_message(operation)
        message = (
            f"  <fg=blue;options=bold>•</> {operation_message}: <info>Downloading...</>"
        )
        progress = None
        if self.supports_fancy_output():
            if wheel_size is None:
                self._write(operation, message)
            else:
                from cleo.ui.progress_bar import ProgressBar

                progress = ProgressBar(self._sections[id(operation)],
                                       max=int(wheel_size))
                progress.set_format(message + " <b>%percent%%</b>")

        if progress:
            with self._lock:
                self._sections[id(operation)].clear()
                progress.start()

        done = 0
        archive: Path = self._chef.get_cache_directory_for_link(
            link) / link.filename
        archive.parent.mkdir(parents=True, exist_ok=True)
        with archive.open("wb") as f:
            for chunk in response.iter_content(chunk_size=4096):
                if not chunk:
                    break

                done += len(chunk)

                if progress:
                    with self._lock:
                        progress.set_progress(done)

                f.write(chunk)

        if progress:
            with self._lock:
                progress.finish()

        return archive
Ejemplo n.º 3
0
def test_advance_over_max(ansi_io):
    bar = ProgressBar(ansi_io, 10)
    bar.set_progress(9)
    bar.advance()
    bar.advance()

    output = [
        "  9/10 [=========================>--]  90%",
        " 10/10 [============================] 100%",
        " 11/11 [============================] 100%",
    ]

    expected = generate_output(output)

    assert expected == ansi_io.fetch_error()
Ejemplo n.º 4
0
def test_clear(ansi_io):
    bar = ProgressBar(ansi_io, 50, 0)
    bar.start()
    bar.set_progress(25)
    bar.clear()

    output = [
        "  0/50 [>---------------------------]   0%",
        " 25/50 [==============>-------------]  50%",
        "",
    ]

    expected = generate_output(output)

    assert expected == ansi_io.fetch_error()
Ejemplo n.º 5
0
def test_set_current_progress(ansi_io):
    bar = ProgressBar(ansi_io, 50, 0)
    bar.start()
    bar.display()
    bar.advance()
    bar.set_progress(15)
    bar.set_progress(25)

    output = [
        "  0/50 [>---------------------------]   0%",
        "  1/50 [>---------------------------]   2%",
        " 15/50 [========>-------------------]  30%",
        " 25/50 [==============>-------------]  50%",
    ]

    expected = generate_output(output)

    assert expected == ansi_io.fetch_error()
Ejemplo n.º 6
0
    def _upload_file(
        self,
        session: requests.Session,
        url: str,
        file: Path,
        dry_run: Optional[bool] = False,
    ) -> requests.Response:
        from cleo.ui.progress_bar import ProgressBar

        data = self.post_data(file)
        data.update({
            # action
            ":action": "file_upload",
            "protocol_version": "1",
        })

        data_to_send = self._prepare_data(data)

        with file.open("rb") as fp:
            data_to_send.append(
                ("content", (file.name, fp, "application/octet-stream")))
            encoder = MultipartEncoder(data_to_send)
            bar = ProgressBar(self._io, max=encoder.len)
            bar.set_format(
                f" - Uploading <c1>{file.name}</c1> <b>%percent%%</b>")
            monitor = MultipartEncoderMonitor(
                encoder, lambda monitor: bar.set_progress(monitor.bytes_read))

            bar.start()

            resp = None

            try:
                if not dry_run:
                    resp = session.post(
                        url,
                        data=monitor,
                        allow_redirects=False,
                        headers={"Content-Type": monitor.content_type},
                    )
                if dry_run or 200 <= resp.status_code < 300:
                    bar.set_format(
                        f" - Uploading <c1>{file.name}</c1> <fg=green>%percent%%</>"
                    )
                    bar.finish()
                elif resp.status_code == 301:
                    if self._io.output.is_decorated():
                        self._io.overwrite(
                            f" - Uploading <c1>{file.name}</c1> <error>FAILED</>"
                        )
                    raise UploadError("Redirects are not supported. "
                                      "Is the URL missing a trailing slash?")
            except (requests.ConnectionError, requests.HTTPError) as e:
                if self._io.output.is_decorated():
                    self._io.overwrite(
                        f" - Uploading <c1>{file.name}</c1> <error>FAILED</>")
                raise UploadError(e)
            finally:
                self._io.write_line("")

        return resp
Ejemplo n.º 7
0
    def _upload_file(
        self,
        session: requests.Session,
        url: str,
        file: Path,
        dry_run: bool = False,
        skip_existing: bool = False,
    ) -> None:
        from cleo.ui.progress_bar import ProgressBar

        data = self.post_data(file)
        data.update({
            # action
            ":action": "file_upload",
            "protocol_version": "1",
        })

        data_to_send: list[tuple[str, Any]] = self._prepare_data(data)

        with file.open("rb") as fp:
            data_to_send.append(
                ("content", (file.name, fp, "application/octet-stream")))
            encoder = MultipartEncoder(data_to_send)
            bar = ProgressBar(self._io, max=encoder.len)
            bar.set_format(
                f" - Uploading <c1>{file.name}</c1> <b>%percent%%</b>")
            monitor = MultipartEncoderMonitor(
                encoder, lambda monitor: bar.set_progress(monitor.bytes_read))

            bar.start()

            resp = None

            try:
                if not dry_run:
                    resp = session.post(
                        url,
                        data=monitor,
                        allow_redirects=False,
                        headers={"Content-Type": monitor.content_type},
                        timeout=REQUESTS_TIMEOUT,
                    )
                if resp is None or 200 <= resp.status_code < 300:
                    bar.set_format(
                        f" - Uploading <c1>{file.name}</c1> <fg=green>%percent%%</>"
                    )
                    bar.finish()
                elif resp.status_code == 301:
                    if self._io.output.is_decorated():
                        self._io.overwrite(
                            f" - Uploading <c1>{file.name}</c1> <error>FAILED</>"
                        )
                    raise UploadError("Redirects are not supported. "
                                      "Is the URL missing a trailing slash?")
                elif resp.status_code == 400 and "was ever registered" in resp.text:
                    self._register(session, url)
                    resp.raise_for_status()
                elif skip_existing and self._is_file_exists_error(resp):
                    bar.set_format(
                        f" - Uploading <c1>{file.name}</c1> <warning>File exists."
                        " Skipping</>")
                    bar.display()
                else:
                    resp.raise_for_status()
            except (requests.ConnectionError, requests.HTTPError) as e:
                if self._io.output.is_decorated():
                    self._io.overwrite(
                        f" - Uploading <c1>{file.name}</c1> <error>FAILED</>")
                raise UploadError(e)
            finally:
                self._io.write_line("")