Расширения оболочки Gnome: как запустить команду с конвейерами
Так что я делаю расширение Gnome Shell. И я хочу иметь возможность выполнить какую-то команду с конвейером. (Команда на самом деле "xrandr --query | awk 'something'"
но это не по теме)
Итак, что я сделал до сих пор
GLib.spawn_async_with_pipes(null,
['/usr/bin/xrandr', '--query', '|', 'awk...'], null,
GLib.SpawnFlags.DO_NOT_REAP_CHILD, null);
Но это не работает! Я не могу найти ни одного примера запуска команды в расширениях гнома с помощью канала.
Должен ли я написать "|"
в команде, как я сделал?
3 ответа
spawn_async_with_pipes
не делает то, что вы хотите (по-простому). Он возвращает трубы для обработки с ним. Вы можете сделать это с помощью двух вызовов и подключения, но это будет немного сложнее.
Простой способ сохранить точный синтаксис - вызвать оболочку, которая будет выполнять конвейерную обработку с помощью этого ответа, дающего способ вызова команды. Я написал следующий код, который вызывает оболочку (bash для этого случая) с правильные аргументы
const Util = imports.misc.util;
Util.spawn(['/bin/bash', '-c', "xrandr --query | awk 'something'"])
Я реализовал класс TerminalReader некоторое время назад в Cinnamon Applet: https://github.com/lestcape/Configurable-Menu/blob/OwnAPI/configurableMenu%40lestcape/pakagesManager.js#L31
Этот класс теперь используется и в других местах, поэтому у вас есть больше примеров, чтобы лучше его понять: https://github.com/search?l=JavaScript&q=TerminalReader&type=Code&utf8=%E2%9C%93
Вот исходный код класса:
function TerminalReader() {
this._init.apply(this, arguments);
}
TerminalReader.prototype = {
_init: function(command, callback) {
this._callbackPipe = callback;
this._commandPipe = command;
this.idle = true;
this._childWatch = null;
},
executeReader: function() {
if(this.idle) {
this.idle = false;
try {
let [success, argv] = GLib.shell_parse_argv("sh -c '" + this._commandPipe + "'");
if(success) {
let [exit, pid, stdin, stdout, stderr] =
GLib.spawn_async_with_pipes(
null, // cwd
argv, // args
null, // env
GLib.SpawnFlags.SEARCH_PATH | GLib.SpawnFlags.DO_NOT_REAP_CHILD, //Use env path and no repet
null // child_setup
);
this._childPid = pid;
this._stdin = new Gio.UnixOutputStream({ fd: stdin, close_fd: true });
this._stdout = new Gio.UnixInputStream({ fd: stdout, close_fd: true });
this._stderr = new Gio.UnixInputStream({ fd: stderr, close_fd: true });
// We need this one too, even if don't actually care of what the process
// has to say on stderr, because otherwise the fd opened by g_spawn_async_with_pipes
// is kept open indefinitely
this._stderrStream = new Gio.DataInputStream({ base_stream: this._stderr });
this._dataStdout = new Gio.DataInputStream({ base_stream: this._stdout });
this._cancellableStderrStream = new Gio.Cancellable();
this._cancellableStdout = new Gio.Cancellable();
this.resOut = 1;
this._readStdout();
this.resErr = 1;
this._readStderror();
this._childWatch = GLib.child_watch_add(GLib.PRIORITY_DEFAULT, pid, Lang.bind(this, function(pid, status, requestObj) {
GLib.source_remove(this._childWatch);
this._childWatch = null;
this._stdin.close(null);
this.idle = true;
}));
}
//throw
} catch(err) {
if(err.code == GLib.SpawnError.G_SPAWN_ERROR_NOENT) {
err.message = _("Command not found.");
} else {
// The exception from gjs contains an error string like:
// Error invoking GLib.spawn_command_line_async: Failed to
// execute child process "foo" (No such file or directory)
// We are only interested in the part in the parentheses. (And
// we can't pattern match the text, since it gets localized.)
err.message = err.message.replace(/.*\((.+)\)/, '$1');
}
throw err;
}
}
},
destroy: function() {
try {
if(this._childWatch) {
GLib.source_remove(this._childWatch);
this._childWatch = null;
}
if(!this._dataStdout.is_closed()) {
this._cancellableStdout.cancel();
this._stdout.close_async(0, null, Lang.bind(this, this.closeStdout));
}
if(!this._stderrStream.is_closed()) {
this._cancellableStderrStream.cancel();
this._stderrStream.close_async(0, null, Lang.bind(this, this.closeStderrStream));
}
this._stdin.close(null);
this.idle = true;
}
catch(e) {
Main.notify("Error on close" + this._dataStdout.is_closed(), e.message);
}
},
closeStderrStream: function(std, result) {
try {
std.close_finish(result);
} catch(e) {
std.close_async(0, null, Lang.bind(this, this.closeStderrStream));
}
},
closeStdout: function(std, result) {
try {
std.close_finish(result);
} catch(e) {
std.close_async(0, null, Lang.bind(this, this.closeStderrStream));
}
},
_readStdout: function() {
this._dataStdout.fill_async(-1, GLib.PRIORITY_DEFAULT, this._cancellableStdout, Lang.bind(this, function(stream, result) {
try {
if(!this._dataStdout.is_closed()) {
if(this.resOut != -1)
this.resOut = this._dataStdout.fill_finish(result);// end of file
if(this.resOut == 0) {
let val = stream.peek_buffer().toString();
if(val != "")
this._callbackPipe(this._commandPipe, true, val);
this._stdout.close(this._cancellableStdout);
} else {
// Try to read more
this._dataStdout.set_buffer_size(2 * this._dataStdout.get_buffer_size());
this._readStdout();
}
}
} catch(e) {
global.log(e.toString());
}
}));
},
_readStderror: function() {
this._stderrStream.fill_async(-1, GLib.PRIORITY_DEFAULT, this._cancellableStderrStream, Lang.bind(this, function(stream, result) {
try {
if(!this._stderrStream.is_closed()) {
if(this.resErr != -1)
this.resErr = this._stderrStream.fill_finish(result);
if(this.resErr == 0) { // end of file
let val = stream.peek_buffer().toString();
if(val != "")
this._callbackPipe(this._commandPipe, false, val);
this._stderr.close(null);
} else {
this._stderrStream.set_buffer_size(2 * this._stderrStream.get_buffer_size());
this._readStderror();
}
}
} catch(e) {
global.log(e.toString());
}
}));
}
};
Призвание spawn_async_with_pipes()
не поможет, так как возвращает объект с доступными каналами для stdin/stdout/stderr, а не вызовы, которые передаются друг другу. За исключением простого вызова экземпляра оболочки и предоставления ему возможности выполнять команды, единственный способ - придерживаться самого расширения, позволяя GNOME обрабатывать все, используя временный файл и оболочку по умолчанию (такую, которая перезаписывает себя, если она уже существует).
Для примера кода, давайте сделаем так же, как journalctl | grep -i js
будет делать на CLI:
//Gio for File I/O and GLib for command execution
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
//Create an object for the temp file
let file = Gio.file_new_tmp(null);
//Now write the output of journalctl to the file
file[1].get_output_stream().write(GLib.spawn_command_line_sync("journalctl")[1], null);
//Execute the command and save the result, this locks the thread though,
//so don't use indefinite commands like journalctl -f
//This returns [Boolean success, String stdout, String stderr, Number exit_status]
let journalJS = GLib.spawn_command_line_sync('grep -i js ' + file[0].get_path())[1]
Теперь не стесняйтесь обращаться с имеющимися у вас данными, так как все завершается после завершения команды.
Не забудьте закрыть файл, используя file[1].close();
и установка любых оставшихся переменных в null
когда закончите.