forked from EasyScreenCast/EasyScreenCast
-
Notifications
You must be signed in to change notification settings - Fork 4
/
utilexecmd.js
249 lines (227 loc) · 7.39 KB
/
utilexecmd.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
Copyright (C) 2017 Borsato Ivano
The JavaScript code in this page is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
*/
const ByteArray = imports.byteArray;
const Lang = imports.lang;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Lib = Me.imports.convenience;
/**
* @type {ExecuteStuff}
*/
var ExecuteStuff = new Lang.Class({
Name: "ExecuteStuff",
/**
* @param scope
* @private
*/
_init: function (scope) {
Lib.TalkativeLog("-¶-init scope:" + scope);
this.Scope = scope;
this.Callback = null;
},
/**
* @param cmd
* @return {*[]}
* @private
*/
_parseCmd: function (cmd) {
let successP, argv;
try {
[successP, argv] = GLib.shell_parse_argv(cmd);
} catch (err) {
Lib.TalkativeLog("-¶-ERROR PARSE");
successP = false;
}
if (successP) {
Lib.TalkativeLog("-¶-parse: " + successP + " argv: " + argv);
return [successP, argv];
} else {
return [successP, null];
}
},
/**
* @param cmd
* @param sync
* @param resCallback
* @param lineCallback
* @constructor
*/
Execute: function (cmd, sync, resCallback, lineCallback) {
Lib.TalkativeLog("-¶-execute: " + cmd);
this.CommandString = cmd;
if (
resCallback === undefined &&
resCallback === null &&
typeof resCallback !== "function"
) {
Lib.TalkativeLog("-¶-resCallback NEED to be a function");
this.Callback = null;
} else {
this.Callback = resCallback;
}
if (sync === true) {
Lib.TalkativeLog("-¶-sync execute (wait for return)");
this._syncCmd(this.CommandString);
} else {
Lib.TalkativeLog("-¶-async execute (fork process)");
if (
lineCallback === undefined &&
lineCallback === null &&
typeof lineCallback !== "function"
) {
Lib.TalkativeLog("-¶-lineCallback NEED to be a function");
this.lineCallback = null;
} else {
this.lineCallback = lineCallback;
}
this._asyncCmd(this.CommandString);
}
},
/**
* @param cmd
* @return {*}
* @constructor
*/
Spawn: function (cmd) {
let [successP, argv] = this._parseCmd(cmd);
if (successP) {
let successS, pid;
try {
[successS, pid] = GLib.spawn_async(
null,
argv,
null,
GLib.SpawnFlags.SEARCH_PATH |
GLib.SpawnFlags.DO_NOT_REAP_CHILD,
null
);
} catch (err) {
Lib.TalkativeLog(
"-¶-ERROR SPAWN err:" + err.message.toString()
);
successS = false;
}
if (successS) {
Lib.TalkativeLog("-¶-spawn: " + successS + " pid: " + pid);
return true;
} else {
Lib.TalkativeLog("-¶-spawn ERROR");
return null;
}
}
},
/**
* @param cmd
* @private
*/
_syncCmd: function (cmd) {
let [successP, argv] = this._parseCmd(cmd);
if (successP) {
Lib.TalkativeLog("-¶-argv: " + argv);
let successS, std_out, std_err, exit;
try {
[successS, std_out, std_err, exit] = GLib.spawn_sync(
null,
argv,
null,
GLib.SpawnFlags.SEARCH_PATH,
function () {}
);
} catch (err) {
Lib.TalkativeLog("-¶-ERROR SPAWN");
successS = false;
}
if (successS) {
Lib.TalkativeLog("-¶-argv: " + argv);
Lib.TalkativeLog("-¶-std_out: " + ByteArray.toString(std_out));
Lib.TalkativeLog("-¶-std_err: " + ByteArray.toString(std_err));
Lib.TalkativeLog("-¶-exe RC");
if (this.Callback !== null) {
this.Callback.apply(this.Scope, [
true,
ByteArray.toString(std_out),
]);
}
} else {
Lib.TalkativeLog("-¶-ERROR exe WC");
if (this.Callback !== null) {
this.Callback.apply(this.Scope, [false]);
}
}
}
},
/**
* @param cmd
* @private
*/
_asyncCmd: function (cmd) {
let [successP, argv] = this._parseCmd(cmd);
if (successP) {
Lib.TalkativeLog("-¶-argv: " + argv);
let successS, pid, std_in, std_out, std_err;
try {
[
successS,
pid,
std_in,
std_out,
std_err,
] = GLib.spawn_async_with_pipes(
null,
argv,
null,
GLib.SpawnFlags.SEARCH_PATH,
function () {},
null,
null
);
} catch (err) {
Lib.TalkativeLog("-¶-ERROR SPAWN");
successS = false;
}
if (successS) {
Lib.TalkativeLog("-¶-argv: " + argv);
Lib.TalkativeLog("-¶-pid: " + pid);
Lib.TalkativeLog("-¶-std_in: " + std_in);
Lib.TalkativeLog("-¶-std_out: " + std_out);
Lib.TalkativeLog("-¶-std_err: " + std_err);
let out_reader = new Gio.DataInputStream({
base_stream: new Gio.UnixInputStream({
fd: std_out,
}),
});
let in_writer = new Gio.DataOutputStream({
base_stream: new Gio.UnixOutputStream({
fd: std_in,
}),
});
let [out, size] = out_reader.read_line(null);
while (out !== null) {
if (this.lineCallback !== null) {
this.lineCallback.apply(this.Scope, [out.toString()]);
}
[out, size] = out_reader.read_line(null);
}
if (this.Callback !== null) {
Lib.TalkativeLog("-¶-exe RC");
this.Callback.apply(this.Scope, [true]);
}
} else {
Lib.TalkativeLog("-¶-ERROR exe WC");
if (this.Callback !== null) {
this.Callback.apply(this.Scope, [false]);
}
}
}
},
});