|
| 1 | +"""Script to generate keras public API in `keras/api` directory. |
| 2 | +
|
| 3 | +Usage: |
| 4 | +
|
| 5 | +Run via `./shell/api_gen.sh`. |
| 6 | +It generates API and formats user and generated APIs. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import shutil |
| 11 | + |
| 12 | +import namex |
| 13 | + |
| 14 | +package = "keras" |
| 15 | + |
| 16 | + |
| 17 | +def ignore_files(_, filenames): |
| 18 | + return [f for f in filenames if f.endswith("_test.py")] |
| 19 | + |
| 20 | + |
| 21 | +def create_legacy_directory(): |
| 22 | + API_DIR = os.path.join(package, "api") |
| 23 | + # Make keras/_tf_keras/ by copying keras/ |
| 24 | + tf_keras_dirpath_parent = os.path.join(API_DIR, "_tf_keras") |
| 25 | + tf_keras_dirpath = os.path.join(tf_keras_dirpath_parent, "keras") |
| 26 | + os.makedirs(tf_keras_dirpath, exist_ok=True) |
| 27 | + with open(os.path.join(tf_keras_dirpath_parent, "__init__.py"), "w") as f: |
| 28 | + f.write("from keras.api._tf_keras import keras\n") |
| 29 | + with open(os.path.join(API_DIR, "__init__.py")) as f: |
| 30 | + init_file = f.read() |
| 31 | + init_file = init_file.replace( |
| 32 | + "from keras.api import _legacy", |
| 33 | + "from keras.api import _tf_keras", |
| 34 | + ) |
| 35 | + with open(os.path.join(API_DIR, "__init__.py"), "w") as f: |
| 36 | + f.write(init_file) |
| 37 | + # Remove the import of `_tf_keras` in `keras/_tf_keras/keras/__init__.py` |
| 38 | + init_file = init_file.replace("from keras.api import _tf_keras\n", "\n") |
| 39 | + with open(os.path.join(tf_keras_dirpath, "__init__.py"), "w") as f: |
| 40 | + f.write(init_file) |
| 41 | + for dirname in os.listdir(API_DIR): |
| 42 | + dirpath = os.path.join(API_DIR, dirname) |
| 43 | + if os.path.isdir(dirpath) and dirname not in ( |
| 44 | + "_legacy", |
| 45 | + "_tf_keras", |
| 46 | + "src", |
| 47 | + ): |
| 48 | + destpath = os.path.join(tf_keras_dirpath, dirname) |
| 49 | + if os.path.exists(destpath): |
| 50 | + shutil.rmtree(destpath) |
| 51 | + shutil.copytree( |
| 52 | + dirpath, |
| 53 | + destpath, |
| 54 | + ignore=ignore_files, |
| 55 | + ) |
| 56 | + |
| 57 | + # Copy keras/_legacy/ file contents to keras/_tf_keras/keras |
| 58 | + legacy_submodules = [ |
| 59 | + path[:-3] |
| 60 | + for path in os.listdir(os.path.join(package, "src", "legacy")) |
| 61 | + if path.endswith(".py") |
| 62 | + ] |
| 63 | + legacy_submodules += [ |
| 64 | + path |
| 65 | + for path in os.listdir(os.path.join(package, "src", "legacy")) |
| 66 | + if os.path.isdir(os.path.join(package, "src", "legacy", path)) |
| 67 | + ] |
| 68 | + |
| 69 | + for root, _, fnames in os.walk(os.path.join(package, "_legacy")): |
| 70 | + for fname in fnames: |
| 71 | + if fname.endswith(".py"): |
| 72 | + legacy_fpath = os.path.join(root, fname) |
| 73 | + tf_keras_root = root.replace("/_legacy", "/_tf_keras/keras") |
| 74 | + core_api_fpath = os.path.join( |
| 75 | + root.replace("/_legacy", ""), fname |
| 76 | + ) |
| 77 | + if not os.path.exists(tf_keras_root): |
| 78 | + os.makedirs(tf_keras_root) |
| 79 | + tf_keras_fpath = os.path.join(tf_keras_root, fname) |
| 80 | + with open(legacy_fpath) as f: |
| 81 | + legacy_contents = f.read() |
| 82 | + legacy_contents = legacy_contents.replace( |
| 83 | + "keras.api._legacy", "keras.api._tf_keras.keras" |
| 84 | + ) |
| 85 | + if os.path.exists(core_api_fpath): |
| 86 | + with open(core_api_fpath) as f: |
| 87 | + core_api_contents = f.read() |
| 88 | + core_api_contents = core_api_contents.replace( |
| 89 | + "from keras.api import _tf_keras\n", "" |
| 90 | + ) |
| 91 | + for legacy_submodule in legacy_submodules: |
| 92 | + core_api_contents = core_api_contents.replace( |
| 93 | + f"from keras.api import {legacy_submodule}\n", |
| 94 | + "", |
| 95 | + ) |
| 96 | + core_api_contents = core_api_contents.replace( |
| 97 | + f"keras.api.{legacy_submodule}", |
| 98 | + f"keras.api._tf_keras.keras.{legacy_submodule}", |
| 99 | + ) |
| 100 | + legacy_contents = core_api_contents + "\n" + legacy_contents |
| 101 | + with open(tf_keras_fpath, "w") as f: |
| 102 | + f.write(legacy_contents) |
| 103 | + |
| 104 | + # Delete keras/api/_legacy/ |
| 105 | + shutil.rmtree(os.path.join(API_DIR, "_legacy")) |
| 106 | + |
| 107 | + |
| 108 | +def export_version_string(): |
| 109 | + API_INIT = os.path.join(package, "api", "__init__.py") |
| 110 | + with open(API_INIT) as f: |
| 111 | + contents = f.read() |
| 112 | + with open(API_INIT, "w") as f: |
| 113 | + contents += "from keras.src.version import __version__\n" |
| 114 | + f.write(contents) |
| 115 | + |
| 116 | + |
| 117 | +def update_package_init(): |
| 118 | + contents = """ |
| 119 | +# Import everything from /api/ into keras. |
| 120 | +from keras.api import * # noqa: F403 |
| 121 | +from keras.api import __version__ # Import * ignores names start with "_". |
| 122 | +
|
| 123 | +import os |
| 124 | +
|
| 125 | +# Add everything in /api/ to the module search path. |
| 126 | +__path__.append(os.path.join(os.path.dirname(__file__), "api")) # noqa: F405 |
| 127 | +
|
| 128 | +# Don't pollute namespace. |
| 129 | +del os |
| 130 | +
|
| 131 | +# Never autocomplete `.src` or `.api` on an imported keras object. |
| 132 | +def __dir__(): |
| 133 | + keys = dict.fromkeys((globals().keys())) |
| 134 | + keys.pop("src") |
| 135 | + keys.pop("api") |
| 136 | + return list(keys) |
| 137 | +
|
| 138 | +
|
| 139 | +# Don't import `.src` or `.api` during `from keras import *`. |
| 140 | +__all__ = [ |
| 141 | + name |
| 142 | + for name in globals().keys() |
| 143 | + if not (name.startswith("_") or name in ("src", "api")) |
| 144 | +]""" |
| 145 | + with open(os.path.join(package, "__init__.py")) as f: |
| 146 | + init_contents = f.read() |
| 147 | + with open(os.path.join(package, "__init__.py"), "w") as f: |
| 148 | + f.write(init_contents.replace("\nfrom keras import api", contents)) |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + # Backup the `keras/__init__.py` and restore it on error in api gen. |
| 153 | + os.makedirs(os.path.join(package, "api"), exist_ok=True) |
| 154 | + init_fname = os.path.join(package, "__init__.py") |
| 155 | + backup_init_fname = os.path.join(package, "__init__.py.bak") |
| 156 | + try: |
| 157 | + if os.path.exists(init_fname): |
| 158 | + shutil.move(init_fname, backup_init_fname) |
| 159 | + # Generates `keras/api` directory. |
| 160 | + namex.generate_api_files( |
| 161 | + "keras", code_directory="src", target_directory="api" |
| 162 | + ) |
| 163 | + # Creates `keras/__init__.py` importing from `keras/api` |
| 164 | + update_package_init() |
| 165 | + except Exception as e: |
| 166 | + if os.path.exists(backup_init_fname): |
| 167 | + shutil.move(backup_init_fname, init_fname) |
| 168 | + raise e |
| 169 | + finally: |
| 170 | + if os.path.exists(backup_init_fname): |
| 171 | + os.remove(backup_init_fname) |
| 172 | + # Add __version__ to keras package |
| 173 | + export_version_string() |
| 174 | + # Creates `_tf_keras` with full keras API |
| 175 | + create_legacy_directory() |
0 commit comments