Skip to content

Commit d721f0f

Browse files
Refactor setup.py: implement cleanup in bdist_wheel command and remove redundant cleanup function
More proper and powerful cleanup
1 parent ede4f11 commit d721f0f

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

tool/setup.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Use python setup.py bdist_wheel
22
import os
3-
import sys
4-
import re
53
import pathlib
4+
import re
65
import shutil
6+
import sys
77

88
try:
9+
# noinspection PyUnusedImports
10+
from setuptools.command.bdist_wheel import bdist_wheel as _bdist_wheel
911
# noinspection PyUnusedImports
1012
from setuptools import setup, find_packages
1113
import wheel
@@ -16,16 +18,12 @@
1618
)
1719

1820

19-
def cleanup():
20-
try:
21-
# Cleanup: remove pyCTools.egg-info and build directories if they exist
22-
for cleanup_dir in ["pyCTools.egg-info", "build", "../pyCTools.egg-info", "../build", "dist"]:
23-
cleanup_path = pathlib.Path(__file__).parent / cleanup_dir
24-
if cleanup_path.exists() and cleanup_path.is_dir():
25-
shutil.rmtree(cleanup_path)
26-
print("\n\033[90m[*] Completed setup.py script cleanup successfully.\033[0m\n")
27-
except Exception as e:
28-
sys.exit(f"[x] Cleanup failed: {e}")
21+
class bdist_wheel_clean(_bdist_wheel):
22+
def run(self):
23+
super().run()
24+
for path in ("build", "pyCTools.egg-info"):
25+
if os.path.exists(path):
26+
shutil.rmtree(path)
2927

3028

3129
def prompt_version() -> str:
@@ -50,7 +48,6 @@ def get_latest_wheel(dist_dir: str, package_name: str) -> pathlib.Path:
5048
reverse=True
5149
)
5250
if not wheel_files:
53-
cleanup()
5451
sys.exit(f"[x] No wheel files matching '{pattern}' found in {dist_dir}??\n")
5552

5653
if len(wheel_files) == 1:
@@ -69,7 +66,6 @@ def get_latest_wheel(dist_dir: str, package_name: str) -> pathlib.Path:
6966
else:
7067
print("[!] Invalid selection. Please enter a valid number.\n")
7168
except (KeyboardInterrupt, EOFError):
72-
cleanup()
7369
sys.exit("\n[!] Selection interrupted. Exiting setup.\n")
7470

7571

@@ -164,13 +160,13 @@ def output_dir_init() -> pathlib.Path:
164160
"bdist_wheel": {"dist_dir": str(o_dir)},
165161
"sdist": {"dist_dir": str(o_dir)},
166162
},
163+
cmdclass={"bdist_wheel": bdist_wheel_clean}
167164
)
168165
print("\033[0m")
169166

170167
whl_filename = get_latest_wheel("dist/libraryWheel", "pyctools")
171168
whl_filename = str(whl_filename).replace("\\", "/")
172169
except Exception as e:
173-
cleanup()
174170
sys.exit(f"\033[0m[x] An error occurred during setup: {e}\n")
175171

176172
success_finale(whl_filename_=whl_filename, version_=version)

0 commit comments

Comments
 (0)