# Copyright 2023 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Script to create (and optionally install) a `.whl` archive for KerasCV.

Usage:

1. Create a `.whl` file in `dist/`:

```
python3 pip_build.py
```

2. Also install the new package immediately after:

```
python3 pip_build.py --install
```
"""
import argparse
import datetime
import glob
import os
import pathlib
import shutil

import namex

package = "keras_cv"
build_directory = "tmp_build_dir"
dist_directory = "dist"
to_copy = [
    "setup.py",
    "setup.cfg",
    "README.md",
]


def ignore_files(_, filenames):
    return [f for f in filenames if "_test" in f]


def export_version_string(version, is_nightly=False):
    """Export Version and Package Name."""
    if is_nightly:
        date = datetime.datetime.now()
        version += f".dev{date.strftime('%Y%m%d%H')}"
        # Replaces `name="keras-cv"` in `setup.py` with `keras-cv-nightly`
        with open("setup.py") as f:
            setup_contents = f.read()
        with open("setup.py", "w") as f:
            setup_contents = setup_contents.replace(
                'name="keras-cv"', 'name="keras-cv-nightly"'
            )
            f.write(setup_contents)

    # Overwrite the version string with our package version.
    with open(os.path.join(package, "src", "version_utils.py")) as f:
        version_contents = f.readlines()
    with open(os.path.join(package, "src", "version_utils.py"), "w") as f:
        for line in version_contents:
            if line.startswith("__version__"):
                f.write(f'__version__ = "{version}"\n')
            else:
                f.write(line)

    # Make sure to export the __version__ string
    with open(os.path.join(package, "__init__.py")) as f:
        init_contents = f.read()
    with open(os.path.join(package, "__init__.py"), "w") as f:
        f.write(init_contents)
        f.write("from keras_cv.src.version_utils import __version__\n")


def copy_source_to_build_directory(root_path):
    # Copy sources (`keras_cv/` directory and setup files) to build
    # directory
    os.chdir(root_path)
    os.mkdir(build_directory)
    shutil.copytree(
        package, os.path.join(build_directory, package), ignore=ignore_files
    )
    for fname in to_copy:
        shutil.copy(fname, os.path.join(f"{build_directory}", fname))
    os.chdir(build_directory)


def run_namex_conversion():
    # Restructure the codebase so that source files live in `keras_cv/src`
    namex.convert_codebase(package, code_directory="src")

    # Generate API __init__.py files in `keras_cv/`
    namex.generate_api_files(package, code_directory="src", verbose=True)


def build_and_save_output(root_path, __version__):
    """Build the package."""
    os.system("python3 -m build")

    # Save the dist files generated by the build process
    os.chdir(root_path)
    if not os.path.exists(dist_directory):
        os.mkdir(dist_directory)
    for fpath in glob.glob(
        os.path.join(build_directory, dist_directory, "*.*")
    ):
        shutil.copy(fpath, dist_directory)

    # Find the .whl file path
    whl_path = None
    for fname in os.listdir(dist_directory):
        if __version__ in fname and fname.endswith(".whl"):
            whl_path = os.path.abspath(os.path.join(dist_directory, fname))
    if whl_path:
        print(f"Build successful. Wheel file available at {whl_path}")
    else:
        print("Build failed.")
    return whl_path


def build(root_path, is_nightly=False):
    if os.path.exists(build_directory):
        raise ValueError(f"Directory already exists: {build_directory}")

    try:
        copy_source_to_build_directory(root_path)
        run_namex_conversion()
        # Make sure to export the __version__ string
        from keras_cv.src import __version__  # noqa: E402

        export_version_string(__version__, is_nightly)
        return build_and_save_output(root_path, __version__)
    finally:
        # Clean up: remove the build directory (no longer needed)
        shutil.rmtree(build_directory)


def install_whl(whl_fpath):
    print("Installing wheel file.")
    os.system(f"pip3 install {whl_fpath} --force-reinstall --no-dependencies")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--install",
        action="store_true",
        help="Whether to install the generated wheel file.",
    )
    parser.add_argument(
        "--nightly",
        action="store_true",
        help="Whether to generate nightly wheel file.",
    )
    args = parser.parse_args()
    root_path = pathlib.Path(__file__).parent.resolve()
    whl_path = build(root_path, args.nightly)
    if whl_path and args.install:
        install_whl(whl_path)
