Search in sources :

Example 1 with BuckPyFunction

use of com.facebook.buck.rules.BuckPyFunction in project buck by facebook.

the class BuckPythonProgram method newInstance.

/**
   * Create a new instance by layout the files in a temporary directory.
   */
public static BuckPythonProgram newInstance(ConstructorArgMarshaller marshaller, ImmutableSet<Description<?>> descriptions) throws IOException {
    Path pythonPath;
    try {
        URL url = Resources.getResource("buck_parser");
        if ("jar".equals(url.getProtocol())) {
            // Buck is being executed from a JAR file. Extract the jar file from the resource path, and
            // verify it is correct.
            // When python attempts to import `buck_parser`, it will see the jar file, and load it via
            // zipimport, and look into the `buck_parser` directory in the root of the jar.
            JarURLConnection connection = (JarURLConnection) url.openConnection();
            Preconditions.checkState(connection.getEntryName().equals("buck_parser"), "buck_parser directory should be at the root of the jar file.");
            URI jarFileURI = connection.getJarFileURL().toURI();
            pythonPath = Paths.get(jarFileURI);
        } else if ("file".equals(url.getProtocol())) {
            // Buck is being executed from classpath on disk. Set the parent directory as the python
            // path.
            // When python attempts to import `buck_parser`, it will look for a `buck_parser` child
            // directory in the given path.
            pythonPath = Paths.get(url.toURI()).getParent();
        } else {
            throw new IllegalStateException("buck_python resource directory should reside in a local directory or in a jar file. " + "Got: " + url);
        }
    } catch (URISyntaxException e) {
        throw new IllegalStateException("Failed to determine location of buck_parser python package", e);
    }
    Path generatedRoot = Files.createTempDirectory("buck_python_program");
    LOG.debug("Writing python rules stub to %s.", generatedRoot);
    try (Writer out = Files.newBufferedWriter(generatedRoot.resolve("generated_rules.py"), UTF_8)) {
        out.write("from buck_parser.buck import *\n\n");
        BuckPyFunction function = new BuckPyFunction(marshaller);
        for (Description<?> description : descriptions) {
            out.write(function.toPythonFunction(Description.getBuildRuleType(description), description.createUnpopulatedConstructorArg()));
            out.write('\n');
        }
    }
    String pathlibDir = PATH_TO_PATHLIB_PY.getParent().toString();
    String watchmanDir = PATH_TO_PYWATCHMAN.toString();
    String typingDir = PATH_TO_TYPING.toString();
    try (Writer out = Files.newBufferedWriter(generatedRoot.resolve("__main__.py"), UTF_8)) {
        out.write(Joiner.on("\n").join("from __future__ import absolute_import", "import sys", "sys.path.insert(0, \"" + Escaper.escapeAsBashString(MorePaths.pathWithUnixSeparators(pathlibDir)) + "\")", "sys.path.insert(0, \"" + Escaper.escapeAsBashString(MorePaths.pathWithUnixSeparators(watchmanDir)) + "\")", "sys.path.insert(0, \"" + Escaper.escapeAsBashString(MorePaths.pathWithUnixSeparators(typingDir)) + "\")", // Path to the bundled python code.
        "sys.path.insert(0, \"" + Escaper.escapeAsBashString(MorePaths.pathWithUnixSeparators(pythonPath)) + "\")", // Path to the generated rules stub.
        "sys.path.insert(0, \"" + Escaper.escapeAsBashString(MorePaths.pathWithUnixSeparators(generatedRoot)) + "\")", "if __name__ == '__main__':", "    try:", "        from buck_parser import buck", "        buck.main()", "    except KeyboardInterrupt:", "        print >> sys.stderr, 'Killed by User'", ""));
    }
    LOG.debug("Created temporary buck.py instance at %s.", generatedRoot);
    return new BuckPythonProgram(generatedRoot);
}
Also used : Path(java.nio.file.Path) BuckPyFunction(com.facebook.buck.rules.BuckPyFunction) JarURLConnection(java.net.JarURLConnection) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) Writer(java.io.Writer)

Aggregations

BuckPyFunction (com.facebook.buck.rules.BuckPyFunction)1 Writer (java.io.Writer)1 JarURLConnection (java.net.JarURLConnection)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1