Search in sources :

Example 11 with RepositoryFunctionException

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

the class SkylarkRepositoryContext method getUrls.

private static List<URL> getUrls(Object urlOrList) throws RepositoryFunctionException {
    List<String> urlStrings;
    if (urlOrList instanceof String) {
        urlStrings = ImmutableList.of((String) urlOrList);
    } else {
        @SuppressWarnings("unchecked") List<String> list = (List<String>) urlOrList;
        urlStrings = list;
    }
    if (urlStrings.isEmpty()) {
        throw new RepositoryFunctionException(new IOException("urls not set"), Transience.PERSISTENT);
    }
    List<URL> urls = new ArrayList<>();
    for (String urlString : urlStrings) {
        URL url;
        try {
            url = new URL(urlString);
        } catch (MalformedURLException e) {
            throw new RepositoryFunctionException(new IOException("Bad URL: " + urlString), Transience.PERSISTENT);
        }
        if (!HttpUtils.isUrlSupportedByDownloader(url)) {
            throw new RepositoryFunctionException(new IOException("Unsupported protocol: " + url.getProtocol()), Transience.PERSISTENT);
        }
        urls.add(url);
    }
    return urls;
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) List(java.util.List) SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) URL(java.net.URL)

Example 12 with RepositoryFunctionException

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

the class JarDecompressor method decompress.

/**
   * The .jar can be used compressed, so this just exposes it in a way Bazel can use.
   *
   * <p>It moves the jar from some-name/x/y/z/foo.jar to some-name/jar/foo.jar and creates a
   * BUILD.bazel file containing one entry: the .jar.
   */
@Override
@Nullable
public Path decompress(DecompressorDescriptor descriptor) throws RepositoryFunctionException {
    // Example: archiveFile is external/some-name/foo.jar.
    String baseName = descriptor.archivePath().getBaseName();
    try {
        FileSystemUtils.createDirectoryAndParents(descriptor.repositoryPath());
        // external/some-name/WORKSPACE.
        RepositoryFunction.createWorkspaceFile(descriptor.repositoryPath(), descriptor.targetKind(), descriptor.targetName());
        // external/some-name/jar.
        Path jarDirectory = descriptor.repositoryPath().getRelative(getPackageName());
        FileSystemUtils.createDirectoryAndParents(jarDirectory);
        // external/some-name/repository/jar/foo.jar is a symbolic link to the jar in
        // external/some-name.
        Path jarSymlink = jarDirectory.getRelative(baseName);
        if (!jarSymlink.exists()) {
            jarSymlink.createSymbolicLink(descriptor.archivePath());
        }
        // external/some-name/repository/jar/BUILD.bazel defines the //jar target.
        Path buildFile = jarDirectory.getRelative("BUILD.bazel");
        FileSystemUtils.writeLinesAs(buildFile, Charset.forName("UTF-8"), "# DO NOT EDIT: automatically generated BUILD.bazel file for " + descriptor.targetKind() + " rule " + descriptor.targetName(), createBuildFile(baseName));
        if (descriptor.executable()) {
            descriptor.archivePath().chmod(0755);
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(new IOException("Error auto-creating jar repo structure: " + e.getMessage()), Transience.TRANSIENT);
    }
    return descriptor.repositoryPath();
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) IOException(java.io.IOException) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) Nullable(javax.annotation.Nullable)

Example 13 with RepositoryFunctionException

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

