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/exec_shell_code.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// import { getLogger } from "@cocalc/backend/logger";
7
// const winston = getLogger('exec-shell-code')
8
9
import { abspath } from "@cocalc/backend/misc_node";
10
import { CoCalcSocket } from "@cocalc/backend/tcp/enable-messaging-protocol";
11
import * as message from "@cocalc/util/message";
12
import { getLogger } from "./logger";
13
import execCode from "@cocalc/project/browser-websocket/exec-code";
14
import { ExecuteCodeOutput } from "@cocalc/util/types/execute-code";
15
16
const { debug: D } = getLogger("exec_shell_code");
17
18
export async function exec_shell_code(socket: CoCalcSocket, mesg) {
19
//winston.debug("project_exec: #{misc.to_json(mesg)} in #{process.cwd()}")
20
if (mesg.command === "smc-jupyter") {
21
socket.write_mesg(
22
"json",
23
message.error({ id: mesg.id, error: "do not run smc-jupyter directly" }),
24
);
25
return;
26
}
27
28
D(`command=${mesg.command} args=${mesg.args} path=${mesg.path}`);
29
30
try {
31
const out = await execCode({
32
path: !!mesg.compute_server_id
33
? mesg.path
34
: abspath(mesg.path != null ? mesg.path : ""),
35
...mesg,
36
});
37
let ret: ExecuteCodeOutput & { id: string } = {
38
id: mesg.id,
39
type: "blocking",
40
stdout: out?.stdout,
41
stderr: out?.stderr,
42
exit_code: out?.exit_code,
43
};
44
if (out?.type === "async") {
45
// extra fields for ExecuteCodeOutputAsync
46
ret = {
47
...ret,
48
...out, // type=async, pid, status, job_id, stats, ...
49
};
50
}
51
socket.write_mesg("json", message.project_exec_output(ret));
52
} catch (err) {
53
let error = `Error executing command '${mesg.command}' with args '${mesg.args}' -- ${err}`;
54
if (error.indexOf("Connection refused") !== -1) {
55
error +=
56
"-- Email [email protected] if you need full internet access, which is disabled by default.";
57
}
58
// Too annoying and doesn't work.
59
//if error.indexOf("=") != -1
60
// error += "-- This is a BASH terminal, not a Sage worksheet. For Sage, use +New and create a Sage worksheet."
61
const err_mesg = message.error({
62
id: mesg.id,
63
error,
64
});
65
socket.write_mesg("json", err_mesg);
66
}
67
}
68
69