Search in sources :

Example 6 with Sha1HashCode

use of com.facebook.buck.util.sha1.Sha1HashCode in project buck by facebook.

the class IjProjectWriter method writeToFile.

@VisibleForTesting
protected void writeToFile(ST contents, Path path) throws IOException {
    StringWriter stringWriter = new StringWriter();
    AutoIndentWriter noIndentWriter = new AutoIndentWriter(stringWriter);
    contents.write(noIndentWriter);
    byte[] renderedContentsBytes = noIndentWriter.toString().getBytes(StandardCharsets.UTF_8);
    if (projectFilesystem.exists(path)) {
        Sha1HashCode fileSha1 = projectFilesystem.computeSha1(path);
        Sha1HashCode contentsSha1 = Sha1HashCode.fromHashCode(Hashing.sha1().hashBytes(renderedContentsBytes));
        if (fileSha1.equals(contentsSha1)) {
            return;
        }
    }
    boolean danglingTempFile = false;
    Path tempFile = projectFilesystem.createTempFile(IDEA_CONFIG_DIR_PREFIX, path.getFileName().toString(), ".tmp");
    try {
        danglingTempFile = true;
        try (OutputStream outputStream = projectFilesystem.newFileOutputStream(tempFile)) {
            outputStream.write(contents.render().getBytes());
        }
        projectFilesystem.createParentDirs(path);
        projectFilesystem.move(tempFile, path, StandardCopyOption.REPLACE_EXISTING);
        danglingTempFile = false;
    } finally {
        if (danglingTempFile) {
            projectFilesystem.deleteFileAtPath(tempFile);
        }
    }
}
Also used : AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) Path(java.nio.file.Path) StringWriter(java.io.StringWriter) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) OutputStream(java.io.OutputStream) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with Sha1HashCode

use of com.facebook.buck.util.sha1.Sha1HashCode in project buck by facebook.

the class BuckBuildLog method fromLogContents.

public static BuckBuildLog fromLogContents(Path root, List<String> logContents) {
    ImmutableMap.Builder<BuildTarget, BuildLogEntry> builder = ImmutableMap.builder();
    for (String line : logContents) {
        Matcher matcher = BUILD_LOG_FINISHED_RULE_REGEX.matcher(line);
        if (!matcher.matches()) {
            continue;
        }
        String buildTargetRaw = matcher.group("BuildTarget");
        BuildTarget buildTarget = BuildTargetFactory.newInstance(root, buildTargetRaw);
        String statusRaw = matcher.group("Status");
        BuildRuleStatus status = BuildRuleStatus.valueOf(statusRaw);
        String ruleKeyRaw = matcher.group("RuleKey");
        Sha1HashCode ruleKey = Sha1HashCode.of(ruleKeyRaw);
        CacheResult cacheResult = null;
        BuildRuleSuccessType successType = null;
        if (status == BuildRuleStatus.SUCCESS) {
            String cacheResultRaw = matcher.group("CacheResult");
            cacheResult = CacheResult.valueOf(cacheResultRaw);
            String successTypeRaw = matcher.group("SuccessType");
            successType = BuildRuleSuccessType.valueOf(successTypeRaw);
        }
        builder.put(buildTarget, new BuildLogEntry(status, Optional.ofNullable(successType), Optional.ofNullable(cacheResult), ruleKey));
    }
    return new BuckBuildLog(root, builder.build());
}
Also used : Matcher(java.util.regex.Matcher) BuildTarget(com.facebook.buck.model.BuildTarget) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) BuildRuleSuccessType(com.facebook.buck.rules.BuildRuleSuccessType) CacheResult(com.facebook.buck.artifact_cache.CacheResult) BuildRuleStatus(com.facebook.buck.rules.BuildRuleStatus) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with Sha1HashCode

use of com.facebook.buck.util.sha1.Sha1HashCode in project buck by facebook.

the class ComputeExopackageDepsAbi method addToHash.

private void addToHash(Hasher hasher, String role, Path absolutePath, Path relativePath) throws IOException {
    // No need to check relative path. That's already been done for us.
    Preconditions.checkState(absolutePath.isAbsolute(), "Expected absolute path to be absolute: %s", absolutePath);
    hasher.putUnencodedChars(relativePath.toString());
    hasher.putByte((byte) 0);
    Sha1HashCode fileSha1 = getProjectFilesystem().computeSha1(absolutePath);
    fileSha1.update(hasher);
    hasher.putByte((byte) 0);
    hasher.putUnencodedChars(role);
    hasher.putByte((byte) 0);
    LOG.verbose("file %s(%s) = %s", relativePath, role, fileSha1);
}
Also used : Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode)

Example 9 with Sha1HashCode

use of com.facebook.buck.util.sha1.Sha1HashCode in project buck by facebook.

