3131import re
3232import 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+
3441hub_package = "keras_hub"
3542nlp_package = "keras_nlp"
3643build_directory = "tmp_build_dir"
3744dist_directory = "dist"
3845to_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
106126def 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
0 commit comments