diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 24bc284..4e5b5db 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -41,8 +41,7 @@ by reading YANG library data [RFC7895]_ from a file: 'Data model ID: 9a9b7d2d28d4d78fa42e12348346990e3fb1c1b9' .. note:: - Distribution directory *tools/python* contains the script *mkylib.py* that - can help with preparing YANG library data. + YANG library data can be created by running *yangson-mkylib*. Here is an ASCII art depicting the schema tree: diff --git a/setup.py b/setup.py index 57e1cac..cb21587 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,8 @@ author_email = "lhotka@nic.cz", url = "https://github.com/CZ-NIC/yangson", entry_points = { - "console_scripts": ["yangson=yangson.__main__:main"] + "console_scripts": ["yangson=yangson.__main__:main", + "yangson-mkylib=yangson.mkylib:main"] }, install_requires = ["PyXB"], tests_require = ["pytest"], diff --git a/tools/python/mkylib.py b/yangson/mkylib.py similarity index 87% rename from tools/python/mkylib.py rename to yangson/mkylib.py index c748cc8..2d4014d 100644 --- a/tools/python/mkylib.py +++ b/yangson/mkylib.py @@ -14,7 +14,6 @@ from yangson.statement import ModuleParser -ydir = sys.argv[1] """Name of the directory with YANG (sub)modules.""" data_kws = ["augment", "container", "leaf", "leaf-list", "list", "rpc", "notification", "identity"] """Keywords of statements that contribute nodes to the schema tree.""" @@ -23,6 +22,9 @@ submodmap = {} """Dictionary for collecting submodule data.""" +class GeneratingError(Exception): + """Error when producing ietf-yang-library data""" + def module_entry(yfile): """Add entry for one file containing YANG module text. @@ -63,7 +65,7 @@ def module_entry(yfile): modmap[(mst.argument, rev)] = rec -def main(): +def process_directory(ydir): for infile in os.listdir(ydir): if not infile.endswith(".yang"): continue @@ -82,13 +84,11 @@ def main(): try: srec = submodmap[subm] except KeyError: - print(f"Submodule {subm} not available.", file=sys.stderr) - return 1 + raise GeneratingError(f"Submodule {subm} not available.") if srev is None or srev == srec["revision"]: sen["revision"] = srec["revision"] else: - print(f"Submodule {subm} revision mismatch.", file=sys.stderr) - return 1 + raise GeneratingError(f"Submodule {subm} revision mismatch.") imp_only = imp_only or srec["import-only"] fts += srec["features"] sarr.append(sen) @@ -104,9 +104,16 @@ def main(): "module": marr } } - print(json.dumps(res, indent=2)) - return 0 + return res + + +def main(): + try: + print(json.dumps(process_directory(sys.argv[1]), indent=2)) + except GeneratingError as e: + print(e, file=sys.stderr) + sys.exit(1) if __name__ == '__main__': - sys.exit(main()) + main()