Chromium Code Reviews| Index: utils/testrunner/pipeline_utils.dart |
| =================================================================== |
| --- utils/testrunner/pipeline_utils.dart (revision 0) |
| +++ utils/testrunner/pipeline_utils.dart (revision 0) |
| @@ -0,0 +1,144 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +List stdout, stderr, log; |
| +var replyPort; |
| +int _procnum = 0; |
| +Map _procs = {}; |
| + |
| +/** |
| + * Create a file path for a temporary file. The file will be in the |
| + * [tmpDir] directory, with name [basis], but with any extension |
| + * stripped and replaced by [suffix]. |
| + */ |
| +String createTempName(String tmpDir, String basis, String suffix) { |
| + var p = new Path(basis); |
|
Siggi Cherem (dart-lang)
2012/09/28 01:53:51
fix indentation, hopefully that will make the next
gram
2012/10/01 21:10:51
Weird, I don't see this indentation issue, althoug
|
| + return '$tmpDir${Platform.pathSeparator}${p.filenameWithoutExtension}${suffix}'; |
| +} |
| + |
| +/** Create a file [fileName] and populate it with [contents]. */ |
| +void writeFile(String fileName, String contents) { |
| + var file = new File(fileName); |
| + var ostream = file.openOutputStream(FileMode.WRITE); |
| + ostream.writeString(contents); |
| + ostream.close(); |
| +} |
| + |
| +/** |
| + * Run an external process [cmd] with command line arguments [args]. |
| + * [timeout] can be used to forcefully terminate the process after |
| + * some number of seconds. If [procId] is null then this will |
| + * return a [Future] for when the process terminates; if [procId] |
| + * is instead a non-null string then a reference to the [Process] |
| + * will be put in a map with key [procId] and [procId] will be |
|
Siggi Cherem (dart-lang)
2012/09/28 01:53:51
why not always return the Future?
If I passed th
gram
2012/10/01 21:10:51
I see your point, but it is a usage issue. There a
Siggi Cherem (dart-lang)
2012/10/02 17:45:20
I really find it confusing when functions return s
gram
2012/10/02 23:01:59
That's somewhat like going back to the way I had i
|
| + * returned; in this case the process can be terminated later by |
| + * calling [stopProcess] and passing in the [procId]. |
| + */ |
| +runCommand(String command, List<String> args, |
| + [int timeout = 300, String procId = null]) { |
| + var completer = null; |
| + if (procId == null) { |
| + completer = new Completer(); |
|
Siggi Cherem (dart-lang)
2012/09/28 01:53:51
nit: un-indent
gram
2012/10/01 21:10:51
Again, I'm not seeing this indent issue. Very stra
|
| + } |
| + log.add('Running $command ${Strings.join(args, " ")}'); |
| + var timer = null; |
| + var process = Process.start(command, args); |
| + |
| + process.onStart = () { |
| + timer = new Timer(1000 * timeout, (t) { |
| + timer = null; |
| + process.kill(); |
| + }); |
| + }; |
| + process.onExit = (exitCode) { |
| + if (timer != null) { |
| + timer.cancel(); |
| + } |
| + process.close(); |
| + if (completer != null) { |
| + completer.complete(exitCode); |
| + } |
| + }; |
| + process.onError = (e) { |
| + stderr.add("Error starting process:"); |
| + stderr.add(" Command: $command"); |
| + stderr.add(" Error: $e"); |
| + completePipeline(-1); |
| + }; |
| + |
| + StringInputStream stdoutStringStream = |
| + new StringInputStream(process.stdout); |
| + StringInputStream stderrStringStream = |
| + new StringInputStream(process.stderr); |
| + stdoutStringStream.onLine = _makeReadHandler(stdoutStringStream, stdout); |
| + stderrStringStream.onLine = _makeReadHandler(stderrStringStream, stderr); |
| + if (procId == null) { |
| + return completer.future; |
| + } else { |
| + _procs[procId] = process; |
| + return procId; |
| + } |
| +} |
| + |
| +Function _makeReadHandler(StringInputStream source, List<String> destination) { |
| + return () { |
| + if (source.closed) return; |
| + var line = source.readLine(); |
| + while (null != line) { |
| + if (config["immediate"] && line.startsWith('###')) { |
| + // TODO - when we dump the list later skip '###' messages if immediate. |
| + print(line.substring(3)); |
| + } |
| + destination.add(line); |
| + line = source.readLine(); |
| + } |
| + }; |
| +} |
| + |
| +/** |
| + * Start an external process [cmd] with command line arguments [args]. |
| + * Returns an ID by which it can later be stopped. |
| + */ |
| +String startProcess(String command, List<String> args) { |
| + return runCommand(command, args, 3000, '${_procnum++}'); |
|
Siggi Cherem (dart-lang)
2012/09/28 01:53:51
ditto
gram
2012/10/01 21:10:51
ditto
|
| +} |
| + |
| +/** |
| + * Stop a process previously started with [startProcess] or [runCommand], |
| + * given the id string. |
| + */ |
| +void stopProcess(String id) { |
| + Process p = _procs.remove(id); |
| + p.kill(); |
| +} |
| + |
| +/** Delete a file named [fname] if it exists. */ |
| +bool cleanup(String fname) { |
| + if (!config['keep-files']) { |
| + var f = new File(fname); |
| + try { |
| + if (f.existsSync()) { |
|
Siggi Cherem (dart-lang)
2012/09/28 01:53:51
ditto
gram
2012/10/01 21:10:51
ditto!
|
| + f.deleteSync(); |
| + } |
| + } catch (e) { |
| + return false; |
| + } |
| + } |
| + return true; |
| +} |
| + |
| +initPipeline(port) { |
| + replyPort = port; |
| + stdout = new List(); |
| + stderr = new List(); |
| + log = new List(); |
| +} |
| + |
| +void completePipeline([exitCode = 0]) { |
| + replyPort.send([stdout, stderr, log, exitCode]); |
| +} |
| + |
| +/** Utility function to log diagnostic messages. */ |
| +void logMessage(msg) => log.add(msg); |
| + |