[kotlin] project-model-updater: cache sha256 sum

So the model updater would reuse results between Bazel, monorepo and community.
Local boost from 75s to 35s

^KTIJ-35777

GitOrigin-RevId: bc5515ca4a30075434364088a473c57f1ab4d3dd
This commit is contained in:
Dmitrii Gridin
2025-09-30 09:07:30 +00:00
committed by intellij-monorepo-bot
parent 5b9b2fc3e0
commit bd337236a9
3 changed files with 16 additions and 49 deletions
@@ -10,6 +10,7 @@ import java.nio.file.Files
import java.nio.file.InvalidPathException
import java.nio.file.Path
import java.security.MessageDigest
import java.util.concurrent.ConcurrentHashMap
import kotlin.io.path.*
import kotlin.jvm.optionals.getOrNull
@@ -179,7 +180,7 @@ object KotlinTestsDependenciesUtil {
if (sha256 == null) {
error("cannot find sha256 in '$block'")
}
val checksum = calculateSha256SumForUrl(url)
val checksum = sha256SumForUrl(url)
if (isUpToDateCheck) {
if (checksum != sha256) {
errors.add("sha256 mismatch for '$url' expected '$sha256' but got '$checksum'")
@@ -201,6 +202,10 @@ object KotlinTestsDependenciesUtil {
}
}
private val sha256Cache = ConcurrentHashMap<String, String>()
internal fun sha256SumForUrl(url: String): String = sha256Cache.getOrPut(url) { calculateSha256SumForUrl(url) }
private fun calculateSha256SumForUrl(url: String): String {
println("Calculating SHA256 checksum for '$url'")
val digest = MessageDigest.getInstance("SHA-256")
@@ -1,5 +1,7 @@
package org.jetbrains.tools.model.updater.impl
import org.jetbrains.tools.model.updater.KotlinTestsDependenciesUtil
data class JpsLibrary(
val name: String,
val type: LibraryType,
@@ -25,10 +27,14 @@ data class JpsLibrary(
xml("properties", *properties) {
if (jpsResolverSettings.sha256ChecksumsEnabled) {
xml("verification") {
computeSha256Checksums(classes, type.remoteRepository).forEach {
xml("artifact", "url" to it.url) {
for (clazz in classes) {
val jpsPath = clazz.path
val fileUrl = JpsUrl.File(jpsPath)
xml("artifact", "url" to fileUrl.url) {
xml("sha256sum") {
raw(it.sha256Checksum)
val url = "${type.remoteRepository.url}/${jpsPath.relativePath}"
val sha256checksum = KotlinTestsDependenciesUtil.sha256SumForUrl(url)
raw(sha256checksum)
}
}
}
@@ -65,10 +71,6 @@ data class JpsLibrary(
val excludes: List<MavenId> = emptyList(),
val includeTransitive: Boolean = true,
val remoteRepository: JpsRemoteRepository
) : LibraryType() {
companion object {
data class ArtifactVerificationEntry(val url: String, val sha256Checksum: String)
}
}
) : LibraryType()
}
}
@@ -1,40 +0,0 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.tools.model.updater.impl
import org.jetbrains.tools.model.updater.impl.JpsLibrary.LibraryType.Repository.Companion.ArtifactVerificationEntry
import java.net.URL
import java.security.MessageDigest
fun computeSha256Checksums(classesRoots: List<JpsUrl>, remoteRepository: JpsRemoteRepository): List<ArtifactVerificationEntry> {
return classesRoots.map {
require(it.path is JpsPath.MavenRepository)
val sha256checksum = downloadFileAndComputeSha256("${remoteRepository.url}/${it.path.relativePath}")
val fileUrl = JpsUrl.File(it.path)
ArtifactVerificationEntry(fileUrl.url, sha256checksum)
}
}
private fun downloadFileAndComputeSha256(url: String): String {
println("Computing SHA256 checksum for '$url'")
val digest = MessageDigest.getInstance("SHA-256")
URL(url).openStream().use { inputStream ->
val buf = ByteArray(65536)
while (true) {
val count = inputStream.read(buf)
if (count <= 0) break
digest.update(buf, 0, count)
}
}
return byteArrayToHexString(digest.digest())
}
private fun byteArrayToHexString(bytes: ByteArray): String {
val builder = StringBuilder(bytes.size * 2)
for (b in bytes) {
builder.append(String.format("%02x", b))
}
return builder.toString()
}