improve ignore handling in `__init__.py` creation script (#15093)

improve ignore handling
This commit is contained in:
Kyle Altendorf 2023-04-24 14:52:51 -04:00 committed by GitHub
parent b493498204
commit dd845b9a3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 12 deletions

View File

@ -12,9 +12,8 @@
from __future__ import annotations
import logging
import os
import pathlib
import sys
from typing import List
import click
@ -28,6 +27,31 @@ log_levels = {
ignores = {"__pycache__", ".pytest_cache"}
def traverse_directory(path: pathlib.Path) -> List[pathlib.Path]:
of_interest: List[pathlib.Path] = []
file_found = False
for member in path.iterdir():
if not member.is_dir():
file_found = True
continue
if member.name in ignores:
continue
found = traverse_directory(path=member)
of_interest.extend(found)
if len(found) > 0:
of_interest.append(member)
if len(of_interest) > 0 or file_found:
of_interest.append(path)
return of_interest
@click.command()
@click.option(
"-r", "--root", "root_str", type=click.Path(dir_okay=True, file_okay=False, resolve_path=True), default="."
@ -43,20 +67,14 @@ def command(verbose, root_str):
tree_roots = ["benchmarks", "build_scripts", "chia", "tests", "tools"]
failed = False
root = pathlib.Path(root_str).resolve()
directories = sorted(
path
for tree_root in tree_roots
for path in root.joinpath(tree_root).rglob("**/")
if all(part not in ignores for part in path.parts)
)
directories = [
directory for tree_root in tree_roots for directory in traverse_directory(path=root.joinpath(tree_root))
]
for path in directories:
init_path = path.joinpath("__init__.py")
# This has plenty of race hazards. If it messes up,
# it will likely get caught the next time.
if len(os.listdir(path)) == 0:
logger.info(f"Skipping (empty directory) : {init_path}")
continue
if init_path.is_file() and not init_path.is_symlink():
logger.info(f"Found : {init_path}")
continue
@ -66,7 +84,7 @@ def command(verbose, root_str):
logger.warning(f"Created : {init_path}")
else:
failed = True
logger.error(f"Fail : present but not a regular file: {init_path}", file=sys.stderr)
logger.error(f"Fail : present but not a regular file: {init_path}")
if failed:
raise click.ClickException("At least one __init__.py created or not a regular file")