|
| 1 | +from __future__ import division, print_function |
| 2 | + |
| 3 | +import argparse |
| 4 | +import re |
| 5 | + |
| 6 | +JAVADOC_START_RE = re.compile(r"^\s*/\*\*$") |
| 7 | +COMMENT_RE = re.compile(r"^\s*\*") |
| 8 | +DEPRECATED_RE = re.compile(r"^\s*\*\s+DEPRECATED:\s*(?P<message>.+)$") |
| 9 | + |
| 10 | + |
| 11 | +def main(input_file): |
| 12 | + filename = input_file.name |
| 13 | + content = [line.rstrip() for line in input_file.readlines()] |
| 14 | + input_file.close() |
| 15 | + |
| 16 | + with open(filename, "wt") as result: |
| 17 | + javadoc_started = False |
| 18 | + obsolete_message = None |
| 19 | + |
| 20 | + for line in content: |
| 21 | + deprecated_match = DEPRECATED_RE.match(line) |
| 22 | + if javadoc_started and deprecated_match: |
| 23 | + # deprecation message found |
| 24 | + obsolete_message = "@Deprecated()" |
| 25 | + |
| 26 | + if javadoc_started and not COMMENT_RE.match(line): |
| 27 | + # comment section ended |
| 28 | + javadoc_started = False |
| 29 | + if obsolete_message: |
| 30 | + if not line.lstrip().startswith("@Deprecated"): |
| 31 | + result.write(obsolete_message + "\n") |
| 32 | + obsolete_message = None |
| 33 | + |
| 34 | + result.write(line + "\n") |
| 35 | + continue |
| 36 | + |
| 37 | + if JAVADOC_START_RE.match(line): |
| 38 | + # comment section started |
| 39 | + javadoc_started = True |
| 40 | + |
| 41 | + result.write(line + "\n") |
| 42 | + |
| 43 | + |
| 44 | +def parse_args(): |
| 45 | + parser = argparse.ArgumentParser() |
| 46 | + parser.add_argument("input_file", type=argparse.FileType("rt")) |
| 47 | + args = parser.parse_args() |
| 48 | + return vars(args) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + main(**parse_args()) |
0 commit comments