use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.
the class PyCommon method getTransitivePythonSourcesFromSkylarkProvider.
private NestedSet<Artifact> getTransitivePythonSourcesFromSkylarkProvider(TransitiveInfoCollection dep) {
SkylarkClassObject pythonSkylarkProvider = null;
SkylarkProviders skylarkProviders = dep.getProvider(SkylarkProviders.class);
try {
if (skylarkProviders != null) {
pythonSkylarkProvider = skylarkProviders.getValue(PYTHON_SKYLARK_PROVIDER_NAME, SkylarkClassObject.class);
}
if (pythonSkylarkProvider != null) {
Object sourceFiles = pythonSkylarkProvider.getValue(TRANSITIVE_PYTHON_SRCS);
String errorType;
if (sourceFiles == null) {
errorType = "null";
} else {
errorType = EvalUtils.getDataTypeNameFromClass(sourceFiles.getClass());
}
String errorMsg = "Illegal Argument: attribute '%s' in provider '%s' is " + "of unexpected type. Should be a set, but got a '%s'";
NestedSet<Artifact> pythonSourceFiles = SkylarkType.cast(sourceFiles, SkylarkNestedSet.class, Artifact.class, null, errorMsg, TRANSITIVE_PYTHON_SRCS, PYTHON_SKYLARK_PROVIDER_NAME, errorType).getSet(Artifact.class);
return pythonSourceFiles;
}
} catch (EvalException e) {
ruleContext.ruleError(e.getMessage());
}
return null;
}
use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.
the class GitCloner method clone.
public static SkyValue clone(Rule rule, Path outputDirectory, ExtendedEventHandler eventHandler, Map<String, String> clientEnvironment) throws RepositoryFunctionException {
WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(rule);
if (mapper.isAttributeValueExplicitlySpecified("commit") == mapper.isAttributeValueExplicitlySpecified("tag")) {
throw new RepositoryFunctionException(new EvalException(rule.getLocation(), "One of either commit or tag must be defined"), Transience.PERSISTENT);
}
GitRepositoryDescriptor descriptor;
String startingPoint;
try {
if (mapper.isAttributeValueExplicitlySpecified("commit")) {
startingPoint = mapper.get("commit", Type.STRING);
} else {
startingPoint = "tags/" + mapper.get("tag", Type.STRING);
}
descriptor = new GitRepositoryDescriptor(mapper.get("remote", Type.STRING), startingPoint, mapper.get("init_submodules", Type.BOOLEAN), outputDirectory);
} catch (EvalException e) {
throw new RepositoryFunctionException(e, Transience.PERSISTENT);
}
// Setup proxy if remote is http or https
if (descriptor.remote != null && Ascii.toLowerCase(descriptor.remote).startsWith("http")) {
try {
new ProxyHelper(clientEnvironment).createProxyIfNeeded(new URL(descriptor.remote));
} catch (IOException ie) {
throw new RepositoryFunctionException(ie, Transience.TRANSIENT);
}
}
Git git = null;
try {
if (descriptor.directory.exists()) {
if (isUpToDate(descriptor)) {
return new HttpDownloadValue(descriptor.directory);
}
try {
FileSystemUtils.deleteTree(descriptor.directory);
} catch (IOException e) {
throw new RepositoryFunctionException(e, Transience.TRANSIENT);
}
}
git = Git.cloneRepository().setURI(descriptor.remote).setCredentialsProvider(new NetRCCredentialsProvider()).setDirectory(descriptor.directory.getPathFile()).setCloneSubmodules(false).setNoCheckout(true).setProgressMonitor(new GitProgressMonitor(descriptor.remote, "Cloning " + descriptor.remote, eventHandler)).call();
git.checkout().setCreateBranch(true).setName("bazel-checkout").setStartPoint(descriptor.checkout).call();
// the first level.
if (descriptor.initSubmodules && !git.submoduleInit().call().isEmpty()) {
git.submoduleUpdate().setProgressMonitor(new GitProgressMonitor(descriptor.remote, "Cloning submodules for " + descriptor.remote, eventHandler)).call();
}
} catch (InvalidRemoteException e) {
throw new RepositoryFunctionException(new IOException("Invalid Git repository URI: " + e.getMessage()), Transience.PERSISTENT);
} catch (RefNotFoundException | InvalidRefNameException e) {
throw new RepositoryFunctionException(new IOException("Invalid branch, tag, or commit: " + e.getMessage()), Transience.PERSISTENT);
} catch (GitAPIException e) {
// This is a sad attempt to actually get a useful error message out of jGit, which will bury
// the actual (useful) cause of the exception under several throws.
StringBuilder errmsg = new StringBuilder();
errmsg.append(e.getMessage());
Throwable throwable = e;
while (throwable.getCause() != null) {
throwable = throwable.getCause();
errmsg.append(" caused by " + throwable.getMessage());
}
throw new RepositoryFunctionException(new IOException("Error cloning repository: " + errmsg), Transience.PERSISTENT);
} catch (JGitInternalException e) {
// caller of the command can handle them effectively." Thanks, jgit.
throw new RepositoryFunctionException(new IOException(e.getMessage()), Transience.PERSISTENT);
} finally {
if (git != null) {
git.close();
}
}
return new HttpDownloadValue(descriptor.directory);
}
use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.
the class NewHttpArchiveFunction method fetch.
@Nullable
@Override
public RepositoryDirectoryValue.Builder fetch(Rule rule, Path outputDirectory, BlazeDirectories directories, Environment env, Map<String, String> markerData) throws RepositoryFunctionException, InterruptedException {
NewRepositoryBuildFileHandler buildFileHandler = new NewRepositoryBuildFileHandler(directories.getWorkspace());
if (!buildFileHandler.prepareBuildFile(rule, env)) {
return null;
}
try {
FileSystemUtils.createDirectoryAndParents(outputDirectory);
} catch (IOException e) {
throw new RepositoryFunctionException(new IOException("Could not create directory for " + rule.getName() + ": " + e.getMessage()), Transience.TRANSIENT);
}
// Download.
Path downloadedPath = downloader.download(rule, outputDirectory, env.getListener(), clientEnvironment);
// Decompress.
Path decompressed;
WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(rule);
String prefix = null;
if (mapper.isAttributeValueExplicitlySpecified("strip_prefix")) {
try {
prefix = mapper.get("strip_prefix", Type.STRING);
} catch (EvalException e) {
throw new RepositoryFunctionException(e, Transience.PERSISTENT);
}
}
decompressed = DecompressorValue.decompress(DecompressorDescriptor.builder().setTargetKind(rule.getTargetKind()).setTargetName(rule.getName()).setArchivePath(downloadedPath).setRepositoryPath(outputDirectory).setPrefix(prefix).build());
// Finally, write WORKSPACE and BUILD files.
createWorkspaceFile(decompressed, rule.getTargetKind(), rule.getName());
buildFileHandler.finishBuildFile(outputDirectory);
return RepositoryDirectoryValue.builder().setPath(outputDirectory);
}
use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.
the class RuleConfiguredTargetBuilder method addSkylarkDeclaredProvider.
/**
* Adds a "declared provider" defined in Skylark to the rule.
* Use this method for declared providers defined in Skyark.
*
* Use {@link #addNativeDeclaredProvider(SkylarkClassObject)} in definitions of
* native rules.
*/
public RuleConfiguredTargetBuilder addSkylarkDeclaredProvider(SkylarkClassObject provider, Location loc) throws EvalException {
ClassObjectConstructor constructor = provider.getConstructor();
SkylarkProviderValidationUtil.validateAndThrowEvalException(constructor.getPrintableName(), provider, loc);
if (!constructor.isExported()) {
throw new EvalException(constructor.getLocation(), "All providers must be top level values");
}
skylarkDeclaredProviders.put(constructor.getKey(), provider);
return this;
}
use of com.google.devtools.build.lib.syntax.EvalException in project bazel by bazelbuild.
the class SkylarkRepositoryContext method getPathFromLabel.
// Resolve the label given by value into a file path.
private SkylarkPath getPathFromLabel(Label label) throws EvalException, InterruptedException {
RootedPath rootedPath = getRootedPathFromLabel(label, env);
SkyKey fileSkyKey = FileValue.key(rootedPath);
FileValue fileValue = null;
try {
fileValue = (FileValue) env.getValueOrThrow(fileSkyKey, IOException.class, FileSymlinkException.class, InconsistentFilesystemException.class);
} catch (IOException | FileSymlinkException | InconsistentFilesystemException e) {
throw new EvalException(Location.BUILTIN, e);
}
if (fileValue == null) {
throw SkylarkRepositoryFunction.restart();
}
if (!fileValue.isFile()) {
throw new EvalException(Location.BUILTIN, "Not a file: " + rootedPath.asPath().getPathString());
}
// A label do not contains space so it safe to use as a key.
markerData.put("FILE:" + label, Integer.toString(fileValue.realFileStateValue().hashCode()));
return new SkylarkPath(rootedPath.asPath());
}
Aggregations