aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools
diff options
context:
space:
mode:
authorJordan R Abrahams-Whitehead <ajordanr@google.com>2022-06-09 23:08:19 +0000
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-06-10 20:39:29 +0000
commit41e93526f0117ee883353c800f063239fff92ad4 (patch)
tree9407a893701abfa89ab78df257ee614de05f1ba8 /llvm_tools
parentaa99b9ab4093f11c12b0c1ebdae57144f3dbb632 (diff)
downloadtoolchain-utils-41e93526f0117ee883353c800f063239fff92ad4.tar.gz
llvm_tools: Add more utils to patch_utils.py
This adds the json_to_patch_entries function which abstracts away a common pattern to convert the PATCHES.json file contents into patch_entries eagerly. Fixes some typing issues too that have presented themselves in later CLs. BUG=None TEST=./patch_utils_unittest.py Change-Id: I5ecb1aedbbfb4f04176c021ff976da417319c17e Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/3699193 Reviewed-by: George Burgess <gbiv@chromium.org> Tested-by: Jordan Abrahams-Whitehead <ajordanr@google.com> Commit-Queue: Jordan Abrahams-Whitehead <ajordanr@google.com>
Diffstat (limited to 'llvm_tools')
-rw-r--r--llvm_tools/patch_utils.py19
1 files changed, 15 insertions, 4 deletions
diff --git a/llvm_tools/patch_utils.py b/llvm_tools/patch_utils.py
index 9117ba72..6fd75c3a 100644
--- a/llvm_tools/patch_utils.py
+++ b/llvm_tools/patch_utils.py
@@ -7,12 +7,12 @@
import collections
import contextlib
import dataclasses
-import io
+import json
from pathlib import Path
import re
import subprocess
import sys
-from typing import Any, Dict, List, Optional, Union
+from typing import Any, Dict, IO, List, Optional, Union
CHECKED_FILE_RE = re.compile(r'^checking file\s+(.*)$')
@@ -67,7 +67,7 @@ class Hunk:
patch_hunk_lineno_end: Optional[int]
-def parse_patch_stream(patch_stream: io.TextIOBase) -> Dict[str, List[Hunk]]:
+def parse_patch_stream(patch_stream: IO[str]) -> Dict[str, List[Hunk]]:
"""Parse a patch file-like into Hunks.
Args:
@@ -144,10 +144,11 @@ class PatchResult:
class PatchEntry:
"""Object mapping of an entry of PATCHES.json."""
workdir: Path
+ """Storage location for the patches."""
metadata: Dict[str, Any]
platforms: List[str]
rel_patch_path: str
- version_range: Dict[str, int]
+ version_range: Dict[str, Optional[int]]
_parsed_hunks = None
def __post_init__(self):
@@ -245,3 +246,13 @@ class PatchEntry:
def title(self) -> str:
return self.metadata['title']
+
+
+def json_to_patch_entries(workdir: Path, json_fd: IO[str]) -> List[PatchEntry]:
+ """Convert a json IO object to List[PatchEntry].
+
+ Examples:
+ >>> f = open('PATCHES.json')
+ >>> patch_entries = json_to_patch_entries(Path(), f)
+ """
+ return [PatchEntry.from_dict(workdir, d) for d in json.load(json_fd)]