From: Jacob Lifshay Date: Tue, 30 Aug 2022 03:53:55 +0000 (-0700) Subject: write progress to terminal, even if output is redirected X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=c8d0b70d97fbe164f1d1c6bbf744edf7dc25845d;p=utils.git write progress to terminal, even if output is redirected --- diff --git a/src/budget_sync/util.py b/src/budget_sync/util.py index f26c1a0..7a4170c 100644 --- a/src/budget_sync/util.py +++ b/src/budget_sync/util.py @@ -5,6 +5,7 @@ from bugzilla.bug import Bug from typing import Any, Callable, Dict, Iterator, List, Type, Union from enum import Enum from io import StringIO +import os class BugStatus(Enum): @@ -37,13 +38,31 @@ class BugStatus(Enum): def all_bugs(bz: Bugzilla) -> Iterator[Bug]: chunk_start = 1 chunk_size = 100 - while True: - bugs = bz.getbugs(list(range(chunk_start, chunk_start + chunk_size))) - chunk_start += chunk_size - print("bugs loaded", len(bugs), chunk_start, flush=True) - if len(bugs) == 0: - return - yield from bugs + try: + if hasattr(os, "ctermid"): + term = open(os.ctermid(), "wt", encoding="utf-8") + elif os.name == "nt": + term = open("CONOUT$", "wt", encoding="utf-8") + else: + term = None + except OSError: + # no terminal available + term = None + try: # can't use `with` since it doesn't work with None + while True: + bugs = list(range(chunk_start, chunk_start + chunk_size)) + bugs = bz.getbugs(bugs) + chunk_start += chunk_size + # progress indicator, should go to terminal + if term is not None: + print("bugs loaded", len(bugs), chunk_start, flush=True, + file=term) + if len(bugs) == 0: + return + yield from bugs + finally: + if term is not None: + term.close() class SequencePrettyPrinter: