Search in sources :

Example 16 with Fingerprint

use of com.google.devtools.build.lib.util.Fingerprint in project bazel by bazelbuild.

the class BinaryFileWriteAction method computeKey.

@Override
protected String computeKey() {
    Fingerprint f = new Fingerprint();
    f.addString(GUID);
    f.addString(String.valueOf(makeExecutable));
    try (InputStream in = source.openStream()) {
        byte[] buffer = new byte[512];
        int amountRead;
        while ((amountRead = in.read(buffer)) != -1) {
            f.addBytes(buffer, 0, amountRead);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return f.hexDigestAndReset();
}
Also used : Fingerprint(com.google.devtools.build.lib.util.Fingerprint) InputStream(java.io.InputStream) IOException(java.io.IOException) Fingerprint(com.google.devtools.build.lib.util.Fingerprint)

Example 17 with Fingerprint

use of com.google.devtools.build.lib.util.Fingerprint in project bazel by bazelbuild.

the class TestRunnerAction method computeKey.

@Override
protected String computeKey() {
    Fingerprint f = new Fingerprint();
    f.addString(GUID);
    f.addStrings(executionSettings.getArgs());
    f.addString(executionSettings.getTestFilter() == null ? "" : executionSettings.getTestFilter());
    RunUnder runUnder = executionSettings.getRunUnder();
    f.addString(runUnder == null ? "" : runUnder.getValue());
    f.addStringMap(getTestEnv());
    f.addString(testProperties.getSize().toString());
    f.addString(testProperties.getTimeout().toString());
    f.addStrings(testProperties.getTags());
    f.addInt(testProperties.isLocal() ? 1 : 0);
    f.addInt(shardNum);
    f.addInt(executionSettings.getTotalShards());
    f.addInt(runNumber);
    f.addInt(configuration.getRunsPerTestForLabel(getOwner().getLabel()));
    f.addInt(configuration.isCodeCoverageEnabled() ? 1 : 0);
    return f.hexDigestAndReset();
}
Also used : Fingerprint(com.google.devtools.build.lib.util.Fingerprint) RunUnder(com.google.devtools.build.lib.analysis.config.RunUnder)

Example 18 with Fingerprint

use of com.google.devtools.build.lib.util.Fingerprint in project bazel by bazelbuild.

the class RepositoryDelegatorFunction method isFilesystemUpToDate.

/**
   * Checks if the state of the repository in the file system is consistent with the rule in the
   * WORKSPACE file.
   *
   * <p>
   * Deletes the marker file if not so that no matter what happens after, the state of the file
   * system stays consistent.
   *
   * <p>
   * Returns null if the file system is not up to date and a hash of the marker file if the file
   * system is up to date.
   */
@Nullable
private final byte[] isFilesystemUpToDate(Path markerPath, Rule rule, String ruleKey, RepositoryFunction handler, Environment env) throws RepositoryFunctionException, InterruptedException {
    try {
        if (!markerPath.exists()) {
            return null;
        }
        String content = FileSystemUtils.readContent(markerPath, StandardCharsets.UTF_8);
        String[] lines = content.split("\n");
        Map<String, String> markerData = new TreeMap<>();
        String markerRuleKey = "";
        boolean firstLine = true;
        for (String line : lines) {
            if (firstLine) {
                markerRuleKey = line;
                firstLine = false;
            } else {
                int sChar = line.indexOf(' ');
                String key = line;
                String value = "";
                if (sChar > 0) {
                    key = unescape(line.substring(0, sChar));
                    value = unescape(line.substring(sChar + 1));
                }
                markerData.put(key, value);
            }
        }
        boolean result = false;
        if (markerRuleKey.equals(ruleKey)) {
            result = handler.verifyMarkerData(rule, markerData, env);
            if (env.valuesMissing()) {
                return null;
            }
        }
        if (result) {
            return new Fingerprint().addString(content).digestAndReset();
        } else {
            // So that we are in a consistent state if something happens while fetching the repository
            markerPath.delete();
            return null;
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
}
Also used : Fingerprint(com.google.devtools.build.lib.util.Fingerprint) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Fingerprint(com.google.devtools.build.lib.util.Fingerprint) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) Nullable(javax.annotation.Nullable)

Example 19 with Fingerprint

use of com.google.devtools.build.lib.util.Fingerprint in project bazel by bazelbuild.

the class RepositoryDelegatorFunction method writeMarkerFile.

private final byte[] writeMarkerFile(Path markerPath, Map<String, String> markerData, String ruleKey) throws RepositoryFunctionException {
    try {
        StringBuilder builder = new StringBuilder();
        builder.append(ruleKey).append("\n");
        for (Map.Entry<String, String> data : markerData.entrySet()) {
            String key = data.getKey();
            String value = data.getValue();
            builder.append(escape(key)).append(" ").append(escape(value)).append("\n");
        }
        String content = builder.toString();
        FileSystemUtils.writeContent(markerPath, StandardCharsets.UTF_8, content);
        return new Fingerprint().addString(content).digestAndReset();
    } catch (IOException e) {
        throw new RepositoryFunctionException(e, Transience.TRANSIENT);
    }
}
Also used : Fingerprint(com.google.devtools.build.lib.util.Fingerprint) IOException(java.io.IOException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) TreeMap(java.util.TreeMap) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)

Example 20 with Fingerprint

use of com.google.devtools.build.lib.util.Fingerprint 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)

Aggregations

Fingerprint (com.google.devtools.build.lib.util.Fingerprint)38 Artifact (com.google.devtools.build.lib.actions.Artifact)9 IOException (java.io.IOException)6 Map (java.util.Map)6 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)5 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Rule (com.google.devtools.build.lib.packages.Rule)2 InputStream (java.io.InputStream)2 TreeMap (java.util.TreeMap)2 Nullable (javax.annotation.Nullable)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 TreeFileArtifact (com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)1 BlazeDirectories (com.google.devtools.build.lib.analysis.BlazeDirectories)1 RunUnder (com.google.devtools.build.lib.analysis.config.RunUnder)1 MavenServerRule (com.google.devtools.build.lib.bazel.rules.workspace.MavenServerRule)1 AspectDescriptor (com.google.devtools.build.lib.packages.AspectDescriptor)1 NoSuchThingException (com.google.devtools.build.lib.packages.NoSuchThingException)1