🔍 Code Extractor

function main_v112

Maturity: 34

Entry point function that parses command-line arguments to fix file timestamps by setting them to the oldest date found, either for a single file or recursively through a directory.

File:
/tf/active/vicechatdev/mailsearch/fix_file_dates.py
Lines:
165 - 238
Complexity:
moderate

Purpose

This main function serves as the CLI interface for a file date fixing utility. It configures an argument parser to handle various options including directory/file processing, pattern matching, dry-run mode, and recursive/non-recursive directory traversal. The function validates inputs and delegates to either fix_file_dates() for single files or process_directory() for directory operations.

Source Code

def main():
    parser = argparse.ArgumentParser(
        description="Fix file dates by setting all timestamps to the oldest date",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  # Dry run on all files in a directory (recursive by default)
  python3 fix_file_dates.py /path/to/folder --dry-run

  # Actually fix the dates for all files in directory
  python3 fix_file_dates.py /path/to/folder

  # Process with custom pattern
  python3 fix_file_dates.py /path/to/folder --pattern "*.pdf"

  # Process only specific pattern
  python3 fix_file_dates.py /path/to/folder --pattern "*_fully_signed.pdf"

  # Process single file
  python3 fix_file_dates.py --file /path/to/file.pdf

  # Process without recursing subdirectories
  python3 fix_file_dates.py /path/to/folder --no-recursive
        """
    )
    
    parser.add_argument(
        'directory',
        nargs='?',
        help='Directory to process (required unless using --file)'
    )
    
    parser.add_argument(
        '--pattern',
        default='*',
        help='File pattern to match (default: * = all files)'
    )
    
    parser.add_argument(
        '--file',
        help='Process a single file instead of a directory'
    )
    
    parser.add_argument(
        '--dry-run',
        action='store_true',
        help='Show what would be done without making changes'
    )
    
    parser.add_argument(
        '--no-recursive',
        action='store_true',
        help='Do not search subdirectories'
    )
    
    args = parser.parse_args()
    
    if args.file:
        # Process single file
        if not os.path.exists(args.file):
            print(f"Error: File {args.file} does not exist")
            sys.exit(1)
        fix_file_dates(args.file, args.dry_run)
    else:
        # Process directory
        if not args.directory:
            parser.error("directory is required unless using --file")
        
        process_directory(
            args.directory,
            pattern=args.pattern,
            dry_run=args.dry_run,
            recursive=not args.no_recursive
        )

Return Value

This function does not return a value (implicitly returns None). It either exits with sys.exit(1) on error or completes execution after calling the appropriate processing functions.

Dependencies

  • argparse
  • os
  • sys
  • pathlib
  • datetime
  • subprocess

Required Imports

import argparse
import os
import sys
from pathlib import Path
from datetime import datetime
import subprocess

Usage Example

# This function is typically called as the script entry point:
# if __name__ == '__main__':
#     main()

# Command-line usage examples:
# python3 fix_file_dates.py /path/to/folder --dry-run
# python3 fix_file_dates.py /path/to/folder --pattern '*.pdf'
# python3 fix_file_dates.py --file /path/to/file.pdf
# python3 fix_file_dates.py /path/to/folder --no-recursive

Best Practices

  • Always use --dry-run flag first to preview changes before modifying file timestamps
  • Ensure the calling script defines fix_file_dates() and process_directory() functions before calling main()
  • Use pattern matching (--pattern) to limit processing to specific file types
  • The function expects to be called with no arguments as it uses argparse to parse sys.argv
  • Error handling exits with code 1 for file not found errors
  • Either --file or directory argument must be provided, but not both
  • Default behavior is recursive directory traversal unless --no-recursive is specified

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function fix_file_dates 66.2% similar

    Normalizes all timestamp attributes (creation, modification, access) of a file to the oldest timestamp among them, with optional dry-run mode for preview.

    From: /tf/active/vicechatdev/mailsearch/fix_file_dates.py
  • function main_v69 58.5% similar

    Entry point function that parses command-line arguments and orchestrates the FileCloud email processing workflow to find, download, and convert .msg files.

    From: /tf/active/vicechatdev/msg_to_eml.py
  • function set_file_dates 57.6% similar

    Sets all file timestamps (creation time, modification time, access time, and change time) to a specified Unix timestamp by directly modifying the file's inode using debugfs.

    From: /tf/active/vicechatdev/mailsearch/fix_file_dates.py
  • function process_directory 57.1% similar

    Processes all files matching a specified pattern in a directory, applying date fixes to each file and providing a summary of results.

    From: /tf/active/vicechatdev/mailsearch/fix_file_dates.py
  • function main_v90 56.5% similar

    A command-line interface (CLI) entry point that parses command-line arguments and dispatches to various development tool functions for managing browser cache, static files, and debug endpoints.

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