tools: Report dependency downloading progress
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
package org.jetbrains.kotlin.konan
|
||||
|
||||
import java.io.File
|
||||
import java.io.RandomAccessFile
|
||||
import java.io.*
|
||||
import java.net.URL
|
||||
import java.nio.file.*
|
||||
import java.util.*
|
||||
import java.util.Properties
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
// TODO: Try to use some dependency management system (Ivy?)
|
||||
// TODO: Show % and size during downloading
|
||||
class DependencyDownloader(dependenciesRoot: File, val properties: Properties, val dependencies: List<String>) {
|
||||
|
||||
val dependenciesRoot = dependenciesRoot.apply { mkdirs() }
|
||||
@@ -63,16 +61,65 @@ class DependencyDownloader(dependenciesRoot: File, val properties: Properties, v
|
||||
}
|
||||
}
|
||||
|
||||
private val Long.humanReadable: String
|
||||
get() {
|
||||
if (this < 0) {
|
||||
return "-"
|
||||
}
|
||||
if (this < 1024) {
|
||||
return "$this bytes"
|
||||
}
|
||||
val exp = (Math.log(this.toDouble()) / Math.log(1024.0)).toInt()
|
||||
val prefix = "kMGTPE"[exp-1]
|
||||
return "%.1f %sB".format(this / Math.pow(1024.0, exp.toDouble()), prefix)
|
||||
}
|
||||
|
||||
private fun updateProgressMsg(url: String, currentBytes: Long, totalBytes: Long) {
|
||||
print("\rDownload dependency: $url (${currentBytes.humanReadable}/${totalBytes.humanReadable}). ")
|
||||
}
|
||||
|
||||
private fun download(dependencyName: String): File {
|
||||
val to = File(dependenciesRoot.canonicalPath, "$dependencyName.tar.gz")
|
||||
if (!to.exists()) {
|
||||
val from = URL("$dependenciesUrl/$dependencyName.tar.gz")
|
||||
println("Download dependency: $dependencyName from $from")
|
||||
from.openStream().use {
|
||||
Files.copy(it, Paths.get(to.toURI()), StandardCopyOption.REPLACE_EXISTING)
|
||||
val outputFile = File(dependenciesRoot.canonicalPath, "$dependencyName.tar.gz")
|
||||
if (!outputFile.exists()) {
|
||||
val url = URL("$dependenciesUrl/$dependencyName.tar.gz")
|
||||
val connection = url.openConnection()
|
||||
val totalBytes = connection.contentLengthLong
|
||||
|
||||
var currentBytes = 0L
|
||||
var done = false
|
||||
var downloadError: Throwable? = null
|
||||
|
||||
thread {
|
||||
try {
|
||||
url.openStream().use { from ->
|
||||
outputFile.outputStream().use { to ->
|
||||
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
|
||||
var read = from.read(buffer)
|
||||
while (read != -1) {
|
||||
to.write(buffer, 0, read)
|
||||
currentBytes += read
|
||||
read = from.read(buffer)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
downloadError = e
|
||||
}
|
||||
done = true
|
||||
}
|
||||
|
||||
// TODO: Improve console logging
|
||||
var msgLen = 0
|
||||
while (!done) {
|
||||
Thread.sleep(1000) // We can use condition variable here.
|
||||
updateProgressMsg(url.toString(), currentBytes, totalBytes)
|
||||
}
|
||||
println("Done.")
|
||||
if (downloadError != null) {
|
||||
throw RuntimeException("Cannot download dependency: $url", downloadError)
|
||||
}
|
||||
}
|
||||
return to
|
||||
return outputFile
|
||||
}
|
||||
|
||||
fun run() {
|
||||
|
||||
Reference in New Issue
Block a user