Skip to content

Commit 73c6f03

Browse files
author
Derek Hower
committed
Make checks optional for yaml_resolver
1 parent 4d0c6a6 commit 73c6f03

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

lib/test/test_yaml_loader.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def resolve_yaml(yaml)
2121

2222
stdout, stderr, status =
2323
Dir.chdir($root) do
24-
Open3.capture3("/bin/bash -c \"source #{$root}/.home/.venv/bin/activate && #{$root}/.home/.venv/bin/python3 #{$root}/lib/yaml_resolver.py resolve --no-progress #{arch_dir} #{resolved_dir}\"")
24+
Open3.capture3("/bin/bash -c \"source #{$root}/.home/.venv/bin/activate && #{$root}/.home/.venv/bin/python3 #{$root}/lib/yaml_resolver.py resolve --no-progress --no-checks #{arch_dir} #{resolved_dir}\"")
2525
end
2626
# puts stdout
2727
# puts stderr
@@ -48,7 +48,7 @@ def resolve_multi_yaml(*yamls)
4848
File.write(test_dir / "test#{i + 1}.yaml", yaml)
4949
end
5050

51-
system "/bin/bash -c \"source #{$root}/.home/.venv/bin/activate && #{$root}/.home/.venv/bin/python3 #{$root}/lib/yaml_resolver.py resolve #{arch_dir} #{resolved_dir}\""
51+
system "/bin/bash -c \"source #{$root}/.home/.venv/bin/activate && #{$root}/.home/.venv/bin/python3 #{$root}/lib/yaml_resolver.py resolve --no-checks #{arch_dir} #{resolved_dir}\""
5252
# `source #{$root}/.home/.venv/bin/activate && python3 #{$root}/lib/yaml_resolver.py resolve #{arch_dir} #{resolved_dir}`
5353

5454
if $CHILD_STATUS == 0

lib/yaml_resolver.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def dig(obj : dict, *keys):
190190
return None
191191

