CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/bin/run-project.py
Views: 687
1
#!/usr/bin/env python
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
DO NOT expect to interact with a project running this way from your
18
frontend browser client. That's not going to work, because the hub
19
will try to start the project again (thus deleting and recreating ~/.smc).
20
The purpose of this script is just to help in figuring out why a project
21
starts up and then JUST CRASHES for mysterious reasons.
22
23
**It won't help with anything else yet.**
24
"""
25
26
import json, subprocess, os, sys
27
28
29
def run_command_with_params(params):
30
# Get the command, args, and cwd from the params
31
cmd = params["cmd"]
32
args = params["args"]
33
args = [x for x in args if 'daemon' not in x]
34
cwd = params["cwd"]
35
36
# Get the environment variables from the params
37
env = params["env"]
38
env['DEBUG'] = '*'
39
env['DEBUG_CONSOLE'] = 'yes'
40
41
# Convert the environment dictionary to a list of key=value strings
42
env_list = [f"{key}={value}" for key, value in env.items()]
43
44
try:
45
# Run the command with the specified arguments and environment in the given cwd
46
subprocess.run([cmd] + args,
47
cwd=cwd,
48
env=dict(os.environ, **env),
49
check=True)
50
except subprocess.CalledProcessError as e:
51
print(f"Command execution failed with error code {e.returncode}.")
52
# Handle the error as needed
53
54
55
if __name__ == "__main__":
56
if len(sys.argv) < 2:
57
print(f"USAGE: {sys.argv[0]} /path/to/launch-params.json")
58
sys.exit(1)
59
try:
60
# Read the JSON data from the file
61
with open(sys.argv[1], "r") as file:
62
params = json.load(file)
63
print(params)
64
run_command_with_params(params)
65
except FileNotFoundError:
66
print("File 'sys.argv[1]' not found.")
67
except json.JSONDecodeError:
68
print("Error parsing JSON data from the file.")
69
70