Skip to content

Agent Zero AI framework

Bundler updater

frdel823957673ecb
diff --git a/.gitignore b/.gitignore
index 5989b8811..d4a3d35ae 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,9 +9,8 @@
# Ignore all contents of the virtual environment directory
.venv/
-# Ignore bundled files
-build/
-dist/
+# ignore all folders under /bundle
+/bundle/*/
# Ignore all contents of the directory "work_dir"
work_dir/*
@@ -59,4 +58,4 @@ instruments/**/*.*
# Explicitly allow the default folder and its contents
!instruments/default/
-!instruments/default/**
\ No newline at end of file
+!instruments/default/**
diff --git a/bundle.py b/bundle/bundle.py
similarity index 58%
rename from bundle.py
rename to bundle/bundle.py
index ac88c3ef3..2975292e7 100644
--- a/bundle.py
+++ b/bundle/bundle.py
@@ -8,83 +8,56 @@ import pathspec
import importlib
import importlib.metadata as metadata
-
def get_package_data_folder(package_name):
"""Return the package path if it contains data files."""
try:
- # Get the module and its file path
package = importlib.import_module(package_name)
package_path = os.path.dirname(package.__file__) # type: ignore
- # only if package path does not end with "site-packages"
if not package_path.endswith("site-packages"):
- # Check if the package contains any relevant data files
has_data = any(
file.endswith((".json", ".txt", ".csv", ".yml", ".yaml"))
for root, dirs, files in os.walk(package_path)
for file in files
)
-
if has_data:
return package_path
-
except ImportError:
- print(
- f"Warning: Unable to import {package_name}. Skipping data folder discovery for this package."
- )
-
+ print(f"Warning: Unable to import {package_name}. Skipping data folder discovery for this package.")
return None
-
def get_add_data_args():
"""Return an array of --add-data arguments for PyInstaller, one per package."""
add_data_args = []
-
- # Use importlib.metadata to get the installed packages
installed_packages = [dist.metadata["Name"] for dist in metadata.distributions()]
-
- # Discover the data folder for each package and add it as a --add-data argument
for package in installed_packages:
package_data_folder = get_package_data_folder(package)
if package_data_folder:
- # Add the whole package directory
- add_data_args.append(
- f"--add-data={package_data_folder}{os.pathsep}{package}"
- )
-
+ add_data_args.append(f"--add-data={package_data_folder}{os.pathsep}{package}")
return add_data_args
-
def get_site_packages_path():
"""Get the path to the site-packages directory of the current environment."""
if hasattr(site, "getsitepackages"):
paths = site.getsitepackages()
else:
paths = [site.getusersitepackages()]
-
if paths:
return paths[0]
else:
raise RuntimeError("Couldn't determine the site-packages path.")
-
def parse_gitignore(gitignore_path):
"""Parse .gitignore file and return a PathSpec object."""
if not os.path.exists(gitignore_path):
return pathspec.PathSpec.from_lines("gitwildmatch", [])
-
with open(gitignore_path, "r") as f:
return pathspec.PathSpec.from_lines("gitwildmatch", f)
-
def copy_project_files(src_dir, dst_dir, spec):
"""Copy project files respecting .gitignore rules using pathspec."""
src_path = Path(src_dir)
-
- # Walk through all the directories and files in the source directory
for root, dirs, files in os.walk(src_dir):
rel_root = Path(root).relative_to(src_path)
-
- # Filter out directories and files based on the .gitignore rules
for file in files:
rel_path = rel_root / file
if not spec.match_file(str(rel_path)):
@@ -93,39 +66,48 @@ def copy_project_files(src_dir, dst_dir, spec):
dst_file.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src_file, dst_file)
-
-def cleanup_directories(bundle_name, keep_dist=False):
+def cleanup_directories(bundle_name, build_dir, dist_dir, keep_dist=False):
"""Remove build directory and .spec file. Optionally keep dist."""
- if not keep_dist and os.path.exists("dist"):
- shutil.rmtree("dist")
-
- if os.path.exists("build"):
- shutil.rmtree("build")
-
+ if not keep_dist and os.path.exists(dist_dir):
+ shutil.rmtree(dist_dir)
+ if os.path.exists(build_dir):
+ shutil.rmtree(build_dir)
spec_file = f"{bundle_name}.spec"
if os.path.exists(spec_file):
os.remove(spec_file)
-
-def build_executable(script_name, exe_name=None):
+def build_executable(script_path, exe_name=None):
"""Run PyInstaller with the correct site-packages path, clean, and additional data."""
try:
+ # Resolve the absolute path to the script, relative to the current file location (__file__)
+ bundling_script_dir = Path(__file__).parent.resolve()
+ script_path = (bundling_script_dir / script_path).resolve()
+ script_name = script_path.name # run_bundle.py
+ project_dir = script_path.parent # Folder containing run_bundle.py
+
+ # Define build and dist paths under the /bundle directory (bundling_script_dir)
+ build_dir = bundling_script_dir / "build"
+ dist_dir = bundling_script_dir / "dist"
+
# Initial cleanup
- cleanup_directories(exe_name, keep_dist=False)
+ cleanup_directories(exe_name, build_dir, dist_dir, keep_dist=False)
site_packages_path = get_site_packages_path()
print(f"Using site-packages path: {site_packages_path}")
+ print(f"Bundling project from: {project_dir}")
+ print(f"Build directory: {build_dir}")
+ print(f"Dist directory: {dist_dir}")
- # Parse .gitignore
- gitignore_path = os.path.join(os.getcwd(), ".gitignore")
+ # Parse .gitignore in the project directory
+ gitignore_path = project_dir / ".gitignore"
spec = parse_gitignore(gitignore_path)
- # Create a temporary directory for project files
- temp_project_dir = os.path.join("build", "temp_project")
+ # Create a temporary directory for project files inside build
+ temp_project_dir = build_dir / "temp_project"
os.makedirs(temp_project_dir, exist_ok=True)
# Copy project files respecting .gitignore
- copy_project_files(os.getcwd(), temp_project_dir, spec)
+ copy_project_files(project_dir, temp_project_dir, spec)
# Construct the PyInstaller command
pyinstaller_command = [
@@ -134,7 +116,8 @@ def build_executable(script_name, exe_name=None):
"--noconfirm",
"--onedir",
f"--paths={site_packages_path}",
- "--workpath=build", # Specify the build directory
+ f"--workpath={build_dir}", # Specify the build directory under /bundle
+ f"--distpath={dist_dir}", # Specify the dist directory under /bundle
]
# Add data arguments
@@ -146,61 +129,31 @@ def build_executable(script_name, exe_name=None):
else:
exe_name = os.path.splitext(script_name)[0]
- # Add the script path
+ # Add the script path (in the temp_project directory)
pyinstaller_command.append(os.path.join(temp_project_dir, script_name))
# Run the PyInstaller command
print("Running PyInstaller...")
subprocess.run(pyinstaller_command, check=True)
- # Post-processing: Manually create the desired structure
- dist_dir = os.path.join("dist", exe_name)
- project_files_dir = os.path.join(dist_dir, exe_name + "-files")
-
- # Move the executable to the root of dist/
- # shutil.move(os.path.join(temp_exe_dir, exe_name), os.path.join(dist_dir, exe_name))
-
- # Move _internal and other PyInstaller files to dist/
- # for item in os.listdir(temp_exe_dir):
- # if item != exe_name:
- # shutil.move(os.path.join(temp_exe_dir, item), os.path.join(dist_dir, item))
-
- # Remove the now-empty temporary executable directory
- # os.rmdir(temp_exe_dir)
-
- # Create project files directory
+ # Post-processing: Create a folder for project files inside dist/
+ project_files_dir = dist_dir / exe_name / f"{exe_name}-files"
os.makedirs(project_files_dir, exist_ok=True)
- # Copy project files
+ # Copy project files to the dist folder
copy_project_files(temp_project_dir, project_files_dir, spec)
print(f"PyInstaller finished successfully.")
- print(f"Executable created at: '{os.path.join(dist_dir, exe_name)}'")
+ print(f"Executable created at: '{dist_dir}/{exe_name}'")
print(f"Project files copied to: '{project_files_dir}'")
- print(f"Bundled contents are in: '{dist_dir}'")
-
- # Remove all __pycache__ directories in the _internal subdirectory
- internal_dir = os.path.join(dist_dir, "_internal")
- if os.path.exists(internal_dir):
- print(f"Cleaning up __pycache__ folders in {internal_dir}...")
- remove_pycache_folders(internal_dir)
# Final cleanup (keeping dist folder)
- cleanup_directories(exe_name, keep_dist=True)
+ cleanup_directories(exe_name, build_dir, dist_dir, keep_dist=True)
except subprocess.CalledProcessError as e:
print(f"Error during PyInstaller execution: {e}")
except Exception as e:
print(f"Error: {e}")
-
-def remove_pycache_folders(root_dir):
- for root, dirs, files in os.walk(root_dir):
- for dir_name in dirs:
- if dir_name == "__pycache__":
- pycache_path = os.path.join(root, dir_name)
- shutil.rmtree(pycache_path)
-
-
if __name__ == "__main__":
- build_executable("run_bundle.py", "agent-zero")
+ build_executable("../run_bundle.py", "agent-zero")
diff --git a/bundle/macos_bundle.sh b/bundle/macos_bundle.sh
new file mode 100755
index 000000000..8819bdd4f
--- /dev/null
+++ b/bundle/macos_bundle.sh
@@ -0,0 +1,70 @@
+#!/bin/bash
+
+set -e
+
+# 1. Remove conda environment if it exists
+echo "Removing conda environment 'az-bundle' if it exists..."
+conda env remove -n az-bundle -y || echo "Conda environment 'az-bundle' does not exist."
+
+# 2. Create new environment with Python 3.12 and activate it
+echo "Creating new conda environment 'az-bundle' with Python 3.12..."
+conda create -n az-bundle python=3.12 -y
+if [ $? -ne 0 ]; then
+ echo "Error creating conda environment."
+ exit 1
+fi
+
+echo "Activating conda environment 'az-bundle'..."
+source $(conda info --base)/etc/profile.d/conda.sh
+conda activate az-bundle
+if [ $? -ne 0 ]; then
+ echo "Error activating conda environment."
+ exit 1
+fi
+
+# 3. Purge folder ./agent-zero (retry mechanism in case of failure)
+if [ -d "agent-zero" ]; then
+ echo "Deleting agent-zero folder..."
+ rm -rf agent-zero
+ if [ -d "agent-zero" ]; then
+ echo "Error: Unable to delete agent-zero folder, retrying..."
+ sleep 3
+ rm -rf agent-zero
+ fi
+ if [ -d "agent-zero" ]; then
+ echo "Error: Failed to purge agent-zero folder after retry."
+ exit 1
+ fi
+fi
+
+# 4. Clone the repository (development branch)
+echo "Cloning the repository (development branch)..."
+git clone --branch development https://github.com/frdel/agent-zero agent-zero
+if [ $? -ne 0 ]; then
+ echo "Error cloning the repository."
+ exit 1
+fi
+
+# 5. Change directory to agent-zero
+cd agent-zero || { echo "Error changing directory"; exit 1; }
+
+# 6. Install requirements
+echo "Installing requirements from requirements.txt..."
+pip install -r requirements.txt
+if [ $? -ne 0 ]; then
+ echo "Error installing requirements."
+ exit 1
+fi
+
+# 7. Install specific version of pefile
+# skip
+
+# 8. Run bundle.py
+echo "Running bundle.py..."
+python ./bundle/bundle.py
+if [ $? -ne 0 ]; then
+ echo "Error running bundle.py."
+ exit 1
+fi
+
+echo "Script completed."
diff --git a/bundle/windows_bundle.bat b/bundle/windows_bundle.bat
new file mode 100644
index 000000000..d70516f10
--- /dev/null
+++ b/bundle/windows_bundle.bat
@@ -0,0 +1,109 @@
+@echo off
+setlocal enabledelayedexpansion
+
+:: Check if conda is recognized
+where conda >nul 2>nul
+if %errorlevel% neq 0 (
+ echo Conda not found in PATH. Checking known location...
+
+ set "CONDA_PATH=C:\Users\%USERNAME%\miniconda3"
+ if exist "!CONDA_PATH!\Scripts\conda.exe" (
+ echo Found Conda at !CONDA_PATH!
+ set "PATH=!CONDA_PATH!;!CONDA_PATH!\Scripts;!CONDA_PATH!\Library\bin;%PATH%"
+ echo Added Conda to PATH
+ ) else (
+ echo Conda installation not found at !CONDA_PATH!
+ echo Please install Conda or add it to PATH manually.
+ pause
+ exit /b 1
+ )
+)
+
+:: Verify conda is now accessible
+where conda >nul 2>nul
+if %errorlevel% neq 0 (
+ echo Failed to add Conda to PATH. Please add it manually.
+ pause
+ exit /b 1
+)
+
+:: Initialize conda shell (if not done before)
+call conda init bash >nul 2>nul
+if %errorlevel% neq 0 (
+ echo Error running 'conda init'. Please check your conda installation.
+ pause
+ exit /b 1
+)
+
+:: 1. Remove conda environment if it exists
+conda env remove -n az-bundle -y 2>nul
+if %errorlevel% neq 0 (
+ echo Error removing conda environment
+ pause
+)
+
+:: 2. Create new environment with Python 3.12 and activate it
+conda create -n az-bundle python=3.12 -y
+if %errorlevel% neq 0 (
+ echo Error creating conda environment
+ pause
+) else (
+ call conda.bat activate az-bundle
+ if %errorlevel% neq 0 (
+ echo Error activating conda environment
+ pause
+ )
+)
+
+:: 3. Purge folder ./agent-zero (retry mechanism in case of failure)
+if exist agent-zero (
+ echo Deleting agent-zero folder...
+ rmdir /s /q agent-zero
+ if exist agent-zero (
+ echo Error: Unable to delete agent-zero folder, retrying...
+ timeout /t 3 /nobreak >nul
+ rmdir /s /q agent-zero
+ )
+ if exist agent-zero (
+ echo Error: Failed to purge agent-zero folder after retry.
+ pause
+ )
+)
+
+:: 4. Clone the repository (development branch)
+git clone --branch development https://github.com/frdel/agent-zero agent-zero
+if %ERRORLEVEL% neq 0 (
+ echo Error cloning the repository
+ pause
+)
+
+:: 5. Change directory to agent-zero
+cd agent-zero
+if %errorlevel% neq 0 (
+ echo Error changing directory
+ pause
+)
+
+:: 6. Install requirements
+pip install -r requirements.txt
+if %errorlevel% neq 0 (
+ echo Error installing requirements
+ pause
+)
+
+:: 7. Install specific version of pefile
+pip install pefile==2023.2.7
+if %errorlevel% neq 0 (
+ echo Error installing pefile
+ pause
+)
+
+:: 8. Run bundle.py
+python bundle.py
+if %errorlevel% neq 0 (
+ echo Error running bundle.py
+ pause
+)
+
+echo Script completed
+pause