Browse Source

[CI] Enable CI to remove unexpected files in /docs/img dir (#9393)

3.0.0/version-upgrade
Eric Gao 3 years ago committed by GitHub
parent
commit
d23616fcbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 35
      docs/img_utils.py

35
docs/img_utils.py

@ -32,16 +32,6 @@ root_dir: Path = Path(__file__).parent
img_dir: Path = root_dir.joinpath("img") img_dir: Path = root_dir.joinpath("img")
doc_dir: Path = root_dir.joinpath("docs") doc_dir: Path = root_dir.joinpath("docs")
expect_img_types: Set = {
"jpg",
"png",
}
def build_pattern() -> re.Pattern:
"""Build current document image regexp pattern."""
return re.compile(f"(/img.*?\\.({'|'.join(expect_img_types)}))")
def get_files_recurse(path: Path) -> Set: def get_files_recurse(path: Path) -> Set:
"""Get all files recursively from given :param:`path`.""" """Get all files recursively from given :param:`path`."""
@ -68,14 +58,15 @@ def get_paths_rel_path(paths: Set[Path], rel: Path) -> Set:
return {f"/{path.relative_to(rel)}" for path in paths} return {f"/{path.relative_to(rel)}" for path in paths}
def get_docs_img_path(paths: Set[Path], pattern: re.Pattern) -> Set: def get_docs_img_path(paths: Set[Path]) -> Set:
"""Get all img syntax from given :param:`paths` using the regexp from :param:`pattern`.""" """Get all img syntax from given :param:`paths` using the regexp from :param:`pattern`."""
res = set() res = set()
pattern = re.compile(r"/img[\w./-]*")
for path in paths: for path in paths:
content = path.read_text() content = path.read_text()
find = pattern.findall(content) find = pattern.findall(content)
if find: if find:
res |= {item[0] for item in find} res |= {item for item in find}
return res return res
@ -102,16 +93,6 @@ def diff_two_set(first: Set, second: Set) -> Tuple[set, set]:
return first.difference(second), second.difference(first) return first.difference(second), second.difference(first)
def check_diff_img_type() -> Tuple[set, set]:
"""Check images difference type.
:return: Tuple[(actual - expect), (expect - actual)]
"""
img = get_files_recurse(img_dir)
img_suffix = get_paths_uniq_suffix(img)
return diff_two_set(img_suffix, expect_img_types)
def check_diff_img() -> Tuple[set, set]: def check_diff_img() -> Tuple[set, set]:
"""Check images difference files. """Check images difference files.
@ -120,20 +101,12 @@ def check_diff_img() -> Tuple[set, set]:
img = get_files_recurse(img_dir) img = get_files_recurse(img_dir)
docs = get_files_recurse(doc_dir) docs = get_files_recurse(doc_dir)
img_rel_path = get_paths_rel_path(img, root_dir) img_rel_path = get_paths_rel_path(img, root_dir)
pat = build_pattern() docs_rel_path = get_docs_img_path(docs)
docs_rel_path = get_docs_img_path(docs, pat)
return diff_two_set(docs_rel_path, img_rel_path) return diff_two_set(docs_rel_path, img_rel_path)
def check() -> None: def check() -> None:
"""Runner for `check` sub command.""" """Runner for `check` sub command."""
img_type_act, img_type_exp = check_diff_img_type()
assert not img_type_act and not img_type_exp, (
f"Images type assert failed: \n"
f"* difference actual types to expect is: {img_type_act if img_type_act else 'None'}\n"
f"* difference expect types to actual is: {img_type_exp if img_type_exp else 'None'}\n"
)
img_docs, img_img = check_diff_img() img_docs, img_img = check_diff_img()
assert not img_docs and not img_img, ( assert not img_docs and not img_img, (
f"Images assert failed: \n" f"Images assert failed: \n"

Loading…
Cancel
Save