forked from pydantic/pydantic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_usage_docs.py
41 lines (33 loc) · 1.1 KB
/
check_usage_docs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Check that all `Usage docs` tags in docstrings link to the latest version of pydantic.
"""
import re
import sys
from pathlib import Path
ROOT_DIR = Path(__file__).parent.parent
PYDANTIC_DIR = ROOT_DIR / 'pydantic'
version_file = PYDANTIC_DIR / 'version.py'
version = re.search(br"VERSION = '(.*)'", version_file.read_bytes()).group(1)
version_major_minor = b'.'.join(version.split(b'.')[:2])
expected_base = b'https://docs.pydantic.dev/' + version_major_minor + b'/'
paths = sys.argv[1:]
error_count = 0
for path_str in paths:
path = ROOT_DIR / path_str
b = path.read_bytes()
changed = 0
def sub(m: re.Match) -> bytes:
global changed
if m.group(2) != expected_base:
changed += 1
return m.group(1) + expected_base
else:
return m.group(0)
b = re.sub(br'(""" *usage.docs: *)(https://.+?/.+?/)', sub, b, flags=re.I)
if changed:
error_count += changed
path.write_bytes(b)
plural = 's' if changed > 1 else ''
print(f'{path_str:50} {changed} usage docs link{plural} updated')
if error_count:
sys.exit(1)