Skip to content

Commit 54db6d8

Browse files
Revert "Fix up package build scripts with better error checking (keras-team#2230)"
This reverts commit db7b7a4.
1 parent abadacb commit 54db6d8

File tree

9 files changed

+78
-101
lines changed

9 files changed

+78
-101
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.py`. If the
51+
On the relase branch, check the version in `src/version_utils.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.py`. If the
167+
On the relase branch, check the version in `src/version_utils.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

+1-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ 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 += (
40-
"from keras_hub.src.version import __version__ as __version__\n"
41-
)
39+
contents += "from keras_hub.src.version_utils import __version__ as __version__\n" # noqa: E501
4240
f.write(contents)
4341

4442

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 import __version__ as __version__
15-
from keras_hub.src.version import version as version
14+
from keras_hub.src.version_utils import __version__ as __version__
15+
from keras_hub.src.version_utils import version as version

keras_hub/src/utils/preset_utils.py

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

852852
def _save_metadata(self, layer):
853853
from keras_hub.src.models.task import Task
854-
from keras_hub.src.version import __version__ as keras_hub_version
854+
from keras_hub.src.version_utils import __version__ as keras_hub_version
855855

856856
# Find all tasks that are compatible with the backbone.
857857
# 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 contains a shim package for `keras-nlp` so that the old style
6+
This directory 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

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

55
[project]
66
name = "keras-nlp"
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
7+
version = "0.0.0" # We be replace in pip_build.py
98
authors = [
109
{name = "Keras team", email = "keras-users@googlegroups.com"},
1110
]
1211
description = "Pretrained models for Keras."
1312
readme = "README.md"
1413
requires-python = ">=3.9"
15-
license = "Apache-2.0"
14+
license = {text = "Apache License 2.0"}
1615
classifiers = [
1716
"Development Status :: 3 - Alpha",
1817
"Programming Language :: Python :: 3",
@@ -27,6 +26,9 @@ classifiers = [
2726
"Topic :: Scientific/Engineering",
2827
"Topic :: Software Development",
2928
]
29+
dependencies = [
30+
"keras-hub==0.0.0", # We be replace in pip_build.py
31+
]
3032

3133
[project.urls]
3234
Home = "https://keras.io/keras_hub/"

pip_build.py

+64-87
Original file line numberDiff line numberDiff line change
@@ -31,79 +31,58 @@
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-
4134
hub_package = "keras_hub"
4235
nlp_package = "keras_nlp"
4336
build_directory = "tmp_build_dir"
4437
dist_directory = "dist"
4538
to_copy = ["pyproject.toml", "README.md"]
4639

4740

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, pkg_name):
69-
"""Rewrite library name with the nightly package name."""
70-
new_pkg_name = f"{pkg_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 = "{pkg_name}"', f'name = "{new_pkg_name}"'
75-
)
76-
pyproj_file.write_text(pyproj_contents)
77-
return new_pkg_name
41+
def ignore_files(_, filenames):
42+
return [f for f in filenames if "_test" in f]
7843

7944

80-
def pin_keras_nlp_version(build_path, pkg_name, version):
81-
"""Pin keras-nlp version and dependency to the keras-hub version."""
45+
def update_build_files(build_path, package, version, is_nightly=False):
46+
package_name = package.replace("-", "_")
47+
build_path = pathlib.Path(build_path)
8248
pyproj_file = build_path / "pyproject.toml"
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):
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):
9974
# Copy sources (`keras_hub/` directory and setup files) to build
10075
# directory
101-
shutil.copytree(src / package, dst / package, ignore=ignore_files)
76+
shutil.copytree(
77+
root_path / package,
78+
root_path / build_directory / package,
79+
ignore=ignore_files,
80+
)
10281
for fname in to_copy:
103-
shutil.copy(src / fname, dst / fname)
82+
shutil.copy(root_path / fname, root_path / build_directory / fname)
10483

10584

106-
def build_wheel(build_path, dist_path, name, version):
85+
def build_wheel(build_path, dist_path, version):
10786
# Build the package
10887
os.chdir(build_path)
10988
os.system("python3 -m build")
@@ -114,52 +93,50 @@ def build_wheel(build_path, dist_path, name, version):
11493
for fpath in (build_path / dist_directory).glob("*.*"):
11594
shutil.copy(fpath, dist_path)
11695

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
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
124104

125105

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

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+
130122
try:
131123
whls = []
132124
build_path = root_path / build_directory
133125
dist_path = root_path / dist_directory
134126
os.mkdir(build_path)
135-
copy_source_to_build_directory(root_path, build_path, hub_package)
136127

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)
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)
146131
whls.append(whl)
147132

148133
if keras_nlp:
149134
build_path = root_path / build_directory / nlp_package
150135
dist_path = root_path / nlp_package / dist_directory
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)
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)
163140
whls.append(whl)
164141

165142
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 = "Apache-2.0"
13+
license = {text = "Apache License 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.__version__"}
45+
version = {attr = "keras_hub.src.version_utils.__version__"}
4646

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

0 commit comments

Comments
 (0)