import subprocess
import json
import os
import time
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def bootstrap_script(script_path, headers_metadata_path, retries=3, retry_delay=5):
"""
Bootstraps a script to generate headers metadata for sandbox usage with retry logic.
Args:
script_path (str): Path to the script to execute.
headers_metadata_path (str): Path to save the generated headers metadata.
retries (int): Number of retry attempts.
retry_delay (int): Delay in seconds between retries.
Returns:
bool: True if successful, False otherwise.
"""
for attempt in range(retries):
try:
# Execute the script and capture output
result = subprocess.run(['python', script_path], capture_output=True, text=True, check=True)
# Save the output as headers metadata
with open(headers_metadata_path, 'w') as f:
f.write(result.stdout)
logging.info(f"Successfully generated headers metadata at {headers_metadata_path}")
return True # Success!
except subprocess.CalledProcessError as e:
logging.error(f"Script failed on attempt {attempt + 1}: {e}")
logging.error(f"Stdout: {e.stdout}")
logging.error(f"Stderr: {e.stderr}")
except Exception as e:
logging.error(f"An unexpected error occurred on attempt {attempt + 1}: {e}")
if attempt < retries - 1:
logging.warning(f"Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
logging.error(f"Failed to generate headers metadata after {retries} attempts.")
return False # Failure after all retries
if __name__ == '__main__':
# Example Usage
script_to_run = 'generate_headers.py' # Replace with your script
metadata_file = 'headers_metadata.json' # Replace with desired file path
# Ensure the script exists
if not os.path.exists(script_to_run):
logging.error(f"Script '{script_to_run}' not found.")
exit(1)
success = bootstrap_script(script_to_run, metadata_file)
if success:
print("Headers metadata generation completed successfully.")
else:
print("Headers metadata generation failed.")
Add your comment