Search in sources :

Example 1 with ExampleWorkOptions

use of com.google.devtools.build.lib.worker.ExampleWorkerOptions.ExampleWorkOptions in project bazel by bazelbuild.

the class ExampleWorker method processRequest.

private static void processRequest(List<String> args) throws Exception {
    ImmutableList.Builder<String> expandedArgs = ImmutableList.builder();
    for (String arg : args) {
        Matcher flagFileMatcher = FLAG_FILE_PATTERN.matcher(arg);
        if (flagFileMatcher.matches()) {
            expandedArgs.addAll(Files.readAllLines(Paths.get(flagFileMatcher.group(1)), UTF_8));
        } else {
            expandedArgs.add(arg);
        }
    }
    OptionsParser parser = OptionsParser.newOptionsParser(ExampleWorkOptions.class);
    parser.setAllowResidue(true);
    parser.parse(expandedArgs.build());
    ExampleWorkOptions options = parser.getOptions(ExampleWorkOptions.class);
    List<String> outputs = new ArrayList<>();
    if (options.writeUUID) {
        outputs.add("UUID " + workerUuid.toString());
    }
    if (options.writeCounter) {
        outputs.add("COUNTER " + workUnitCounter++);
    }
    String residueStr = Joiner.on(' ').join(parser.getResidue());
    if (options.uppercase) {
        residueStr = residueStr.toUpperCase();
    }
    outputs.add(residueStr);
    if (options.printInputs) {
        for (Entry<String, String> input : inputs.entrySet()) {
            outputs.add("INPUT " + input.getKey() + " " + input.getValue());
        }
    }
    if (options.printEnv) {
        for (Entry<String, String> entry : System.getenv().entrySet()) {
            outputs.add(entry.getKey() + "=" + entry.getValue());
        }
    }
    String outputStr = Joiner.on('\n').join(outputs);
    if (options.outputFile.isEmpty()) {
        System.out.println(outputStr);
    } else {
        try (PrintStream outputFile = new PrintStream(options.outputFile)) {
            outputFile.println(outputStr);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) OptionsParser(com.google.devtools.common.options.OptionsParser) ExampleWorkOptions(com.google.devtools.build.lib.worker.ExampleWorkerOptions.ExampleWorkOptions)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ExampleWorkOptions (com.google.devtools.build.lib.worker.ExampleWorkerOptions.ExampleWorkOptions)1 OptionsParser (com.google.devtools.common.options.OptionsParser)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1