-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfix-uppercase-lowercase.py
74 lines (61 loc) · 2.13 KB
/
fix-uppercase-lowercase.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from pathlib import Path
import re
import sys
DOC_DIR = Path(__file__).parent / "source"
IMAGE_DIR = DOC_DIR / "image"
IMAGE_LINK_REGEX = re.compile(
r"""
image/ # image/ prefix
\S+ # some chars/numbers
\. # a dot
[jpngJPGN]+ # .jpg, .png, .PNG, .JPG
""",
re.VERBOSE,
)
def fix_image_files():
"""Rename any offending file to its lowercase version."""
did_something = False
images = IMAGE_DIR.glob("*.*")
for image in images:
lowercase_name = str(image).lower()
if lowercase_name != str(image):
did_something = True
print("Renaming %s into %s" % (image, lowercase_name))
image.replace(lowercase_name)
if not did_something:
print("No uppercase image files found. All is fine.")
return did_something
def fix_image_links():
"""Check and fix image links in the documentation."""
did_something = False
docs = DOC_DIR.glob("*.rst")
for doc in docs:
to_replace = {} # old: new
contents = doc.read_text()
image_links = IMAGE_LINK_REGEX.findall(contents)
for image_link in image_links:
lowercase_image_link = image_link.lower()
if image_link != lowercase_image_link:
to_replace[image_link] = lowercase_image_link
if to_replace:
did_something = True
print("Fixing document %s" % doc)
for old, new in to_replace.items():
print(" Changing %s into %s" % (old, new))
contents = contents.replace(old, new)
doc.write_text(contents)
if not did_something:
print("No uppercase image links found in the documentation. All is fine.")
return did_something
def main():
did_something1 = fix_image_files()
did_something2 = fix_image_links()
if did_something1 or did_something2:
print(
"ERROR: All image filenames must be lowercase, or the build will fail!" +
" Please make all your image filenames lowercase, and then try again."
)
# Exit with an error code.
sys.exit(1)
if __name__ == "__main__":
main()