ford_qc/utils/path_resolver.py
2025-09-03 07:03:21 -05:00

53 lines
No EOL
1.9 KiB
Python

import os
import json
import logging
def get_working_dir():
"""
Returns the working directory path.
Tries the local path first, then falls back to server 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 server path
if os.path.exists('/home/box-cli/FORD_SCRIPTS/FORD_ASSET_PACK_QC_NEW/working'):
working_dir = '/home/box-cli/FORD_SCRIPTS/FORD_ASSET_PACK_QC_NEW/working'
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