1. import json
  2. import subprocess
  3. import os
  4. def bootstrap_scripts(config_file):
  5. """
  6. Bootstraps scripts for user record data migration based on a configuration file.
  7. Args:
  8. config_file (str): Path to the configuration file (JSON).
  9. """
  10. try:
  11. with open(config_file, 'r') as f:
  12. config = json.load(f)
  13. except FileNotFoundError:
  14. print(f"Error: Configuration file not found: {config_file}")
  15. return
  16. except json.JSONDecodeError:
  17. print(f"Error: Invalid JSON format in: {config_file}")
  18. return
  19. for user in config.get('users', []):
  20. user_id = user.get('id')
  21. script_name = user.get('script')
  22. script_path = os.path.join('scripts', script_name) # Assuming scripts are in a 'scripts' directory
  23. if not os.path.exists(script_path):
  24. print(f"Warning: Script not found: {script_path}")
  25. continue
  26. # Create a temporary file with user-specific data
  27. temp_data_file = f"temp_user_data_{user_id}.json"
  28. with open(temp_data_file, 'w') as temp_file:
  29. json.dump(user, temp_file, indent=4) # Serialize user data to JSON
  30. # Execute the script with the temporary data file as an argument
  31. try:
  32. result = subprocess.run(['python', script_path, temp_data_file], capture_output=True, text=True, check=True)
  33. print(f"Successfully executed script for user {user_id}")
  34. print("Script Output:\n", result.stdout)
  35. except subprocess.CalledProcessError as e:
  36. print(f"Error executing script for user {user_id}:")
  37. print("Return code:", e.returncode)
  38. print("Stdout:", e.stdout)
  39. print("Stderr:", e.stderr)
  40. # Clean up the temporary file
  41. os.remove(temp_data_file)
  42. if __name__ == '__main__':
  43. # Example Usage (replace with your config file)
  44. config_file = 'config.json'
  45. bootstrap_scripts(config_file)

Add your comment