From 2f5de1c95dbf5c6439f281f40148e2d9fbc78e65 Mon Sep 17 00:00:00 2001 From: Guruprasad Kamath <48196632+gurukamath@users.noreply.github.com> Date: Wed, 20 Sep 2023 17:41:58 +0200 Subject: [PATCH] fix minor issue in patch tool (#837) The patch tool currently throws an error while patching forks because options.prefix is None. This commit fixes it. Also updates the tool for install with pip (ethereum-spec-patch) --- setup.cfg | 1 + src/ethereum_spec_tools/patch_tool.py | 84 +++++++++++++++------------ 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/setup.cfg b/setup.cfg index 174e14be83..fae53ca717 100644 --- a/setup.cfg +++ b/setup.cfg @@ -125,6 +125,7 @@ console_scripts = ethereum-spec-sync = ethereum_spec_tools.sync:main ethereum-spec-diff = ethereum_spec_tools.diff:main ethereum-spec-new-fork = ethereum_spec_tools.new_fork:main + ethereum-spec-patch = ethereum_spec_tools.patch_tool:main ethereum-spec-evm = ethereum_spec_tools.evm_tools:main [options.extras_require] diff --git a/src/ethereum_spec_tools/patch_tool.py b/src/ethereum_spec_tools/patch_tool.py index 99987f77ec..46f5a975c2 100644 --- a/src/ethereum_spec_tools/patch_tool.py +++ b/src/ethereum_spec_tools/patch_tool.py @@ -24,47 +24,55 @@ help="Patch the tests instead", ) -options = parser.parse_args() -source_fork_path = options.prefix + options.source_fork[0] +def main() -> None: + """ + Patch the changes from one fork into the others. + """ + options = parser.parse_args() -if options.prefix == "tests/" and "dao_fork" in options.targets: - raise Exception("dao_fork has no tests") + if not options.prefix: + options.prefix = "src/ethereum/" -git_diff = subprocess.run( - ["git", "diff", source_fork_path], capture_output=True, text=True -) - -output_lines = git_diff.stdout.splitlines(keepends=True) + source_fork_path = options.prefix + options.source_fork[0] -for target in options.targets: - patch = "" - for line in output_lines: - if line.startswith("diff --git"): - pass - elif line.startswith("index"): - pass - elif line.startswith("--- a/"): - patch += line.replace( - "--- a/" + options.prefix + options.source_fork[0], - "--- " + options.prefix + target, - ) - elif line.startswith("+++ b/"): - patch += line.replace( - "+++ b/" + options.prefix + options.source_fork[0], - "+++ " + options.prefix + target, - ) - else: - patch += line + if options.prefix == "tests/" and "dao_fork" in options.targets: + raise Exception("dao_fork has no tests") - subprocess.run( - [ - "patch", - "-p0", - "--no-backup-if-mismatch", - ], - input=patch, - text=True, - stdout=stdout, - stderr=stderr, + git_diff = subprocess.run( + ["git", "diff", source_fork_path], capture_output=True, text=True ) + + output_lines = git_diff.stdout.splitlines(keepends=True) + + for target in options.targets: + patch = "" + for line in output_lines: + if line.startswith("diff --git"): + pass + elif line.startswith("index"): + pass + elif line.startswith("--- a/"): + patch += line.replace( + "--- a/" + options.prefix + options.source_fork[0], + "--- " + options.prefix + target, + ) + elif line.startswith("+++ b/"): + patch += line.replace( + "+++ b/" + options.prefix + options.source_fork[0], + "+++ " + options.prefix + target, + ) + else: + patch += line + + subprocess.run( + [ + "patch", + "-p0", + "--no-backup-if-mismatch", + ], + input=patch, + text=True, + stdout=stdout, + stderr=stderr, + )