Bundler update
cleanup __pycache__
frdel committed
Oct 10, 2024 at 22:11 UTC
78c14fa48515dd4bdcacc413cc867c90a5c071ee
1 file changed
+27
-8
bundle.py
+27
-8
@@ -8,17 +8,18 @@ import pathspec
8
import importlib
9
import importlib.metadata as metadata
10
11
+
12
def get_package_data_folder(package_name):
13
"""Return the package path if it contains data files."""
14
try:
15
# Get the module and its file path
16
package = importlib.import_module(package_name)
17
package_path = os.path.dirname(package.__file__) # type: ignore
17
- #only if package path does not end with "site-packages"
18
+ # only if package path does not end with "site-packages"
19
if not package_path.endswith("site-packages"):
19
- # Check if the package contains any relevant data files
20
+ # Check if the package contains any relevant data files
21
has_data = any(
21
- file.endswith(('.json', '.txt', '.csv', '.yml', '.yaml'))
22
+ file.endswith((".json", ".txt", ".csv", ".yml", ".yaml"))
23
for root, dirs, files in os.walk(package_path)
24
for file in files
25
)
@@ -27,28 +28,32 @@ def get_package_data_folder(package_name):
28
return package_path
29
30
except ImportError:
30
- print(f"Warning: Unable to import {package_name}. Skipping data folder discovery for this package.")
31
-
31
+ print(
32
+ f"Warning: Unable to import {package_name}. Skipping data folder discovery for this package."
33
+ )
34
+
35
return None
36
37
+
38
def get_add_data_args():
39
"""Return an array of --add-data arguments for PyInstaller, one per package."""
40
add_data_args = []
41
42
# Use importlib.metadata to get the installed packages
39
- installed_packages = [dist.metadata['Name'] for dist in metadata.distributions()]
43
+ installed_packages = [dist.metadata["Name"] for dist in metadata.distributions()]
44
45
# Discover the data folder for each package and add it as a --add-data argument
46
for package in installed_packages:
47
package_data_folder = get_package_data_folder(package)
48
if package_data_folder:
49
# Add the whole package directory
46
- add_data_args.append(f"--add-data={package_data_folder}{os.pathsep}{package}")
50
+ add_data_args.append(
51
+ f"--add-data={package_data_folder}{os.pathsep}{package}"
52
+ )
53
54
return add_data_args
55
56
51
-
57
def get_site_packages_path():
58
"""Get the path to the site-packages directory of the current environment."""
59
if hasattr(site, "getsitepackages"):
@@ -174,6 +179,12 @@ def build_executable(script_name, exe_name=None):
179
print(f"Project files copied to: '{project_files_dir}'")
180
print(f"Bundled contents are in: '{dist_dir}'")
181
182
+ # Remove all __pycache__ directories in the _internal subdirectory
183
+ internal_dir = os.path.join(dist_dir, "_internal")
184
+ if os.path.exists(internal_dir):
185
+ print(f"Cleaning up __pycache__ folders in {internal_dir}...")
186
+ remove_pycache_folders(internal_dir)
187
+
188
# Final cleanup (keeping dist folder)
189
cleanup_directories(exe_name, keep_dist=True)
190
@@ -183,5 +194,13 @@ def build_executable(script_name, exe_name=None):
194
print(f"Error: {e}")
195
196
197
+def remove_pycache_folders(root_dir):
198
+ for root, dirs, files in os.walk(root_dir):
199
+ for dir_name in dirs:
200
+ if dir_name == "__pycache__":
201
+ pycache_path = os.path.join(root, dir_name)
202
+ shutil.rmtree(pycache_path)
203
+
204
+
205
if __name__ == "__main__":
206
build_executable("run_bundle.py", "agent-zero")