Skip to content

Commit

Permalink
Merge pull request bazelbuild#4 from Wyverald/registry
Browse files Browse the repository at this point in the history
Index registry impl
  • Loading branch information
Wyverald authored Apr 21, 2021
2 parents 70ae3d2 + b6b53c6 commit 931ac6f
Show file tree
Hide file tree
Showing 17 changed files with 492 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.google.devtools.build.lib.bazel.bzlmod;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.vfs.Path;
import java.net.URL;
import java.util.Objects;

public class ArchiveFetcher implements EarlyFetcher {

private final ImmutableList<URL> urls;
private final String integrity;
private final String stripPrefix;

ArchiveFetcher(ImmutableList<URL> urls, String integrity, String stripPrefix) {
this.urls = urls;
this.integrity = integrity;
this.stripPrefix = stripPrefix;
}

@Override
public Path earlyFetch() {
return null;
}

@Override
public Path fetch(String repoName, Path vendorDir) {
return null;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ArchiveFetcher that = (ArchiveFetcher) o;
return Objects.equals(urls, that.urls) &&
Objects.equals(integrity, that.integrity) &&
Objects.equals(stripPrefix, that.stripPrefix);
}

@Override
public int hashCode() {
return Objects.hash(urls, integrity, stripPrefix);
}

@Override
public String toString() {
return "ArchiveFetcher{" +
"urls=" + urls +
", integrity='" + integrity + '\'' +
", stripPrefix='" + stripPrefix + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package com.google.devtools.build.lib.bazel.bzlmod;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import java.net.URL;

@AutoValue
public abstract class ArchiveOverride implements NonRegistryOverride {

public static ArchiveOverride create(String url, String integrity) {
return new AutoValue_ArchiveOverride(url, integrity);
public static ArchiveOverride create(ImmutableList<URL> urls, String integrity,
String stripPrefix) {
return new AutoValue_ArchiveOverride(urls, integrity, stripPrefix);
}

public abstract String getUrl();
public abstract ImmutableList<URL> getUrls();

public abstract String getIntegrity();

public abstract String getStripPrefix();

@Override
public EarlyFetcher toEarlyFetcher(FetcherFactory fetcherFactory) {
return fetcherFactory.createArchiveFetcher(getUrl(), getIntegrity());
return fetcherFactory.createArchiveFetcher(getUrls(), getIntegrity(), getStripPrefix());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ java_library(
java_library(
name = "fetch",
srcs = [
"ArchiveFetcher.java",
"EarlyFetcher.java",
"Fetcher.java",
"FetcherFactory.java",
Expand All @@ -30,18 +31,26 @@ java_library(
deps = [
":common",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//third_party:guava",
]
)

java_library(
name = "registry",
srcs = [
"Registry.java",
"IndexRegistry.java",
"RegistryFactory.java",
"RegistryFactoryImpl.java",
],
deps = [
":common",
":fetch",
"//src/main/java/com/google/devtools/build/lib/bazel/repository/downloader",
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//third_party:gson",
"//third_party:guava",
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.google.devtools.build.lib.bazel.bzlmod;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.vfs.Path;
import java.net.URL;

public class FetcherFactory {

Expand All @@ -10,8 +12,10 @@ public FetcherFactory(Path workspaceRoot) {
this.workspaceRoot = workspaceRoot;
}

public EarlyFetcher createArchiveFetcher(String url, String integrity) {
return null;
public EarlyFetcher createArchiveFetcher(ImmutableList<URL> urls, String integrity,
String stripPrefix) {
// TODO: add patches
return new ArchiveFetcher(urls, integrity, stripPrefix);
}

public LocalPathFetcher createLocalPathFetcher(String path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.google.devtools.build.lib.bazel.bzlmod;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.bazel.repository.downloader.HttpDownloader;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.vfs.Path;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;

public class IndexRegistry implements Registry {

private final URI uri;
private final HttpDownloader httpDownloader;
private final Map<String, String> clientEnv;
private final FetcherFactory fetcherFactory;
private final Gson gson;

IndexRegistry(URI uri, HttpDownloader httpDownloader, Map<String, String> clientEnv,
FetcherFactory fetcherFactory) {
this.uri = uri;
this.httpDownloader = httpDownloader;
this.clientEnv = clientEnv;
this.fetcherFactory = fetcherFactory;
this.gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
}

@Override
public String getUrl() {
return uri.toString();
}

private Optional<byte[]> grabFile(String relativePath, ExtendedEventHandler eventHandler)
throws IOException, InterruptedException {
String plainBaseUrl = getUrl();
if (!plainBaseUrl.endsWith("/")) {
plainBaseUrl += "/";
}
try {
return Optional.of(httpDownloader
.downloadAndReadOneUrl(new URL(plainBaseUrl + relativePath), eventHandler, clientEnv));
} catch (FileNotFoundException e) {
return Optional.empty();
}
}

@Override
public Optional<byte[]> getModuleFile(ModuleKey key, ExtendedEventHandler eventHandler)
throws IOException, InterruptedException {
return grabFile(String.format("modules/%s/%s/MODULE.bazel", key.getName(), key.getVersion()),
eventHandler);
}

private static class BazelRegistryJson {

String[] mirrors;
}

private static class SourceJson {

URL url;
String integrity;
String stripPrefix;
String[] patchFiles;
int patchStrip;
}

private <T> Optional<T> grabJson(String relativePath, Class<T> klass,
ExtendedEventHandler eventHandler)
throws IOException, InterruptedException {
Optional<byte[]> bytes = grabFile(relativePath, eventHandler);
if (!bytes.isPresent()) {
return Optional.empty();
}
String jsonString = new String(bytes.get(), StandardCharsets.UTF_8);
return Optional.of(gson.fromJson(jsonString, klass));
}

@Override
public Fetcher getFetcher(ModuleKey key, ExtendedEventHandler eventHandler)
throws IOException, InterruptedException {
Optional<BazelRegistryJson> bazelRegistryJson =
grabJson("bazel_registry.json", BazelRegistryJson.class, eventHandler);
Optional<SourceJson> sourceJson = grabJson(
String.format("modules/%s/%s/source.json", key.getName(), key.getVersion()),
SourceJson.class, eventHandler);
if (!sourceJson.isPresent()) {
throw new FileNotFoundException(
String.format("Module %s's source information not found in registry %s", key, getUrl()));
}
URL sourceUrl = sourceJson.get().url;
ImmutableList.Builder<URL> urls = new ImmutableList.Builder<>();
if (bazelRegistryJson.isPresent()) {
for (String mirror : bazelRegistryJson.get().mirrors) {
StringBuilder url = new StringBuilder(mirror);
if (url.charAt(url.length() - 1) != '/') {
url.append('/');
}
url.append(sourceUrl.getAuthority());
if (url.charAt(url.length() - 1) != '/' && sourceUrl.getFile().charAt(0) != '/') {
url.append('/');
}
url.append(sourceUrl.getFile());
urls.add(new URL(url.toString()));
}
}
urls.add(sourceUrl);
return fetcherFactory.createArchiveFetcher(
urls.build(),
sourceJson.get().integrity,
sourceJson.get().stripPrefix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.actions.FileValue;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.packages.NoSuchThingException;
import com.google.devtools.build.lib.skyframe.PrecomputedValue;
import com.google.devtools.build.lib.skyframe.PrecomputedValue.Precomputed;
Expand All @@ -19,6 +20,8 @@
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -186,9 +189,19 @@ private Optional<GetModuleFileResult> getModuleFile(ModuleKey key, StarlarkOverr
} else if (override != null) {
throw errorf("unrecognized override type %s", override.getClass().getName());
}
return getModuleFileFromRegistries(key,
registries.stream().map(registryFactory::getRegistryWithUrl)
.collect(ImmutableList.toImmutableList()));
ImmutableList.Builder<Registry> registryObjects = new ImmutableList.Builder<>();
for (String registryUrl : registries) {
try {
registryObjects.add(registryFactory.getRegistryWithUrl(registryUrl));
} catch (URISyntaxException e) {
throw new ModuleFileFunctionException(e);
}
}
try {
return getModuleFileFromRegistries(key, registryObjects.build(), env.getListener());
} catch (IOException e) {
throw new ModuleFileFunctionException(e);
}
}

private static byte[] readFile(Path path) throws ModuleFileFunctionException {
Expand All @@ -200,12 +213,12 @@ private static byte[] readFile(Path path) throws ModuleFileFunctionException {
}
}

@VisibleForTesting
static Optional<GetModuleFileResult> getModuleFileFromRegistries(ModuleKey key,
List<Registry> registries) {
private static Optional<GetModuleFileResult> getModuleFileFromRegistries(ModuleKey key,
List<Registry> registries, ExtendedEventHandler eventHandler)
throws IOException, InterruptedException {
GetModuleFileResult result = new GetModuleFileResult();
for (Registry registry : registries) {
Optional<byte[]> moduleFile = registry.getModuleFile(key);
Optional<byte[]> moduleFile = registry.getModuleFile(key, eventHandler);
if (!moduleFile.isPresent()) {
continue;
}
Expand All @@ -232,7 +245,7 @@ private static ModuleFileFunctionException errorf(String format, Object... args)
return new ModuleFileFunctionException(new NoSuchThingException(String.format(format, args)));
}

private static final class ModuleFileFunctionException extends SkyFunctionException {
static final class ModuleFileFunctionException extends SkyFunctionException {

ModuleFileFunctionException(Exception cause) {
super(cause, Transience.TRANSIENT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.google.devtools.build.lib.bazel.bzlmod;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileFunction.ModuleFileFunctionException;
import com.google.devtools.build.lib.starlarkbuildapi.repository.ModuleFileGlobalsApi;
import com.google.devtools.build.lib.starlarkbuildapi.repository.StarlarkOverrideApi;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -55,8 +59,21 @@ public StarlarkOverrideApi singleVersionOverride(String version, String registry
}

@Override
public StarlarkOverrideApi archiveOverride(String url, String integrity) {
return ArchiveOverride.create(url, integrity);
public StarlarkOverrideApi archiveOverride(Object urls, String integrity,
String stripPrefix) throws ModuleFileFunctionException {
ImmutableList.Builder<URL> urlList = new ImmutableList.Builder<>();
try {
if (urls instanceof String) {
urlList.add(new URL((String) urls));
} else {
for (String urlString : (Iterable<String>) urls) {
urlList.add(new URL(urlString));
}
}
} catch (MalformedURLException e) {
throw new ModuleFileFunctionException(e);
}
return ArchiveOverride.create(urlList.build(), integrity, stripPrefix);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.google.devtools.build.lib.bazel.bzlmod;

import com.google.devtools.build.lib.events.ExtendedEventHandler;
import java.io.IOException;
import java.util.Optional;

public interface Registry {
Expand All @@ -9,10 +11,9 @@ public interface Registry {
/**
* Returns Optional.empty() when the module is not found in this registry.
*/
Optional<byte[]> getModuleFile(ModuleKey key);
Optional<byte[]> getModuleFile(ModuleKey key, ExtendedEventHandler eventHandler)
throws IOException, InterruptedException;

/**
* Returns Optional.empty() when the module is not found in this registry.
*/
Optional<Fetcher> getFetcher(ModuleKey key);
Fetcher getFetcher(ModuleKey key, ExtendedEventHandler eventHandler)
throws IOException, InterruptedException;
}
Loading

0 comments on commit 931ac6f

Please sign in to comment.