From 74f9f993175f48941dbc4c57f0146065aa5b0db0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9D=B4=EC=83=81=EA=B7=9C/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 5 Aug 2019 18:48:01 +0900 Subject: [PATCH] [nnpackage] Introduce nnpackager (#6145) This PR introduces nnpackager, which provides verify function only. It checks: - the existence of nnpackager root directory - the existence of MANIFEST - the validity of json format - the existence of models file Signed-off-by: Sanggyu Lee --- tools/nnpackager/nnpackager.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 tools/nnpackager/nnpackager.py diff --git a/tools/nnpackager/nnpackager.py b/tools/nnpackager/nnpackager.py new file mode 100755 index 0000000..457cf56 --- /dev/null +++ b/tools/nnpackager/nnpackager.py @@ -0,0 +1,47 @@ +#!/usr/bin/python3 + +import json +import os +from collections import OrderedDict +import sys +import argparse + + +def verify(path): + nnpackage_root_path = path + + # Check nnpackage_root existence + if not os.path.isdir(nnpackage_root_path): + print("Error: nnpackage_root {} does not exist.".format(nnpackage_root_path)) + sys.exit(-1) + + # Check MANIFEST existence + manifest_path = os.path.join(nnpackage_root_path, "metadata", "MANIFEST") + if not os.path.exists(manifest_path): + print("Error: MANIFEST {} does not exist.".format(manifest_path)) + sys.exit(-1) + + # Check MANIFEST + with open(manifest_path, "r") as f: + try: + json_dict = json.load(f, object_pairs_hook=OrderedDict) + # Check models attributes + for m in json_dict["models"]: + model_path = os.path.join(nnpackage_root_path, m) + if not os.path.exists(model_path): + print("Error: model {} does not exist.".format(model_path)) + sys.exit(-1) + print("nnpackage validation check passed.") + except ValueError: + print("MANIFEST is not valid JSON.") + except KeyError: + print("models attribute does not exist.") + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('path', type=str, help='the path to nnpackage') + parser.add_argument( + '-v', '--verify', action='store_true', help="verify nnpackage (default: true)") + args = parser.parse_args() + verify(args.path) -- 2.7.4