@@ -18,28 +18,41 @@ def ignore_files(_, filenames):
18
18
return [f for f in filenames if f .endswith ("_test.py" )]
19
19
20
20
21
- def create_legacy_directory ():
22
- API_DIR = os .path .join (package , "api" )
21
+ def copy_source_to_build_directory (root_path ):
22
+ # Copy sources (`keras/` directory and setup files) to build dir
23
+ build_dir = os .path .join (root_path , "tmp_build_dir" )
24
+ if os .path .exists (build_dir ):
25
+ shutil .rmtree (build_dir )
26
+ os .mkdir (build_dir )
27
+ shutil .copytree (
28
+ package , os .path .join (build_dir , package ), ignore = ignore_files
29
+ )
30
+ return build_dir
31
+
32
+
33
+ def create_legacy_directory (package_dir ):
34
+ src_dir = os .path .join (package_dir , "src" )
35
+ api_dir = os .path .join (package_dir , "api" )
23
36
# Make keras/_tf_keras/ by copying keras/
24
- tf_keras_dirpath_parent = os .path .join (API_DIR , "_tf_keras" )
37
+ tf_keras_dirpath_parent = os .path .join (api_dir , "_tf_keras" )
25
38
tf_keras_dirpath = os .path .join (tf_keras_dirpath_parent , "keras" )
26
39
os .makedirs (tf_keras_dirpath , exist_ok = True )
27
40
with open (os .path .join (tf_keras_dirpath_parent , "__init__.py" ), "w" ) as f :
28
41
f .write ("from keras.api._tf_keras import keras\n " )
29
- with open (os .path .join (API_DIR , "__init__.py" )) as f :
42
+ with open (os .path .join (api_dir , "__init__.py" )) as f :
30
43
init_file = f .read ()
31
44
init_file = init_file .replace (
32
45
"from keras.api import _legacy" ,
33
46
"from keras.api import _tf_keras" ,
34
47
)
35
- with open (os .path .join (API_DIR , "__init__.py" ), "w" ) as f :
48
+ with open (os .path .join (api_dir , "__init__.py" ), "w" ) as f :
36
49
f .write (init_file )
37
50
# Remove the import of `_tf_keras` in `keras/_tf_keras/keras/__init__.py`
38
51
init_file = init_file .replace ("from keras.api import _tf_keras\n " , "\n " )
39
52
with open (os .path .join (tf_keras_dirpath , "__init__.py" ), "w" ) as f :
40
53
f .write (init_file )
41
- for dirname in os .listdir (API_DIR ):
42
- dirpath = os .path .join (API_DIR , dirname )
54
+ for dirname in os .listdir (api_dir ):
55
+ dirpath = os .path .join (api_dir , dirname )
43
56
if os .path .isdir (dirpath ) and dirname not in (
44
57
"_legacy" ,
45
58
"_tf_keras" ,
@@ -57,16 +70,16 @@ def create_legacy_directory():
57
70
# Copy keras/_legacy/ file contents to keras/_tf_keras/keras
58
71
legacy_submodules = [
59
72
path [:- 3 ]
60
- for path in os .listdir (os .path .join (package , "src" , "legacy" ))
73
+ for path in os .listdir (os .path .join (src_dir , "legacy" ))
61
74
if path .endswith (".py" )
62
75
]
63
76
legacy_submodules += [
64
77
path
65
- for path in os .listdir (os .path .join (package , "src" , "legacy" ))
66
- if os .path .isdir (os .path .join (package , "src" , "legacy" , path ))
78
+ for path in os .listdir (os .path .join (src_dir , "legacy" ))
79
+ if os .path .isdir (os .path .join (src_dir , "legacy" , path ))
67
80
]
68
81
69
- for root , _ , fnames in os .walk (os .path .join (package , "_legacy" )):
82
+ for root , _ , fnames in os .walk (os .path .join (package_dir , "_legacy" )):
70
83
for fname in fnames :
71
84
if fname .endswith (".py" ):
72
85
legacy_fpath = os .path .join (root , fname )
@@ -102,19 +115,18 @@ def create_legacy_directory():
102
115
f .write (legacy_contents )
103
116
104
117
# Delete keras/api/_legacy/
105
- shutil .rmtree (os .path .join (API_DIR , "_legacy" ))
118
+ shutil .rmtree (os .path .join (api_dir , "_legacy" ))
106
119
107
120
108
- def export_version_string ():
109
- API_INIT = os .path .join (package , "api" , "__init__.py" )
110
- with open (API_INIT ) as f :
121
+ def export_version_string (api_init_fname ):
122
+ with open (api_init_fname ) as f :
111
123
contents = f .read ()
112
- with open (API_INIT , "w" ) as f :
124
+ with open (api_init_fname , "w" ) as f :
113
125
contents += "from keras.src.version import __version__\n "
114
126
f .write (contents )
115
127
116
128
117
- def update_package_init ():
129
+ def update_package_init (init_fname ):
118
130
contents = """
119
131
# Import everything from /api/ into keras.
120
132
from keras.api import * # noqa: F403
@@ -142,34 +154,48 @@ def __dir__():
142
154
for name in globals().keys()
143
155
if not (name.startswith("_") or name in ("src", "api"))
144
156
]"""
145
- with open (os . path . join ( package , "__init__.py" ) ) as f :
157
+ with open (init_fname ) as f :
146
158
init_contents = f .read ()
147
- with open (os . path . join ( package , "__init__.py" ) , "w" ) as f :
159
+ with open (init_fname , "w" ) as f :
148
160
f .write (init_contents .replace ("\n from keras import api" , contents ))
149
161
150
162
151
- if __name__ == "__main__" :
163
+ def build () :
152
164
# Backup the `keras/__init__.py` and restore it on error in api gen.
153
- os .makedirs (os .path .join (package , "api" ), exist_ok = True )
154
- init_fname = os .path .join (package , "__init__.py" )
155
- backup_init_fname = os .path .join (package , "__init__.py.bak" )
165
+ root_path = os .path .dirname (os .path .abspath (__file__ ))
166
+ code_api_dir = os .path .join (root_path , package , "api" )
167
+ code_init_fname = os .path .join (root_path , package , "__init__.py" )
168
+ # Create temp build dir
169
+ build_dir = copy_source_to_build_directory (root_path )
170
+ build_api_dir = os .path .join (build_dir , package , "api" )
171
+ build_init_fname = os .path .join (build_dir , package , "__init__.py" )
172
+ build_api_init_fname = os .path .join (build_api_dir , "__init__.py" )
156
173
try :
157
- if os .path .exists (init_fname ):
158
- shutil .move (init_fname , backup_init_fname )
174
+ os .chdir (build_dir )
159
175
# Generates `keras/api` directory.
176
+ if os .path .exists (build_api_dir ):
177
+ shutil .rmtree (build_api_dir )
178
+ if os .path .exists (build_init_fname ):
179
+ os .remove (build_init_fname )
180
+ os .makedirs (build_api_dir )
160
181
namex .generate_api_files (
161
182
"keras" , code_directory = "src" , target_directory = "api"
162
183
)
163
184
# Creates `keras/__init__.py` importing from `keras/api`
164
- update_package_init ()
165
- except Exception as e :
166
- if os .path .exists (backup_init_fname ):
167
- shutil .move (backup_init_fname , init_fname )
168
- raise e
185
+ update_package_init (build_init_fname )
186
+ # Add __version__ to keras package
187
+ export_version_string (build_api_init_fname )
188
+ # Creates `_tf_keras` with full keras API
189
+ create_legacy_directory (package_dir = os .path .join (build_dir , package ))
190
+ # Copy back the keras/api and keras/__init__.py from build directory
191
+ if os .path .exists (code_api_dir ):
192
+ shutil .rmtree (code_api_dir )
193
+ shutil .copytree (build_api_dir , code_api_dir )
194
+ shutil .copy (build_init_fname , code_init_fname )
169
195
finally :
170
- if os . path . exists ( backup_init_fname ):
171
- os . remove ( backup_init_fname )
172
- # Add __version__ to keras package
173
- export_version_string ()
174
- # Creates `_tf_keras` with full keras API
175
- create_legacy_directory ()
196
+ # Clean up: remove the build directory (no longer needed)
197
+ shutil . rmtree ( build_dir )
198
+
199
+
200
+ if __name__ == "__main__" :
201
+ build ()
0 commit comments