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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2023 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later
"""
This script is to validate the markdown page that documents Blender's file-structure, see:
https://developer.blender.org/docs/features/code_layout/
It can run without any arguments, where it will download the markdown to Blender's source root:
You may pass the markdown text as an argument, e.g.
check_docs_code_layout.py --markdown=markdown.txt
"""
import os
import argparse
from typing import (
List,
Optional,
)
# -----------------------------------------------------------------------------
# Constants
CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
SOURCE_DIR = os.path.normpath(os.path.join(CURRENT_DIR, "..", ".."))
MARKDOWN_URL = (
"https://projects.blender.org/blender/blender-developer-docs/raw/branch/main/docs/features/code_layout.md"
)
# -----------------------------------------------------------------------------
# HTML Utilities
def text_with_title_underline(text: str, underline: str = "=") -> str:
return "\n{:s}\n{:s}\n".format(text, len(text) * underline)
def html_extract_markdown_from_url(url: str) -> Optional[str]:
"""
Download
"""
import urllib.request
req = urllib.request.Request(url=url)
with urllib.request.urlopen(req) as fh:
data = fh.read().decode('utf-8')
# Quiet `mypy` checker warning.
assert isinstance(data, str)
return data
# -----------------------------------------------------------------------------
# markdown Text Parsing
def markdown_to_paths(markdown: str) -> List[str]:
file_paths = []
markdown = markdown.replace("<p>", "")
markdown = markdown.replace("</p>", "")
markdown = markdown.replace("<strong>", "")
markdown = markdown.replace("</strong>", "")
markdown = markdown.replace("</td>", "")
path_prefix = "<td markdown>/"
for line in markdown.splitlines():
line = line.strip()
if line.startswith(path_prefix):
file_path = line[len(path_prefix):]
file_path = file_path.rstrip("/")
file_paths.append(file_path)
return file_paths
# -----------------------------------------------------------------------------
# Reporting
def report_known_markdown_paths(file_paths: List[str]) -> None:
heading = "Paths Found in markdown Table"
print(text_with_title_underline(heading))
for p in file_paths:
print("-", p)
def report_missing_source(file_paths: List[str]) -> int:
heading = "Missing in Source Dir"
test = [p for p in file_paths if not os.path.exists(os.path.join(SOURCE_DIR, p))]
amount = str(len(test)) if test else "none found"
print(text_with_title_underline("{:s} ({:s})".format(heading, amount)))
if not test:
return 0
print("The following paths were found in the markdown\n"
"but were not found in Blender's source directory:\n")
for p in test:
print("-", p)
return len(test)
def report_incomplete(file_paths: List[str]) -> int:
heading = "Missing Documentation"
test = []
basedirs = {os.path.dirname(p) for p in file_paths}
for base in sorted(basedirs):
base_abs = os.path.join(SOURCE_DIR, base)
if os.path.exists(base_abs):
for p in os.listdir(base_abs):
if not p.startswith("."):
p_abs = os.path.join(base_abs, p)
if os.path.isdir(p_abs):
p_rel = os.path.join(base, p)
if p_rel not in file_paths:
test.append(p_rel)
amount = str(len(test)) if test else "none found"
print(text_with_title_underline("{:s} ({:s})".format(heading, amount)))
if not test:
return 0
print("The following paths were found in Blender's source directory\n"
"but are missing from the markdown:\n")
for p in sorted(test):
print("-", p)
return len(test)
def report_alphabetical_order(file_paths: List[str]) -> int:
heading = "Non-Alphabetically Ordered"
test = []
p_prev = ""
p_prev_dir = ""
for p in file_paths:
p_dir = os.path.dirname(p)
if p_prev:
if p_dir == p_prev_dir:
if p < p_prev:
test.append((p_prev, p))
p_prev_dir = p_dir
p_prev = p
amount = str(len(test)) if test else "none found"
print(text_with_title_underline("{:s} ({:s})".format(heading, amount)))
if not test:
return 0
for p_prev, p in test:
print("-", p, "(should be before)\n ", p_prev)
return len(test)
# -----------------------------------------------------------------------------
# Argument Parser
def create_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"-m",
"--markdown",
dest="markdown",
metavar='PATH',
default=os.path.join(SOURCE_DIR, "markdown_file_structure.txt"),
help="markdown text file path, NOTE: this will be downloaded if not found!",
)
return parser
# -----------------------------------------------------------------------------
# Main Function
def main() -> None:
parser = create_parser()
args = parser.parse_args()
if os.path.exists(args.markdown):
print("Using existing markdown text:", args.markdown)
else:
data = html_extract_markdown_from_url(MARKDOWN_URL)
if data is not None:
with open(args.markdown, 'w', encoding='utf-8') as fh:
fh.write(data)
print("Downloaded markdown text to:", args.markdown)
print("Update and save to:", MARKDOWN_URL)
else:
print("Failed to downloaded or extract markdown text, aborting!")
return
with open(args.markdown, 'r', encoding='utf-8') as fh:
file_paths = markdown_to_paths(fh.read())
# Disable, mostly useful when debugging why paths might not be found.
# report_known_markdown_paths()
issues = 0
issues += report_missing_source(file_paths)
issues += report_incomplete(file_paths)
issues += report_alphabetical_order(file_paths)
if issues:
print("Warning, found {:d} issues!\n".format(issues))
else:
print("Success! The markdown text is up to date with Blender's source tree!\n")
if __name__ == "__main__":
main()
|