Skip to content

Fix up package build scripts again #2230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions RELEASE_PROCESS.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Use the following steps to create an `X.Y.0` release.
of the package. Development releases will have version numbers like
`X.Y.0.dev0`, and critically will never be installed by default by `pip`.

On the relase branch, check the version in `src/version_utils.py`. If the
On the relase branch, check the version in `src/version.py`. If the
current version is not `X.Y.0.dev0`, make a PR following [this template](https://github.com/keras-team/keras-hub/pull/1638)
to update the our version number fo look like `X.Y.0.dev0`. This PR should
base off our new release branch instead of the master branch. You can use the
Expand Down Expand Up @@ -164,7 +164,7 @@ to push certain fixes out to our users.
of the package. Development releases will have version numbers like
`X.Y.Z.dev0`, and critically will never be installed by default by `pip`.

On the relase branch, check the version in `src/version_utils.py`. If the
On the relase branch, check the version in `src/version.py`. If the
current version is not `X.Y.Z.dev0`, make a PR following [this template](https://github.com/keras-team/keras-hub/pull/1638)
to update the our version number fo look like `X.Y.Z.dev0`. This PR should
base off our new release branch instead of the master branch. You can use the
Expand Down
4 changes: 3 additions & 1 deletion api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def export_version_string(api_init_fname):
with open(api_init_fname) as f:
contents = f.read()
with open(api_init_fname, "w") as f:
contents += "from keras_hub.src.version_utils import __version__ as __version__\n" # noqa: E501
contents += (
"from keras_hub.src.version import __version__ as __version__\n"
)
f.write(contents)


Expand Down
4 changes: 2 additions & 2 deletions keras_hub/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
from keras_hub import tokenizers as tokenizers
from keras_hub import utils as utils
from keras_hub.src.utils.preset_utils import upload_preset as upload_preset
from keras_hub.src.version_utils import __version__ as __version__
from keras_hub.src.version_utils import version as version
from keras_hub.src.version import __version__ as __version__
from keras_hub.src.version import version as version
2 changes: 1 addition & 1 deletion keras_hub/src/utils/preset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ def _save_serialized_object(self, layer, config_file):

def _save_metadata(self, layer):
from keras_hub.src.models.task import Task
from keras_hub.src.version_utils import __version__ as keras_hub_version
from keras_hub.src.version import __version__ as keras_hub_version

# Find all tasks that are compatible with the backbone.
# E.g. for `BertBackbone` we would have `TextClassifier` and `MaskedLM`.
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion keras_nlp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
KerasNLP has renamed to KerasHub! Read the announcement
[here](https://github.com/keras-team/keras-nlp/issues/1831).

This directory contains a shim package for `keras-nlp` so that the old style
This contains a shim package for `keras-nlp` so that the old style
`pip install keras-nlp` and `import keras_nlp` continue to work.
8 changes: 3 additions & 5 deletions keras_nlp/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ build-backend = "setuptools.build_meta"

[project]
name = "keras-nlp"
version = "0.0.0" # We be replace in pip_build.py
version = "0.0.0" # Will be replaced in pip_build.py
dependencies = ["keras-hub==0.0.0"] # Will be replaced in pip_build.py
authors = [
{name = "Keras team", email = "[email protected]"},
]
description = "Pretrained models for Keras."
readme = "README.md"
requires-python = ">=3.9"
license = {text = "Apache License 2.0"}
license = "Apache-2.0"
classifiers = [
"Development Status :: 3 - Alpha",
"Programming Language :: Python :: 3",
Expand All @@ -26,9 +27,6 @@ classifiers = [
"Topic :: Scientific/Engineering",
"Topic :: Software Development",
]
dependencies = [
"keras-hub==0.0.0", # We be replace in pip_build.py
]

[project.urls]
Home = "https://keras.io/keras_hub/"
Expand Down
151 changes: 87 additions & 64 deletions pip_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,58 +31,79 @@
import re
import shutil

from keras_hub.src.version import __version__


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


hub_package = "keras_hub"
nlp_package = "keras_nlp"
build_directory = "tmp_build_dir"
dist_directory = "dist"
to_copy = ["pyproject.toml", "README.md"]


def ignore_files(_, filenames):
return [f for f in filenames if "_test" in f]
def update_nightly_version(build_path, version):
"""Rewrite library version with the nightly package version."""
date = datetime.datetime.now()
new_version = re.sub(
r"([0-9]+\.[0-9]+\.[0-9]+).*", # Match version without suffix.
r"\1.dev" + date.strftime("%Y%m%d%H%M"), # Add dev{date} suffix.
version,
)

version_file = build_path / hub_package / "src" / "version.py"
version_contents = version_file.read_text()
version_contents = re.sub(
"\n__version__ = .*\n",
f'\n__version__ = "{new_version}"\n',
version_contents,
)
version_file.write_text(version_contents)
return new_version


def update_nightly_name(build_path, pkg_name):
"""Rewrite library name with the nightly package name."""
new_pkg_name = f"{pkg_name}-nightly"
pyproj_file = build_path / "pyproject.toml"
pyproj_contents = pyproj_file.read_text()
pyproj_contents = pyproj_contents.replace(
f'name = "{pkg_name}"', f'name = "{new_pkg_name}"'
)
pyproj_file.write_text(pyproj_contents)
return new_pkg_name


def update_build_files(build_path, package, version, is_nightly=False):
package_name = package.replace("-", "_")
build_path = pathlib.Path(build_path)
def pin_keras_nlp_version(build_path, pkg_name, version):
"""Pin keras-nlp version and dependency to the keras-hub version."""
pyproj_file = build_path / "pyproject.toml"
if is_nightly:
pyproj_contents = pyproj_file.read_text().replace(
f'name = "{package_name}"', f'name = "{package_name}-nightly"'
)
pyproj_file.write_text(pyproj_contents)

# Update the version.
if package == hub_package:
# KerasHub pyproject reads the version dynamically from source.
version_file = build_path / package / "src" / "version_utils.py"
version_contents = version_file.read_text()
version_contents = re.sub(
"\n__version__ = .*\n",
f'\n__version__ = "{version}"\n',
version_contents,
)
version_file.write_text(version_contents)
elif package == nlp_package:
# For the KerasNLP shim we need to replace the version in the pyproject
# file, so we can pin the version of the keras-hub in dependencies.
pyproj_str = pyproj_file.read_text().replace("0.0.0", version)
pyproj_file.write_text(pyproj_str)


def copy_source_to_build_directory(root_path, package):
pyproj_contents = pyproj_file.read_text()
pyproj_contents = re.sub(
"version = .*\n",
f'version = "{version}"\n',
pyproj_contents,
)

pyproj_contents = re.sub(
"dependencies = .*\n",
f'dependencies = ["{pkg_name}=={version}"]\n',
pyproj_contents,
)
pyproj_file.write_text(pyproj_contents)


def copy_source_to_build_directory(src, dst, package):
# Copy sources (`keras_hub/` directory and setup files) to build
# directory
shutil.copytree(
root_path / package,
root_path / build_directory / package,
ignore=ignore_files,
)
shutil.copytree(src / package, dst / package, ignore=ignore_files)
for fname in to_copy:
shutil.copy(root_path / fname, root_path / build_directory / fname)
shutil.copy(src / fname, dst / fname)


def build_wheel(build_path, dist_path, version):
def build_wheel(build_path, dist_path, name, version):
# Build the package
os.chdir(build_path)
os.system("python3 -m build")
Expand All @@ -93,50 +114,52 @@ def build_wheel(build_path, dist_path, version):
for fpath in (build_path / dist_directory).glob("*.*"):
shutil.copy(fpath, dist_path)

# Find the .whl file path
for fname in os.listdir(dist_path):
if version in fname and fname.endswith(".whl"):
whl_path = dist_path / fname
print(f"Build successful. Wheel file available at {whl_path}")
return whl_path
print("Build failed.")
return None
# Check for the expected .whl file path
name = name.replace("-", "_")
whl_path = dist_path / f"{name}-{version}-py3-none-any.whl"
if not os.path.exists(whl_path):
raise ValueError(f"Could not find whl {whl_path}")
print(f"Build successful. Wheel file available at {whl_path}")
return whl_path


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

from keras_hub.src.version_utils import __version__ # noqa: E402

if is_nightly:
date = datetime.datetime.now()
version = re.sub(
r"([0-9]+\.[0-9]+\.[0-9]+).*", # Match version without suffix.
r"\1.dev" + date.strftime("%Y%m%d%H%M"), # Add dev{date} suffix.
__version__,
)
else:
version = __version__

try:
whls = []
build_path = root_path / build_directory
dist_path = root_path / dist_directory
os.mkdir(build_path)
copy_source_to_build_directory(root_path, build_path, hub_package)

copy_source_to_build_directory(root_path, hub_package)
update_build_files(build_path, hub_package, version, is_nightly)
whl = build_wheel(build_path, dist_path, version)
version = __version__
pkg_name = hub_package.replace("_", "-")
if is_nightly:
version = update_nightly_version(build_path, version)
pkg_name = update_nightly_name(build_path, pkg_name)
assert "dev" in version, "Version should contain dev"
assert "nightly" in pkg_name, "Name should contain nightly"

whl = build_wheel(build_path, dist_path, pkg_name, version)
whls.append(whl)

if keras_nlp:
build_path = root_path / build_directory / nlp_package
dist_path = root_path / nlp_package / dist_directory

copy_source_to_build_directory(root_path, nlp_package)
update_build_files(build_path, nlp_package, version, is_nightly)
whl = build_wheel(build_path, dist_path, version)
copy_source_to_build_directory(
root_path / nlp_package, build_path, nlp_package
)

pin_keras_nlp_version(build_path, pkg_name, version)
nlp_pkg_name = nlp_package.replace("_", "-")
if is_nightly:
nlp_pkg_name = update_nightly_name(build_path, nlp_pkg_name)
assert "dev" in version, "Version should contain dev"
assert "nightly" in nlp_pkg_name, "Name should contain nightly"

whl = build_wheel(build_path, dist_path, nlp_pkg_name, version)
whls.append(whl)

return whls
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ authors = [
description = "Pretrained models for Keras."
readme = "README.md"
requires-python = ">=3.9"
license = {text = "Apache License 2.0"}
license = "Apache-2.0"
dynamic = ["version"]
classifiers = [
"Development Status :: 3 - Alpha",
Expand Down Expand Up @@ -42,7 +42,7 @@ Home = "https://keras.io/keras_hub/"
Repository = "https://github.com/keras-team/keras/keras_hub"

[tool.setuptools.dynamic]
version = {attr = "keras_hub.src.version_utils.__version__"}
version = {attr = "keras_hub.src.version.__version__"}

[tool.setuptools.package-dir]
"" = "."
Expand Down
Loading