def build_cmd(files, include) -> None: project_path = Path.cwd() clvm_files = [] for glob in files: for path in Path(project_path).rglob(glob): if path.is_dir(): for clvm_path in Path(path).rglob('*.cl[vs][mp]'): clvm_files.append(clvm_path) else: clvm_files.append(path) for filename in clvm_files: hex_file_name = (filename.name + ".hex") full_hex_file_name = Path(filename.parent).joinpath(hex_file_name) if not (full_hex_file_name.exists() and full_hex_file_name.stat().st_mtime > filename.stat().st_mtime): outfile = str(filename) + ".hex" try: print("Beginning compilation of " + filename.name + "...") compile_clvm(str(filename), outfile, search_paths=append_include(include)) print("...Compilation finished") except Exception as e: print("Couldn't build " + filename.name + ": " + str(e)) pass
def load_serialized_clvm(clvm_filename, package_or_requirement=__name__) -> SerializedProgram: """ This function takes a .clvm file in the given package and compiles it to a .clvm.hex file if the .hex file is missing or older than the .clvm file, then returns the contents of the .hex file as a `Program`. clvm_filename: file name package_or_requirement: usually `__name__` if the clvm file is in the same package """ hex_filename = f"{clvm_filename}.hex" try: if pkg_resources.resource_exists(package_or_requirement, clvm_filename): full_path = pathlib.Path( pkg_resources.resource_filename(package_or_requirement, clvm_filename)) output = full_path.parent / hex_filename compile_clvm(full_path, output, search_paths=[full_path.parent]) except NotImplementedError: # pyinstaller doesn't support `pkg_resources.resource_exists` # so we just fall through to loading the hex clvm pass clvm_hex = pkg_resources.resource_string(package_or_requirement, hex_filename).decode("utf8") clvm_blob = bytes.fromhex(clvm_hex) return SerializedProgram.from_bytes(clvm_blob)
def test_recompilation_matches(self): self.maxDiff = None for f in wallet_program_files: f = Path(f) compile_clvm(f, path_with_ext(f, ".recompiled"), search_paths=[f.parent]) orig_hex = path_with_ext(f, ".hex").read_text().strip() new_hex = path_with_ext(f, ".recompiled").read_text().strip() self.assertEqual(orig_hex, new_hex, msg=f"Compilation of {f} does not match {f}.hex") pass
def dev_util(args=sys.argv): cmd = args[1].lower() project_path = Path.cwd() script_root = Path(__file__).parent #Initialize a new project if cmd == "init": project_path_lib = Path(project_path).joinpath("lib") project_path_std = Path(project_path_lib).joinpath("std") if not project_path_lib.exists(): os.mkdir(project_path_lib) if project_path_std.exists(): shutil.rmtree(project_path_std) shutil.copytree(Path(script_root).joinpath("std"), project_path_std) hello_world_py = project_path_std.joinpath("examples", "helloworld.py") hello_world_clvm = project_path_std.joinpath("clvm", "helloworld.clvm") shutil.copy(hello_world_py, project_path) shutil.copy(hello_world_clvm, project_path) print("Run 'chialisp build' and then 'py helloworld.py'") #Build all the clvm in the current directory if cmd == "build": clvm_files = list(Path(project_path).rglob("*.[cC][lL][vV][mM]")) already_compiled = [] #Adjust for building only one file if (cmd == "build") & (len(args) > 2): clvm_files = list(filter(lambda e: e.name in args, clvm_files)) all_hex_files = list(Path(project_path).rglob("*.[hH][eE][xX]")) staying_hex_files = list( filter(lambda e: args[2] not in e.name, all_hex_files)) for hex_file in staying_hex_files: already_compiled.append(hex_file) for filename in clvm_files: filehash = "" with open(filename, "rb") as afile: buf = afile.read() afile.close() filehash = hashlib.sha256(buf).hexdigest() hex_file_name = (filename.name + "." + filehash + ".hex") full_hex_file_name = Path(filename.parent).joinpath(hex_file_name) already_compiled.append(full_hex_file_name) if not full_hex_file_name.exists(): outfile = str(filename) + "." + filehash + ".hex" try: print("Beginning compilation of " + filename.name + "...") compile_clvm(str(filename), outfile) print("...Compilation finished") except Exception as e: print("Couldn't build " + filename + ": " + e) pass #clean up old hex files garbage_files = list(Path(project_path).rglob("*.[hH][eE][xX]")) garbage_files = list( filter(lambda e: e not in already_compiled, garbage_files)) for file in garbage_files: file.unlink()
def test_compile_clvm(): with TemporaryDirectory() as include_dir: with TemporaryDirectory() as source_dir: with open(f"{include_dir}/include.clvm", "w") as f: f.write(INCLUDE_CODE) main_path = f"{source_dir}/main.clvm" main_output = f"{source_dir}/main.hex" with open(main_path, "w") as f: f.write(MAIN_CODE) output = clvmc.compile_clvm(main_path, main_output, search_paths=[include_dir]) t = open(output).read() assert t == f"{EXPECTED_HEX_OUTPUT}\n"
def load_clvm(clvm_filename, package_or_requirement=__name__): """ This function takes a .clvm file in the given package and compiles it to a .clvm.hex file if the .hex file is missing or older than the .clvm file, then returns the contents of the .hex file as a `Program`. clvm_filename: file name package_or_requirement: usually `__name__` if the clvm file is in the same package """ hex_filename = f"{clvm_filename}.hex" if pkg_resources.resource_exists(package_or_requirement, clvm_filename): full_path = pathlib.Path( pkg_resources.resource_filename(package_or_requirement, clvm_filename)) output = full_path.parent / hex_filename compile_clvm(full_path, output) clvm_hex = pkg_resources.resource_string(package_or_requirement, hex_filename).decode("utf8") clvm_blob = bytes.fromhex(clvm_hex) return Program.from_bytes(clvm_blob)
def run(self): file_list = self.distribution.clvm_extensions for _ in file_list: log.info("build_clvm on %s" % _) target = "%s.hex" % _ compile_clvm(_, target)