diff options
author | George Burgess IV <gbiv@google.com> | 2024-04-03 09:52:31 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2024-04-08 15:01:46 +0000 |
commit | 4db0e3155bf85e5c4f977105e870b3d76eb70972 (patch) | |
tree | 2bc27981643100bb67329348a10d28435efc025e | |
parent | 3f4382d0af07b86c5caf3f0e119c6473509037d8 (diff) | |
download | toolchain-utils-4db0e3155bf85e5c4f977105e870b3d76eb70972.tar.gz |
llvm_tools: fix `cros lint` & mypy issues in patch_utils
`mypy` doesn't infer the right types for some of these lists, and
_really_ dislikes multiple calls to the same Callable with different
signatures.
`cros lint` was unhappy with `==` to compare fns, and unmentioned things
in docstrings.
BUG=b:332589934
TEST=repo upload
Change-Id: Ic6929e8524797b2fdc96db65a94c0f98ea9dd8bb
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/5420774
Commit-Queue: George Burgess <gbiv@chromium.org>
Reviewed-by: Jordan Abrahams-Whitehead <ajordanr@google.com>
Tested-by: George Burgess <gbiv@chromium.org>
-rw-r--r-- | llvm_tools/patch_utils.py | 23 | ||||
-rwxr-xr-x | llvm_tools/patch_utils_unittest.py | 4 |
2 files changed, 12 insertions, 15 deletions
diff --git a/llvm_tools/patch_utils.py b/llvm_tools/patch_utils.py index b21cf314..b5383ac9 100644 --- a/llvm_tools/patch_utils.py +++ b/llvm_tools/patch_utils.py @@ -256,11 +256,8 @@ class PatchEntry: f"Cannot apply: patch {abs_patch_path} is not a file" ) - if not patch_cmd: - patch_cmd = gnu_patch - - if patch_cmd == gnu_patch: - cmd = patch_cmd(root_dir, abs_patch_path) + (extra_args or []) + if not patch_cmd or patch_cmd is gnu_patch: + cmd = gnu_patch(root_dir, abs_patch_path) + (extra_args or []) else: cmd = patch_cmd(abs_patch_path) + (extra_args or []) @@ -272,7 +269,7 @@ class PatchEntry: parsed_hunks = self.parsed_hunks() failed_hunks_id_dict = parse_failed_patch_output(e.stdout) failed_hunks = {} - if patch_cmd == gnu_patch: + if patch_cmd is gnu_patch: for path, failed_hunk_ids in failed_hunks_id_dict.items(): hunks_for_file = parsed_hunks[path] failed_hunks[path] = [ @@ -291,7 +288,7 @@ class PatchEntry: self, root_dir: Path, patch_cmd: Optional[Callable] = None ) -> PatchResult: """Dry run applying a patch to a given directory.""" - extra_args = [] if patch_cmd == git_am else ["--dry-run"] + extra_args = [] if patch_cmd is git_am else ["--dry-run"] return self.apply(root_dir, patch_cmd, extra_args) def title(self) -> str: @@ -376,6 +373,7 @@ def apply_all_from_json( svn_version: LLVM Subversion revision to patch. llvm_src_dir: llvm-project root-level source directory to patch. patches_json_fp: Filepath to the PATCHES.json file. + patch_cmd: The function to use when actually applying the patch. continue_on_failure: Skip any patches which failed to apply, rather than throw an Exception. """ @@ -565,6 +563,7 @@ def update_version_ranges_with_entries( svn_version: LLVM revision number. llvm_src_dir: llvm-project directory path. patch_entries: PatchEntry objects to modify. + patch_cmd: The function to use when actually applying the patch. Returns: Tuple of (modified entries, applied patches) @@ -641,20 +640,18 @@ def remove_old_patches( def git_am(patch_path: Path) -> List[Union[str, Path]]: - cmd = ["git", "am", "--3way", str(patch_path)] - return cmd + return ["git", "am", "--3way", patch_path] def gnu_patch(root_dir: Path, patch_path: Path) -> List[Union[str, Path]]: - cmd = [ + return [ "patch", "-d", - str(root_dir.absolute()), + root_dir.absolute(), "-f", "-E", "-p1", "--no-backup-if-mismatch", "-i", - str(patch_path), + patch_path, ] - return cmd diff --git a/llvm_tools/patch_utils_unittest.py b/llvm_tools/patch_utils_unittest.py index 26a211ee..362a8dfd 100755 --- a/llvm_tools/patch_utils_unittest.py +++ b/llvm_tools/patch_utils_unittest.py @@ -170,7 +170,7 @@ a patch_dir, TestPatchUtils._default_json_dict() ) - """Make a deepcopy of the case for testing commit patch option.""" + # Make a deepcopy of the case for testing commit patch option. e1 = copy.deepcopy(e) with mock.patch("pathlib.Path.is_file", return_value=True): @@ -178,7 +178,7 @@ a result = e.apply(src_dir) self.assertTrue(result.succeeded) - """Test that commit patch option works.""" + # Test that commit patch option works. with mock.patch("pathlib.Path.is_file", return_value=True): with mock.patch("subprocess.run", mock.MagicMock()): result1 = e1.apply(src_dir, pu.git_am) |