Skip to content

Commit 49dbb5e

Browse files
committed
build: normalize generated patch hunk counts
1 parent 700cb6b commit 49dbb5e

1 file changed

Lines changed: 65 additions & 4 deletions

File tree

scripts/patch_tfjava.py

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import difflib
4+
import re
45
import sys
56
from pathlib import Path
67

@@ -1337,8 +1338,8 @@ def patch_workspace(path: Path) -> None:
13371338
ANDROID_PORTABLE_LIB_SHIM_PATCH = """--- a/tensorflow/core/BUILD
13381339
+++ b/tensorflow/core/BUILD
13391340
@@ -1378,7 +1378,7 @@
1340-
# --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
1341-
cc_library(
1341+
# --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
1342+
cc_library(
13421343
name = "portable_tensorflow_lib_lite",
13431344
- srcs = if_mobile([":mobile_srcs"]),
13441345
+ srcs = if_ios([":mobile_srcs"]),
@@ -1409,11 +1410,70 @@ def patch_workspace(path: Path) -> None:
14091410
+ "@com_google_protobuf//:protobuf",
14101411
+ ],
14111412
+ }),
1412-
alwayslink = 1,
1413-
)
1413+
alwayslink = 1,
1414+
)
14141415
"""
14151416

14161417

1418+
HUNK_HEADER_RE = re.compile(
1419+
r"^@@ -(?P<old_start>\d+)(?:,(?P<old_count>\d+))? "
1420+
r"\+(?P<new_start>\d+)(?:,(?P<new_count>\d+))? @@(?P<section>.*)$"
1421+
)
1422+
1423+
1424+
def normalize_unified_diff_hunk_counts(text: str) -> str:
1425+
lines = text.splitlines(keepends=True)
1426+
normalized: list[str] = []
1427+
i = 0
1428+
while i < len(lines):
1429+
line = lines[i]
1430+
match = HUNK_HEADER_RE.match(line.rstrip("\r\n"))
1431+
if not match:
1432+
normalized.append(line)
1433+
i += 1
1434+
continue
1435+
1436+
old_count = 0
1437+
new_count = 0
1438+
j = i + 1
1439+
while j < len(lines):
1440+
body_line = lines[j]
1441+
if body_line.startswith(("--- ", "+++ ", "@@ ")):
1442+
break
1443+
if body_line.startswith("diff --git "):
1444+
break
1445+
1446+
prefix = body_line[:1]
1447+
if prefix == " ":
1448+
old_count += 1
1449+
new_count += 1
1450+
elif prefix == "-":
1451+
old_count += 1
1452+
elif prefix == "+":
1453+
new_count += 1
1454+
elif prefix == "\\":
1455+
pass
1456+
else:
1457+
break
1458+
j += 1
1459+
1460+
newline = "\r\n" if line.endswith("\r\n") else "\n"
1461+
normalized.append(
1462+
"@@ -{old_start},{old_count} +{new_start},{new_count} @@{section}{newline}".format(
1463+
old_start=match.group("old_start"),
1464+
old_count=old_count,
1465+
new_start=match.group("new_start"),
1466+
new_count=new_count,
1467+
section=match.group("section"),
1468+
newline=newline,
1469+
)
1470+
)
1471+
normalized.extend(lines[i + 1 : j])
1472+
i = j
1473+
1474+
return "".join(normalized)
1475+
1476+
14171477
def write_tensorflow_android_absl_patch(path: Path) -> None:
14181478
path.parent.mkdir(parents=True, exist_ok=True)
14191479
absl_patch_text = (
@@ -1441,6 +1501,7 @@ def write_tensorflow_android_absl_patch(path: Path) -> None:
14411501
text += SAVED_MODEL_ANDROID_LOADER_PATCH
14421502
text += TENSORFLOW_FRAMEWORK_ANDROID_PATCH
14431503
text += ANDROID_PORTABLE_LIB_SHIM_PATCH
1504+
text = normalize_unified_diff_hunk_counts(text)
14441505
if not text.endswith("\n"):
14451506
text += "\n"
14461507
path.write_text(text, encoding="utf-8")

0 commit comments

Comments
 (0)