-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.d
executable file
·116 lines (93 loc) · 2.58 KB
/
install.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/rdmd
import std.stdio;
import std.getopt;
import std.exception : enforce;
import std.array : empty, front, popFront;
import std.process : execute;
string jdir = "/usr/local/bin/";
string getKernelPath() {
import std.algorithm : splitter, startsWith;
import std.string : strip, splitLines;
auto jp = execute(["jupyter", "kernelspec", "list"]);
enforce(jp.status == 0, "Failed to quere jupyter for the kernelspecs");
auto sout = jp.output.splitLines;
/* transform output of form
Available kernels:
python2 /usr/local/share/jupyter/kernels/python2
into: /usr/local/share/jupyter/kernels/
*/
if(sout.startsWith("Available kernels:")) {
sout.popFront();
} else {
throw new Exception("No available kernels found");
}
while(!sout.empty) {
enum p = "python2";
string f = sout.front.strip();
if(f.startsWith(p)) {
f = f[p.length .. $].strip();
f = f[0 .. $ - p.length];
return f;
}
sout.popFront();
}
throw new Exception("python2 kernel path not found");
}
void installJupyterd(const string kernelPath, const bool build) {
import std.file : exists, mkdir, copy, isDir, isFile;
const ddir = kernelPath ~ 'd';
if(!exists(ddir)) {
mkdir(ddir);
}
enum k = "kernel.json";
enforce(isFile(k), "kernel.json not found in pwd");
auto g = execute(["cp", k, ddir]);
enum j = "jupyterd";
if(!exists(j) || build) {
auto e = execute(["dub", "build"]);
writeln(e.output);
}
auto f = execute(["cp", j, jdir]);
}
void removeJupyterd(const string kernelPath) {
import std.file : rmdir, copy, isFile;
const ddir = kernelPath ~ "d/";
const k = ddir ~ "kernel.json";
enforce(isFile(k), "kernel.json not found in " ~ ddir);
auto g = execute(["rm", k]);
rmdir(ddir);
const l = jdir ~ "jupyterd";
enforce(isFile(l), "jupyterd not found in " ~ jdir);
g = execute(["rm", l]);
}
int main(string[] args) {
bool install;
bool remove;
bool path;
bool build;
auto helpInformation = getopt(args,
"install|i", "install or update the D Jupyter Kernel", &install,
"remove|r", "remove the D Jupyter Kernel", &remove,
"build|b", "force jupyterd build", &remove,
"path|p", "display the path where Jupyter Kernel are installed",
&path
);
if(helpInformation.helpWanted) {
defaultGetoptPrinter("Installer for the D Jupyter Kernel.\n"
~ "This needs to be executed with root privileges.",
helpInformation.options);
return 1;
}
string ppath = getKernelPath();
if(path) {
writefln!"python2 kernel path: '%s'"(ppath);
}
if(install) {
installJupyterd(ppath, build);
return 0;
} else if(remove) {
removeJupyterd(ppath);
return 0;
}
return 0;
}