Search in sources :

Example 1 with OutputFormatterCallback

use of com.google.devtools.build.lib.query2.engine.OutputFormatterCallback in project bazel by bazelbuild.

the class XmlOutputFormatter method createPostFactoStreamCallback.

@Override
public OutputFormatterCallback<Target> createPostFactoStreamCallback(final OutputStream out, final QueryOptions options) {
    return new OutputFormatterCallback<Target>() {

        private Document doc;

        private Element queryElem;

        @Override
        public void start() {
            try {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                doc = factory.newDocumentBuilder().newDocument();
            } catch (ParserConfigurationException e) {
                // This shouldn't be possible: all the configuration is hard-coded.
                throw new IllegalStateException("XML output failed", e);
            }
            doc.setXmlVersion("1.1");
            queryElem = doc.createElement("query");
            queryElem.setAttribute("version", "2");
            doc.appendChild(queryElem);
        }

        @Override
        public void processOutput(Iterable<Target> partialResult) throws IOException, InterruptedException {
            for (Target target : partialResult) {
                queryElem.appendChild(createTargetElement(doc, target));
            }
        }

        @Override
        public void close(boolean failFast) throws IOException {
            if (!failFast) {
                try {
                    Transformer transformer = TransformerFactory.newInstance().newTransformer();
                    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                    transformer.transform(new DOMSource(doc), new StreamResult(out));
                } catch (TransformerFactoryConfigurationError | TransformerException e) {
                    // This shouldn't be possible: all the configuration is hard-coded.
                    throw new IllegalStateException("XML output failed", e);
                }
            }
        }
    };
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) SynchronizedDelegatingOutputFormatterCallback(com.google.devtools.build.lib.query2.engine.SynchronizedDelegatingOutputFormatterCallback) ThreadSafeOutputFormatterCallback(com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback) OutputFormatterCallback(com.google.devtools.build.lib.query2.engine.OutputFormatterCallback) Document(org.w3c.dom.Document) FakeSubincludeTarget(com.google.devtools.build.lib.query2.FakeSubincludeTarget) Target(com.google.devtools.build.lib.packages.Target) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 2 with OutputFormatterCallback

use of com.google.devtools.build.lib.query2.engine.OutputFormatterCallback in project bazel by bazelbuild.

the class FetchCommand method exec.

@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
    BlazeRuntime runtime = env.getRuntime();
    if (options.getResidue().isEmpty()) {
        env.getReporter().handle(Event.error(String.format("missing fetch expression. Type '%s help fetch' for syntax and help", env.getRuntime().getProductName())));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    try {
        env.setupPackageCache(options, runtime.getDefaultsPackageContent());
    } catch (InterruptedException e) {
        env.getReporter().handle(Event.error("fetch interrupted"));
        return ExitCode.INTERRUPTED;
    } catch (AbruptExitException e) {
        env.getReporter().handle(Event.error(null, "Unknown error: " + e.getMessage()));
        return e.getExitCode();
    }
    PackageCacheOptions pkgOptions = options.getOptions(PackageCacheOptions.class);
    if (!pkgOptions.fetch) {
        env.getReporter().handle(Event.error(null, "You cannot run fetch with --fetch=false"));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    // Querying for all of the dependencies of the targets has the side-effect of populating the
    // Skyframe graph for external targets, which requires downloading them. The JDK is required to
    // build everything but isn't counted as a dep in the build graph so we add it manually.
    ImmutableList.Builder<String> labelsToLoad = new ImmutableList.Builder<String>().addAll(options.getResidue());
    String query = Joiner.on(" union ").join(labelsToLoad.build());
    query = "deps(" + query + ")";
    AbstractBlazeQueryEnvironment<Target> queryEnv = QueryCommand.newQueryEnvironment(env, options.getOptions(FetchOptions.class).keepGoing, false, Lists.<String>newArrayList(), 200, Sets.<Setting>newHashSet());
    // 1. Parse query:
    QueryExpression expr;
    try {
        expr = QueryExpression.parse(query, queryEnv);
    } catch (QueryException e) {
        env.getReporter().handle(Event.error(null, "Error while parsing '" + query + "': " + e.getMessage()));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    // 2. Evaluate expression:
    try {
        queryEnv.evaluateQuery(expr, new ThreadSafeOutputFormatterCallback<Target>() {

            @Override
            public void processOutput(Iterable<Target> partialResult) {
            // Throw away the result.
            }
        });
    } catch (InterruptedException e) {
        return ExitCode.COMMAND_LINE_ERROR;
    } catch (QueryException e) {
        // Keep consistent with reportBuildFileError()
        env.getReporter().handle(Event.error(e.getMessage()));
        return ExitCode.COMMAND_LINE_ERROR;
    } catch (IOException e) {
        // Should be impossible since our OutputFormatterCallback doesn't throw IOException.
        throw new IllegalStateException(e);
    }
    env.getReporter().handle(Event.progress("All external dependencies fetched successfully."));
    return ExitCode.SUCCESS;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) PackageCacheOptions(com.google.devtools.build.lib.pkgcache.PackageCacheOptions) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime) Target(com.google.devtools.build.lib.packages.Target) QueryException(com.google.devtools.build.lib.query2.engine.QueryException) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException) QueryExpression(com.google.devtools.build.lib.query2.engine.QueryExpression)

Aggregations

Target (com.google.devtools.build.lib.packages.Target)2 ImmutableList (com.google.common.collect.ImmutableList)1 PackageCacheOptions (com.google.devtools.build.lib.pkgcache.PackageCacheOptions)1 FakeSubincludeTarget (com.google.devtools.build.lib.query2.FakeSubincludeTarget)1 OutputFormatterCallback (com.google.devtools.build.lib.query2.engine.OutputFormatterCallback)1 QueryException (com.google.devtools.build.lib.query2.engine.QueryException)1 QueryExpression (com.google.devtools.build.lib.query2.engine.QueryExpression)1 SynchronizedDelegatingOutputFormatterCallback (com.google.devtools.build.lib.query2.engine.SynchronizedDelegatingOutputFormatterCallback)1 ThreadSafeOutputFormatterCallback (com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback)1 BlazeRuntime (com.google.devtools.build.lib.runtime.BlazeRuntime)1 AbruptExitException (com.google.devtools.build.lib.util.AbruptExitException)1 IOException (java.io.IOException)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 TransformerFactoryConfigurationError (javax.xml.transform.TransformerFactoryConfigurationError)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Document (org.w3c.dom.Document)1