Bundle compression dev

dev in progress

frdel committed Oct 17, 2024 at 13:45 UTC c38cc1e0769543af1958fa9f30f42e513fe92af5
3 files changed +58 -2
bundle/bundle.py
+33 -2
@@ -7,6 +7,7 @@ from pathlib import Path
7 import pathspec
8 import importlib
9 import importlib.metadata as metadata
10 +import py7zr # New import for 7z compression
11
12 def get_package_data_folder(package_name):
13 """Return the package path if it contains data files."""
@@ -76,7 +77,31 @@ def cleanup_directories(bundle_name, build_dir, dist_dir, keep_dist=False):
77 if os.path.exists(spec_file):
78 os.remove(spec_file)
79
79 -def build_executable(script_path, exe_name=None):
80 +def compress_dist_folder(dist_dir, exe_name):
81 + """Compress the dist folder using py7zr library."""
82 + try:
83 + archive_path = Path(dist_dir) / f"{exe_name}.7z"
84 + files_path = Path(dist_dir) / exe_name
85 +
86 +
87 + # Remove existing archive if it exists
88 + if archive_path.exists():
89 + archive_path.unlink()
90 +
91 + print(f"Compressing dist folder to: {archive_path}")
92 +
93 + # Create the 7z archive with maximum compression
94 + with py7zr.SevenZipFile(archive_path, 'w', filters=[{'id': py7zr.FILTER_LZMA2, 'preset': 9}]) as archive:
95 + archive.writeall(files_path, arcname=dist_dir.name)
96 +
97 + print("Compression completed successfully")
98 + return str(archive_path)
99 +
100 + except Exception as e:
101 + print(f"Error during compression: {e}")
102 + return None
103 +
104 +def build_executable(script_path, exe_name=None, compress=False):
105 """Run PyInstaller with the correct site-packages path, clean, and additional data."""
106 try:
107 # Resolve the absolute path to the script, relative to the current file location (__file__)
@@ -147,6 +172,12 @@ def build_executable(script_path, exe_name=None):
172 print(f"Executable created at: '{dist_dir}/{exe_name}'")
173 print(f"Project files copied to: '{project_files_dir}'")
174
175 + # Compress the dist folder if requested
176 + if compress:
177 + archive_path = compress_dist_folder(dist_dir, exe_name)
178 + if archive_path:
179 + print(f"Created compressed archive at: {archive_path}")
180 +
181 # Final cleanup (keeping dist folder)
182 cleanup_directories(exe_name, build_dir, dist_dir, keep_dist=True)
183
@@ -156,4 +187,4 @@ def build_executable(script_path, exe_name=None):
187 print(f"Error: {e}")
188
189 if __name__ == "__main__":
159 - build_executable("../run_bundle.py", "agent-zero")
190 + build_executable("../run_bundle.py", "agent-zero", compress=True)
\ No newline at end of file
bundle/macos_bundle.sh
+24
@@ -67,4 +67,28 @@ if [ $? -ne 0 ]; then
67 exit 1
68 fi
69
70 +# 9. Move the generated 7z file to the script directory and remove agent-zero folder
71 +BUNDLE_FILE="bundle/dist/agent-zero.7z"
72 +if [ -f "$BUNDLE_FILE" ]; then
73 + SCRIPT_DIR=$(dirname "$0")
74 + echo "Moving $BUNDLE_FILE to $SCRIPT_DIR..."
75 + mv "$BUNDLE_FILE" "$SCRIPT_DIR"
76 + if [ $? -ne 0 ]; then
77 + echo "Error moving $BUNDLE_FILE to $SCRIPT_DIR."
78 + exit 1
79 + fi
80 +else
81 + echo "Error: $BUNDLE_FILE not found."
82 + exit 1
83 +fi
84 +
85 +# 10. Remove the agent-zero folder
86 +echo "Deleting agent-zero folder..."
87 +cd ..
88 +rm -rf agent-zero
89 +if [ -d "agent-zero" ]; then
90 + echo "Error: Failed to delete agent-zero folder."
91 + exit 1
92 +fi
93 +
94 echo "Script completed."
requirements.txt
+1
@@ -19,6 +19,7 @@ markdown==3.7
19 newspaper3k==0.2.8
20 paramiko==3.4.0
21 pathspec==0.12.1
22 +py7zr==0.22.0
23 pyinstaller==6.10.0
24 pypdf==4.3.1
25 python-dotenv==1.0.1