the class MavenServerFunction method compute.

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException, RepositoryFunctionException {
    String repository = (String) skyKey.argument();
    Rule repositoryRule = null;
    try {
        repositoryRule = RepositoryFunction.getRule(repository, env);
    } catch (RepositoryNotFoundException ex) {
    // Ignored. We throw a new one below.
    }
    BlazeDirectories directories = PrecomputedValue.BLAZE_DIRECTORIES.get(env);
    if (env.valuesMissing()) {
        return null;
    }
    String serverName;
    String url;
    Map<String, FileValue> settingsFiles;
    boolean foundRepoRule = repositoryRule != null && repositoryRule.getRuleClass().equals(MavenServerRule.NAME);
    if (!foundRepoRule) {
        if (repository.equals(MavenServerValue.DEFAULT_ID)) {
            settingsFiles = getDefaultSettingsFile(directories, env);
            serverName = MavenServerValue.DEFAULT_ID;
            url = MavenConnector.getMavenCentralRemote().getUrl();
        } else {
            throw new RepositoryFunctionException(new IOException("Could not find maven repository " + repository), Transience.TRANSIENT);
        }
    } else {
        WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(repositoryRule);
        serverName = repositoryRule.getName();
        try {
            url = mapper.get("url", Type.STRING);
            if (!mapper.isAttributeValueExplicitlySpecified("settings_file")) {
                settingsFiles = getDefaultSettingsFile(directories, env);
            } else {
                PathFragment settingsFilePath = new PathFragment(mapper.get("settings_file", Type.STRING));
                RootedPath settingsPath = RootedPath.toRootedPath(directories.getWorkspace().getRelative(settingsFilePath), PathFragment.EMPTY_FRAGMENT);
                FileValue fileValue = (FileValue) env.getValue(FileValue.key(settingsPath));
                if (fileValue == null) {
                    return null;
                }
                if (!fileValue.exists()) {
                    throw new RepositoryFunctionException(new IOException("Could not find settings file " + settingsPath), Transience.TRANSIENT);
                }
                settingsFiles = ImmutableMap.<String, FileValue>builder().put(USER_KEY, fileValue).build();
            }
        } catch (EvalException e) {
            throw new RepositoryFunctionException(e, Transience.PERSISTENT);
        }
    }
    if (settingsFiles == null) {
        return null;
    }
    Fingerprint fingerprint = new Fingerprint();
    try {
        for (Map.Entry<String, FileValue> entry : settingsFiles.entrySet()) {
            fingerprint.addString(entry.getKey());
            Path path = entry.getValue().realRootedPath().asPath();
            if (path.exists()) {
                fingerprint.addBoolean(true);
                fingerprint.addBytes(path.getDigest());
            } else {
                fingerprint.addBoolean(false);
            }
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
    byte[] fingerprintBytes = fingerprint.digestAndReset();
    if (settingsFiles.isEmpty()) {
        return new MavenServerValue(serverName, url, new Server(), fingerprintBytes);
    }
    DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
    if (settingsFiles.containsKey(SYSTEM_KEY)) {
        request.setGlobalSettingsFile(settingsFiles.get(SYSTEM_KEY).realRootedPath().asPath().getPathFile());
    }
    if (settingsFiles.containsKey(USER_KEY)) {
        request.setUserSettingsFile(settingsFiles.get(USER_KEY).realRootedPath().asPath().getPathFile());
    }
    DefaultSettingsBuilder builder = (new DefaultSettingsBuilderFactory()).newInstance();
    SettingsBuildingResult result;
    try {
        result = builder.build(request);
    } catch (SettingsBuildingException e) {
        throw new RepositoryFunctionException(new IOException("Error parsing settings files: " + e.getMessage()), Transience.TRANSIENT);
    }
    if (!result.getProblems().isEmpty()) {
        throw new RepositoryFunctionException(new IOException("Errors interpreting settings file: " + Arrays.toString(result.getProblems().toArray())), Transience.PERSISTENT);
    }
    Settings settings = result.getEffectiveSettings();
    Server server = settings.getServer(serverName);
    server = server == null ? new Server() : server;
    return new MavenServerValue(serverName, url, server, fingerprintBytes);
}
Also used : FileValue(com.google.devtools.build.lib.skyframe.FileValue) Server(org.apache.maven.settings.Server) SettingsBuildingResult(org.apache.maven.settings.building.SettingsBuildingResult) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) Settings(org.apache.maven.settings.Settings) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) Fingerprint(com.google.devtools.build.lib.util.Fingerprint) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) RepositoryNotFoundException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryNotFoundException) IOException(java.io.IOException) EvalException(com.google.devtools.build.lib.syntax.EvalException) DefaultSettingsBuilderFactory(org.apache.maven.settings.building.DefaultSettingsBuilderFactory) BlazeDirectories(com.google.devtools.build.lib.analysis.BlazeDirectories) DefaultSettingsBuilder(org.apache.maven.settings.building.DefaultSettingsBuilder) MavenServerRule(com.google.devtools.build.lib.bazel.rules.workspace.MavenServerRule) Rule(com.google.devtools.build.lib.packages.Rule) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) WorkspaceAttributeMapper(com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper) Nullable(javax.annotation.Nullable)

Example 14 with RepositoryFunctionException

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

the class ZipDecompressor method decompress.

