-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
y2c.py is like a compiler which translates .yaml to .cc and .h files, it does not have access to all .yaml files. to validate the dangling see-also issue, we need to do this with a "linker". in this change, validate-options.py is introduced to check if any of option name included by the see-also property is valid. Fixes: https://tracker.ceph.com/issues/51483 Signed-off-by: Kefu Chai <kchai@redhat.com>
- Loading branch information
Showing
2 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import argparse | ||
import fileinput | ||
import sys | ||
import yaml | ||
from typing import Any, Dict | ||
|
||
|
||
class ValidationError(Exception): | ||
pass | ||
|
||
|
||
OptionType = Dict[str, Any] | ||
|
||
|
||
def validate_see_also(opt: OptionType, opts: Dict[str, OptionType]) -> None: | ||
see_also = opt.get('see_also') | ||
if see_also is None: | ||
return | ||
for ref in see_also: | ||
if ref not in opts: | ||
msg = f'see_also contains "{ref}". But it is not found.' | ||
raise ValidationError(msg) | ||
|
||
|
||
def main() -> None: | ||
parser = argparse.ArgumentParser( | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||
parser.add_argument('yamls', nargs='*') | ||
opts = parser.parse_args() | ||
options = {} | ||
for yaml_file in opts.yamls: | ||
with open(yaml_file) as f: | ||
yml = yaml.load(f, yaml.SafeLoader) | ||
options.update({opt['name']: opt for opt in yml['options']}) | ||
for name, opt in options.items(): | ||
try: | ||
validate_see_also(opt, options) | ||
except ValidationError as e: | ||
raise Exception(f'failed to validate "{name}": {e}') | ||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
main() | ||
except Exception as e: | ||
print(e, file=sys.stderr) | ||
sys.exit(1) |