From 65090ade5f5fa75f3247b522bcb234a8b57e144c Mon Sep 17 00:00:00 2001 From: Jason Nichols Date: Wed, 10 May 2023 19:02:11 -0400 Subject: [PATCH] Handle files that don't start with the id on the first line --- dupe_check.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/dupe_check.py b/dupe_check.py index 4c5a65a..dd53320 100644 --- a/dupe_check.py +++ b/dupe_check.py @@ -1,6 +1,16 @@ import os +def extract_id(file): + with open(sub_path + file, 'r') as f: + lines = f.readlines() + for line in lines: + line = line.strip().split(":") + if len(line) == 2 and line[0].strip() == "id": + return line[1] + return None + + print(f"Initializing in {os.getcwd()}") sub_path = "resources/rules/" id_to_files = {} @@ -8,13 +18,14 @@ for file in os.listdir(sub_path): if file.endswith(".yaml"): print(f"Processing {file}") - with open(sub_path + file, 'r') as f: - line = f.readline().strip() - uuid = line.split(":")[1].strip() + uuid = extract_id(file) + if uuid is None: + print(f"Couldn't process {file}, ignoring.") + else : if uuid in id_to_files.keys(): msg = f"Duplicate key found for id={uuid}, file1={id_to_files[uuid]}, file2={file}" raise Exception(msg) id_to_files[uuid] = file -print(f"Successfully validated {id_to_files.size()} files for duplicate IDs") +print(f"Successfully validated {len(id_to_files)} files for duplicate IDs")