Add back support for some checksum format

This commit is contained in:
Alexandre Archambault 2016-03-06 14:56:12 +01:00
parent af5275c014
commit dee115c1b6
2 changed files with 46 additions and 21 deletions

View File

@ -444,18 +444,28 @@ object Cache {
Nondeterminism[Task].gather(tasks)
}
def parseChecksum(content: String): Option[BigInteger] = {
val lines = content
.linesIterator
.toVector
parseChecksumLine(lines) orElse parseChecksumAlternative(lines)
}
// matches md5 or sha1
private val checksumPattern = Pattern.compile("^[0-9a-f]{32}([0-9a-f]{8})?")
def parseChecksum(content: String): Option[BigInteger] =
content
.linesIterator
.toStream
.map(_.toLowerCase.replaceAll("\\s", ""))
.collectFirst {
case rawSum if checksumPattern.matcher(rawSum).matches() =>
new BigInteger(rawSum, 16)
}
private def findChecksum(elems: Seq[String]): Option[BigInteger] =
elems.collectFirst {
case rawSum if checksumPattern.matcher(rawSum).matches() =>
new BigInteger(rawSum, 16)
}
private def parseChecksumLine(lines: Seq[String]): Option[BigInteger] =
findChecksum(lines.map(_.toLowerCase.replaceAll("\\s", "")))
private def parseChecksumAlternative(lines: Seq[String]): Option[BigInteger] =
findChecksum(lines.flatMap(_.toLowerCase.split("\\s+")))
def validateChecksum(
artifact: Artifact,

View File

@ -14,22 +14,37 @@ object ChecksumTests extends TestSuite {
val tests = TestSuite {
'parse - {
// https://repo1.maven.org/maven2/org/apache/spark/spark-core_2.11/1.2.0/spark-core_2.11-1.2.0.pom.sha1
// as of 2016-03-02
val junkSha1 =
"./spark-core_2.11/1.2.0/spark-core_2.11-1.2.0.pom:\n" +
"5630 42A5 4B97 E31A F452 9EA0 DB79 BA2C 4C2B B6CC"
val cleanSha1 = "563042a54b97e31af4529ea0db79ba2c4c2bb6cc"
def sha1ParseTest(clean: String, others: String*): Unit = {
val expected = Some(new BigInteger(clean, 16))
val checksum = Some(new BigInteger(cleanSha1, 16))
'junk - {
assert(Cache.parseChecksum(junkSha1) == checksum)
assert(Cache.parseChecksum(clean) == expected)
for (other <- others)
assert(Cache.parseChecksum(other) == expected)
}
'clean - {
assert(Cache.parseChecksum(cleanSha1) == checksum)
* - {
// https://repo1.maven.org/maven2/org/apache/spark/spark-core_2.11/1.2.0/spark-core_2.11-1.2.0.pom.sha1
// as of 2016-03-02
val junkSha1 =
"./spark-core_2.11/1.2.0/spark-core_2.11-1.2.0.pom:\n" +
"5630 42A5 4B97 E31A F452 9EA0 DB79 BA2C 4C2B B6CC"
val cleanSha1 = "563042a54b97e31af4529ea0db79ba2c4c2bb6cc"
sha1ParseTest(cleanSha1, junkSha1)
}
* - {
// https://repo1.maven.org/maven2/org/json/json/20080701/json-20080701.pom.sha1
// as of 2016-03-05
val dirtySha1 =
"4bf5daa95eb5c12d753a359a3e00621fdc73d187 " + // no CR here
"/home/maven/repository-staging/to-ibiblio/maven2/org/json/json/20080701/json-20080701.pom"
val cleanSha1 = "4bf5daa95eb5c12d753a359a3e00621fdc73d187"
sha1ParseTest(cleanSha1, dirtySha1)
}
}