Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/bin/run-project.py
1447 views
1
#!/usr/bin/env python3
2
"""
3
This is a script that is useful for debugging why a project crashes
4
on startup when logfiles are not sufficient.
5
6
When a project starts the hub also writes a file ~/.smc/launch-param.json,
7
where ~ is the HOME directory of the project. This launch-params.json
8
records the command, args, environment and working directory used to
9
launch the project. Using the script you're reading now, you can
10
manually launch the project, but in the foreground in your terminal,
11
and see what's going on when it "mysteriously crashes".
12
13
To use this script:
14
15
./run-project.py /path/to/launch-params.json
16
17
The purpose of this script is just to help in figuring out why a project
18
starts up and then JUST CRASHES for mysterious reasons.
19
"""
20
21
import json, subprocess, os, sys
22
from datetime import datetime, timezone
23
24
25
def print_time():
26
current_time = datetime.now(timezone.utc)
27
formatted_time = current_time.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
28
print(formatted_time)
29
30
31
def run_command_with_params(params):
32
# Get the command, args, and cwd from the params
33
cmd = params["cmd"]
34
args = params["args"]
35
#args = [x for x in args if 'daemon' not in x]
36
cwd = params["cwd"]
37
38
# Get the environment variables from the params
39
env = params["env"]
40
if 'DEBUG' not in env:
41
env['DEBUG'] = 'cocalc:*'
42
env['DEBUG_CONSOLE'] = 'yes'
43
44
# Convert the environment dictionary to a list of key=value strings
45
env_list = [f"{key}={value}" for key, value in env.items()]
46
47
print(
48
"Running the following command with the environment setup for the project:\n"
49
)
50
print(" ".join([cmd] + args))
51
try:
52
# Run the command with the specified arguments and environment in the given cwd
53
subprocess.run([cmd] + args,
54
cwd=cwd,
55
env=dict(os.environ, **env),
56
check=True)
57
except subprocess.CalledProcessError as e:
58
print(f"Command execution failed with error code {e.returncode}.")
59
# Handle the error as needed
60
61
62
if __name__ == "__main__":
63
if len(sys.argv) < 2:
64
print(f"USAGE: {sys.argv[0]} /path/to/launch-params.json")
65
sys.exit(1)
66
try:
67
print_time()
68
# Read the JSON data from the file
69
with open(sys.argv[1], "r") as file:
70
params = json.load(file)
71
run_command_with_params(params)
72
except FileNotFoundError:
73
print(f"File '{sys.argv[1]}' not found.")
74
except json.JSONDecodeError:
75
print("Error parsing JSON data from the file.")
76
77