1 ///<script type="text/javascript">
4 GLib = imports.gi.GLib;
10 * Library to wrap GLib.spawn_async_with_pipes
14 * Spawn = import.Spawn;
17 * var output = Spawn.run({
19 * args : [ 'ls', '-l' ],
20 * env : [], // optional
22 output : function (line) { Seed.print(line); },
23 * stderr : function (line) {Seed.print("ERROR" + line); },
24 * input : function() { return 'xxx' },
34 * @param cfg {Object} settings - see properties.
36 * @arg cwd {String} working directory. (defaults to home directory)
37 * @arg args {Array} arguments eg. [ 'ls', '-l' ]
38 * @arg listeners {Object} (optional) handlers for output, stderr, input
39 * stderr/output both receive output line as argument
40 * input should return any standard input
41 * finish recieves result as argument.
42 * @arg env {Array} enviroment eg. [ 'GITDIR=/home/test' ]
43 * @arg async {Boolean} (optional)return instantly, or wait for exit. (default no)
44 * @arg exceptions {Boolean} throw exception on failure (default no)
45 * @arg debug {Boolean} print out what's going on.. (default no)
53 this.listeners = this.listeners || {};
54 this.cwd = this.cwd || GLib.get_home_dir();
55 if (!this.args || !this.args.length) {
70 * @property output {String} resulting output
74 * @property stderr {String} resulting output from stderr
78 * @property result {Number} execution result.
82 * @property pid {Number} pid of child process (of false if it's not running)
89 * @property in_ch {GLib.IOChannel} input io channel
93 * @property out_ch {GLib.IOChannel} output io channel
97 * @property err_ch {GLib.IOChannel} stderr io channel
104 * Run the configured command.
120 print("spawn : " + this.args.join(" "));
123 GLib.spawn_async_with_pipes(this.cwd, this.args, this.env,
124 GLib.SpawnFlags.DO_NOT_REAP_CHILD + GLib.SpawnFlags.SEARCH_PATH ,
127 this.pid = ret.child_pid;
130 print("PID: " + this.pid);
135 GLib.spawn_close_pid(_this.pid); // hopefully kills it..
138 if (_this.in_ch) GLib.io_channel_close(_this.in_ch);
139 if (_this.out_ch) GLib.io_channel_close(_this.out_ch);
140 if (_this.err_ch) GLib.io_channel_close(_this.err_ch);
141 // blank out channels
143 _this.err_ch = false;
144 _this.out_ch = false;
145 // rmeove listeners !! important otherwise we kill the CPU
146 if (err_src !== false) GLib.source_remove(err_src);
147 if (out_src !== false) GLib.source_remove(out_src);
155 GLib.child_watch_add(GLib.PRIORITY_DEFAULT, this.pid, function(pid, result) {
156 _this.result = result;
158 print("result: " + result);
160 _this.read(_this.out_ch);
161 _this.read(_this.err_ch);
163 GLib.spawn_close_pid(_this.pid);
166 GLib.main_loop_quit(ctx);
169 if (_this.listeners.finish) {
170 _this.listeners.finish.call(this, _this.result);
175 this.in_ch = GLib.io_channel_unix_new(ret.standard_input);
176 this.out_ch = GLib.io_channel_unix_new(ret.standard_output);
177 this.err_ch = GLib.io_channel_unix_new(ret.standard_error);
179 // make everything non-blocking!
180 GLib.io_channel_set_flags (this.in_ch,GLib.IOFlags.NONBLOCK);
181 GLib.io_channel_set_flags (this.out_ch,GLib.IOFlags.NONBLOCK);
182 GLib.io_channel_set_flags (this.err_ch,GLib.IOFlags.NONBLOCK);
185 // add handlers for output and stderr.
186 out_src= GLib.io_add_watch(this.out_ch, GLib.PRIORITY_DEFAULT,
187 GLib.IOCondition.OUT + GLib.IOCondition.IN + GLib.IOCondition.PRI, function()
189 _this.read(_this.out_ch);
192 err_src= GLib.io_add_watch(this.err_ch, GLib.PRIORITY_DEFAULT,
193 GLib.IOCondition.ERR + GLib.IOCondition.IN + GLib.IOCondition.PRI + GLib.IOCondition.OUT,
196 _this.read(_this.err_ch);
203 if (this.pid !== false) {
204 // child can exit before we get this far..
205 if (this.listeners.input) {
207 this.write(this.listeners.input.call(this));
216 // async - if running - return..
217 if (this.async && this.pid) {
221 // start mainloop if not async..
223 if (this.pid !== false) {
225 print("starting main loop");
227 ctx = GLib.main_loop_new (null, false);
228 GLib.main_loop_run(ctx, false); // wait fore exit?
231 tidyup(); // tidyup get's called in main loop.
234 if (this.exceptions && this.result != 0) {
235 throw this; // we throw self...
238 // finally throw, or return self..
244 * write to stdin of process
245 * @arg str {String} string to write to stdin of process
246 * @returns GLib.IOStatus (0 == error, 1= NORMAL)
248 write : function(str) // write a line to
251 return; // input is closed
254 var res = GLib.io_channel_write_chars(this.in_ch, str, str.length);
255 if (res != GLib.IOStatus.NORMAL) {
256 throw "Write failed";
258 return ret.bytes_written;
262 * read from pipe and call appropriate listerner and add to output or stderr string.
263 * @arg giochannel to read from.
268 var prop = ch == this.out_ch ? 'output' : 'stderr';
271 var x = new GLib.String();
272 var status = GLib.io_channel_read_line_string (ch, x);
274 case GLib.IOStatus.NORMAL:
276 if (this.listeners[prop]) {
277 this.listeners[prop].call(this, x.str);
279 _this[prop] += x.str;
281 print(prop + ':' + x.str);
284 case GLib.IOStatus.AGAIN:
286 case GLib.IOStatus.ERROR:
287 case GLib.IOStatus.EOF:
299 * simple run a process - returns result, or throws stderr result...
300 * @param cfg {Object} see spawn
303 cfg.exceptions = true;
305 var s = new Spawn(cfg);
314 // args: ['ls', '/tmp'],