Skip to content

Commit b8c1586

Browse files
committed
Fix up package build scripts with better error checking
1 parent e9a62ca commit b8c1586

File tree

9 files changed

+101
-78
lines changed

9 files changed

+101
-78
lines changed

RELEASE_PROCESS.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Use the following steps to create an `X.Y.0` release.
4848
of the package. Development releases will have version numbers like
4949
`X.Y.0.dev0`, and critically will never be installed by default by `pip`.
5050

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

167-
On the relase branch, check the version in `src/version_utils.py`. If the
167+
On the relase branch, check the version in `src/version.py`. If the
168168
current version is not `X.Y.Z.dev0`, make a PR following [this template](https://github.com/keras-team/keras-hub/pull/1638)
169169
to update the our version number fo look like `X.Y.Z.dev0`. This PR should
170170
base off our new release branch instead of the master branch. You can use the

api_gen.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ def export_version_string(api_init_fname):
3636
with open(api_init_fname) as f:
3737
contents = f.read()
3838
with open(api_init_fname, "w") as f:
39-
contents += "from keras_hub.src.version_utils import __version__ as __version__\n" # noqa: E501
39+
contents += (
40+
"from keras_hub.src.version import __version__ as __version__\n" # noqa: E501
41+
)
4042
f.write(contents)
4143

4244

keras_hub/api/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
from keras_hub import tokenizers as tokenizers
1212
from keras_hub import utils as utils
1313
from keras_hub.src.utils.preset_utils import upload_preset as upload_preset
14-
from keras_hub.src.version_utils import __version__ as __version__
15-
from keras_hub.src.version_utils import version as version
14+
from keras_hub.src.version import __version__ as __version__
15+
from keras_hub.src.version import version as version

keras_hub/src/utils/preset_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ def _save_serialized_object(self, layer, config_file):
801801

802802
def _save_metadata(self, layer):
803803
from keras_hub.src.models.task import Task
804-
from keras_hub.src.version_utils import __version__ as keras_hub_version
804+
from keras_hub.src.version import __version__ as keras_hub_version
805805

806806
# Find all tasks that are compatible with the backbone.
807807
# E.g. for `BertBackbone` we would have `TextClassifier` and `MaskedLM`.
File renamed without changes.

keras_nlp/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
KerasNLP has renamed to KerasHub! Read the announcement
44
[here](https://github.com/keras-team/keras-nlp/issues/1831).
55

6-
This directory contains a shim package for `keras-nlp` so that the old style
6+
This contains a shim package for `keras-nlp` so that the old style
77
`pip install keras-nlp` and `import keras_nlp` continue to work.

keras_nlp/pyproject.toml

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "keras-nlp"
7-
version = "0.0.0" # We be replace in pip_build.py
7+
version = "0.0.0" # Will be replaced in pip_build.py
8+
dependencies = ["keras-hub==0.0.0"] # Will be replaced in pip_build.py
89
authors = [
910
{name = "Keras team", email = "[email protected]"},
1011
]
1112
description = "Pretrained models for Keras."
1213
readme = "README.md"
1314
requires-python = ">=3.9"
14-
license = {text = "Apache License 2.0"}
15+
license = "Apache-2.0"
1516
classifiers = [
1617
"Development Status :: 3 - Alpha",
1718
"Programming Language :: Python :: 3",
@@ -26,9 +27,6 @@ classifiers = [
2627
"Topic :: Scientific/Engineering",
2728
"Topic :: Software Development",
2829
]
29-
dependencies = [
30-
"keras-hub==0.0.0", # We be replace in pip_build.py
31-
]
3230

3331
[project.urls]
3432
Home = "https://keras.io/keras_hub/"

pip_build.py

+87-64
Original file line numberDiff line numberDiff line change
@@ -31,58 +31,79 @@
3131
import re
3232
import shutil
3333

34+
from keras_hub.src.version import __version__
35+
36+
37+
def ignore_files(_, filenames):
38+
return [f for f in filenames if "_test" in f]
39+
40+
3441
hub_package = "keras_hub"
3542
nlp_package = "keras_nlp"
3643
build_directory = "tmp_build_dir"
3744
dist_directory = "dist"
3845
to_copy = ["pyproject.toml", "README.md"]
3946

4047

41-
def ignore_files(_, filenames):
42-
return [f for f in filenames if "_test" in f]
48+
def update_nightly_version(build_path, version):
49+
"""Rewrite library version with the nightly package version."""
50+
date = datetime.datetime.now()
51+
new_version = re.sub(
52+
r"([0-9]+\.[0-9]+\.[0-9]+).*", # Match version without suffix.
53+
r"\1.dev" + date.strftime("%Y%m%d%H%M"), # Add dev{date} suffix.
54+
version,
55+
)
56+
57+
version_file = build_path / hub_package / "src" / "version.py"
58+
version_contents = version_file.read_text()
59+
version_contents = re.sub(
60+
"\n__version__ = .*\n",
61+
f'\n__version__ = "{new_version}"\n',
62+
version_contents,
63+
)
64+
version_file.write_text(version_contents)
65+
return new_version
66+
67+
68+
def update_nightly_name(build_path, name):
69+
"""Rewrite library name with the nightly package name."""
70+
new_name = f"{name}-nightly"
71+
pyproj_file = build_path / "pyproject.toml"
72+
pyproj_contents = pyproj_file.read_text()
73+
pyproj_contents = pyproj_contents.replace(
74+
f'name = "{name}"', f'name = "{new_name}"'
75+
)
76+
pyproj_file.write_text(pyproj_contents)
77+
return new_name
4378

4479

45-
def update_build_files(build_path, package, version, is_nightly=False):
46-
package_name = package.replace("-", "_")
47-
build_path = pathlib.Path(build_path)
80+
def pin_keras_nlp_version(build_path, pkg_name, version):
81+
"""Pin keras-nlp version and dependency to the keras-hub version."""
4882
pyproj_file = build_path / "pyproject.toml"
49-
if is_nightly:
50-
pyproj_contents = pyproj_file.read_text().replace(
51-
f'name = "{package_name}"', f'name = "{package_name}-nightly"'
52-
)
53-
pyproj_file.write_text(pyproj_contents)
54-
55-
# Update the version.
56-
if package == hub_package:
57-
# KerasHub pyproject reads the version dynamically from source.
58-
version_file = build_path / package / "src" / "version_utils.py"
59-
version_contents = version_file.read_text()
60-
version_contents = re.sub(
61-
"\n__version__ = .*\n",
62-
f'\n__version__ = "{version}"\n',
63-
version_contents,
64-
)
65-
version_file.write_text(version_contents)
66-
elif package == nlp_package:
67-
# For the KerasNLP shim we need to replace the version in the pyproject
68-
# file, so we can pin the version of the keras-hub in dependencies.
69-
pyproj_str = pyproj_file.read_text().replace("0.0.0", version)
70-
pyproj_file.write_text(pyproj_str)
71-
72-
73-
def copy_source_to_build_directory(root_path, package):
83+
pyproj_contents = pyproj_file.read_text()
84+
pyproj_contents = re.sub(
85+
"version = .*\n",
86+
f'version = "{version}"\n',
87+
pyproj_contents,
88+
)
89+
90+
pyproj_contents = re.sub(
91+
"dependencies = .*\n",
92+
f'dependencies = ["{pkg_name}=={version}"]\n',
93+
pyproj_contents,
94+
)
95+
pyproj_file.write_text(pyproj_contents)
96+
97+
98+
def copy_source_to_build_directory(src, dst, package):
7499
# Copy sources (`keras_hub/` directory and setup files) to build
75100
# directory
76-
shutil.copytree(
77-
root_path / package,
78-
root_path / build_directory / package,
79-
ignore=ignore_files,
80-
)
101+
shutil.copytree(src / package, dst / package, ignore=ignore_files)
81102
for fname in to_copy:
82-
shutil.copy(root_path / fname, root_path / build_directory / fname)
103+
shutil.copy(src / fname, dst / fname)
83104

84105

85-
def build_wheel(build_path, dist_path, version):
106+
def build_wheel(build_path, dist_path, name, version):
86107
# Build the package
87108
os.chdir(build_path)
88109
os.system("python3 -m build")
@@ -93,50 +114,52 @@ def build_wheel(build_path, dist_path, version):
93114
for fpath in (build_path / dist_directory).glob("*.*"):
94115
shutil.copy(fpath, dist_path)
95116

96-
# Find the .whl file path
97-
for fname in os.listdir(dist_path):
98-
if version in fname and fname.endswith(".whl"):
99-
whl_path = dist_path / fname
100-
print(f"Build successful. Wheel file available at {whl_path}")
101-
return whl_path
102-
print("Build failed.")
103-
return None
117+
# Check for the expected .whl file path
118+
name = name.replace("-", "_")
119+
whl_path = dist_path / f"{name}-{version}-py3-none-any.whl"
120+
if not os.path.exists(whl_path):
121+
raise ValueError(f"Could not find whl {whl_path}")
122+
print(f"Build successful. Wheel file available at {whl_path}")
123+
return whl_path
104124

105125

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

110-
from keras_hub.src.version_utils import __version__ # noqa: E402
111-
112-
if is_nightly:
113-
date = datetime.datetime.now()
114-
version = re.sub(
115-
r"([0-9]+\.[0-9]+\.[0-9]+).*", # Match version without suffix.
116-
r"\1.dev" + date.strftime("%Y%m%d%H%M"), # Add dev{date} suffix.
117-
__version__,
118-
)
119-
else:
120-
version = __version__
121-
122130
try:
123131
whls = []
124132
build_path = root_path / build_directory
125133
dist_path = root_path / dist_directory
126134
os.mkdir(build_path)
135+
copy_source_to_build_directory(root_path, build_path, hub_package)
127136

128-
copy_source_to_build_directory(root_path, hub_package)
129-
update_build_files(build_path, hub_package, version, is_nightly)
130-
whl = build_wheel(build_path, dist_path, version)
137+
version = __version__
138+
pkg_name = hub_package.replace("_", "-")
139+
if is_nightly:
140+
version = update_nightly_version(build_path, version)
141+
pkg_name = update_nightly_name(build_path, pkg_name)
142+
assert "dev" in version, "Version should contain dev"
143+
assert "nightly" in pkg_name, "Name should contain nightly"
144+
145+
whl = build_wheel(build_path, dist_path, pkg_name, version)
131146
whls.append(whl)
132147

133148
if keras_nlp:
134149
build_path = root_path / build_directory / nlp_package
135150
dist_path = root_path / nlp_package / dist_directory
136-
137-
copy_source_to_build_directory(root_path, nlp_package)
138-
update_build_files(build_path, nlp_package, version, is_nightly)
139-
whl = build_wheel(build_path, dist_path, version)
151+
copy_source_to_build_directory(
152+
root_path / nlp_package, build_path, nlp_package
153+
)
154+
155+
pin_keras_nlp_version(build_path, pkg_name, version)
156+
nlp_pkg_name = nlp_package.replace("_", "-")
157+
if is_nightly:
158+
nlp_pkg_name = update_nightly_name(build_path, nlp_pkg_name)
159+
assert "dev" in version, "Version should contain dev"
160+
assert "nightly" in nlp_pkg_name, "Name should contain nightly"
161+
162+
whl = build_wheel(build_path, dist_path, nlp_pkg_name, version)
140163
whls.append(whl)
141164

142165
return whls

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ authors = [
1010
description = "Pretrained models for Keras."
1111
readme = "README.md"
1212
requires-python = ">=3.9"
13-
license = {text = "Apache License 2.0"}
13+
license = "Apache-2.0"
1414
dynamic = ["version"]
1515
classifiers = [
1616
"Development Status :: 3 - Alpha",
@@ -42,7 +42,7 @@ Home = "https://keras.io/keras_hub/"
4242
Repository = "https://github.com/keras-team/keras/keras_hub"
4343

4444
[tool.setuptools.dynamic]
45-
version = {attr = "keras_hub.src.version_utils.__version__"}
45+
version = {attr = "keras_hub.src.version.__version__"}
4646

4747
[tool.setuptools.package-dir]
4848
"" = "."

0 commit comments

Comments
 (0)