ci: add sort_yaml.py

This commit is contained in:
Fu Hanxi
2024-01-23 11:28:06 +01:00
parent 946fdfce62
commit d42e3fce04
5 changed files with 108 additions and 44 deletions

View File

@@ -6,7 +6,6 @@ import inspect
import os
import re
import sys
from io import StringIO
from pathlib import Path
from typing import Dict
from typing import List
@@ -345,38 +344,6 @@ def check_test_scripts(
sys.exit(exit_code)
def sort_yaml(files: List[str]) -> None:
from ruamel.yaml import YAML, CommentedMap
yaml = YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.width = 4096 # avoid wrap lines
exit_code = 0
for f in files:
with open(f) as fr:
file_s = fr.read()
fr.seek(0)
file_d: CommentedMap = yaml.load(fr)
sorted_yaml = CommentedMap(dict(sorted(file_d.items())))
file_d.copy_attributes(sorted_yaml)
with StringIO() as s:
yaml.dump(sorted_yaml, s)
string = s.getvalue()
if string != file_s:
with open(f, 'w') as fw:
fw.write(string)
print(
f'Sorted yaml file {f}. Please take a look. sometimes the format is a bit messy'
)
exit_code = 1
sys.exit(exit_code)
def check_exist() -> None:
exit_code = 0
@@ -422,9 +389,6 @@ if __name__ == '__main__':
help='default build test rules config file',
)
_sort_yaml = action.add_parser('sort-yaml')
_sort_yaml.add_argument('files', nargs='+', help='all specified yaml files')
_check_exist = action.add_parser('check-exist')
arg = parser.parse_args()
@@ -434,9 +398,7 @@ if __name__ == '__main__':
os.path.join(os.path.dirname(__file__), '..', '..')
)
if arg.action == 'sort-yaml':
sort_yaml(arg.files)
elif arg.action == 'check-exist':
if arg.action == 'check-exist':
check_exist()
else:
check_dirs = set()