From 8f433f8de9ec3ead39e1691c45e2821fe8e3922b Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Sat, 23 Nov 2024 21:39:41 +0100 Subject: [PATCH] refactor[tool]: refactor `compile_from_zip()` (#4366) refactor `compile_from_zip()`, and also a generalized `outputs_from_compiler_data()` so the user can pass a `CompilerData` instead of `FileInput` + a bunch of settings. --- vyper/cli/compile_archive.py | 12 ++++++++---- vyper/compiler/__init__.py | 20 ++++++++++++-------- vyper/compiler/phases.py | 4 ++++ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/vyper/cli/compile_archive.py b/vyper/cli/compile_archive.py index 1b52343c1c..c6d07de9f1 100644 --- a/vyper/cli/compile_archive.py +++ b/vyper/cli/compile_archive.py @@ -8,8 +8,9 @@ import zipfile from pathlib import PurePath -from vyper.compiler import compile_from_file_input +from vyper.compiler import outputs_from_compiler_data from vyper.compiler.input_bundle import FileInput, ZipInputBundle +from vyper.compiler.phases import CompilerData from vyper.compiler.settings import Settings, merge_settings from vyper.exceptions import BadArchive @@ -19,6 +20,11 @@ class NotZipInput(Exception): def compile_from_zip(file_name, output_formats, settings, no_bytecode_metadata): + compiler_data = compiler_data_from_zip(file_name, settings, no_bytecode_metadata) + return outputs_from_compiler_data(compiler_data, output_formats) + + +def compiler_data_from_zip(file_name, settings, no_bytecode_metadata): with open(file_name, "rb") as f: bcontents = f.read() @@ -59,11 +65,9 @@ def compile_from_zip(file_name, output_formats, settings, no_bytecode_metadata): settings, archive_settings, lhs_source="command line", rhs_source="archive settings" ) - # TODO: validate integrity sum (probably in CompilerData) - return compile_from_file_input( + return CompilerData( file, input_bundle=input_bundle, - output_formats=output_formats, integrity_sum=integrity, settings=settings, no_bytecode_metadata=no_bytecode_metadata, diff --git a/vyper/compiler/__init__.py b/vyper/compiler/__init__.py index 0345c24931..d885599cec 100644 --- a/vyper/compiler/__init__.py +++ b/vyper/compiler/__init__.py @@ -99,13 +99,6 @@ def compile_from_file_input( """ settings = settings or get_global_settings() or Settings() - if output_formats is None: - output_formats = ("bytecode",) - - # make IR output the same between runs - # TODO: move this to CompilerData.__init__() - codegen.reset_names() - compiler_data = CompilerData( file_input, input_bundle, @@ -116,6 +109,17 @@ def compile_from_file_input( no_bytecode_metadata=no_bytecode_metadata, ) + return outputs_from_compiler_data(compiler_data, output_formats, exc_handler) + + +def outputs_from_compiler_data( + compiler_data: CompilerData, + output_formats: Optional[OutputFormats] = None, + exc_handler: Optional[Callable] = None, +): + if output_formats is None: + output_formats = ("bytecode",) + ret = {} with anchor_settings(compiler_data.settings): for output_format in output_formats: @@ -126,7 +130,7 @@ def compile_from_file_input( ret[output_format] = formatter(compiler_data) except Exception as exc: if exc_handler is not None: - exc_handler(str(file_input.path), exc) + exc_handler(str(compiler_data.file_input.path), exc) else: raise exc diff --git a/vyper/compiler/phases.py b/vyper/compiler/phases.py index d9b6b13b48..503281a867 100644 --- a/vyper/compiler/phases.py +++ b/vyper/compiler/phases.py @@ -4,6 +4,7 @@ from pathlib import Path, PurePath from typing import Any, Optional +import vyper.codegen.core as codegen from vyper import ast as vy_ast from vyper.ast import natspec from vyper.codegen import module @@ -304,6 +305,9 @@ def generate_ir_nodes(global_ctx: ModuleT, settings: Settings) -> tuple[IRnode, IR to generate deployment bytecode IR to generate runtime bytecode """ + # make IR output the same between runs + codegen.reset_names() + with anchor_settings(settings): ir_nodes, ir_runtime = module.generate_ir_for_module(global_ctx) if settings.optimize != OptimizationLevel.NONE: