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);
}
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations