Skip to content

Commit

Permalink
Merge branch 'fix-py-signal-handling' into 'master'
Browse files Browse the repository at this point in the history
Fix Python broken pipe signal handling

See merge request ogs/ogs!5062
  • Loading branch information
bilke committed Aug 6, 2024
2 parents 9fdef1a + 981ea88 commit 2553cc3
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions Applications/Python/ogs/dev/ogs_log_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
from collections import defaultdict
from decimal import ROUND_DOWN, ROUND_UP, Decimal
from pathlib import Path
from signal import SIG_DFL, SIGPIPE, signal

import pandas as pd

signal(SIGPIPE, SIG_DFL)

logger = logging.getLogger(__name__)

re_prj_file = re.compile("info: Reading project file (.*)[.]$")
Expand Down Expand Up @@ -274,9 +271,13 @@ def aggregate_log_files(log_files_dirs):
def run(log_files_dirs, xml_out_dir, verbose):
map_prj_file_to_agg_vtkdiff_stats = aggregate_log_files(log_files_dirs)

for prj_file, df_vtkdiff_max in map_prj_file_to_agg_vtkdiff_stats.items():
for i, (prj_file, df_vtkdiff_max) in enumerate(
map_prj_file_to_agg_vtkdiff_stats.items()
):
if verbose:
print(f"\n###### {prj_file}\n")
if i != 0:
print() # blank line as a separator
print(f"###### {prj_file}\n")

df_vtkdiff_max = round_up_2_digits(df_vtkdiff_max) # noqa: PLW2901

Expand Down Expand Up @@ -317,11 +318,18 @@ def run(log_files_dirs, xml_out_dir, verbose):
if snippet_out is not None:
assert snippet_out.is_dir()

logger.setLevel(logging.DEBUG)
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setLevel(logging.INFO)
formatter = logging.Formatter("[%(levelname)s] %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)

run(args.log_files_dirs, snippet_out, args.verbose)
# suppress error message from Python interpreter, e.g., if a command
# pipeline exits early, see
# https://docs.python.org/3/library/signal.html#note-on-sigpipe
try:
run(args.log_files_dirs, snippet_out, args.verbose)
except BrokenPipeError:
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())

0 comments on commit 2553cc3

Please sign in to comment.