Python script to load & re-save YAMLs recursively
This can be used to fix PyYAML generating slightly different YAML files by formatting than any manually generated file. For example, you can use this script first to generate a clean diff for just re-saving, then run another script to modify the YAML for a cleaner diff.
#!/usr/bin/env python3
import argparse
import yaml
import pathlib
def load_and_save_yaml(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
data = yaml.safe_load(file)
with open(file_path, 'w', encoding='utf-8') as file:
yaml.safe_dump(data, file)
def process_files(file_paths):
for file_path in file_paths:
try:
load_and_save_yaml(file_path)
print(f"Processed: {file_path}")
except Exception as e:
print(f"Failed to process {file_path}: {e}")
def main():
parser = argparse.ArgumentParser(description="Load and re-save YAML files.")
parser.add_argument('files', nargs='*', help="YAML files to process. If not provided, searches for all *.yaml files in the current directory and subdirectories.")
args = parser.parse_args()
if args.files:
file_paths = [pathlib.Path(file) for file in args.files]
else:
file_paths = list(pathlib.Path('.').rglob('*.yaml'))
process_files(file_paths)
if __name__ == "__main__":
main()