/**
   * This unzips the zip file to a sibling directory of {@link DecompressorDescriptor#archivePath}.
   * The zip file is expected to have the WORKSPACE file at the top level, e.g.:
   *
   * <pre>
   * $ unzip -lf some-repo.zip
   * Archive:  ../repo.zip
   *  Length      Date    Time    Name
   * ---------  ---------- -----   ----
   *        0  2014-11-20 15:50   WORKSPACE
   *        0  2014-11-20 16:10   foo/
   *      236  2014-11-20 15:52   foo/BUILD
   *      ...
   * </pre>
   */
@Override
@Nullable
public Path decompress(DecompressorDescriptor descriptor) throws RepositoryFunctionException {
    Path destinationDirectory = descriptor.archivePath().getParentDirectory();
    Optional<String> prefix = descriptor.prefix();
    boolean foundPrefix = false;
    try (ZipReader reader = new ZipReader(descriptor.archivePath().getPathFile())) {
        Collection<ZipFileEntry> entries = reader.entries();
        for (ZipFileEntry entry : entries) {
            StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entry.getName(), prefix);
            foundPrefix = foundPrefix || entryPath.foundPrefix();
            if (entryPath.skip()) {
                continue;
            }
            extractZipEntry(reader, entry, destinationDirectory, entryPath.getPathFragment());
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(new IOException(String.format("Error extracting %s to %s: %s", descriptor.archivePath(), destinationDirectory, e.getMessage())), Transience.TRANSIENT);
    }
    if (prefix.isPresent() && !foundPrefix) {
        throw new RepositoryFunctionException(new IOException("Prefix " + prefix.get() + " was given, but not found in the zip"), Transience.PERSISTENT);
    }
    return destinationDirectory;
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) ZipReader(com.google.devtools.build.zip.ZipReader) ZipFileEntry(com.google.devtools.build.zip.ZipFileEntry) IOException(java.io.IOException) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) Nullable(javax.annotation.Nullable)

Example 15 with RepositoryFunctionException

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

the class HttpDownloader method download.

/** Validates native repository rule attributes and calls the other download method. */
public Path download(Rule rule, Path outputDirectory, ExtendedEventHandler eventHandler, Map<String, String> clientEnv) throws RepositoryFunctionException, InterruptedException {
    WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(rule);
    List<URL> urls = new ArrayList<>();
    String sha256;
    String type;
    try {
        String urlString = Strings.nullToEmpty(mapper.get("url", Type.STRING));
        if (!urlString.isEmpty()) {
            try {
                URL url = new URL(urlString);
                if (!HttpUtils.isUrlSupportedByDownloader(url)) {
                    throw new EvalException(rule.getAttributeLocation("url"), "Unsupported protocol: " + url.getProtocol());
                }
                urls.add(url);
            } catch (MalformedURLException e) {
                throw new EvalException(rule.getAttributeLocation("url"), e.toString());
            }
        }
        List<String> urlStrings = MoreObjects.firstNonNull(mapper.get("urls", Type.STRING_LIST), ImmutableList.<String>of());
        if (!urlStrings.isEmpty()) {
            if (!urls.isEmpty()) {
                throw new EvalException(rule.getAttributeLocation("url"), "Don't set url if urls is set");
            }
            try {
                for (String urlString2 : urlStrings) {
                    URL url = new URL(urlString2);
                    if (!HttpUtils.isUrlSupportedByDownloader(url)) {
                        throw new EvalException(rule.getAttributeLocation("urls"), "Unsupported protocol: " + url.getProtocol());
                    }
                    urls.add(url);
                }
            } catch (MalformedURLException e) {
                throw new EvalException(rule.getAttributeLocation("urls"), e.toString());
            }
        }
        if (urls.isEmpty()) {
            throw new EvalException(rule.getLocation(), "urls attribute not set");
        }
        sha256 = Strings.nullToEmpty(mapper.get("sha256", Type.STRING));
        if (!sha256.isEmpty() && !RepositoryCache.KeyType.SHA256.isValid(sha256)) {
            throw new EvalException(rule.getAttributeLocation("sha256"), "Invalid SHA256 checksum");
        }
        type = Strings.nullToEmpty(mapper.get("type", Type.STRING));
    } catch (EvalException e) {
        throw new RepositoryFunctionException(e, Transience.PERSISTENT);
    }
    try {
        return download(urls, sha256, Optional.of(type), outputDirectory, eventHandler, clientEnv);
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) EvalException(com.google.devtools.build.lib.syntax.EvalException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) WorkspaceAttributeMapper(com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper) URL(java.net.URL) 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