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.
119 GLib.spawn_async_with_pipes(this.cwd, this.args, null,
120 GLib.SpawnFlags.DO_NOT_REAP_CHILD + GLib.SpawnFlags.SEARCH_PATH ,
123 this.pid = ret.child_pid;
129 GLib.spawn_close_pid(_this.pid); // hopefully kills it..
132 if (_this.in_ch) GLib.io_channel_close(_this.in_ch);
133 if (_this.out_ch) GLib.io_channel_close(_this.out_ch);
134 if (_this.err_ch) GLib.io_channel_close(_this.err_ch);
135 // blank out channels
137 _this.err_ch = false;
138 _this.out_ch = false;
139 // rmeove listeners !! important otherwise we kill the CPU
140 if (err_src !== false) GLib.source_remove(err_src);
141 if (out_src !== false) GLib.source_remove(out_src);
149 GLib.child_watch_add(GLib.PRIORITY_DEFAULT, this.pid, function(pid, result) {
150 _this.result = result;
152 GLib.spawn_close_pid(_this.pid);
155 GLib.main_loop_quit(ctx);
158 if (_this.listeners.finish) {
159 _this.listeners.finish.call(this, _this.result);
164 this.in_ch = GLib.io_channel_unix_new(ret.standard_input);
165 this.out_ch = GLib.io_channel_unix_new(ret.standard_output);
166 this.err_ch = GLib.io_channel_unix_new(ret.standard_error);
168 // make everything non-blocking!
169 GLib.io_channel_set_flags (this.in_ch,GLib.IOFlags.NONBLOCK);
170 GLib.io_channel_set_flags (this.out_ch,GLib.IOFlags.NONBLOCK);
171 GLib.io_channel_set_flags (this.err_ch,GLib.IOFlags.NONBLOCK);
174 // add handlers for output and stderr.
175 out_src= GLib.io_add_watch(this.out_ch, GLib.PRIORITY_DEFAULT,
176 GLib.IOCondition.OUT + GLib.IOCondition.IN + GLib.IOCondition.PRI, function()
178 _this.read(this.out_ch);
181 err_src= GLib.io_add_watch(this.err_ch, GLib.PRIORITY_DEFAULT,
182 GLib.IOCondition.ERR + GLib.IOCondition.IN + GLib.IOCondition.PRI + GLib.IOCondition.OUT,
185 _this.read(this.err_ch);
192 if (this.pid !== false) {
193 // child can exit before we get this far..
194 if (this.listeners.input) {
196 this.write(this.listeners.input.call(this));
205 // start mainloop if not async..
207 if (this.pid !== false) {
208 ctx = GLib.main_loop_new (null, false);
209 GLib.main_loop_run(ctx, false); // wait fore exit?
211 tidyup(); // tidyup get's called in main loop.
214 if (this.exceptions && this.result != 0) {
215 throw this; // we throw self...
220 // finally throw, or return self..
226 * write to stdin of process
227 * @arg str {String} string to write to stdin of process
228 * @returns GLib.IOStatus (0 == error, 1= NORMAL)
230 write : function(str) // write a line to
233 return; // input is closed
236 var res = GLib.io_channel_write_chars(this.in_ch, str, str.length);
237 if (res != GLib.IOStatus.NORMAL) {
238 throw "Write failed";
240 return ret.bytes_written;
244 * read from pipe and call appropriate listerner and add to output or stderr string.
245 * @arg giochannel to read from.
250 var prop = ch == this.out_ch ? 'output' : 'stderr';
252 var x = new GLib.String();
253 var status = GLib.io_channel_read_line_string (ch, x);
255 case GLib.IOStatus.NORMAL:
257 if (this.listeners[prop]) {
258 this.listeners[prop].call(this, x.str);
260 _this[prop] += x.str;
262 print(prop + ':' + x.str);
265 case GLib.IOStatus.AGAIN:
267 case GLib.IOStatus.ERROR:
268 case GLib.IOStatus.EOF:
280 * simple run a process - returns result, or throws stderr result...
281 * @param cfg {Object} see spawn
284 cfg.exceptions = true;
286 var s = new Spawn(cfg);