192192
resolved_objs = {}
193-
def resolve(rel_path : str | Path, arch_root : str | Path) -> dict:
193+
def resolve(rel_path : str | Path, arch_root : str | Path, do_checks: bool) -> dict:
194194
"""Resolve the file at arch_root/rel_path by expanding operators and applying defaults
195195
196196
Parameters
@@ -209,22 +209,22 @@ def resolve(rel_path : str | Path, arch_root : str | Path) -> dict:
209209
return resolved_objs[str(rel_path)]
210210
else:
211211
unresolved_arch_data = read_yaml(os.path.join(arch_root, rel_path))
212-
if not "name" in unresolved_arch_data:
212+
if do_checks and (not "name" in unresolved_arch_data):
213213
print(f"ERROR: Missing 'name' key in {arch_root}/{rel_path}", file=sys.stderr)
214214
exit(1)
215215
fn_name = Path(rel_path).stem
216-
if fn_name != unresolved_arch_data["name"]:
216+
if do_checks and (fn_name != unresolved_arch_data["name"]):
217217
print(f"ERROR: 'name' key ({unresolved_arch_data["name"]}) must match filename ({fn_name} in {arch_root}/{rel_path}", file=sys.stderr)
218218
exit(1)
219-
resolved_objs[str(rel_path)] = _resolve(unresolved_arch_data, [], rel_path, unresolved_arch_data, arch_root)
219+
resolved_objs[str(rel_path)] = _resolve(unresolved_arch_data, [], rel_path, unresolved_arch_data, arch_root, do_checks)
220220
return resolved_objs[str(rel_path)]
221221

222-
def _resolve(obj, obj_path, obj_file_path, doc_obj, arch_root):
222+
def _resolve(obj, obj_path, obj_file_path, doc_obj, arch_root, do_checks):
223223
if not (isinstance(obj, list) or isinstance(obj, dict)):
224224
return obj
225225

226226
if isinstance(obj, list):
227-
obj = list(map(lambda o: _resolve(o, obj_path, obj_file_path, doc_obj, arch_root), obj))
227+
obj = list(map(lambda o: _resolve(o, obj_path, obj_file_path, doc_obj, arch_root, do_checks), obj))
228228
return obj
229229

230230
if "$inherits" in obj:
@@ -245,16 +245,16 @@ def _resolve(obj, obj_path, obj_file_path, doc_obj, arch_root):
245245
ref_obj = dig(doc_obj, *ref_obj_path)
246246
if ref_obj == None:
247247
raise ValueError(f"{ref_obj_path} cannot be found in #{doc_obj}")
248-
ref_obj = _resolve(ref_obj, ref_obj_path, ref_file_path, doc_obj, arch_root)
248+
ref_obj = _resolve(ref_obj, ref_obj_path, ref_file_path, doc_obj, arch_root, do_checks)
249249
else:
250250
# this is a reference to another doc
251251
if not os.path.exists(os.path.join(arch_root, ref_file_path)):
252252
raise ValueError(f"{ref_file_path} does not exist in {arch_root}/")
253253

254-
ref_doc_obj = resolve(ref_file_path, arch_root)
254+
ref_doc_obj = resolve(ref_file_path, arch_root, do_checks)
255255
ref_obj = dig(ref_doc_obj, *ref_obj_path)
256256

257-
ref_obj = _resolve(ref_obj, ref_obj_path, ref_file_path, ref_doc_obj, arch_root)
257+
ref_obj = _resolve(ref_obj, ref_obj_path, ref_file_path, ref_doc_obj, arch_root, do_checks)
258258

259259
for key in ref_obj:
260260
if key == "$parent_of" or key == "$child_of":
@@ -288,12 +288,12 @@ def _resolve(obj, obj_path, obj_file_path, doc_obj, arch_root):
288288
if not key in obj:
289289
final_obj[key] = parent_obj[key]
290290
elif not key in parent_obj:
291-
final_obj[key] = _resolve(obj[key], obj_path + [key], obj_file_path, doc_obj, arch_root)
291+
final_obj[key] = _resolve(obj[key], obj_path + [key], obj_file_path, doc_obj, arch_root, do_checks)
292292
else:
293293
if isinstance(parent_obj[key], dict):
294294
final_obj[key] = merge(yaml.load('{}'), parent_obj[key], obj[key], strategy=Strategy.REPLACE)
295295
else:
296-
final_obj[key] = _resolve(obj[key], obj_path + [key], obj_file_path, doc_obj, arch_root)
296+
final_obj[key] = _resolve(obj[key], obj_path + [key], obj_file_path, doc_obj, arch_root, do_checks)
297297

298298
if "$remove" in final_obj:
299299
if isinstance(final_obj["$remove"], list):
@@ -308,7 +308,7 @@ def _resolve(obj, obj_path, obj_file_path, doc_obj, arch_root):
308308
return final_obj
309309
else:
310310
for key in obj:
311-
obj[key] = _resolve(obj[key], obj_path + [key], obj_file_path, doc_obj, arch_root)
311+
obj[key] = _resolve(obj[key], obj_path + [key], obj_file_path, doc_obj, arch_root, do_checks)
312312

313313
if "$remove" in obj:
314314
if isinstance(obj["$remove"], list):
@@ -394,7 +394,7 @@ def _get_schema(uri):
394394
return schemas[rel_path]
395395

396396

397-
def resolve_file(rel_path : str | Path, arch_dir: str | Path, resolved_dir: str | Path):
397+
def resolve_file(rel_path : str | Path, arch_dir: str | Path, resolved_dir: str | Path, do_checks: bool):
398398
"""Read object at arch_dir/rel_path, resolve it, and write it as YAML to resolved_dir/rel_path
399399
400400
Parameters
@@ -414,10 +414,10 @@ def resolve_file(rel_path : str | Path, arch_dir: str | Path, resolved_dir: str
414414
elif not os.path.exists(resolved_path) or (os.path.getmtime(arch_path) > os.path.getmtime(resolved_path)) or (os.path.getmtime(__file__) > os.path.getmtime(resolved_path)):
415415
if os.path.exists(resolved_path):
416416
os.remove(resolved_path)
417-
resolved_obj = resolve(rel_path, args.arch_dir)
417+
resolved_obj = resolve(rel_path, args.arch_dir, do_checks)
418418
resolved_obj["$source"] = os.path.join(args.arch_dir, rel_path)
419419

420-
if "$schema" in resolved_obj:
420+
if do_checks and ("$schema" in resolved_obj):
421421
schema = _get_schema(resolved_obj["$schema"])
422422
try:
423423
schema.validate(instance=resolved_obj)
@@ -443,6 +443,7 @@ def resolve_file(rel_path : str | Path, arch_dir: str | Path, resolved_dir: str
443443
all_parser.add_argument("arch_dir", type=str, help="Unresolved architecture (input) directory")
444444
all_parser.add_argument("resolved_dir", type=str, help="Resolved architecture (output) directory")
445445
all_parser.add_argument("--no-progress", action="store_true", help="Don't display progress bar")
446+
all_parser.add_argument("--no-checks", action="store_true", help="Don't verify schema")
446447

447448
args = cmdparser.parse_args()
448449

@@ -473,6 +474,6 @@ def resolve_file(rel_path : str | Path, arch_dir: str | Path, resolved_dir: str
473474
for arch_path in iter:
474475
resolved_arch_path = f"{UDB_ROOT}/{args.resolved_dir}/{arch_path}" if not os.path.isabs(args.resolved_dir) else f"{args.resolved_dir}/{arch_path}"
475476
os.makedirs(os.path.dirname(resolved_arch_path), exist_ok=True)
476-
resolve_file(arch_path, args.arch_dir, args.resolved_dir)
477+
resolve_file(arch_path, args.arch_dir, args.resolved_dir, not args.no_checks)
477478

478479
print(f"[INFO] Resolved architecture files written to {args.resolved_dir}")

0 commit comments

Comments
 (0)