CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-webassets

Media asset management for Python, with glue code for various web frameworks

Overview
Eval results
Files

updater-system.mddocs/

Updater System

The auto-rebuild system in webassets determines whether bundles need to be rebuilt by analyzing timestamps, bundle definitions, and dependencies. This system is optional and particularly useful during development and on small sites where rebuild checks on every request are acceptable.

Capabilities

Base Updater

Abstract base class for all updater implementations, using a registry pattern for automatic updater resolution.

class BaseUpdater:
    def needs_rebuild(self, bundle, ctx):
        """
        Returns True if the given bundle needs to be rebuilt, False otherwise.
        
        Args:
            bundle: Bundle instance to check
            ctx: Build context containing cache, manifest, and environment info
            
        Returns:
            bool: True if rebuild needed, False otherwise
        """
    
    def build_done(self, bundle, ctx):
        """
        Called once a bundle has been successfully built.
        
        Args:
            bundle: Bundle instance that was built
            ctx: Build context
        """

Bundle Definition Updater

Supports bundle definition cache update checks that detect when bundle configuration changes (source files added/removed, filters modified).

class BundleDefUpdater(BaseUpdater):
    def check_bundle_definition(self, bundle, ctx):
        """
        Check if the bundle definition has changed since last build.
        
        Args:
            bundle: Bundle to check
            ctx: Build context
            
        Returns:
            bool: True if bundle definition changed
        """
    
    def needs_rebuild(self, bundle, ctx):
        """Check if rebuild needed based on bundle definition changes."""
    
    def build_done(self, bundle, ctx):
        """Cache current bundle definition hash."""

Timestamp Updater

Default updater that compares modification timestamps of source files, dependencies, and output files.

class TimestampUpdater(BundleDefUpdater):
    id = 'timestamp'
    
    def check_timestamps(self, bundle, ctx, o_modified=None):
        """
        Compare timestamps of all source files and dependencies against output.
        
        Args:
            bundle: Bundle to check
            ctx: Build context
            o_modified: Output file modification time (auto-detected if None)
            
        Returns:
            bool or SKIP_CACHE: True if rebuild needed, SKIP_CACHE if dependencies changed
        """
    
    def needs_rebuild(self, bundle, ctx):
        """Check if rebuild needed based on timestamps and bundle definition."""
    
    def build_done(self, bundle, ctx):
        """Reset resolved dependencies cache and update bundle definition."""

Always Updater

Forces rebuilds on every check, useful for development or testing scenarios.

class AlwaysUpdater(BaseUpdater):
    id = 'always'
    
    def needs_rebuild(self, bundle, ctx):
        """Always returns True to force rebuilds."""

Updater Resolution

def get_updater(updater_id):
    """
    Resolve updater by string identifier.
    
    Args:
        updater_id: String identifier ('timestamp', 'always') or updater instance
        
    Returns:
        BaseUpdater: Updater instance
    """

Constants

SKIP_CACHE = object()

Special return value indicating that cache should not be used for rebuild. Used when bundle dependencies have changed, which would not normally cause a different cache key.

Key Features

Dependency Tracking

The updater system handles multiple types of dependencies:

  • Source files: Direct bundle contents
  • Nested bundles: Recursive timestamp checking
  • External dependencies: Files referenced via depends argument
  • Filter dependencies: Files discovered by filters (e.g., SASS imports)

Bundle Definition Caching

Detects changes in bundle configuration by maintaining a hash-based cache:

  • Tracks changes to source file lists
  • Monitors filter modifications
  • Detects option changes
  • Requires environment cache to be configured

Smart Rebuild Logic

The timestamp updater uses sophisticated logic:

  • Handles missing output files gracefully
  • Supports versioned output with placeholders
  • Recurses through nested bundle hierarchies
  • Manages dependency resolution and caching
  • Returns SKIP_CACHE hint when dependencies change

Usage Examples

Basic Configuration

from webassets import Environment
from webassets.updater import TimestampUpdater

env = Environment('./static', '/static')
env.updater = TimestampUpdater()

# Bundle will be checked for rebuild automatically
bundle = env['my_bundle']
if env.updater.needs_rebuild(bundle, env):
    bundle.build()

Custom Updater Implementation

from webassets.updater import BaseUpdater

class CustomUpdater(BaseUpdater):
    id = 'custom'
    
    def needs_rebuild(self, bundle, ctx):
        # Custom rebuild logic
        return self.custom_check(bundle)
    
    def build_done(self, bundle, ctx):
        # Post-build actions
        self.log_build(bundle)

# Register and use
env.updater = 'custom'  # Resolves to CustomUpdater

Development vs Production

import os
from webassets.updater import TimestampUpdater, AlwaysUpdater

if os.environ.get('DEBUG'):
    env.updater = AlwaysUpdater()  # Always rebuild in development
else:
    env.updater = TimestampUpdater()  # Smart rebuilds in production

Install with Tessl CLI

npx tessl i tessl/pypi-webassets

docs

bundle-management.md

caching-versioning.md

command-line.md

configuration-loading.md

environment-configuration.md

filter-system.md

framework-integration.md

index.md

merge-system.md

updater-system.md

utilities.md

tile.json