|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +""" |
| 17 | +Guard the hand-maintained <sourcepath> in tika-parent/pom.xml used by the |
| 18 | +TIKA-4318 javadoc:aggregate workaround. That list must name every module's |
| 19 | +src/main/java (except tika-grpc); a module missing from it is silently dropped |
| 20 | +from the aggregated API docs. Run this right before building the javadocs. |
| 21 | +
|
| 22 | + check: python3 .github/scripts/check_javadoc_sourcepath.py [repo_root] |
| 23 | + fix: python3 .github/scripts/check_javadoc_sourcepath.py --fix [repo_root] |
| 24 | +
|
| 25 | +Exit 0 = list matches the reactor; 1 = drift (missing/stale roots) or --fix rewrote it. |
| 26 | +""" |
| 27 | +import os |
| 28 | +import re |
| 29 | +import sys |
| 30 | + |
| 31 | +PRUNE = {"target", ".git", ".local_m2_repo", "node_modules", ".mvn"} |
| 32 | +EXCLUDE_MODULE_PREFIX = "tika-grpc/" # protobuf gen-sources not on the aggregate classpath |
| 33 | +POM = "tika-parent/pom.xml" |
| 34 | + |
| 35 | + |
| 36 | +def actual_roots(root: str): |
| 37 | + roots = set() |
| 38 | + for dirpath, dirnames, _ in os.walk(root): |
| 39 | + dirnames[:] = [d for d in dirnames if d not in PRUNE] |
| 40 | + if not dirpath.replace(os.sep, "/").endswith("/src/main/java"): |
| 41 | + continue |
| 42 | + rel = os.path.relpath(dirpath, root).replace(os.sep, "/") |
| 43 | + if rel.startswith(EXCLUDE_MODULE_PREFIX): |
| 44 | + continue |
| 45 | + for _, _, files in os.walk(dirpath): |
| 46 | + if any(f.endswith(".java") and f != "package-info.java" for f in files): |
| 47 | + roots.add(rel) |
| 48 | + break |
| 49 | + return roots |
| 50 | + |
| 51 | + |
| 52 | +def listed_roots(pom_text: str): |
| 53 | + m = re.search(r"<sourcepath>([^<]*)</sourcepath>", pom_text) |
| 54 | + if not m: |
| 55 | + sys.exit(f"ERROR: no <sourcepath> found in {POM}") |
| 56 | + return set(p.strip() for p in m.group(1).split(";") if p.strip()), m |
| 57 | + |
| 58 | + |
| 59 | +def main() -> int: |
| 60 | + args = [a for a in sys.argv[1:] if a != "--fix"] |
| 61 | + fix = "--fix" in sys.argv |
| 62 | + root = os.path.abspath(args[0] if args else ".") |
| 63 | + pom_path = os.path.join(root, POM) |
| 64 | + text = open(pom_path, encoding="utf-8").read() |
| 65 | + |
| 66 | + actual = actual_roots(root) |
| 67 | + listed, m = listed_roots(text) |
| 68 | + missing = sorted(actual - listed) # modules present but NOT in the list -> dropped from docs |
| 69 | + stale = sorted(listed - actual) # entries in the list that no longer exist |
| 70 | + |
| 71 | + if not missing and not stale: |
| 72 | + print(f"OK: javadoc <sourcepath> covers all {len(actual)} module source roots.") |
| 73 | + return 0 |
| 74 | + |
| 75 | + if fix: |
| 76 | + new_list = ";".join(sorted(actual)) |
| 77 | + open(pom_path, "w", encoding="utf-8").write( |
| 78 | + text[: m.start(1)] + new_list + text[m.end(1):]) |
| 79 | + print(f"FIXED: rewrote <sourcepath> in {POM} ({len(actual)} roots).") |
| 80 | + return 0 |
| 81 | + |
| 82 | + print(f"ERROR: javadoc <sourcepath> in {POM} is out of sync with the reactor.\n") |
| 83 | + if missing: |
| 84 | + print(" MISSING (module exists but is not in <sourcepath> -> its API docs are dropped):") |
| 85 | + for r in missing: |
| 86 | + print(f" {r}") |
| 87 | + if stale: |
| 88 | + print(" STALE (in <sourcepath> but no longer a module source root):") |
| 89 | + for r in stale: |
| 90 | + print(f" {r}") |
| 91 | + print("\nFix: re-run with --fix, or edit tika-parent/pom.xml's javadoc <sourcepath>.") |
| 92 | + print("See TIKA-4318.") |
| 93 | + return 1 |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + sys.exit(main()) |
0 commit comments