Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/project/exec_shell_code.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2023 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// import { getLogger } from "@cocalc/backend/logger";6// const winston = getLogger('exec-shell-code')78import { abspath } from "@cocalc/backend/misc_node";9import { CoCalcSocket } from "@cocalc/backend/tcp/enable-messaging-protocol";10import * as message from "@cocalc/util/message";11import { getLogger } from "./logger";12import execCode from "@cocalc/project/browser-websocket/exec-code";13import { ExecuteCodeOutput } from "@cocalc/util/types/execute-code";1415const { debug: D } = getLogger("exec_shell_code");1617export async function exec_shell_code(socket: CoCalcSocket, mesg) {18//winston.debug("project_exec: #{misc.to_json(mesg)} in #{process.cwd()}")19if (mesg.command === "smc-jupyter") {20socket.write_mesg(21"json",22message.error({ id: mesg.id, error: "do not run smc-jupyter directly" }),23);24return;25}2627D(`command=${mesg.command} args=${mesg.args} path=${mesg.path}`);2829try {30const out = await execCode({31path: !!mesg.compute_server_id32? mesg.path33: abspath(mesg.path != null ? mesg.path : ""),34...mesg,35});36let ret: ExecuteCodeOutput & { id: string } = {37id: mesg.id,38type: "blocking",39stdout: out?.stdout,40stderr: out?.stderr,41exit_code: out?.exit_code,42};43if (out?.type === "async") {44// extra fields for ExecuteCodeOutputAsync45ret = {46...ret,47...out, // type=async, pid, status, job_id, stats, ...48};49}50socket.write_mesg("json", message.project_exec_output(ret));51} catch (err) {52let error = `Error executing command '${mesg.command}' with args '${mesg.args}' -- ${err}`;53if (error.indexOf("Connection refused") !== -1) {54error +=55"-- Email [email protected] if you need full internet access, which is disabled by default.";56}57// Too annoying and doesn't work.58//if error.indexOf("=") != -159// error += "-- This is a BASH terminal, not a Sage worksheet. For Sage, use +New and create a Sage worksheet."60const err_mesg = message.error({61id: mesg.id,62error,63});64socket.write_mesg("json", err_mesg);65}66}676869