🔍 Code Extractor

function check_static_files

Maturity: 42

Scans a 'static' directory for CSS and JavaScript files and prints their modification times relative to the current time.

File:
/tf/active/vicechatdev/vice_ai/dev_tools.py
Lines:
22 - 36
Complexity:
simple

Purpose

This diagnostic function helps developers monitor static file changes during development or debugging. It checks for the existence of a 'static' directory relative to the current file's location, then recursively searches for .css and .js files, displaying how recently each file was modified. This is useful for cache busting verification, deployment checks, or understanding when static assets were last updated.

Source Code

def check_static_files():
    """Check static file modification times"""
    static_dir = Path(__file__).parent / "static"
    if not static_dir.exists():
        print("❌ Static directory not found")
        return
    
    print("📁 Static Files Status:")
    for file_path in static_dir.rglob("*"):
        if file_path.is_file() and file_path.suffix in ['.css', '.js']:
            mtime = file_path.stat().st_mtime
            relative_path = file_path.relative_to(static_dir)
            age = time.time() - mtime
            print(f"   📄 {relative_path}: Modified {age:.1f}s ago")
    print()

Return Value

This function returns None (implicitly). It produces side effects by printing status information to stdout, including error messages if the static directory doesn't exist, or a formatted list of static files with their modification times.

Required Imports

import time
from pathlib import Path

Usage Example

import time
from pathlib import Path

def check_static_files():
    """Check static file modification times"""
    static_dir = Path(__file__).parent / "static"
    if not static_dir.exists():
        print("❌ Static directory not found")
        return
    
    print("📁 Static Files Status:")
    for file_path in static_dir.rglob("*"):
        if file_path.is_file() and file_path.suffix in ['.css', '.js']:
            mtime = file_path.stat().st_mtime
            relative_path = file_path.relative_to(static_dir)
            age = time.time() - mtime
            print(f"   📄 {relative_path}: Modified {age:.1f}s ago")
    print()

# Call the function to check static files
check_static_files()

Best Practices

  • This function assumes it's called from a file that has a sibling 'static' directory - ensure proper file structure
  • The function only checks .css and .js files; modify the suffix list if other file types are needed
  • Uses Path(__file__).parent which requires the function to be defined in a file (won't work in interactive Python shells)
  • Consider adding error handling for permission issues when accessing files
  • The function prints directly to stdout; consider returning data instead for better testability and flexibility
  • Modification times are shown in seconds; consider formatting for better readability (e.g., minutes, hours, days for older files)

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function touch_static_files 73.8% similar

    Updates the modification timestamp of CSS and JavaScript files in a static directory to force browser cache refresh.

    From: /tf/active/vicechatdev/vice_ai/dev_tools.py
  • function file_cleanup 62.9% similar

    Removes files older than 60 seconds from the './static/files/' directory.

    From: /tf/active/vicechatdev/datacapture_integrated.py
  • function get_file_version 58.1% similar

    Generates a version string for static files to enable cache busting, using current time in debug mode or file modification time in production.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function get_file_version_v1 57.2% similar

    Generates a version string for static files based on their modification time, used for cache busting in web applications.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function debug_cache_info 52.4% similar

    Flask debug endpoint that provides comprehensive cache busting information including static file versions, modification times, and cache buster values.

    From: /tf/active/vicechatdev/vice_ai/app.py
← Back to Browse