|
| 1 | +# Copyright 2023 The KerasNLP Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Script to generate keras_nlp public API in `keras_nlp/api` directory. |
| 15 | +
|
| 16 | +Usage: |
| 17 | +
|
| 18 | +Run via `./shell/api_gen.sh`. |
| 19 | +It generates API and formats user and generated APIs. |
| 20 | +""" |
| 21 | + |
| 22 | +import os |
| 23 | +import shutil |
| 24 | + |
| 25 | +import namex |
| 26 | + |
| 27 | +package = "keras_nlp" |
| 28 | + |
| 29 | + |
| 30 | +def ignore_files(_, filenames): |
| 31 | + return [f for f in filenames if f.endswith("_test.py")] |
| 32 | + |
| 33 | + |
| 34 | +def copy_source_to_build_directory(root_path): |
| 35 | + # Copy sources (`keras_nlp/` directory and setup files) to build dir |
| 36 | + build_dir = os.path.join(root_path, "tmp_build_dir") |
| 37 | + if os.path.exists(build_dir): |
| 38 | + shutil.rmtree(build_dir) |
| 39 | + os.mkdir(build_dir) |
| 40 | + shutil.copytree( |
| 41 | + package, os.path.join(build_dir, package), ignore=ignore_files |
| 42 | + ) |
| 43 | + return build_dir |
| 44 | + |
| 45 | + |
| 46 | +def export_version_string(api_init_fname): |
| 47 | + with open(api_init_fname) as f: |
| 48 | + contents = f.read() |
| 49 | + with open(api_init_fname, "w") as f: |
| 50 | + contents += "from keras_nlp.src.version_utils import __version__\n" |
| 51 | + f.write(contents) |
| 52 | + |
| 53 | + |
| 54 | +def build(): |
| 55 | + # Backup the `keras_nlp/__init__.py` and restore it on error in api gen. |
| 56 | + root_path = os.path.dirname(os.path.abspath(__file__)) |
| 57 | + code_api_dir = os.path.join(root_path, package, "api") |
| 58 | + # Create temp build dir |
| 59 | + build_dir = copy_source_to_build_directory(root_path) |
| 60 | + build_api_dir = os.path.join(build_dir, package, "api") |
| 61 | + build_init_fname = os.path.join(build_dir, package, "__init__.py") |
| 62 | + build_api_init_fname = os.path.join(build_api_dir, "__init__.py") |
| 63 | + try: |
| 64 | + os.chdir(build_dir) |
| 65 | + # Generates `keras_nlp/api` directory. |
| 66 | + if os.path.exists(build_api_dir): |
| 67 | + shutil.rmtree(build_api_dir) |
| 68 | + if os.path.exists(build_init_fname): |
| 69 | + os.remove(build_init_fname) |
| 70 | + os.makedirs(build_api_dir) |
| 71 | + namex.generate_api_files( |
| 72 | + "keras_nlp", code_directory="src", target_directory="api" |
| 73 | + ) |
| 74 | + # Add __version__ to keras package |
| 75 | + export_version_string(build_api_init_fname) |
| 76 | + # Copy back the keras_nlp/api and keras_nlp/__init__.py from build dir |
| 77 | + if os.path.exists(code_api_dir): |
| 78 | + shutil.rmtree(code_api_dir) |
| 79 | + shutil.copytree(build_api_dir, code_api_dir) |
| 80 | + finally: |
| 81 | + # Clean up: remove the build directory (no longer needed) |
| 82 | + shutil.rmtree(build_dir) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + build() |
0 commit comments