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)
53 this.listeners = this.listeners || {};
54 this.cwd = this.cwd || GLib.get_home_dir();
55 if (!this.args || !this.args.length) {
69 * @property output {String} resulting output
73 * @property stderr {String} resulting output from stderr
77 * @property result {Number} execution result.
81 * @property pid {Number} pid of child process (of false if it's not running)
86 * @property done {Boolean} has the process completed.
91 * @property in_ch {GLib.IOChannel} input io channel
95 * @property out_ch {GLib.IOChannel} output io channel
99 * @property err_ch {GLib.IOChannel} stderr io channel
106 * Run the configured command.
121 GLib.spawn_async_with_pipes(this.cwd, this.args, null,
122 GLib.SpawnFlags.DO_NOT_REAP_CHILD + GLib.SpawnFlags.SEARCH_PATH ,
125 this.pid = ret.child_pid;
131 GLib.spawn_close_pid(_this.pid); // hopefully kills it..
134 if (_this.in_ch) GLib.io_channel_close(_this.in_ch);
135 if (_this.out_ch) GLib.io_channel_close(_this.out_ch);
136 if (_this.err_ch) GLib.io_channel_close(_this.err_ch);
137 // blank out channels
139 _this.err_ch = false;
140 _this.out_ch = false;
141 // rmeove listeners !! important otherwise we kill the CPU
142 if (err_src !== false) GLib.source_remove(err_src);
143 if (out_src !== false) GLib.source_remove(out_src);
151 GLib.child_watch_add(GLib.PRIORITY_DEFAULT, this.pid, function(pid, result) {
152 _this.result = result;
154 GLib.spawn_close_pid(_this.pid);
157 GLib.main_loop_quit(ctx);
160 if (this.listeners.finish) {
161 this.listeners.finish.call(this, this.result);
166 this.in_ch = GLib.io_channel_unix_new(ret.standard_input);
167 this.out_ch = GLib.io_channel_unix_new(ret.standard_output);
168 this.err_ch = GLib.io_channel_unix_new(ret.standard_error);
170 // make everything non-blocking!
171 GLib.io_channel_set_flags (this.in_ch,GLib.IOFlags.NONBLOCK);
172 GLib.io_channel_set_flags (this.out_ch,GLib.IOFlags.NONBLOCK);
173 GLib.io_channel_set_flags (this.err_ch,GLib.IOFlags.NONBLOCK);
176 // add handlers for output and stderr.
177 out_src= GLib.io_add_watch(this.out_ch, GLib.PRIORITY_DEFAULT,
178 GLib.IOCondition.OUT + GLib.IOCondition.IN + GLib.IOCondition.PRI, function()
180 _this.read(this.out_ch);
183 err_src= GLib.io_add_watch(this.err_ch, GLib.PRIORITY_DEFAULT,
184 GLib.IOCondition.ERR + GLib.IOCondition.IN + GLib.IOCondition.PRI + GLib.IOCondition.OUT,
187 _this.read(this.err_ch);
194 if (this.pid !== false) {
195 // child can exit before we get this far..
196 if (this.listeners.input) {
198 this.write(this.listeners.input.call(this));
207 // start mainloop if not async..
209 if (this.pid !== false) {
210 ctx = GLib.main_loop_new (null, false);
211 GLib.main_loop_run(ctx, false); // wait fore exit?
213 tidyup(); // tidyup get's called in main loop.
216 if (this.exceptions && this.result != 0) {
217 throw this; // we throw self...
222 // finally throw, or return self..
228 * write to stdin of process
229 * @arg str {String} string to write to stdin of process
230 * @returns GLib.IOStatus (0 == error, 1= NORMAL)
232 write : function(str) // write a line to
235 return; // input is closed
238 var res = GLib.io_channel_write_chars(this.in_ch, str, str.length);
239 if (res != GLib.IOStatus.NORMAL) {
240 throw "Write failed";
242 return ret.bytes_written;
246 * read from pipe and call appropriate listerner and add to output or stderr string.
247 * @arg giochannel to read from.
252 var prop = ch == this.out_ch ? 'output' : 'stderr';
254 var x = new GLib.String();
255 var status = GLib.io_channel_read_line_string (ch, x);
257 case GLib.IOStatus.NORMAL:
259 if (this.listeners[prop]) {
260 this.listeners[prop].call(this, x.str);
262 _this[prop] += x.str;
264 case GLib.IOStatus.AGAIN:
266 case GLib.IOStatus.ERROR:
267 case GLib.IOStatus.EOF:
279 * simple run a process - returns result, or throws stderr result...
280 * @param cfg {Object} see spawn
283 cfg.exceptions = true;
285 var s = new Spawn(cfg);