@@ -60,6 +60,57 @@ def export_version_string(version, is_nightly=False, rc_index=None):
60
60
f .write (init_contents )
61
61
62
62
63
+ def ignore_files (_ , filenames ):
64
+ return [f for f in filenames if f .endswith ("_test.py" )]
65
+
66
+
67
+ def copy_source_to_build_directory (root_path ):
68
+ # Copy sources (`keras/` directory and setup files) to build
69
+ # directory
70
+ os .chdir (root_path )
71
+ os .mkdir (build_directory )
72
+ shutil .copytree (
73
+ package , os .path .join (build_directory , package ), ignore = ignore_files
74
+ )
75
+ for fname in to_copy :
76
+ shutil .copy (fname , os .path .join (f"{ build_directory } " , fname ))
77
+ os .chdir (build_directory )
78
+
79
+
80
+ def build (root_path , is_nightly = False , rc_index = None ):
81
+ if os .path .exists (build_directory ):
82
+ raise ValueError (f"Directory already exists: { build_directory } " )
83
+
84
+ try :
85
+ copy_source_to_build_directory (root_path )
86
+ create_legacy_directory ()
87
+ from keras .src .version import __version__ # noqa: E402
88
+
89
+ export_version_string (__version__ , is_nightly , rc_index )
90
+ return build_and_save_output (root_path , __version__ )
91
+ finally :
92
+ # Clean up: remove the build directory (no longer needed)
93
+ shutil .rmtree (build_directory )
94
+
95
+
96
+ def create_legacy_directory ():
97
+ shutil .move (os .path .join ("keras" , "api" , "_tf_keras" ), "keras" )
98
+ with open (os .path .join ("keras" , "api" , "__init__.py" )) as f :
99
+ contents = f .read ()
100
+ contents = contents .replace ("from keras.api import _tf_keras" , "" )
101
+ with open (os .path .join ("keras" , "api" , "__init__.py" ), "w" ) as f :
102
+ f .write (contents )
103
+
104
+ with open (os .path .join ("keras" , "_tf_keras" , "__init__.py" )) as f :
105
+ contents = f .read ()
106
+ contents = contents .replace (
107
+ "from keras.api._tf_keras import keras" ,
108
+ "from keras._tf_keras import keras" ,
109
+ )
110
+ with open (os .path .join ("keras" , "_tf_keras" , "__init__.py" ), "w" ) as f :
111
+ f .write (contents )
112
+
113
+
63
114
def build_and_save_output (root_path , __version__ ):
64
115
# Build the package
65
116
os .system ("python3 -m build" )
@@ -85,13 +136,6 @@ def build_and_save_output(root_path, __version__):
85
136
return whl_path
86
137
87
138
88
- def build (root_path , is_nightly = False , rc_index = None ):
89
- from keras .src .version import __version__ # noqa: E402
90
-
91
- export_version_string (__version__ , is_nightly , rc_index )
92
- return build_and_save_output (root_path , __version__ )
93
-
94
-
95
139
def install_whl (whl_fpath ):
96
140
print (f"Installing wheel file: { whl_fpath } " )
97
141
os .system (f"pip3 install { whl_fpath } --force-reinstall --no-dependencies" )
0 commit comments