Search in sources :

Example 1 with TokenizationException

use of com.google.devtools.build.lib.shell.ShellUtils.TokenizationException in project bazel by bazelbuild.

the class RunUnderConverter method convert.

@Override
public RunUnder convert(final String input) throws OptionsParsingException {
    final List<String> runUnderList = new ArrayList<>();
    try {
        ShellUtils.tokenize(runUnderList, input);
    } catch (TokenizationException e) {
        throw new OptionsParsingException("Not a valid command prefix " + e.getMessage());
    }
    if (runUnderList.isEmpty()) {
        throw new OptionsParsingException("Empty command");
    }
    final String runUnderCommand = runUnderList.get(0);
    if (runUnderCommand.startsWith("//")) {
        try {
            final Label runUnderLabel = Label.parseAbsolute(runUnderCommand);
            return new RunUnderLabel(input, runUnderLabel, runUnderList);
        } catch (LabelSyntaxException e) {
            throw new OptionsParsingException("Not a valid label " + e.getMessage());
        }
    } else {
        return new RunUnderCommand(input, runUnderCommand, runUnderList);
    }
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) TokenizationException(com.google.devtools.build.lib.shell.ShellUtils.TokenizationException) ArrayList(java.util.ArrayList) Label(com.google.devtools.build.lib.cmdline.Label) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException)

Example 2 with TokenizationException

use of com.google.devtools.build.lib.shell.ShellUtils.TokenizationException in project bazel by bazelbuild.

the class OptionFileExpander method expandArgument.

/**
   * Expands a single argument, expanding options &at;filename to read in
   * the content of the file and add it to the list of processed arguments.
   *
   * @param arg the argument to pre-process.
   * @param expanded the List of pre-processed arguments.
   * @throws IOException if one of the files containing options cannot be read.
   */
private void expandArgument(String arg, List<String> expanded) throws IOException {
    if (arg.startsWith("@")) {
        InputStream in = fileSystem.getInputStream(arg.substring(1));
        try {
            // ShellUtils doesn't support them either.
            for (String line : readAllLines(new InputStreamReader(in, ISO_8859_1))) {
                List<String> parsedTokens = new ArrayList<>();
                try {
                    ShellUtils.tokenize(parsedTokens, line);
                } catch (TokenizationException e) {
                    throw new IOException("Could not tokenize parameter file!", e);
                }
                for (String token : parsedTokens) {
                    expandArgument(token, expanded);
                }
            }
            InputStream inToClose = in;
            in = null;
            inToClose.close();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                // Ignore the exception. It can only occur if an exception already
                // happened and in that case, we want to preserve the original one.
                }
            }
        }
    } else {
        expanded.add(arg);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) TokenizationException(com.google.devtools.build.lib.shell.ShellUtils.TokenizationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

TokenizationException (com.google.devtools.build.lib.shell.ShellUtils.TokenizationException)2 ArrayList (java.util.ArrayList)2 Label (com.google.devtools.build.lib.cmdline.Label)1 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1