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/util/jupyter-api/setenv-commands.ts
Views: 687
1
/*
2
Code to set an environment variable to a given value in
3
several different languages.
4
*/
5
6
export default function createSetEnvCommand(
7
lang: string,
8
varName: string,
9
varValue: string
10
): string {
11
// throws exception if don't know how.
12
lang = lang.toLowerCase(); // xeus-cling is 'C++-17'
13
if (lang == "sparql") {
14
// there is no notion of environment variables for sparql.
15
return "";
16
}
17
if (lang.startsWith("python") || lang.startsWith("sage")) {
18
return createPythonSetEnvCode(varName, varValue);
19
} else if (lang == "r") {
20
return createRSetEnvCode(varName, varValue);
21
} else if (lang == "julia") {
22
return createJuliaSetEnvCode(varName, varValue);
23
} else if (lang == "octave" || lang == "matlab") {
24
return createOctaveSetEnvCode(varName, varValue);
25
} else if (lang == "javascript") {
26
return createNodeSetEnvCode(varName, varValue);
27
} else if (lang == "bash" || lang == "sh") {
28
return createBashSetEnvCommand(varName, varValue);
29
} else if (lang == "prolog") {
30
return createPrologSetEnvCode(varName, varValue);
31
} else if (lang == "c" || lang.startsWith("c++")) {
32
return createCppSetEnvCode(varName, varValue);
33
} else {
34
// e.g., "gap" -- I didn't try.
35
throw Error(
36
`unable to set environment variable: ${varName} for language ${lang} is not implemented.`
37
);
38
}
39
}
40
41
// mostly written by ChatGPT4
42
function createPythonSetEnvCode(varName: string, varValue: string): string {
43
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
44
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
45
return `import os; os.environ['${escapedVarName}'] = '${escapedVarValue}'`;
46
}
47
48
function createRSetEnvCode(varName: string, varValue: string): string {
49
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
50
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
51
return `Sys.setenv("${escapedVarName}"="${escapedVarValue}")`;
52
}
53
54
function createJuliaSetEnvCode(varName: string, varValue: string): string {
55
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
56
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
57
return `ENV["${escapedVarName}"] = "${escapedVarValue}"`;
58
}
59
60
function createOctaveSetEnvCode(varName: string, varValue: string): string {
61
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
62
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
63
return `setenv('${escapedVarName}','${escapedVarValue}')`;
64
}
65
66
function createNodeSetEnvCode(varName: string, varValue: string): string {
67
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/`/g, "\\`");
68
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/`/g, "\\`");
69
return `process.env.${escapedVarName} = \`${escapedVarValue}\``;
70
}
71
72
function createBashSetEnvCommand(varName: string, varValue: string): string {
73
const escapedVarName = varName.replace(/(["'$`\\])/g, "\\$1");
74
const escapedVarValue = varValue.replace(/(["'$`\\])/g, "\\$1");
75
return `${escapedVarName}="${escapedVarValue}"`;
76
}
77
78
function createPrologSetEnvCode(varName: string, varValue: string): string {
79
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
80
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
81
return `setenv('${escapedVarName}', '${escapedVarValue}').`;
82
}
83
84
function createCppSetEnvCode(varName: string, varValue: string): string {
85
const escapedVarName = varName.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
86
const escapedVarValue = varValue.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
87
return `#include <cstdlib>\nstd::putenv("${escapedVarName}=${escapedVarValue}")\n`;
88
}
89
90