python: Simplify stdio flushing using print(…, flush=True).

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-06-09 13:11:26 -07:00 committed by Tim Abbott
parent d8adeb66ca
commit 342834ee9c
6 changed files with 13 additions and 25 deletions

View File

@ -198,9 +198,9 @@ def get_deployment_lock(error_rerun_script: str) -> None:
WARNING WARNING
+ "Another deployment in progress; waiting for lock... " + "Another deployment in progress; waiting for lock... "
+ "(If no deployment is running, rmdir {})".format(LOCK_DIR) + "(If no deployment is running, rmdir {})".format(LOCK_DIR)
+ ENDC + ENDC,
flush=True,
) )
sys.stdout.flush()
time.sleep(3) time.sleep(3)
if not got_lock: if not got_lock:

View File

@ -78,18 +78,17 @@ def test_server_running(
try: try:
# Wait for the server to start up. # Wait for the server to start up.
sys.stdout.write("\nWaiting for test server (may take a while)") print(end="\nWaiting for test server (may take a while)")
if not dots: if not dots:
sys.stdout.write("\n\n") print("\n", flush=True)
t = time.time() t = time.time()
while not server_is_up(server, log_file): while not server_is_up(server, log_file):
if dots: if dots:
sys.stdout.write(".") print(end=".", flush=True)
sys.stdout.flush()
time.sleep(0.4) time.sleep(0.4)
if time.time() - t > MAX_SERVER_WAIT: if time.time() - t > MAX_SERVER_WAIT:
raise Exception("Timeout waiting for server") raise Exception("Timeout waiting for server")
sys.stdout.write("\n\n--- SERVER IS UP! ---\n\n") print("\n\n--- SERVER IS UP! ---\n", flush=True)
# DO OUR ACTUAL TESTING HERE!!! # DO OUR ACTUAL TESTING HERE!!!
yield yield

View File

@ -1,5 +1,4 @@
import os import os
import sys
import tempfile import tempfile
from argparse import ArgumentParser from argparse import ArgumentParser
from typing import Any from typing import Any
@ -207,8 +206,7 @@ class Command(ZulipBaseCommand):
do_deactivate_realm(realm, acting_user=None) do_deactivate_realm(realm, acting_user=None)
def percent_callback(bytes_transferred: Any) -> None: def percent_callback(bytes_transferred: Any) -> None:
sys.stdout.write(".") print(end=".", flush=True)
sys.stdout.flush()
# Allows us to trigger exports separately from command line argument parsing # Allows us to trigger exports separately from command line argument parsing
export_realm_wrapper( export_realm_wrapper(

View File

@ -1,7 +1,5 @@
# Generated by Django 1.11.13 on 2018-06-14 13:39 # Generated by Django 1.11.13 on 2018-06-14 13:39
import sys
import bitfield.models import bitfield.models
from django.db import migrations from django.db import migrations
from django.db.backends.postgresql.schema import DatabaseSchemaEditor from django.db.backends.postgresql.schema import DatabaseSchemaEditor
@ -19,8 +17,7 @@ def reset_is_private_flag(apps: StateApps, schema_editor: DatabaseSchemaEditor)
i = 0 i = 0
total = len(user_profile_ids) total = len(user_profile_ids)
print("Setting default values for the new flag...") print("Setting default values for the new flag...", flush=True)
sys.stdout.flush()
for user_id in user_profile_ids: for user_id in user_profile_ids:
while True: while True:
# Ideally, we'd just do a single database query per user. # Ideally, we'd just do a single database query per user.
@ -45,8 +42,7 @@ def reset_is_private_flag(apps: StateApps, schema_editor: DatabaseSchemaEditor)
i += 1 i += 1
if i % 50 == 0 or i == total: if i % 50 == 0 or i == total:
percent = round((i / total) * 100, 2) percent = round((i / total) * 100, 2)
print(f"Processed {i}/{total} {percent}%") print(f"Processed {i}/{total} {percent}%", flush=True)
sys.stdout.flush()
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -1,5 +1,3 @@
import sys
from django.db import migrations from django.db import migrations
from django.db.backends.postgresql.schema import DatabaseSchemaEditor from django.db.backends.postgresql.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps from django.db.migrations.state import StateApps
@ -19,8 +17,7 @@ def set_initial_value_of_is_private_flag(
total = Message.objects.filter(recipient__type__in=[1, 3]).count() total = Message.objects.filter(recipient__type__in=[1, 3]).count()
processed = 0 processed = 0
print("\nStart setting initial value for is_private flag...") print("\nStart setting initial value for is_private flag...", flush=True)
sys.stdout.flush()
while True: while True:
range_end = i + 10000 range_end = i + 10000
# Can't use [Recipient.PERSONAL, Recipient.HUDDLE] in migration files # Can't use [Recipient.PERSONAL, Recipient.HUDDLE] in migration files
@ -41,8 +38,7 @@ def set_initial_value_of_is_private_flag(
percent = round((processed / total) * 100, 2) percent = round((processed / total) * 100, 2)
else: else:
percent = 100.00 percent = 100.00
print(f"Processed {processed}/{total} {percent}%") print(f"Processed {processed}/{total} {percent}%", flush=True)
sys.stdout.flush()
class Migration(migrations.Migration): class Migration(migrations.Migration):

View File

@ -5,7 +5,6 @@
# and zerver/lib/data_types.py systems for validating the schemas of # and zerver/lib/data_types.py systems for validating the schemas of
# events; it also uses the OpenAPI tools to validate our documentation. # events; it also uses the OpenAPI tools to validate our documentation.
import copy import copy
import sys
import time import time
from io import StringIO from io import StringIO
from typing import Any, Callable, Dict, List, Optional, Set from typing import Any, Callable, Dict, List, Optional, Set
@ -389,10 +388,10 @@ class BaseAction(ZulipTestCase):
we apply events after fetching data. If you we apply events after fetching data. If you
do not know how to debug it, you can ask for do not know how to debug it, you can ask for
help on chat. help on chat.
""" """,
flush=True,
) )
sys.stdout.flush()
raise AssertionError("Mismatching states") raise AssertionError("Mismatching states")