import os import json import logging # Try to load configuration for fallback path try: from utils.config import config _fallback_working_dir = config.FALLBACK_WORKING_DIR except ImportError: # Fallback when config module not available (backwards compatibility) _fallback_working_dir = '/home/box-cli/FORD_SCRIPTS/FORD_ASSET_PACK_QC_NEW/working' def get_working_dir(): """ Returns the working directory path. Tries the local path first, then falls back to configured fallback path if necessary. """ script_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) working_dir = os.path.join(script_dir, 'working') # Create the directory if it doesn't exist if not os.path.exists(working_dir): try: os.makedirs(working_dir, exist_ok=True) except Exception as e: logging.warning(f"Failed to create local working directory: {e}") # Fall back to configured path if os.path.exists(_fallback_working_dir): working_dir = _fallback_working_dir return working_dir def update_profile_paths(profile_path): """ Updates the profile file by replacing __WORKING_DIR__ placeholders with actual paths. Returns a temporary profile path with the updated content. """ working_dir = get_working_dir() try: # Read the profile file with open(profile_path, 'r', encoding='utf-8') as f: profile_content = f.read() # Replace the placeholder with the actual working directory updated_content = profile_content.replace('"__WORKING_DIR__"', f'"{working_dir}"') # Create a temporary file with the updated content timestamp = os.path.basename(profile_path).split('.')[0] temp_dir = os.path.dirname(profile_path) temp_profile_path = os.path.join(temp_dir, f"{timestamp}_temp.json") with open(temp_profile_path, 'w', encoding='utf-8') as f: f.write(updated_content) return temp_profile_path except Exception as e: logging.error(f"Failed to update profile paths: {e}") return profile_path # Return original path in case of error