|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +from shutil import rmtree |
| 4 | +import json |
| 5 | +import glob |
| 6 | +import logging |
| 7 | + |
| 8 | +TMP_FILE = "./missing_init_files.json" |
| 9 | +NFILES = [] |
| 10 | + |
| 11 | +# ----------------------------------------------------------------------------- |
| 12 | + |
| 13 | + |
| 14 | +class ColorFormatter(logging.Formatter): |
| 15 | + grey = "\x1b[38;20m" |
| 16 | + green = "\x1b[32;20m" |
| 17 | + yellow = "\x1b[33;20m" |
| 18 | + red = "\x1b[31;20m" |
| 19 | + bold_red = "\x1b[31;1m" |
| 20 | + reset = "\x1b[0m" |
| 21 | + fmt = ( |
| 22 | + "%(asctime)s - %(name)s - %(levelname)s - %(message)s " # noqa |
| 23 | + "(%(filename)s:%(lineno)d)" |
| 24 | + ) |
| 25 | + |
| 26 | + FORMATS = { |
| 27 | + logging.DEBUG: grey + fmt + reset, |
| 28 | + logging.INFO: green + fmt + reset, |
| 29 | + logging.WARNING: yellow + fmt + reset, |
| 30 | + logging.ERROR: red + fmt + reset, |
| 31 | + logging.CRITICAL: bold_red + fmt + reset, |
| 32 | + } |
| 33 | + |
| 34 | + def format(self, record): |
| 35 | + log_fmt = self.FORMATS.get(record.levelno) |
| 36 | + formatter = logging.Formatter(log_fmt) |
| 37 | + return formatter.format(record) |
| 38 | + |
| 39 | + |
| 40 | +ch = logging.StreamHandler() |
| 41 | +ch.setFormatter(ColorFormatter()) |
| 42 | + |
| 43 | +logging.basicConfig( |
| 44 | + level=logging.INFO, |
| 45 | + handlers=[ch], |
| 46 | +) |
| 47 | + |
| 48 | + |
| 49 | +# ----------------------------------------------------------------------------- |
| 50 | + |
| 51 | + |
| 52 | +def create_init_file(dirpath, msg): |
| 53 | + global NFILES |
| 54 | + ini_file = f"{dirpath}/__init__.py" |
| 55 | + Path(ini_file).touch() |
| 56 | + NFILES.append(ini_file) |
| 57 | + logging.info(f"{msg}: created '{ini_file}'") |
| 58 | + |
| 59 | + |
| 60 | +def create_parent_init_files(dirpath: str, rootpath: str, msg: str): |
| 61 | + parent_path = dirpath |
| 62 | + while parent_path != rootpath: |
| 63 | + parent_path = os.path.dirname(parent_path) |
| 64 | + parent_init = os.path.join(parent_path, "__init__.py") |
| 65 | + if not os.path.exists(parent_init): |
| 66 | + create_init_file(parent_path, msg) |
| 67 | + else: |
| 68 | + break |
| 69 | + |
| 70 | + |
| 71 | +def add_missing_init_files(*roots, msg=""): |
| 72 | + """ |
| 73 | + This function takes in one or more root directories as arguments and scans |
| 74 | + them for Python files without an `__init__.py` file. It generates a JSON |
| 75 | + file named `missing_init_files.json` containing the paths of these files. |
| 76 | +
|
| 77 | + Args: |
| 78 | + *roots: Variable number of root directories to scan. |
| 79 | +
|
| 80 | + Returns: |
| 81 | + None |
| 82 | + """ |
| 83 | + |
| 84 | + for root in roots: |
| 85 | + if not os.path.exists(root): |
| 86 | + continue |
| 87 | + rootpath = os.path.abspath(root) |
| 88 | + for dirpath, dirs, files in os.walk(rootpath): |
| 89 | + if "__init__.py" in files: |
| 90 | + continue |
| 91 | + |
| 92 | + if "." in dirpath: |
| 93 | + continue |
| 94 | + |
| 95 | + if ( |
| 96 | + not glob.glob(os.path.join(dirpath, "*.py")) |
| 97 | + and "vendor" not in dirpath |
| 98 | + ): |
| 99 | + continue |
| 100 | + |
| 101 | + create_init_file(dirpath, msg) |
| 102 | + create_parent_init_files(dirpath, rootpath, msg) |
| 103 | + |
| 104 | + with open(TMP_FILE, "w") as f: |
| 105 | + json.dump(NFILES, f) |
| 106 | + |
| 107 | + |
| 108 | +def remove_missing_init_files(msg=""): |
| 109 | + """ |
| 110 | + This function removes temporary `__init__.py` files created in the |
| 111 | + `add_missing_init_files()` function. It reads the paths of these files from |
| 112 | + a JSON file named `missing_init_files.json`. |
| 113 | +
|
| 114 | + Args: |
| 115 | + None |
| 116 | +
|
| 117 | + Returns: |
| 118 | + None |
| 119 | + """ |
| 120 | + global NFILES |
| 121 | + nfiles = [] |
| 122 | + if os.path.exists(TMP_FILE): |
| 123 | + with open(TMP_FILE, "r") as f: |
| 124 | + nfiles = json.load(f) |
| 125 | + else: |
| 126 | + nfiles = NFILES |
| 127 | + |
| 128 | + for file in nfiles: |
| 129 | + Path(file).unlink() |
| 130 | + logging.info(f"{msg}: removed {file}") |
| 131 | + |
| 132 | + os.remove(TMP_FILE) |
| 133 | + NFILES = [] |
| 134 | + |
| 135 | + |
| 136 | +def remove_pychache_dirs(msg=""): |
| 137 | + """ |
| 138 | + This function walks the current directory and removes all existing |
| 139 | + '__pycache__' directories. |
| 140 | +
|
| 141 | + Args: |
| 142 | + msg: An optional message to display during the removal process. |
| 143 | +
|
| 144 | + Returns: |
| 145 | + None |
| 146 | + """ |
| 147 | + nremoved = 0 |
| 148 | + |
| 149 | + for dirpath, dirs, files in os.walk("."): |
| 150 | + if "__pycache__" in dirs: |
| 151 | + pydir = Path(f"{dirpath}/__pycache__") |
| 152 | + rmtree(pydir) |
| 153 | + nremoved += 1 |
| 154 | + logging.info(f"{msg}: removed '{pydir}'") |
| 155 | + |
| 156 | + if not nremoved: |
| 157 | + logging.info(f"{msg}: no __pycache__ dirs found") |
| 158 | + |
| 159 | + |
| 160 | +# mkdocs hooks ---------------------------------------------------------------- |
| 161 | + |
| 162 | + |
| 163 | +def on_startup(command, dirty): |
| 164 | + remove_pychache_dirs(msg="HOOK - on_startup") |
| 165 | + |
| 166 | + |
| 167 | +def on_pre_build(config): |
| 168 | + """ |
| 169 | + This function is called before the MkDocs build process begins. It adds |
| 170 | + temporary `__init__.py` files to directories that do not contain one, to |
| 171 | + make sure mkdocs doesn't ignore them. |
| 172 | + """ |
| 173 | + try: |
| 174 | + add_missing_init_files( |
| 175 | + "client", |
| 176 | + "server", |
| 177 | + "services", |
| 178 | + msg="HOOK - on_pre_build", |
| 179 | + ) |
| 180 | + except BaseException as e: |
| 181 | + logging.error(e) |
| 182 | + remove_missing_init_files( |
| 183 | + msg="HOOK - on_post_build: cleaning up on error !" |
| 184 | + ) |
| 185 | + raise |
| 186 | + |
| 187 | + |
| 188 | +def on_post_build(config): |
| 189 | + """ |
| 190 | + This function is called after the MkDocs build process ends. It removes |
| 191 | + temporary `__init__.py` files that were added in the `on_pre_build()` |
| 192 | + function. |
| 193 | + """ |
| 194 | + remove_missing_init_files(msg="HOOK - on_post_build") |
0 commit comments