diff --git a/pip_build.py b/pip_build.py index 24ad3799dd..3abfb95003 100644 --- a/pip_build.py +++ b/pip_build.py @@ -47,7 +47,7 @@ def ignore_files(_, filenames): - return [f for f in filenames if "test" in f] + return [f for f in filenames if "_test" in f] def export_version_string(version, is_nightly=False): @@ -71,54 +71,66 @@ def export_version_string(version, is_nightly=False): f.write(init_contents + "\n\n" + f'__version__ = "{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/src` + namex.convert_codebase(package, code_directory="src") + + # Generate API __init__.py files in `keras/` + 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}") - whl_path = None try: - # Copy sources (`keras_cv/` directory and setup files) to build dir - 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) - - # 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) - - # Make sure to export the __version__ string + copy_source_to_build_directory(root_path) + run_namex_conversion() from keras_cv.src import __version__ # noqa: E402 export_version_string(__version__, is_nightly) - - # 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 - 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)) - print(f"Build successful. Wheel file available at {whl_path}") + return build_and_save_output(root_path, __version__) finally: # Clean up: remove the build directory (no longer needed) shutil.rmtree(build_directory) - return whl_path def install_whl(whl_fpath):