|
| 1 | +import pathlib |
| 2 | +import re |
| 3 | +import sys |
| 4 | +from functools import cached_property |
| 5 | +from typing import Iterator, List, Pattern, Tuple |
| 6 | + |
| 7 | +from packaging import version |
| 8 | + |
| 9 | +import abstracts |
| 10 | + |
| 11 | +from aio.run import checker |
| 12 | + |
| 13 | +from envoy.code.check import abstract |
| 14 | +from .project import Project, VERSION_HISTORY_SECTIONS |
| 15 | + |
| 16 | +INVALID_REFLINK = r"[^:]ref:`" |
| 17 | +REF_WITH_PUNCTUATION_REGEX = r".*\. <[^<]*>`\s*" |
| 18 | + |
| 19 | +# Make sure backticks come in pairs. |
| 20 | +# Exceptions: reflinks (ref:`` where the backtick won't be preceded by a space |
| 21 | +# links `title <link>`_ where the _ is checked for in the regex. |
| 22 | +SINGLE_TICK_REGEX = re.compile(r"[^`]`[^`].*`[^`]") |
| 23 | +REF_TICKS_REGEX = re.compile(r":`[^`]*`") |
| 24 | +LINK_TICKS_REGEX = re.compile(r"[^`]`[^`].*`_") |
| 25 | + |
| 26 | + |
| 27 | +class VersionFile: |
| 28 | + |
| 29 | + def __init__(self, changelog): |
| 30 | + self.changelog = changelog |
| 31 | + |
| 32 | + @cached_property |
| 33 | + def invalid_reflink_re(self) -> Pattern[str]: |
| 34 | + return re.compile(INVALID_REFLINK) |
| 35 | + |
| 36 | + @cached_property |
| 37 | + def link_ticks_re(self) -> Pattern[str]: |
| 38 | + return re.compile(LINK_TICKS_REGEX) |
| 39 | + |
| 40 | + @cached_property |
| 41 | + def punctuation_re(self) -> Pattern[str]: |
| 42 | + return re.compile(REF_WITH_PUNCTUATION_REGEX) |
| 43 | + |
| 44 | + @cached_property |
| 45 | + def ref_ticks_re(self) -> Pattern[str]: |
| 46 | + return re.compile(REF_TICKS_REGEX) |
| 47 | + |
| 48 | + @cached_property |
| 49 | + def single_tick_re(self) -> Pattern[str]: |
| 50 | + return re.compile(SINGLE_TICK_REGEX) |
| 51 | + |
| 52 | + def check_punctuation(self, section, entry) -> List[str]: |
| 53 | + change = entry["change"] |
| 54 | + if change.strip().endswith("."): |
| 55 | + return [] |
| 56 | + # Ends with punctuated link |
| 57 | + if change.strip().endswith('`') and self.punctuation_re.match(change.strip()): |
| 58 | + return [] |
| 59 | + # Ends with a list |
| 60 | + if change.strip().split("\n")[-1].startswith(" *"): |
| 61 | + return [] |
| 62 | + return [ |
| 63 | + f"{self.changelog.version}: Missing punctuation ({section}/{entry['area']}) ...{change[-30:]}\n{entry['change']}" |
| 64 | + ] |
| 65 | + |
| 66 | + def check_reflinks(self, section, entry) -> List[str]: |
| 67 | + return ([ |
| 68 | + f"{self.changelog.version}: Found text \" ref:\" ({section}/{entry['area']}) This should probably be \" :ref:\"\n{entry['change']}" |
| 69 | + ] if self.invalid_reflink_re.findall(entry["change"]) else []) |
| 70 | + |
| 71 | + def check_change(self, section, entry): |
| 72 | + return [ |
| 73 | + *self.check_reflinks(section, entry), *self.check_ticks(section, entry), |
| 74 | + *self.check_punctuation(section, entry) |
| 75 | + ] |
| 76 | + |
| 77 | + def check_ticks(self, section, entry) -> List[str]: |
| 78 | + _change = entry["change"] |
| 79 | + for reflink in self.ref_ticks_re.findall(_change): |
| 80 | + _change = _change.replace(reflink, "") |
| 81 | + for extlink in self.link_ticks_re.findall(_change): |
| 82 | + _change = _change.replace(extlink, "") |
| 83 | + single_ticks = self.single_tick_re.findall(_change) |
| 84 | + return ([ |
| 85 | + f"{self.changelog.version}: Single backticks found ({section}/{entry['area']}) {', '.join(single_ticks)}\n{_change}" |
| 86 | + ] if single_ticks else []) |
| 87 | + |
| 88 | + def run_checks(self) -> Iterator[str]: |
| 89 | + errors = [] |
| 90 | + for section, entries in self.changelog.data.items(): |
| 91 | + if section == "date": |
| 92 | + continue |
| 93 | + if section not in VERSION_HISTORY_SECTIONS: |
| 94 | + errors.append(f"{self.changelog.version} Unrecognized changelog section: {section}") |
| 95 | + if section == "changes": |
| 96 | + if version.Version(self.changelog.version) > version.Version("1.16"): |
| 97 | + errors.append(f"Removed `changes` section found: {self.changelog.version}") |
| 98 | + if not entries: |
| 99 | + continue |
| 100 | + for entry in entries: |
| 101 | + errors.extend(self.check_change(section, entry)) |
| 102 | + return errors |
| 103 | + |
| 104 | + |
| 105 | +class AVersionHistoryCheck(abstract.ACodeCheck, metaclass=abstracts.Abstraction): |
| 106 | + """Extensions check.""" |
| 107 | + |
| 108 | + version_file = VersionFile |
| 109 | + _version_path = "VERSION.txt" |
| 110 | + |
| 111 | + @property |
| 112 | + def changelogs(self) -> Tuple[pathlib.Path, ...]: |
| 113 | + return tuple(self.directory.path.joinpath("changelogs").glob("*.yaml")) |
| 114 | + |
| 115 | + @cached_property |
| 116 | + def project(self): |
| 117 | + return Project(self.version_path, self.changelogs) |
| 118 | + |
| 119 | + @cached_property |
| 120 | + def version_path(self): |
| 121 | + return self.directory.path.joinpath(self._version_path) |
0 commit comments