Skip to content

Commit 68dda2b

Browse files
committed
Fix mergefiles.py to recursively process subdirectories
- Replace os.listdir() with glob.glob() to enable recursive directory traversal - Use glob patterns with '**' and recursive=True to find YAML files in nested subdirectories - Update file processing logic to handle full file paths instead of just filenames - Use os.path.basename() to extract filename for warning messages This fix allows the merge script to properly discover and process YAML files located in subdirectories rather than being limited to the immediate directory, making it more flexible for complex directory structures. Fixes the script's ability to merge YAML configuration files from nested module directories in the website's data structure. Signed-off-by: Harvey Lynden <[email protected]>
1 parent ac58165 commit 68dda2b

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

scripts/mergefiles.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import glob
12
import os
23

34
import yaml
@@ -6,21 +7,27 @@
67
def merge_yaml_files(directory, output_file):
78
merged_data = [] # Initialize an empty list to hold merged data
89

9-
# Iterate over all files in the directory
10-
for filename in sorted(os.listdir(directory)):
11-
if filename.endswith(".yml") or filename.endswith(".yaml"):
12-
file_path = os.path.join(directory, filename)
13-
with open(file_path, "r") as file:
14-
# Load the YAML data while preserving order
15-
data = yaml.load(file, Loader=yaml.SafeLoader) # Use SafeLoader
16-
17-
# Check if the loaded data is a dictionary
18-
if isinstance(data, dict):
19-
merged_data.append(data) # Append the dictionary to merged_data
20-
else:
21-
print(
22-
f"Warning: The content of {filename} is not a dictionary. Skipping."
23-
)
10+
# Find all YAML files recursively in subdirectories
11+
12+
yaml_files = glob.glob(os.path.join(directory, "**", "*.yml"), recursive=True)
13+
yaml_files.extend(
14+
glob.glob(os.path.join(directory, "**", "*.yaml"), recursive=True)
15+
)
16+
17+
# Process each YAML file found
18+
for file_path in sorted(yaml_files):
19+
filename = os.path.basename(file_path)
20+
with open(file_path, "r") as file:
21+
# Load the YAML data while preserving order
22+
data = yaml.load(file, Loader=yaml.SafeLoader) # Use SafeLoader
23+
24+
# Check if the loaded data is a dictionary
25+
if isinstance(data, dict):
26+
merged_data.append(data) # Append the dictionary to merged_data
27+
else:
28+
print(
29+
f"Warning: The content of {filename} is not a dictionary. Skipping."
30+
)
2431

2532
# Write the merged data to the output YAML file
2633
with open(output_file, "w") as outfile:

0 commit comments

Comments
 (0)