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 json_info(entries, subargs): afu = AFU(subargs.afu_json) # C header if (subargs.c_hdr): print("Writing {0}".format(subargs.c_hdr)) with open(subargs.c_hdr, 'w') as p: emit_header_comment(p, subargs.afu_json) p.write('#ifndef __AFU_JSON_INFO__\n') p.write('#define __AFU_JSON_INFO__\n\n') for k in sorted(entries.keys()): v = entries[k] if (k == 'afu_accel_uuid'): v = '"' + v.upper() + '"' p.write('#define {0} {1}\n'.format(k.upper(), v)) p.write('\n#endif // __AFU_JSON_INFO__\n') # Verilog header if (subargs.verilog_hdr): print("Writing {0}".format(subargs.verilog_hdr)) with open(subargs.verilog_hdr, 'w') as p: emit_header_comment(p, subargs.afu_json) p.write('`ifndef __AFU_JSON_INFO__\n') p.write('`define __AFU_JSON_INFO__\n\n') for k in sorted(entries.keys()): v = entries[k] if (k == 'afu_accel_uuid'): v = "128'h" + entries[k].replace('-', '_') p.write('`define {0} {1}\n'.format(k.upper(), v)) p.write('\n`endif // __AFU_JSON_INFO__\n')
def flatten_json(subargs): afu = AFU(subargs.afu_json) entries = dict() emit_types = (int, bool, str, unicode, float) # Don't emit some keys. For example, user clock frequency may not be # known at compile time, so avoid emitting potentially false information. skip_keys = ['clock-frequency-low', 'clock-frequency-high'] # Flattan all entries in afu-image that are of type emit_types image = afu.afu_json['afu-image'] for k in sorted(image.keys()): if (isinstance(image[k], emit_types) and k not in skip_keys): tag = str(k).replace('-', '_') v = image[k] # Does it look like a number? if (not re.match('^[0-9.]+$', str(v))): v = '"' + str(v) + '"' entries['afu_image_' + tag] = v # Some special names, taken from other levels accel = image['accelerator-clusters'][0] entries['afu_accel_name'] = '"' + accel['name'] + '"' entries['afu_accel_uuid'] = accel['accelerator-type-uuid'] try: # May not be present. (Will become required eventually.) entries['afu_top_ifc'] = '"' + \ afu.afu_json['afu-image']['afu-top-interface']['class'] + '"' except Exception: None return entries
def json_info(subargs): afu = AFU(subargs.afu_json) entries = dict() accel = afu.afu_json['afu-image']['accelerator-clusters'][0] entries['afu_image_name'] = accel['name'] entries['afu_image_uuid'] = accel['accelerator-type-uuid'] try: # May not be present. (Will become required eventually.) entries['afu_top_ifc'] = \ afu.afu_json['afu-image']['afu-top-interface']['name'] except Exception: None # C header if (subargs.c_hdr): print("Writing {0}".format(subargs.c_hdr)) with open(subargs.c_hdr, 'w') as p: emit_header_comment(p, subargs.afu_json) p.write('#ifndef __AFU_JSON_INFO__\n') p.write('#define __AFU_JSON_INFO__\n\n') for k in sorted(entries.keys()): v = entries[k] if (k == 'afu_image_uuid'): v = v.upper() p.write('#define {0} "{1}"\n'.format(k.upper(), v)) p.write('\n#endif // __AFU_JSON_INFO__\n') # Verilog header if (subargs.verilog_hdr): print("Writing {0}".format(subargs.verilog_hdr)) with open(subargs.verilog_hdr, 'w') as p: emit_header_comment(p, subargs.afu_json) p.write('`ifndef __AFU_JSON_INFO__\n') p.write('`define __AFU_JSON_INFO__\n\n') for k in sorted(entries.keys()): v = '"{0}"'.format(entries[k]) if (k == 'afu_image_uuid'): v = "128'h" + entries[k].replace('-', '_') p.write('`define {0} {1}\n'.format(k.upper(), v)) p.write('\n`endif // __AFU_JSON_INFO__\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))
def testEmptyAFU(self): try: AFU(None) except Exception: self.fail( "AFU() should not throw exception if no AFU JSON is specified")
def testGoodAFU(self): afu = AFU(filepath + "/test_data/good_afu_test.json") self.assertTrue(afu.validate())