1. import subprocess
  2. import json
  3. import os
  4. import time
  5. import logging
  6. # Configure logging
  7. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  8. def bootstrap_script(script_path, headers_metadata_path, retries=3, retry_delay=5):
  9. """
  10. Bootstraps a script to generate headers metadata for sandbox usage with retry logic.
  11. Args:
  12. script_path (str): Path to the script to execute.
  13. headers_metadata_path (str): Path to save the generated headers metadata.
  14. retries (int): Number of retry attempts.
  15. retry_delay (int): Delay in seconds between retries.
  16. Returns:
  17. bool: True if successful, False otherwise.
  18. """
  19. for attempt in range(retries):
  20. try:
  21. # Execute the script and capture output
  22. result = subprocess.run(['python', script_path], capture_output=True, text=True, check=True)
  23. # Save the output as headers metadata
  24. with open(headers_metadata_path, 'w') as f:
  25. f.write(result.stdout)
  26. logging.info(f"Successfully generated headers metadata at {headers_metadata_path}")
  27. return True # Success!
  28. except subprocess.CalledProcessError as e:
  29. logging.error(f"Script failed on attempt {attempt + 1}: {e}")
  30. logging.error(f"Stdout: {e.stdout}")
  31. logging.error(f"Stderr: {e.stderr}")
  32. except Exception as e:
  33. logging.error(f"An unexpected error occurred on attempt {attempt + 1}: {e}")
  34. if attempt < retries - 1:
  35. logging.warning(f"Retrying in {retry_delay} seconds...")
  36. time.sleep(retry_delay)
  37. logging.error(f"Failed to generate headers metadata after {retries} attempts.")
  38. return False # Failure after all retries
  39. if __name__ == '__main__':
  40. # Example Usage
  41. script_to_run = 'generate_headers.py' # Replace with your script
  42. metadata_file = 'headers_metadata.json' # Replace with desired file path
  43. # Ensure the script exists
  44. if not os.path.exists(script_to_run):
  45. logging.error(f"Script '{script_to_run}' not found.")
  46. exit(1)
  47. success = bootstrap_script(script_to_run, metadata_file)
  48. if success:
  49. print("Headers metadata generation completed successfully.")
  50. else:
  51. print("Headers metadata generation failed.")

Add your comment