Search in sources :

Example 16 with RepositoryFunctionException

use of com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException in project bazel by bazelbuild.

the class SkylarkRepositoryContext method createDirectory.

private void createDirectory(Path directory) throws RepositoryFunctionException {
    try {
        if (!directory.exists()) {
            makeDirectories(directory);
            directory.createDirectory();
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
}
Also used : IOException(java.io.IOException) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)

Example 17 with RepositoryFunctionException

use of com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException 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 18 with RepositoryFunctionException

use of com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException 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 19 with RepositoryFunctionException

use of com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException 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)

Example 20 with RepositoryFunctionException

use of com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException in project bazel by bazelbuild.

the class NewRepositoryBuildFileHandler method prepareBuildFile.

/**
   * Prepares for writing a build file by validating the build_file and build_file_content
   * attributes of the rule.
   *
   * @return true if the build file was successfully created, false if the environment is missing
   *     values (the calling fetch() function should return null in this case).
   * @throws RepositoryFunctionException if the rule does not define the build_file or
   *     build_file_content attributes, or if it defines both, or if the build file could not be
   *     retrieved, written, or symlinked.
   */
public boolean prepareBuildFile(Rule rule, Environment env) throws RepositoryFunctionException, InterruptedException {
    WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(rule);
    boolean hasBuildFile = mapper.isAttributeValueExplicitlySpecified("build_file");
    boolean hasBuildFileContent = mapper.isAttributeValueExplicitlySpecified("build_file_content");
    if (hasBuildFile && hasBuildFileContent) {
        String error = String.format("Rule %s cannot have both a 'build_file' and 'build_file_content' attribute", rule);
        throw new RepositoryFunctionException(new EvalException(rule.getLocation(), error), Transience.PERSISTENT);
    } else if (hasBuildFile) {
        buildFileValue = getBuildFileValue(rule, env);
        if (env.valuesMissing()) {
            return false;
        }
    } else if (hasBuildFileContent) {
        try {
            buildFileContent = mapper.get("build_file_content", Type.STRING);
        } catch (EvalException e) {
            throw new RepositoryFunctionException(e, Transience.PERSISTENT);
        }
    } else {
        String error = String.format("Rule %s requires a 'build_file' or 'build_file_content' attribute", rule);
        throw new RepositoryFunctionException(new EvalException(rule.getLocation(), error), Transience.PERSISTENT);
    }
    return true;
}
Also used : EvalException(com.google.devtools.build.lib.syntax.EvalException) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)

Aggregations

RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)20 IOException (java.io.IOException)18 Path (com.google.devtools.build.lib.vfs.Path)8 EvalException (com.google.devtools.build.lib.syntax.EvalException)6 SkylarkCallable (com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)5 URL (java.net.URL)5 Nullable (javax.annotation.Nullable)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 WorkspaceAttributeMapper (com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper)3 FileValue (com.google.devtools.build.lib.skyframe.FileValue)3 Fingerprint (com.google.devtools.build.lib.util.Fingerprint)3 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)3 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 BlazeDirectories (com.google.devtools.build.lib.analysis.BlazeDirectories)2 Rule (com.google.devtools.build.lib.packages.Rule)2 OutputStream (java.io.OutputStream)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2