31
31
import re
32
32
import shutil
33
33
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
-
41
34
hub_package = "keras_hub"
42
35
nlp_package = "keras_nlp"
43
36
build_directory = "tmp_build_dir"
44
37
dist_directory = "dist"
45
38
to_copy = ["pyproject.toml" , "README.md" ]
46
39
47
40
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 ]
78
43
79
44
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 )
82
48
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 ):
99
74
# Copy sources (`keras_hub/` directory and setup files) to build
100
75
# 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
+ )
102
81
for fname in to_copy :
103
- shutil .copy (src / fname , dst / fname )
82
+ shutil .copy (root_path / fname , root_path / build_directory / fname )
104
83
105
84
106
- def build_wheel (build_path , dist_path , name , version ):
85
+ def build_wheel (build_path , dist_path , version ):
107
86
# Build the package
108
87
os .chdir (build_path )
109
88
os .system ("python3 -m build" )
@@ -114,52 +93,50 @@ def build_wheel(build_path, dist_path, name, version):
114
93
for fpath in (build_path / dist_directory ).glob ("*.*" ):
115
94
shutil .copy (fpath , dist_path )
116
95
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
124
104
125
105
126
106
def build (root_path , is_nightly = False , keras_nlp = True ):
127
107
if os .path .exists (build_directory ):
128
108
raise ValueError (f"Directory already exists: { build_directory } " )
129
109
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
+
130
122
try :
131
123
whls = []
132
124
build_path = root_path / build_directory
133
125
dist_path = root_path / dist_directory
134
126
os .mkdir (build_path )
135
- copy_source_to_build_directory (root_path , build_path , hub_package )
136
127
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 )
146
131
whls .append (whl )
147
132
148
133
if keras_nlp :
149
134
build_path = root_path / build_directory / nlp_package
150
135
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 )
163
140
whls .append (whl )
164
141
165
142
return whls
0 commit comments