Base Class
BuilderConfigFile
Abstract base class for artifact builders.BuilderConfigFile provides a framework for creating build artifacts with platform-specific naming and organized output. It extends ListConfigFile but adapts its interface for build operations rather than configuration management.The build lifecycle:
- A temporary directory is created
create_artifacts()is called (implemented by subclasses)- Artifacts are collected from the temporary directory
- Artifacts are renamed with platform suffixes (e.g.,
-Linux,-Windows) - Artifacts are moved to the final output directory (default:
dist/)
create_artifacts(temp_artifacts_dir: Path) -> None: Define build logic
Abstract Methods (Must Implement)
Create artifacts in the temporary directory.Subclasses must implement this method to define their build logic. All artifacts should be written to the provided temporary directory. The Builder framework will automatically collect, rename, and move artifacts to the final output directory.Parameters:Example:
Temporary directory where artifacts should be created. All files created here will be moved to the final output directory with platform-specific naming.
Instance Methods
Execute the complete build process.Main orchestration method that manages the build lifecycle: creates a temporary directory, invokes
create_artifacts(), collects artifacts, renames them with platform-specific suffixes, and moves them to the final output directory.Get the parent directory for artifacts.For builders, this is the directory where artifacts are stored. The default is “dist”, but can be overridden by subclasses.Returns:
Path to the artifacts directory (e.g., “dist”).
Get the name of the artifacts directory.Default is “dist”, but can be overridden by subclasses. Is a classmethod because it’s used in WorkflowConfigFile subclasses and BuilderConfigFile itself is abstract.Returns:
Name of the artifacts directory (e.g., “dist”).
Return the application name from pyproject.toml.Returns:
Project name from package manager.
Return the absolute path to the project root directory.Returns:
Path to project root.
Return the absolute path to the main.py entry point.Returns:
Path to main.py file.
Return the absolute path to the resources directory.Returns:
Path to resources directory.
Return the absolute path to the source package directory.Returns:
Path to source package.
Generate a platform-specific filename (e.g., Returns:
Filename with platform suffix.Example:
myapp-Linux.exe).Parameters:Path to the artifact.
PyInstaller Builder
PyInstallerBuilder
Abstract builder for creating PyInstaller standalone executables.Extends BuilderConfigFile to provide PyInstaller-specific functionality for creating single-file executables. Handles PyInstaller configuration, resource bundling, and icon conversion.Creates executables with:
- Single-file executable (
--onefile) - No console window (
--noconsole) - Platform-specific icon (ICO/ICNS/PNG)
- All resources bundled and accessible at runtime
- Clean build (
--clean)
additional_resource_packages().Subclasses must implement:additional_resource_packages() -> Iterable[ModuleType]: Specify resource packages to bundle
PyInstallerBuilder Methods
Return packages containing additional resources to bundle.Subclasses must implement this method to specify resource packages beyond the automatically discovered ones. All files in the specified packages will be included in the executable and accessible at runtime.Returns:
Iterable of module objects representing resource packages.Example:
Get resource packages from all pyrig-dependent packages.Automatically discovers all
resources modules from packages that depend on pyrig, enabling multi-package applications to bundle resources from their entire dependency chain.Returns:
Generator of module objects representing resources packages from all packages in the dependency chain.Get all resource packages to bundle in the executable.Combines auto-discovered resource packages with additional packages specified by the subclass.Returns:
Generator of all resource packages to bundle.
Build the —add-data arguments for PyInstaller.Collects all data files from all resource packages and formats them as PyInstaller —add-data arguments.Returns:
List of (source_path, destination_path) tuples for PyInstaller’s —add-data argument.
Build the complete PyInstaller command-line options.Constructs the full list of command-line arguments for PyInstaller, including entry point, flags, paths, icon, and resource files.Parameters:Returns:
Tuple of command-line arguments for PyInstaller.
Temporary directory for the executable.
Get the platform-appropriate icon path.Converts the PNG icon to the appropriate format for the current platform: Windows (ICO), macOS (ICNS), or Linux (PNG).Parameters:Returns:
Path to the converted icon file.
Temporary directory for the converted icon.
Get the path to the application icon PNG.Returns the path to the source PNG icon file. Override this method to use a custom icon location.Returns:
Absolute path to the PNG icon file (
<src_package>/resources/icon.png).Convert the application icon PNG to another format.Uses PIL/Pillow to convert the source PNG icon to the specified format (ico, icns, or png).Parameters:Returns:
Path to the converted icon file.
Target format extension (“ico”, “icns”, or “png”).
Directory where the converted icon should be written.
Usage Examples
Basic Builder
PyInstaller Executable
Custom Icon Location
Multiple Artifacts
Builder with Priority
Custom Output Directory
Testing Builders
Best Practices
- Clean builds: Use
--cleanor equivalent to ensure reproducible builds - Error handling: Check subprocess return codes and handle build failures
- Resource bundling: Use
additional_resource_packages()for all runtime resources - Icon formats: Provide PNG icon and let framework handle conversion
- Platform testing: Test on all target platforms before release
- Artifact validation: Verify artifacts are executable/usable after build
- Dependencies: Document required build tools (PyInstaller, compilers, etc.)
- Temporary cleanup: Framework handles temp directory cleanup automatically
- Priority: Set appropriate priority if build order matters
- Output location: Use default
dist/unless there’s a strong reason to change
Platform-Specific Considerations
Windows
- Icon format: ICO
- Executable extension: .exe
- Console vs. GUI: Use
--noconsolefor GUI applications
macOS
- Icon format: ICNS
- Executable: No extension (Unix executable)
- Code signing: Consider signing executables for distribution
Linux
- Icon format: PNG
- Executable: No extension (Unix executable)
- Permissions: Ensure executable bit is set
Related APIs
- CLI API - For command-line interface framework
- Configuration API - For managing project configuration files
- Tools API - For CLI tool wrappers