Commit 3317581a9698f4dea925e73ebe8a41aa4705fbfb

Authored by hitier
1 parent 7709b344
Exists in rhitier-dev

Rewrite the csv_file_exist test

Showing 1 changed file with 29 additions and 28 deletions   Show diff stats
@@ -976,16 +976,18 @@ def generate_csv_file_if_needed(target_slug, input_slug, @@ -976,16 +976,18 @@ def generate_csv_file_if_needed(target_slug, input_slug,
976 stopped_at.strftime(FILE_DATE_FMT)) 976 stopped_at.strftime(FILE_DATE_FMT))
977 local_csv_file = join(CACHE_DIR, filename) 977 local_csv_file = join(CACHE_DIR, filename)
978 978
979 - generate = True 979 + skip_generation = False
  980 +
  981 + # It needs to have more than one line to not be empty (headers)
980 if isfile(local_csv_file): 982 if isfile(local_csv_file):
981 - # It needs to have more than one line to not be empty (headers)  
982 with open(local_csv_file) as f: 983 with open(local_csv_file) as f:
983 - cnt = 0  
984 - for _ in f:  
985 - cnt += 1  
986 - if cnt > 1:  
987 - generate = False  
988 - break 984 + skip_generation = len(f.readlines()) > 0
  985 +
  986 + if skip_generation:
  987 + hp_logger.debug(f"{local_csv_file} already exists")
  988 + return None
  989 + else:
  990 + hp_logger.debug(f"{local_csv_file} doesnt exist or is empty")
989 991
990 # temporary switch while migrating each target to spz 992 # temporary switch while migrating each target to spz
991 if target_slug in ['rosetta', 'juno', 'p67']: 993 if target_slug in ['rosetta', 'juno', 'p67']:
@@ -993,26 +995,25 @@ def generate_csv_file_if_needed(target_slug, input_slug, @@ -993,26 +995,25 @@ def generate_csv_file_if_needed(target_slug, input_slug,
993 else: 995 else:
994 csv_generator = generate_csv_contents_spz 996 csv_generator = generate_csv_contents_spz
995 997
996 - if generate:  
997 - hp_logger.info("Generating CSV '%s'..." % local_csv_file)  
998 - try:  
999 - with open(local_csv_file, mode="w+") as f:  
1000 - f.write(csv_generator(  
1001 - target_slug=target_slug, input_slug=input_slug,  
1002 - started_at=started_at, stopped_at=stopped_at  
1003 - ))  
1004 - hp_logger.info("Generation of '%s' done." % filename)  
1005 - except Exception as e:  
1006 - from sys import exc_info  
1007 - from traceback import extract_tb  
1008 - exc_type, exc_value, exc_traceback = exc_info()  
1009 - hp_logger.error(e)  
1010 - for trace in extract_tb(exc_traceback):  
1011 - hp_logger.error(trace)  
1012 - if isfile(local_csv_file):  
1013 - hp_logger.warning("Removing failed CSV '%s'..." % local_csv_file)  
1014 - removefile(local_csv_file)  
1015 - abort(500, "Failed creating CSV '%s' : %s" % (filename, e)) 998 + hp_logger.info("Generating CSV '%s'..." % local_csv_file)
  999 + try:
  1000 + with open(local_csv_file, mode="w+") as f:
  1001 + f.write(csv_generator(
  1002 + target_slug=target_slug, input_slug=input_slug,
  1003 + started_at=started_at, stopped_at=stopped_at
  1004 + ))
  1005 + hp_logger.info("Generation of '%s' done." % filename)
  1006 + except Exception as e:
  1007 + from sys import exc_info
  1008 + from traceback import extract_tb
  1009 + exc_type, exc_value, exc_traceback = exc_info()
  1010 + hp_logger.error(e)
  1011 + for trace in extract_tb(exc_traceback):
  1012 + hp_logger.error(trace)
  1013 + if isfile(local_csv_file):
  1014 + hp_logger.warning("Removing failed CSV '%s'..." % local_csv_file)
  1015 + removefile(local_csv_file)
  1016 + abort(500, "Failed creating CSV '%s' : %s" % (filename, e))
1016 1017
1017 1018
1018 def remove_all_files(in_directory): 1019 def remove_all_files(in_directory):