Skip to content

Commit 34a0388

Browse files
authored
Encapsulate build logic (#679)
* Encapsulate build logic Separated out build logic into it's own functional components to be able to pick and choose for different build systems. * Formatting * Separate version export
1 parent 610f91c commit 34a0388

File tree

1 file changed

+126
-112
lines changed

1 file changed

+126
-112
lines changed

pip_build.py

+126-112
Original file line numberDiff line numberDiff line change
@@ -35,132 +35,146 @@ def ignore_files(_, filenames):
3535
return [f for f in filenames if f.endswith("_test.py")]
3636

3737

38-
def build(root_path):
39-
if os.path.exists(build_directory):
40-
raise ValueError(f"Directory already exists: {build_directory}")
38+
def copy_source_to_build_directory(root_path):
39+
# Copy sources (`keras_core/` directory and setup files) to build
40+
# directory
41+
os.chdir(root_path)
42+
os.mkdir(build_directory)
43+
shutil.copytree(
44+
package, os.path.join(build_directory, package), ignore=ignore_files
45+
)
46+
for fname in to_copy:
47+
shutil.copy(fname, os.path.join(f"{build_directory}", fname))
48+
os.chdir(build_directory)
4149

42-
whl_path = None
43-
try:
44-
# Copy sources (`keras_core/` directory and setup files) to build
45-
# directory
46-
os.chdir(root_path)
47-
os.mkdir(build_directory)
48-
shutil.copytree(
49-
package, os.path.join(build_directory, package), ignore=ignore_files
50+
51+
def run_namex_conversion():
52+
# Restructure the codebase so that source files live in `keras_core/src`
53+
namex.convert_codebase(package, code_directory="src")
54+
55+
# Generate API __init__.py files in `keras_core/`
56+
namex.generate_api_files(package, code_directory="src", verbose=True)
57+
58+
59+
def create_legacy_directory():
60+
# Make keras_core/_tf_keras/ by copying keras_core/
61+
tf_keras_dirpath = os.path.join(package, "_tf_keras")
62+
os.makedirs(tf_keras_dirpath)
63+
with open(os.path.join(package, "__init__.py")) as f:
64+
init_file = f.read()
65+
init_file = init_file.replace(
66+
"from keras_core import _legacy",
67+
"from keras_core import _tf_keras",
5068
)
51-
for fname in to_copy:
52-
shutil.copy(fname, os.path.join(f"{build_directory}", fname))
53-
os.chdir(build_directory)
54-
55-
# Restructure the codebase so that source files live in `keras_core/src`
56-
namex.convert_codebase(package, code_directory="src")
57-
58-
# Generate API __init__.py files in `keras_core/`
59-
namex.generate_api_files(package, code_directory="src", verbose=True)
60-
61-
# Make keras_core/_tf_keras/ by copying keras_core/
62-
tf_keras_dirpath = os.path.join(package, "_tf_keras")
63-
os.makedirs(tf_keras_dirpath)
64-
with open(os.path.join(package, "__init__.py")) as f:
65-
init_file = f.read()
66-
init_file = init_file.replace(
67-
"from keras_core import _legacy",
68-
"from keras_core import _tf_keras",
69+
with open(os.path.join(package, "__init__.py"), "w") as f:
70+
f.write(init_file)
71+
with open(os.path.join(tf_keras_dirpath, "__init__.py"), "w") as f:
72+
f.write(init_file)
73+
for dirname in os.listdir(package):
74+
dirpath = os.path.join(package, dirname)
75+
if os.path.isdir(dirpath) and dirname not in (
76+
"_legacy",
77+
"_tf_keras",
78+
"src",
79+
):
80+
shutil.copytree(
81+
dirpath,
82+
os.path.join(tf_keras_dirpath, dirname),
83+
ignore=ignore_files,
6984
)
70-
with open(os.path.join(package, "__init__.py"), "w") as f:
71-
f.write(init_file)
72-
with open(os.path.join(tf_keras_dirpath, "__init__.py"), "w") as f:
73-
f.write(init_file)
74-
for dirname in os.listdir(package):
75-
dirpath = os.path.join(package, dirname)
76-
if os.path.isdir(dirpath) and dirname not in (
77-
"_legacy",
78-
"_tf_keras",
79-
"src",
80-
):
81-
shutil.copytree(
82-
dirpath,
83-
os.path.join(tf_keras_dirpath, dirname),
84-
ignore=ignore_files,
85-
)
8685

87-
# Copy keras_core/_legacy/ file contents to keras_core/_tf_keras/
88-
legacy_submodules = [
89-
path[:-3]
90-
for path in os.listdir(os.path.join(package, "src", "legacy"))
91-
if path.endswith(".py")
92-
]
93-
legacy_submodules += [
94-
path
95-
for path in os.listdir(os.path.join(package, "src", "legacy"))
96-
if os.path.isdir(os.path.join(package, "src", "legacy", path))
97-
]
98-
99-
for root, _, fnames in os.walk(os.path.join(package, "_legacy")):
100-
for fname in fnames:
101-
if fname.endswith(".py"):
102-
legacy_fpath = os.path.join(root, fname)
103-
tf_keras_root = root.replace("/_legacy", "/_tf_keras")
104-
core_api_fpath = os.path.join(
105-
root.replace("/_legacy", ""), fname
86+
# Copy keras_core/_legacy/ file contents to keras_core/_tf_keras/
87+
legacy_submodules = [
88+
path[:-3]
89+
for path in os.listdir(os.path.join(package, "src", "legacy"))
90+
if path.endswith(".py")
91+
]
92+
legacy_submodules += [
93+
path
94+
for path in os.listdir(os.path.join(package, "src", "legacy"))
95+
if os.path.isdir(os.path.join(package, "src", "legacy", path))
96+
]
97+
98+
for root, _, fnames in os.walk(os.path.join(package, "_legacy")):
99+
for fname in fnames:
100+
if fname.endswith(".py"):
101+
legacy_fpath = os.path.join(root, fname)
102+
tf_keras_root = root.replace("/_legacy", "/_tf_keras")
103+
core_api_fpath = os.path.join(
104+
root.replace("/_legacy", ""), fname
105+
)
106+
if not os.path.exists(tf_keras_root):
107+
os.makedirs(tf_keras_root)
108+
tf_keras_fpath = os.path.join(tf_keras_root, fname)
109+
with open(legacy_fpath) as f:
110+
legacy_contents = f.read()
111+
legacy_contents = legacy_contents.replace(
112+
"keras_core._legacy", "keras_core._tf_keras"
106113
)
107-
if not os.path.exists(tf_keras_root):
108-
os.makedirs(tf_keras_root)
109-
tf_keras_fpath = os.path.join(tf_keras_root, fname)
110-
with open(legacy_fpath) as f:
111-
legacy_contents = f.read()
112-
legacy_contents = legacy_contents.replace(
113-
"keras_core._legacy", "keras_core._tf_keras"
114-
)
115-
if os.path.exists(core_api_fpath):
116-
with open(core_api_fpath) as f:
117-
core_api_contents = f.read()
114+
if os.path.exists(core_api_fpath):
115+
with open(core_api_fpath) as f:
116+
core_api_contents = f.read()
117+
core_api_contents = core_api_contents.replace(
118+
"from keras_core import _tf_keras\n", ""
119+
)
120+
for legacy_submodule in legacy_submodules:
118121
core_api_contents = core_api_contents.replace(
119-
"from keras_core import _tf_keras\n", ""
122+
f"from keras_core import {legacy_submodule}\n",
123+
"",
120124
)
121-
for legacy_submodule in legacy_submodules:
122-
core_api_contents = core_api_contents.replace(
123-
f"from keras_core import {legacy_submodule}\n",
124-
"",
125-
)
126-
core_api_contents = core_api_contents.replace(
127-
f"keras_core.{legacy_submodule}",
128-
f"keras_core._tf_keras.{legacy_submodule}",
129-
)
130-
legacy_contents = (
131-
core_api_contents + "\n" + legacy_contents
125+
core_api_contents = core_api_contents.replace(
126+
f"keras_core.{legacy_submodule}",
127+
f"keras_core._tf_keras.{legacy_submodule}",
132128
)
133-
with open(tf_keras_fpath, "w") as f:
134-
f.write(legacy_contents)
129+
legacy_contents = core_api_contents + "\n" + legacy_contents
130+
with open(tf_keras_fpath, "w") as f:
131+
f.write(legacy_contents)
135132

136-
# Delete keras_core/_legacy/
137-
shutil.rmtree(os.path.join(package, "_legacy"))
133+
# Delete keras_core/_legacy/
134+
shutil.rmtree(os.path.join(package, "_legacy"))
138135

139-
# Make sure to export the __version__ string
140-
from keras_core.src.version import __version__ # noqa: E402
141136

142-
with open(os.path.join(package, "__init__.py")) as f:
143-
init_contents = f.read()
144-
with open(os.path.join(package, "__init__.py"), "w") as f:
145-
f.write(init_contents + "\n\n" + f'__version__ = "{__version__}"\n')
137+
def export_version_string(__version__):
138+
# Make sure to export the __version__ string
139+
with open(os.path.join(package, "__init__.py")) as f:
140+
init_contents = f.read()
141+
with open(os.path.join(package, "__init__.py"), "w") as f:
142+
f.write(init_contents + "\n\n" + f'__version__ = "{__version__}"\n')
146143

147-
# Build the package
148-
os.system("python3 -m build")
149144

150-
# Save the dist files generated by the build process
151-
os.chdir(root_path)
152-
if not os.path.exists(dist_directory):
153-
os.mkdir(dist_directory)
154-
for fpath in glob.glob(
155-
os.path.join(build_directory, dist_directory, "*.*")
156-
):
157-
shutil.copy(fpath, dist_directory)
145+
def build_and_save_output(root_path, __version__):
146+
# Build the package
147+
os.system("python3 -m build")
148+
149+
# Save the dist files generated by the build process
150+
os.chdir(root_path)
151+
if not os.path.exists(dist_directory):
152+
os.mkdir(dist_directory)
153+
for fpath in glob.glob(
154+
os.path.join(build_directory, dist_directory, "*.*")
155+
):
156+
shutil.copy(fpath, dist_directory)
157+
158+
# Find the .whl file path
159+
for fname in os.listdir(dist_directory):
160+
if __version__ in fname and fname.endswith(".whl"):
161+
whl_path = os.path.abspath(os.path.join(dist_directory, fname))
162+
print(f"Build successful. Wheel file available at {whl_path}")
163+
164+
165+
def build(root_path):
166+
if os.path.exists(build_directory):
167+
raise ValueError(f"Directory already exists: {build_directory}")
168+
169+
whl_path = None
170+
try:
171+
copy_source_to_build_directory(root_path)
172+
run_namex_conversion()
173+
create_legacy_directory()
174+
from keras_core.src.version import __version__ # noqa: E402
158175

159-
# Find the .whl file path
160-
for fname in os.listdir(dist_directory):
161-
if __version__ in fname and fname.endswith(".whl"):
162-
whl_path = os.path.abspath(os.path.join(dist_directory, fname))
163-
print(f"Build successful. Wheel file available at {whl_path}")
176+
export_version_string(__version__)
177+
build_and_save_output(root_path, __version__)
164178
finally:
165179
# Clean up: remove the build directory (no longer needed)
166180
shutil.rmtree(build_directory)

0 commit comments

Comments
 (0)