From 3317581a9698f4dea925e73ebe8a41aa4705fbfb Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Mon, 30 Oct 2023 16:25:18 +0100 Subject: [PATCH] Rewrite the csv_file_exist test --- web/run.py | 57 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/web/run.py b/web/run.py index 96ac575..20e1417 100755 --- a/web/run.py +++ b/web/run.py @@ -976,16 +976,18 @@ def generate_csv_file_if_needed(target_slug, input_slug, stopped_at.strftime(FILE_DATE_FMT)) local_csv_file = join(CACHE_DIR, filename) - generate = True + skip_generation = False + + # It needs to have more than one line to not be empty (headers) if isfile(local_csv_file): - # It needs to have more than one line to not be empty (headers) with open(local_csv_file) as f: - cnt = 0 - for _ in f: - cnt += 1 - if cnt > 1: - generate = False - break + skip_generation = len(f.readlines()) > 0 + + if skip_generation: + hp_logger.debug(f"{local_csv_file} already exists") + return None + else: + hp_logger.debug(f"{local_csv_file} doesnt exist or is empty") # temporary switch while migrating each target to spz if target_slug in ['rosetta', 'juno', 'p67']: @@ -993,26 +995,25 @@ def generate_csv_file_if_needed(target_slug, input_slug, else: csv_generator = generate_csv_contents_spz - if generate: - hp_logger.info("Generating CSV '%s'..." % local_csv_file) - try: - with open(local_csv_file, mode="w+") as f: - f.write(csv_generator( - target_slug=target_slug, input_slug=input_slug, - started_at=started_at, stopped_at=stopped_at - )) - hp_logger.info("Generation of '%s' done." % filename) - except Exception as e: - from sys import exc_info - from traceback import extract_tb - exc_type, exc_value, exc_traceback = exc_info() - hp_logger.error(e) - for trace in extract_tb(exc_traceback): - hp_logger.error(trace) - if isfile(local_csv_file): - hp_logger.warning("Removing failed CSV '%s'..." % local_csv_file) - removefile(local_csv_file) - abort(500, "Failed creating CSV '%s' : %s" % (filename, e)) + hp_logger.info("Generating CSV '%s'..." % local_csv_file) + try: + with open(local_csv_file, mode="w+") as f: + f.write(csv_generator( + target_slug=target_slug, input_slug=input_slug, + started_at=started_at, stopped_at=stopped_at + )) + hp_logger.info("Generation of '%s' done." % filename) + except Exception as e: + from sys import exc_info + from traceback import extract_tb + exc_type, exc_value, exc_traceback = exc_info() + hp_logger.error(e) + for trace in extract_tb(exc_traceback): + hp_logger.error(trace) + if isfile(local_csv_file): + hp_logger.warning("Removing failed CSV '%s'..." % local_csv_file) + removefile(local_csv_file) + abort(500, "Failed creating CSV '%s' : %s" % (filename, e)) def remove_all_files(in_directory): -- libgit2 0.21.2