Path: blob/master/src/packages/project/bin/run-project.py
1447 views
#!/usr/bin/env python31"""2This is a script that is useful for debugging why a project crashes3on startup when logfiles are not sufficient.45When a project starts the hub also writes a file ~/.smc/launch-param.json,6where ~ is the HOME directory of the project. This launch-params.json7records the command, args, environment and working directory used to8launch the project. Using the script you're reading now, you can9manually launch the project, but in the foreground in your terminal,10and see what's going on when it "mysteriously crashes".1112To use this script:1314./run-project.py /path/to/launch-params.json1516The purpose of this script is just to help in figuring out why a project17starts up and then JUST CRASHES for mysterious reasons.18"""1920import json, subprocess, os, sys21from datetime import datetime, timezone222324def print_time():25current_time = datetime.now(timezone.utc)26formatted_time = current_time.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'27print(formatted_time)282930def run_command_with_params(params):31# Get the command, args, and cwd from the params32cmd = params["cmd"]33args = params["args"]34#args = [x for x in args if 'daemon' not in x]35cwd = params["cwd"]3637# Get the environment variables from the params38env = params["env"]39if 'DEBUG' not in env:40env['DEBUG'] = 'cocalc:*'41env['DEBUG_CONSOLE'] = 'yes'4243# Convert the environment dictionary to a list of key=value strings44env_list = [f"{key}={value}" for key, value in env.items()]4546print(47"Running the following command with the environment setup for the project:\n"48)49print(" ".join([cmd] + args))50try:51# Run the command with the specified arguments and environment in the given cwd52subprocess.run([cmd] + args,53cwd=cwd,54env=dict(os.environ, **env),55check=True)56except subprocess.CalledProcessError as e:57print(f"Command execution failed with error code {e.returncode}.")58# Handle the error as needed596061if __name__ == "__main__":62if len(sys.argv) < 2:63print(f"USAGE: {sys.argv[0]} /path/to/launch-params.json")64sys.exit(1)65try:66print_time()67# Read the JSON data from the file68with open(sys.argv[1], "r") as file:69params = json.load(file)70run_command_with_params(params)71except FileNotFoundError:72print(f"File '{sys.argv[1]}' not found.")73except json.JSONDecodeError:74print("Error parsing JSON data from the file.")757677