import json
import subprocess
import os
def bootstrap_scripts(config_file):
"""
Bootstraps scripts for user record data migration based on a configuration file.
Args:
config_file (str): Path to the configuration file (JSON).
"""
try:
with open(config_file, 'r') as f:
config = json.load(f)
except FileNotFoundError:
print(f"Error: Configuration file not found: {config_file}")
return
except json.JSONDecodeError:
print(f"Error: Invalid JSON format in: {config_file}")
return
for user in config.get('users', []):
user_id = user.get('id')
script_name = user.get('script')
script_path = os.path.join('scripts', script_name) # Assuming scripts are in a 'scripts' directory
if not os.path.exists(script_path):
print(f"Warning: Script not found: {script_path}")
continue
# Create a temporary file with user-specific data
temp_data_file = f"temp_user_data_{user_id}.json"
with open(temp_data_file, 'w') as temp_file:
json.dump(user, temp_file, indent=4) # Serialize user data to JSON
# Execute the script with the temporary data file as an argument
try:
result = subprocess.run(['python', script_path, temp_data_file], capture_output=True, text=True, check=True)
print(f"Successfully executed script for user {user_id}")
print("Script Output:\n", result.stdout)
except subprocess.CalledProcessError as e:
print(f"Error executing script for user {user_id}:")
print("Return code:", e.returncode)
print("Stdout:", e.stdout)
print("Stderr:", e.stderr)
# Clean up the temporary file
os.remove(temp_data_file)
if __name__ == '__main__':
# Example Usage (replace with your config file)
config_file = 'config.json'
bootstrap_scripts(config_file)
Add your comment