def create_json(subargs): # Read the template JSON file filepath = os.path.dirname(os.path.realpath(__file__)) template_path = "schema/afu_template.json" afu = AFU() if (zipfile.is_zipfile(filepath)): archive = zipfile.ZipFile(filepath, 'r') afu.load_afu_desc_file_hdl(archive.open(template_path, "r")) else: afu.load_afu_desc_file_hdl( open(os.path.join(filepath, template_path), "r")) accel = afu.afu_json['afu-image']['accelerator-clusters'][0] accel['name'] = subargs.name # Top-level interface specified? if (subargs.top_ifc): afu.update_afu_json( ['afu-image/afu-top-interface/name:' + subargs.top_ifc]) # Either set the specified UUID or pick one if (subargs.uuid): accel['accelerator-type-uuid'] = subargs.uuid else: accel['accelerator-type-uuid'] = str(uuid.uuid1()) # The output file name is either the AFU name or a specified file path json_path = subargs.name + '.json' if (subargs.afu_json): json_path = subargs.afu_json print("Writing {0}".format(json_path)) with open(json_path, 'w') as a: a.write(afu.dumps() + '\n')
def create_json(subargs): # Read the template JSON file filepath = os.path.dirname(os.path.realpath(__file__)) template_path = "schema/afu_template.json" afu = AFU() if (zipfile.is_zipfile(filepath)): archive = zipfile.ZipFile(filepath, 'r') afu.load_afu_desc_file_hdl(archive.open(template_path, "r")) else: afu.load_afu_desc_file_hdl(open(os.path.join(filepath, template_path), "r")) accel = afu.afu_json['afu-image']['accelerator-clusters'][0] accel['name'] = subargs.name # Top-level interface specified? if (subargs.top_ifc): afu.update_afu_json(['afu-image/afu-top-interface/class:' + subargs.top_ifc]) # Either set the specified UUID or pick one if (subargs.uuid): accel['accelerator-type-uuid'] = subargs.uuid else: accel['accelerator-type-uuid'] = str(uuid.uuid1()) # The output file name is either the AFU name or a specified file path json_path = subargs.name + '.json' if (subargs.afu_json): json_path = subargs.afu_json print("Writing {0}".format(json_path)) with open(json_path, 'w') as a: a.write(afu.dumps() + '\n')
def run_packager(): parser = argparse.ArgumentParser(usage=USAGE, add_help=False) parser.add_argument("cmd", nargs="?") parser.add_argument("remain_args", nargs=argparse.REMAINDER) args = parser.parse_args(sys.argv[1:]) cmd_description = "{0} {1}".format(PACKAGER_EXEC, args.cmd) subparser = argparse.ArgumentParser(description=cmd_description) subparser._optionals.title = 'Options' if args.cmd == "help" or not args.cmd: print(USAGE) elif args.cmd == "version": if VERSION.startswith("@"): try: devnull = open(os.devnull, 'w') repo = subprocess.check_output('git remote -v', shell=True, stderr=devnull) version = (subprocess.check_output('git describe --tags', shell=True, stderr=devnull).split()[0] if "opae-sdk" in repo else "UNKNOWN REPO") except subprocess.CalledProcessError: version = "UNKNOWN" else: version = VERSION print("{0}: version {1}".format(DESCRIPTION, version)) elif args.cmd == "create-gbs": subparser.usage = "\n" + cmd_description + \ " --rbf=<RBF_PATH> --afu-json=<AFU_JSON_PATH>"\ " --gbs=<NAME_FOR_GBS> --set-value <key>:<value>\n" subparser.add_argument('--rbf', required=True, help='RBF file (REQUIRED)') subparser.add_argument('--afu-json', required=False, help='AFU JSON file that contains metadata') subparser.add_argument('--no-metadata', default=False, action='store_true', help='Empty metadata section will be appended') subparser.add_argument('--gbs', required=False, help='Output location for gbs file. ' 'Default is <rbf_name>.gbs') subparser.add_argument('--set-value', required=False, nargs='*', help='set values for keys in JSON metadata as ' '<key>:<value>. Can be followed by more than ' 'one <key>:<value> pairs.') subargs = subparser.parse_args(args.remain_args) afu = AFU(subargs.afu_json) gbs_file = afu.create_gbs(subargs.rbf, subargs.gbs, subargs.set_value) print("Wrote {0}".format(gbs_file)) elif args.cmd == "modify-gbs": subparser.usage = "\n" + cmd_description + \ " --input-gbs=<PATH_TO_GBS_TO_BE_MODIFIED>"\ " --output-gbs=<NAME_FOR_NEW_GBS> --set-value <key>:<value>\n" subparser.add_argument('--input-gbs', required=True, help='Path to input gbs file') subparser.add_argument('--output-gbs', required=False, help='Path to output gbs file. Will replace ' 'original file if not provided') subparser.add_argument('--set-value', required=True, nargs='*', help='set values for keys in JSON metadata as ' '<key>:<value>. Can be followed by more than ' 'one <key>:<value> pairs.') subargs = subparser.parse_args(args.remain_args) gbs = GBS(subargs.input_gbs) afu = AFU.create_afu_from_gbs(gbs) afu.update_afu_json(subargs.set_value) afu.validate(packaging=True) gbs.update_gbs_info(afu.afu_json) gbs_file = gbs.write_gbs(subargs.output_gbs) print("Wrote {0}".format(gbs_file)) elif args.cmd == "package": subparser.usage = "\n" + cmd_description + \ " --afu-json=<AFU_JSON_PATH> --rbf=<RBF_PATH>"\ " --out=<NAME_OF_PACKAGE>\n" subparser.usage += cmd_description + \ " --afu-json=<AFU_JSON_PATH> --rbf=<RBF_PATH> --sw-dir=<SW_DIR>"\ " --doc-dir=<DOC_DIR>" subparser.add_argument('--afu-json', required=True, help='AFU JSON file that contains metadata ' '(REQUIRED)') subparser.add_argument('--rbf', required=True, help='RBF file (REQUIRED)') subparser.add_argument('--sw-dir', required=False, help='Location of software files to include') subparser.add_argument('--doc-dir', required=False, help='Location of documentation files to ' 'include') subparser.add_argument('--out', required=False, default="afu", help='Used to specify name of package') subargs = subparser.parse_args(args.remain_args) afu = AFU(subargs.afu_json) afu.package(subargs.rbf, subargs.sw_dir, subargs.doc_dir, subargs.out) print("Wrote {0}.zip".format(subargs.out)) elif args.cmd == "gbs-info": subparser.usage = "\n" + cmd_description + " --gbs=<GBS_PATH>" subparser.add_argument('--gbs', required=True, help='Path to GBS file') subargs = subparser.parse_args(args.remain_args) gbs = GBS(subargs.gbs) gbs.print_gbs_info() elif args.cmd == "get-rbf": subparser.usage = "\n" + cmd_description + \ "--gbs=<GBS_PATH> --rbf=<NAME_FOR_RBF>" subparser.add_argument('--gbs', required=True, help='Path to GBS file from which rbf is to be ' 'extracted') subparser.add_argument('--rbf', required=False, help='Output location for rbf file. Default is ' '<gbs_name>.rbf') subargs = subparser.parse_args(args.remain_args) gbs = GBS(subargs.gbs) rbf_file = gbs.write_rbf(subargs.rbf) print("Wrote {0}".format(rbf_file)) else: raise Exception("{0} is not a command for {1}!".format( args.cmd, DESCRIPTION))