|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import difflib |
| 4 | +import re |
4 | 5 | import sys |
5 | 6 | from pathlib import Path |
6 | 7 |
|
@@ -1337,8 +1338,8 @@ def patch_workspace(path: Path) -> None: |
1337 | 1338 | ANDROID_PORTABLE_LIB_SHIM_PATCH = """--- a/tensorflow/core/BUILD |
1338 | 1339 | +++ b/tensorflow/core/BUILD |
1339 | 1340 | @@ -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( |
1342 | 1343 | name = "portable_tensorflow_lib_lite", |
1343 | 1344 | - srcs = if_mobile([":mobile_srcs"]), |
1344 | 1345 | + srcs = if_ios([":mobile_srcs"]), |
@@ -1409,11 +1410,70 @@ def patch_workspace(path: Path) -> None: |
1409 | 1410 | + "@com_google_protobuf//:protobuf", |
1410 | 1411 | + ], |
1411 | 1412 | + }), |
1412 | | - alwayslink = 1, |
1413 | | - ) |
| 1413 | + alwayslink = 1, |
| 1414 | +) |
1414 | 1415 | """ |
1415 | 1416 |
|
1416 | 1417 |
|
| 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 | + |
1417 | 1477 | def write_tensorflow_android_absl_patch(path: Path) -> None: |
1418 | 1478 | path.parent.mkdir(parents=True, exist_ok=True) |
1419 | 1479 | absl_patch_text = ( |
@@ -1441,6 +1501,7 @@ def write_tensorflow_android_absl_patch(path: Path) -> None: |
1441 | 1501 | text += SAVED_MODEL_ANDROID_LOADER_PATCH |
1442 | 1502 | text += TENSORFLOW_FRAMEWORK_ANDROID_PATCH |
1443 | 1503 | text += ANDROID_PORTABLE_LIB_SHIM_PATCH |
| 1504 | + text = normalize_unified_diff_hunk_counts(text) |
1444 | 1505 | if not text.endswith("\n"): |
1445 | 1506 | text += "\n" |
1446 | 1507 | path.write_text(text, encoding="utf-8") |
|
0 commit comments