the class CopyNativeLibraries method getBuildSteps.

@Override
public ImmutableList<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Step> steps = ImmutableList.builder();
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), getBinPath()));
    final Path pathToNativeLibs = getPathToNativeLibsDir();
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), pathToNativeLibs));
    final Path pathToNativeLibsAssets = getPathToNativeLibsAssetsDir();
    steps.add(new MakeCleanDirectoryStep(getProjectFilesystem(), pathToNativeLibsAssets));
    for (SourcePath nativeLibDir : nativeLibDirectories.asList().reverse()) {
        copyNativeLibrary(getProjectFilesystem(), context.getSourcePathResolver().getAbsolutePath(nativeLibDir), pathToNativeLibs, cpuFilters, steps);
    }
    addStepsForCopyingStrippedNativeLibrariesOrAssets(context.getSourcePathResolver(), getProjectFilesystem(), stripLibRules, pathToNativeLibs, steps);
    addStepsForCopyingStrippedNativeLibrariesOrAssets(context.getSourcePathResolver(), getProjectFilesystem(), stripLibAssetRules, pathToNativeLibsAssets, steps);
    final Path pathToMetadataTxt = getPathToMetadataTxt();
    steps.add(new AbstractExecutionStep("hash_native_libs") {

        @Override
        public StepExecutionResult execute(ExecutionContext context) {
            ProjectFilesystem filesystem = getProjectFilesystem();
            ImmutableList.Builder<String> metadataLines = ImmutableList.builder();
            try {
                for (Path nativeLib : filesystem.getFilesUnderPath(getPathToAllLibsDir())) {
                    Sha1HashCode filesha1 = filesystem.computeSha1(nativeLib);
                    Path relativePath = getPathToAllLibsDir().relativize(nativeLib);
                    metadataLines.add(String.format("%s %s", relativePath, filesha1));
                }
                filesystem.writeLinesToPath(metadataLines.build(), pathToMetadataTxt);
            } catch (IOException e) {
                context.logError(e, "There was an error hashing native libraries.");
                return StepExecutionResult.ERROR;
            }
            return StepExecutionResult.SUCCESS;
        }
    });
    buildableContext.recordArtifact(pathToNativeLibs);
    buildableContext.recordArtifact(pathToNativeLibsAssets);
    buildableContext.recordArtifact(pathToMetadataTxt);
    return steps.build();
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) ImmutableList(com.google.common.collect.ImmutableList) Step(com.facebook.buck.step.Step) CopyStep(com.facebook.buck.step.fs.CopyStep) MkdirStep(com.facebook.buck.step.fs.MkdirStep) AbstractExecutionStep(com.facebook.buck.step.AbstractExecutionStep) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) IOException(java.io.IOException) SourcePath(com.facebook.buck.rules.SourcePath) ExecutionContext(com.facebook.buck.step.ExecutionContext) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) MakeCleanDirectoryStep(com.facebook.buck.step.fs.MakeCleanDirectoryStep) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem)

Example 10 with Sha1HashCode

use of com.facebook.buck.util.sha1.Sha1HashCode in project buck by facebook.

the class DexProducedFromJavaLibrary method computeAbiKey.

@VisibleForTesting
static Sha1HashCode computeAbiKey(ImmutableSortedMap<String, HashCode> classNames) {
    Hasher hasher = Hashing.sha1().newHasher();
    for (Map.Entry<String, HashCode> entry : classNames.entrySet()) {
        hasher.putUnencodedChars(entry.getKey());
        hasher.putByte((byte) 0);
        hasher.putUnencodedChars(entry.getValue().toString());
        hasher.putByte((byte) 0);
    }
    return Sha1HashCode.fromHashCode(hasher.hash());
}
Also used : Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

Sha1HashCode (com.facebook.buck.util.sha1.Sha1HashCode)17 Path (java.nio.file.Path)10 Test (org.junit.Test)6 ImmutableList (com.google.common.collect.ImmutableList)4 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)3 SourcePath (com.facebook.buck.rules.SourcePath)3 AbstractExecutionStep (com.facebook.buck.step.AbstractExecutionStep)3 ExecutionContext (com.facebook.buck.step.ExecutionContext)3 Step (com.facebook.buck.step.Step)3 StepExecutionResult (com.facebook.buck.step.StepExecutionResult)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 HashCode (com.google.common.hash.HashCode)3 IOException (java.io.IOException)3 BuildTarget (com.facebook.buck.model.BuildTarget)2 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)2 MkdirStep (com.facebook.buck.step.fs.MkdirStep)2 BuckBuildLog (com.facebook.buck.testutil.integration.BuckBuildLog)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 AbstractMap (java.util.AbstractMap)2 List (java.util.List)2