Search in sources :

Example 11 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class SkylarkRepositoryContext method downloadAndExtract.

@SkylarkCallable(name = "download_and_extract", doc = "Download a file to the output path for the provided url, and extract it.", parameters = { @Param(name = "url", allowedTypes = { @ParamType(type = String.class), @ParamType(type = SkylarkList.class, generic1 = String.class) }, named = true, doc = "List of mirror URLs referencing the same file."), @Param(name = "output", allowedTypes = { @ParamType(type = String.class), @ParamType(type = Label.class), @ParamType(type = SkylarkPath.class) }, defaultValue = "''", named = true, doc = "path to the directory where the archive will be unpacked," + " relative to the repository directory."), @Param(name = "sha256", type = String.class, defaultValue = "''", named = true, doc = "the expected SHA-256 hash of the file downloaded." + " This must match the SHA-256 hash of the file downloaded. It is a security risk" + " to omit the SHA-256 as remote files can change. At best omitting this field" + " will make your build non-hermetic. It is optional to make development easier" + " but should be set before shipping."), @Param(name = "type", type = String.class, defaultValue = "''", named = true, doc = "the archive type of the downloaded file." + " By default, the archive type is determined from the file extension of the URL." + " If the file has no extension, you can explicitly specify either \"zip\"," + " \"jar\", \"war\", \"tar.gz\", \"tgz\", \"tar.bz2\", or \"tar.xz\" here."), @Param(name = "stripPrefix", type = String.class, defaultValue = "''", named = true, doc = "a directory prefix to strip from the extracted files." + "\nMany archives contain a top-level directory that contains all files in the" + " archive. Instead of needing to specify this prefix over and over in the" + " <code>build_file</code>, this field can be used to strip it from extracted" + " files.") })
public void downloadAndExtract(Object url, Object output, String sha256, String type, String stripPrefix) throws RepositoryFunctionException, InterruptedException, EvalException {
    validateSha256(sha256);
    List<URL> urls = getUrls(url);
    // Download to outputDirectory and delete it after extraction
    SkylarkPath outputPath = getPath("download_and_extract()", output);
    checkInOutputDirectory(outputPath);
    createDirectory(outputPath.getPath());
    Path downloadedPath;
    try {
        downloadedPath = httpDownloader.download(urls, sha256, Optional.of(type), outputPath.getPath(), env.getListener(), osObject.getEnvironmentVariables());
    } catch (InterruptedException e) {
        throw new RepositoryFunctionException(new IOException("thread interrupted"), Transience.TRANSIENT);
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
    DecompressorValue.decompress(DecompressorDescriptor.builder().setTargetKind(rule.getTargetKind()).setTargetName(rule.getName()).setArchivePath(downloadedPath).setRepositoryPath(outputPath.getPath()).setPrefix(stripPrefix).build());
    try {
        if (downloadedPath.exists()) {
            downloadedPath.delete();
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(new IOException("Couldn't delete temporary file (" + downloadedPath.getPathString() + ")", e), Transience.TRANSIENT);
    }
}
Also used : RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) IOException(java.io.IOException) URL(java.net.URL) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 12 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class SkylarkRepositoryContext method createFileFromTemplate.

@SkylarkCallable(name = "template", doc = "Generate a new file using a <code>template</code>. Every occurrence in " + "<code>template</code> of a key of <code>substitutions</code> will be replaced by " + "the corresponding value. The result is written in <code>path</code>. An optional" + "<code>executable</code> argument (default to true) can be set to turn on or off" + "the executable bit.", parameters = { @Param(name = "path", allowedTypes = { @ParamType(type = String.class), @ParamType(type = Label.class), @ParamType(type = SkylarkPath.class) }, doc = "path of the file to create, relative to the repository directory."), @Param(name = "template", allowedTypes = { @ParamType(type = String.class), @ParamType(type = Label.class), @ParamType(type = SkylarkPath.class) }, doc = "path to the template file."), @Param(name = "substitutions", type = SkylarkDict.class, defaultValue = "{}", named = true, doc = "substitutions to make when expanding the template."), @Param(name = "executable", type = Boolean.class, defaultValue = "True", named = true, doc = "set the executable flag on the created file, true by default.") })
public void createFileFromTemplate(Object path, Object template, SkylarkDict<String, String> substitutions, Boolean executable) throws RepositoryFunctionException, EvalException, InterruptedException {
    SkylarkPath p = getPath("template()", path);
    SkylarkPath t = getPath("template()", template);
    try {
        checkInOutputDirectory(p);
        makeDirectories(p.getPath());
        String tpl = FileSystemUtils.readContent(t.getPath(), StandardCharsets.UTF_8);
        for (Map.Entry<String, String> substitution : substitutions.entrySet()) {
            tpl = StringUtilities.replaceAllLiteral(tpl, substitution.getKey(), substitution.getValue());
        }
        try (OutputStream stream = p.getPath().getOutputStream()) {
            stream.write(tpl.getBytes(StandardCharsets.UTF_8));
        }
        if (executable) {
            p.getPath().setExecutable(true);
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
}
Also used : OutputStream(java.io.OutputStream) IOException(java.io.IOException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 13 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class SkylarkRepositoryContext method download.

@SkylarkCallable(name = "download", doc = "Download a file to the output path for the provided url.", parameters = { @Param(name = "url", allowedTypes = { @ParamType(type = String.class), @ParamType(type = SkylarkList.class, generic1 = String.class) }, named = true, doc = "List of mirror URLs referencing the same file."), @Param(name = "output", allowedTypes = { @ParamType(type = String.class), @ParamType(type = Label.class), @ParamType(type = SkylarkPath.class) }, defaultValue = "''", named = true, doc = "path to the output file, relative to the repository directory."), @Param(name = "sha256", type = String.class, defaultValue = "''", named = true, doc = "the expected SHA-256 hash of the file downloaded." + " This must match the SHA-256 hash of the file downloaded. It is a security risk" + " to omit the SHA-256 as remote files can change. At best omitting this field" + " will make your build non-hermetic. It is optional to make development easier" + " but should be set before shipping."), @Param(name = "executable", type = Boolean.class, defaultValue = "False", named = true, doc = "set the executable flag on the created file, false by default.") })
public void download(Object url, Object output, String sha256, Boolean executable) throws RepositoryFunctionException, EvalException, InterruptedException {
    validateSha256(sha256);
    List<URL> urls = getUrls(url);
    SkylarkPath outputPath = getPath("download()", output);
    try {
        checkInOutputDirectory(outputPath);
        makeDirectories(outputPath.getPath());
        httpDownloader.download(urls, sha256, Optional.<String>absent(), outputPath.getPath(), env.getListener(), osObject.getEnvironmentVariables());
        if (executable) {
            outputPath.getPath().setExecutable(true);
        }
    } catch (InterruptedException e) {
        throw new RepositoryFunctionException(new IOException("thread interrupted"), Transience.TRANSIENT);
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
}
Also used : IOException(java.io.IOException) URL(java.net.URL) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 14 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class SkylarkRepositoryContext method symlink.

@SkylarkCallable(name = "symlink", doc = "Create a symlink on the filesystem.", parameters = { @Param(name = "from", allowedTypes = { @ParamType(type = String.class), @ParamType(type = Label.class), @ParamType(type = SkylarkPath.class) }, doc = "path to which the created symlink should point to."), @Param(name = "to", allowedTypes = { @ParamType(type = String.class), @ParamType(type = Label.class), @ParamType(type = SkylarkPath.class) }, doc = "path of the symlink to create, relative to the repository directory.") })
public void symlink(Object from, Object to) throws RepositoryFunctionException, EvalException, InterruptedException {
    SkylarkPath fromPath = getPath("symlink()", from);
    SkylarkPath toPath = getPath("symlink()", to);
    try {
        checkInOutputDirectory(toPath);
        makeDirectories(toPath.getPath());
        toPath.getPath().createSymbolicLink(fromPath.getPath());
    } catch (IOException e) {
        throw new RepositoryFunctionException(new IOException("Could not create symlink from " + fromPath + " to " + toPath + ": " + e.getMessage(), e), Transience.TRANSIENT);
    }
}
Also used : IOException(java.io.IOException) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 15 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class SkylarkRepositoryContext method createFile.

@SkylarkCallable(name = "file", doc = "Generate a file in the repository directory with the provided content.", parameters = { @Param(name = "path", allowedTypes = { @ParamType(type = String.class), @ParamType(type = Label.class), @ParamType(type = SkylarkPath.class) }, doc = "path of the file to create, relative to the repository directory."), @Param(name = "content", type = String.class, named = true, defaultValue = "''", doc = "the content of the file to create, empty by default."), @Param(name = "executable", named = true, type = Boolean.class, defaultValue = "True", doc = "set the executable flag on the created file, true by default.") })
public void createFile(Object path, String content, Boolean executable) throws RepositoryFunctionException, EvalException, InterruptedException {
    SkylarkPath p = getPath("file()", path);
    try {
        checkInOutputDirectory(p);
        makeDirectories(p.getPath());
        try (OutputStream stream = p.getPath().getOutputStream()) {
            stream.write(content.getBytes(StandardCharsets.UTF_8));
        }
        if (executable) {
            p.getPath().setExecutable(true);
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
}
Also used : OutputStream(java.io.OutputStream) IOException(java.io.IOException) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Aggregations

SkylarkCallable (com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)27 Method (java.lang.reflect.Method)8 Test (org.junit.Test)7 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)6 ImmutableList (com.google.common.collect.ImmutableList)5 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)5 IOException (java.io.IOException)5 ImmutableMap (com.google.common.collect.ImmutableMap)2 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)2 Param (com.google.devtools.build.lib.skylarkinterface.Param)2 OutputStream (java.io.OutputStream)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 SkylarkJavaMethodDoc (com.google.devtools.build.docgen.skylark.SkylarkJavaMethodDoc)1 SkylarkModuleDoc (com.google.devtools.build.docgen.skylark.SkylarkModuleDoc)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 Root (com.google.devtools.build.lib.actions.Root)1 MiddlemanProvider (com.google.devtools.build.lib.analysis.MiddlemanProvider)1