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 * @arg env {Array} enviroment eg. [ 'GITDIR=/home/test' ]
42 * @arg async {Boolean} (optional)return instantly, or wait for exit. (default no)
43 * @arg exceptions {Boolean} throw exception on failure (default no)
52 this.listeners = this.listeners || {};
53 this.cwd = this.cwd || GLib.get_home_dir();
54 if (!this.args || !this.args.length) {
68 * @property output {String} resulting output
72 * @property stderr {String} resulting output from stderr
76 * @property result {Number} execution result.
80 * @property pid {Number} pid of child process (of false if it's not running)
85 * @property done {Boolean} has the process completed.
92 * Run the configured command.
100 GLib.spawn_async_with_pipes(this.cwd, this.args, null,
101 GLib.SpawnFlags.DO_NOT_REAP_CHILD + GLib.SpawnFlags.SEARCH_PATH ,
104 this.pid = ret.child_pid;
110 GLib.child_watch_add(GLib.PRIORITY_DEFAULT, this.pid, function(pid, result) {
111 _this.result = result;
113 GLib.spawn_close_pid(_this.pid);
116 GLib.main_loop_quit(ctx);
120 this.in_ch = GLib.io_channel_unix_new(ret.standard_input);
121 var out_ch = GLib.io_channel_unix_new(ret.standard_output);
122 var err_ch = GLib.io_channel_unix_new(ret.standard_error);
124 // make everything non-blocking!
125 GLib.io_channel_set_flags (this.in_ch,GLib.IOFlags.NONBLOCK);
126 GLib.io_channel_set_flags (out_ch,GLib.IOFlags.NONBLOCK);
127 GLib.io_channel_set_flags (err_ch,GLib.IOFlags.NONBLOCK);
129 function readstr(ch, prop) {
131 var x = new GLib.String();
132 var status = GLib.io_channel_read_line_string (ch, x);
134 case GLib.IOStatus.NORMAL:
136 if (this.listeners[prop]) {
137 this.listeners[prop].call(this, x.str);
139 _this[prop] += x.str;
141 case GLib.IOStatus.AGAIN:
143 case GLib.IOStatus.ERROR:
144 case GLib.IOStatus.EOF:
152 var out_src= GLib.io_add_watch(out_ch, GLib.PRIORITY_DEFAULT,
153 GLib.IOCondition.OUT + GLib.IOCondition.IN + GLib.IOCondition.PRI, function()
155 readstr(out_ch, 'output');
158 var err_src= GLib.io_add_watch(err_ch, GLib.PRIORITY_DEFAULT,
159 GLib.IOCondition.ERR + GLib.IOCondition.IN + GLib.IOCondition.PRI + GLib.IOCondition.OUT,
162 readstr(err_ch, 'stderr');
165 if (this.pid !== false) {
166 // child can exit before we get this far..
167 if (this.listeners.input) {
168 this.write(this.listeners.input.call(this));
171 if (this.pid !== false && !this.async) {
173 ctx = GLib.main_loop_new (null, false);
174 GLib.main_loop_run(ctx, false); // wait fore exit?
176 // read any resulting data.
177 readstr(out_ch, 'output');
178 readstr(err_ch, 'error');
183 GLib.io_channel_close(this.in_ch);
185 GLib.io_channel_close(out_ch);
186 GLib.io_channel_close(err_ch);
187 GLib.source_remove(err_src);
188 GLib.source_remove(out_src);
189 if (this.exceptions && this.result != 0) {
196 * write to stdin of process
197 * @returns GLib.IOStatus (0 == error, 1= NORMAL)
201 write : function(str) // write a line to
204 return; // input is closed
207 var res = GLib.io_channel_write_chars(this.in_ch, str, str.length);
208 if (res != GLib.IOStatus.NORMAL) {
209 throw "Write failed";
211 return ret.bytes_written;
219 * simple run a process - returns result, or throws error..
220 * @param cfg {Object} see spawn
223 cfg.exceptions = true;
225 var s = new Spawn(cfg);