Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of pipeline; | 5 part of pipeline; |
| 6 | 6 |
| 7 List stdout, stderr, log; | 7 List stdout, stderr, log; |
| 8 var replyPort; | 8 var replyPort; |
| 9 int _procId = 1; | 9 int _procId = 1; |
| 10 Map _procs = {}; | 10 Map _procs = {}; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 var ostream = file.openOutputStream(FileMode.WRITE); | 44 var ostream = file.openOutputStream(FileMode.WRITE); |
| 45 ostream.writeString(contents); | 45 ostream.writeString(contents); |
| 46 ostream.close(); | 46 ostream.close(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 /* | 49 /* |
| 50 * Run an external process [cmd] with command line arguments [args]. | 50 * Run an external process [cmd] with command line arguments [args]. |
| 51 * [timeout] can be used to forcefully terminate the process after | 51 * [timeout] can be used to forcefully terminate the process after |
| 52 * some number of seconds. This is used by runCommand and startProcess. | 52 * some number of seconds. This is used by runCommand and startProcess. |
| 53 * If [procId] is 0 (runCommand) then this will return a [Future] for | 53 * If [procId] is 0 (runCommand) then this will return a [Future] for |
| 54 * when the process terminates; if [procId] is instead non-zero | 54 * when the process terminates; if [procId] is instead non-zero |
|
Siggi Cherem (dart-lang)
2012/10/10 22:07:28
nit: fix comment now that the future is always ret
gram
2012/10/10 23:36:04
Done.
| |
| 55 * (startProcess) then a reference to the [Process] will be put in a | 55 * (startProcess) then a reference to the [Process] will be put in a |
| 56 * map with key [procId]; in this case the process can be terminated | 56 * map with key [procId]; in this case the process can be terminated |
| 57 * later by calling [stopProcess] and passing in the [procId]. | 57 * later by calling [stopProcess] and passing in the [procId]. |
| 58 */ | 58 */ |
| 59 Future _processHelper(String command, List<String> args, | 59 Future _processHelper(String command, List<String> args, |
| 60 [int timeout = 300, int procId = 0]) { | 60 [int timeout = 300, int procId = 0]) { |
| 61 var completer = null; | 61 var completer = new Completer(); |
| 62 log.add('Running $command ${Strings.join(args, " ")}'); | 62 log.add('Running $command ${Strings.join(args, " ")}'); |
| 63 var timer = null; | 63 var timer = null; |
| 64 var stdoutHandler, stderrHandler; | 64 var stdoutHandler, stderrHandler; |
| 65 var process = Process.start(command, args); | 65 var process = Process.start(command, args); |
| 66 if (procId == 0) { | 66 if (procId != 0) { |
| 67 completer = new Completer(); | |
| 68 } else { | |
| 69 _procs[procId] = process; | 67 _procs[procId] = process; |
| 70 } | 68 } |
| 71 process.onStart = () { | 69 process.onStart = () { |
| 72 timer = new Timer(1000 * timeout, (t) { | 70 timer = new Timer(1000 * timeout, (t) { |
| 73 timer = null; | 71 timer = null; |
| 74 process.kill(); | 72 process.kill(); |
| 75 }); | 73 }); |
| 76 }; | 74 }; |
| 77 process.onExit = (exitCode) { | 75 process.onExit = (exitCode) { |
| 78 if (timer != null) { | 76 if (timer != null) { |
| 79 timer.cancel(); | 77 timer.cancel(); |
| 80 } | 78 } |
| 81 process.close(); | 79 process.close(); |
| 82 if (completer != null) { | 80 if (completer != null) { |
| 83 completer.complete(exitCode); | 81 completer.complete(exitCode); |
| 84 } | 82 } |
| 85 }; | 83 }; |
| 86 process.onError = (e) { | 84 process.onError = (e) { |
| 87 stderr.add("Error starting process:"); | 85 stderr.add("Error starting process:"); |
| 88 stderr.add(" Command: $command"); | 86 stderr.add(" Command: $command"); |
| 89 stderr.add(" Error: $e"); | 87 stderr.add(" Error: $e"); |
| 90 completePipeline(-1); | 88 completePipeline(-1); |
| 91 }; | 89 }; |
| 92 | 90 |
| 93 _pipeStream(process.stdout, stdout); | 91 _pipeStream(process.stdout, stdout); |
| 94 _pipeStream(process.stderr, stderr); | 92 _pipeStream(process.stderr, stderr); |
| 95 | 93 |
| 96 return (completer == null) ? null : completer.future; | 94 return completer.future; |
| 97 } | 95 } |
| 98 | 96 |
| 99 void _pipeStream(InputStream stream, List<String> destination) { | 97 void _pipeStream(InputStream stream, List<String> destination) { |
| 100 var source = new StringInputStream(stream); | 98 var source = new StringInputStream(stream); |
| 101 source.onLine = () { | 99 source.onLine = () { |
| 102 if (source.available() == 0) return; | 100 if (source.available() == 0) return; |
| 103 var line = source.readLine(); | 101 var line = source.readLine(); |
| 104 while (null != line) { | 102 while (null != line) { |
| 105 if (config["immediate"] && line.startsWith('###')) { | 103 if (config["immediate"] && line.startsWith('###')) { |
| 106 // TODO - when we dump the list later skip '###' messages if immediate. | 104 // TODO - when we dump the list later skip '###' messages if immediate. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 121 Future runCommand(String command, List<String> args, [int timeout = 300]) { | 119 Future runCommand(String command, List<String> args, [int timeout = 300]) { |
| 122 return _processHelper(command, args, timeout); | 120 return _processHelper(command, args, timeout); |
| 123 } | 121 } |
| 124 | 122 |
| 125 /** | 123 /** |
| 126 * Start an external process [cmd] with command line arguments [args]. | 124 * Start an external process [cmd] with command line arguments [args]. |
| 127 * Returns an ID by which it can later be stopped. | 125 * Returns an ID by which it can later be stopped. |
| 128 */ | 126 */ |
| 129 int startProcess(String command, List<String> args) { | 127 int startProcess(String command, List<String> args) { |
| 130 int id = _procId++; | 128 int id = _procId++; |
| 131 _processHelper(command, args, 3000, id); | 129 _processHelper(command, args, 3000, id).then((e) { |
| 130 _procs.remove(id); | |
| 131 }); | |
| 132 return id; | 132 return id; |
| 133 } | 133 } |
| 134 | 134 |
| 135 /** Checks if a process is still running. */ | |
| 136 bool isProcessRunning(int id) { | |
| 137 return _procs.containsKey(id); | |
| 138 } | |
| 139 | |
| 135 /** | 140 /** |
| 136 * Stop a process previously started with [startProcess] or [runCommand], | 141 * Stop a process previously started with [startProcess] or [runCommand], |
| 137 * given the id string. | 142 * given the id string. |
| 138 */ | 143 */ |
| 139 void stopProcess(int id) { | 144 void stopProcess(int id) { |
| 140 Process p = _procs.remove(id); | 145 if (_procs.containsKey(id)) { |
| 141 p.kill(); | 146 Process p = _procs.remove(id); |
| 147 p.kill(); | |
| 148 } | |
| 142 } | 149 } |
| 143 | 150 |
| 144 /** Delete a file named [fname] if it exists. */ | 151 /** Delete a file named [fname] if it exists. */ |
| 145 bool cleanup(String fname) { | 152 bool cleanup(String fname) { |
| 146 if (fname != null && !config['keep-files']) { | 153 if (fname != null && !config['keep-files']) { |
| 147 var f = new File(fname); | 154 var f = new File(fname); |
| 148 try { | 155 try { |
| 149 if (f.existsSync()) { | 156 if (f.existsSync()) { |
| 150 logMessage('Removing $fname'); | 157 logMessage('Removing $fname'); |
| 151 f.deleteSync(); | 158 f.deleteSync(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 163 stderr = new List(); | 170 stderr = new List(); |
| 164 log = new List(); | 171 log = new List(); |
| 165 } | 172 } |
| 166 | 173 |
| 167 void completePipeline([exitCode = 0]) { | 174 void completePipeline([exitCode = 0]) { |
| 168 replyPort.send([stdout, stderr, log, exitCode]); | 175 replyPort.send([stdout, stderr, log, exitCode]); |
| 169 } | 176 } |
| 170 | 177 |
| 171 /** Utility function to log diagnostic messages. */ | 178 /** Utility function to log diagnostic messages. */ |
| 172 void logMessage(msg) => log.add(msg); | 179 void logMessage(msg) => log.add(msg); |
| OLD | NEW |