|
| 1 | +package org.jetbrains.kotlinx.jupyter.common |
| 2 | + |
| 3 | +import kotlinx.serialization.json.JsonObject |
| 4 | +import kotlinx.serialization.json.JsonPrimitive |
| 5 | +import kotlinx.serialization.json.jsonPrimitive |
| 6 | +import org.slf4j.Logger |
| 7 | +import org.slf4j.LoggerFactory |
| 8 | +import java.io.File |
| 9 | + |
| 10 | +fun interface ExceptionsHandler { |
| 11 | + fun handle(logger: Logger, message: String, exception: Throwable) |
| 12 | + |
| 13 | + object DEFAULT : ExceptionsHandler { |
| 14 | + override fun handle(logger: Logger, message: String, exception: Throwable) { |
| 15 | + logger.error(message) |
| 16 | + throw exception |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +class LibraryDescriptorsManager private constructor( |
| 22 | + user: String, |
| 23 | + repo: String, |
| 24 | + private val remotePath: String, |
| 25 | + localPath: String, |
| 26 | + private val homePath: String, |
| 27 | + userPath: String, |
| 28 | + private val exceptionsHandler: ExceptionsHandler, |
| 29 | + userSettingsDir: File, |
| 30 | + private val logger: Logger, |
| 31 | +) { |
| 32 | + private val apiPrefix = "https://$GITHUB_API_HOST/repos/$user/$repo" |
| 33 | + val userLibrariesDir = userSettingsDir.resolve(userPath) |
| 34 | + val userCacheDir = userSettingsDir.resolve("cache") |
| 35 | + val localLibrariesDir = File(localPath) |
| 36 | + val defaultBranch = "master" |
| 37 | + val latestCommitOnDefaultBranch by lazy { |
| 38 | + getLatestCommitToLibraries(defaultBranch)!!.first |
| 39 | + } |
| 40 | + |
| 41 | + fun homeLibrariesDir(homeDir: File? = null) = (homeDir ?: File("")).resolve(homePath) |
| 42 | + |
| 43 | + val localPropertiesFile = localLibrariesDir.resolve(PROPERTIES_FILE) |
| 44 | + val commitHashFile by lazy { |
| 45 | + localLibrariesDir.resolve(COMMIT_HASH_FILE).also { file -> |
| 46 | + if (!file.exists()) { |
| 47 | + file.createDirsAndWrite() |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + fun descriptorFileName(name: String) = "$name.$DESCRIPTOR_EXTENSION" |
| 53 | + |
| 54 | + fun isLibraryDescriptor(file: File): Boolean { |
| 55 | + return file.isFile && file.name.endsWith(".$DESCRIPTOR_EXTENSION") |
| 56 | + } |
| 57 | + |
| 58 | + fun getLatestCommitToLibraries(ref: String, sinceTimestamp: String? = null): Pair<String, String>? { |
| 59 | + return catchAll { |
| 60 | + var url = "$apiPrefix/commits?path=$remotePath&sha=$ref" |
| 61 | + if (sinceTimestamp != null) { |
| 62 | + url += "&since=$sinceTimestamp" |
| 63 | + } |
| 64 | + logger.info("Checking for new commits to library descriptors at $url") |
| 65 | + val arr = getHttp(url).jsonArray |
| 66 | + if (arr.isEmpty()) { |
| 67 | + if (sinceTimestamp != null) { |
| 68 | + getLatestCommitToLibraries(ref, null) |
| 69 | + } else { |
| 70 | + logger.info("Didn't find any commits to libraries at $url") |
| 71 | + null |
| 72 | + } |
| 73 | + } else { |
| 74 | + val commit = arr[0] as JsonObject |
| 75 | + val sha = (commit["sha"] as JsonPrimitive).content |
| 76 | + val timestamp = (((commit["commit"] as JsonObject)["committer"] as JsonObject)["date"] as JsonPrimitive).content |
| 77 | + sha to timestamp |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + fun downloadLibraryDescriptor(ref: String, name: String): String { |
| 83 | + val url = "$apiPrefix/contents/$remotePath/$name.$DESCRIPTOR_EXTENSION?ref=$ref" |
| 84 | + logger.info("Requesting library descriptor at $url") |
| 85 | + return downloadSingleFile(url) |
| 86 | + } |
| 87 | + |
| 88 | + fun checkRefExistence(ref: String): Boolean { |
| 89 | + val response = getHttp("$apiPrefix/contents/$remotePath?ref=$ref") |
| 90 | + return response.status.successful |
| 91 | + } |
| 92 | + |
| 93 | + fun checkIfRefUpToDate(remoteRef: String): Boolean { |
| 94 | + if (!commitHashFile.exists()) return false |
| 95 | + val localRef = commitHashFile.readText() |
| 96 | + return localRef == remoteRef |
| 97 | + } |
| 98 | + |
| 99 | + fun downloadLibraries(ref: String) { |
| 100 | + localLibrariesDir.mkdirs() |
| 101 | + |
| 102 | + val url = "$apiPrefix/contents/$remotePath?ref=$ref" |
| 103 | + logger.info("Requesting library descriptors at $url") |
| 104 | + val response = getHttp(url).jsonArray |
| 105 | + |
| 106 | + for (item in response) { |
| 107 | + item as JsonObject |
| 108 | + if (item["type"]?.jsonPrimitive?.content != "file") continue |
| 109 | + |
| 110 | + val fileName = item["name"]!!.jsonPrimitive.content |
| 111 | + if (!fileName.endsWith(".$DESCRIPTOR_EXTENSION")) continue |
| 112 | + |
| 113 | + val downloadUrl = item["download_url"]!!.jsonPrimitive.content |
| 114 | + val descriptorResponse = getHttp(downloadUrl) |
| 115 | + |
| 116 | + val descriptorText = descriptorResponse.text |
| 117 | + val file = localLibrariesDir.resolve(fileName) |
| 118 | + file.writeText(descriptorText) |
| 119 | + } |
| 120 | + |
| 121 | + saveLocalRef(ref) |
| 122 | + } |
| 123 | + |
| 124 | + fun downloadLatestPropertiesFile() { |
| 125 | + val ref = latestCommitOnDefaultBranch |
| 126 | + val url = "$apiPrefix/contents/$remotePath/$PROPERTIES_FILE?ref=$ref" |
| 127 | + logger.info("Requesting $PROPERTIES_FILE file at $url") |
| 128 | + val text = downloadSingleFile(url) |
| 129 | + localPropertiesFile.createDirsAndWrite(text) |
| 130 | + } |
| 131 | + |
| 132 | + private fun downloadSingleFile(contentsApiUrl: String): String { |
| 133 | + val response = getHttp(contentsApiUrl).jsonObject |
| 134 | + val downloadUrl = response["download_url"]!!.jsonPrimitive.content |
| 135 | + val res = getHttp(downloadUrl) |
| 136 | + return res.text |
| 137 | + } |
| 138 | + |
| 139 | + private fun saveLocalRef(ref: String) { |
| 140 | + commitHashFile.createDirsAndWrite(ref) |
| 141 | + } |
| 142 | + |
| 143 | + private fun File.createDirsAndWrite(text: String = "") { |
| 144 | + parentFile.mkdirs() |
| 145 | + writeText(text) |
| 146 | + } |
| 147 | + |
| 148 | + private fun <T> catchAll(message: String = "", body: () -> T): T? = try { |
| 149 | + body() |
| 150 | + } catch (e: Throwable) { |
| 151 | + exceptionsHandler.handle(logger, message, e) |
| 152 | + null |
| 153 | + } |
| 154 | + |
| 155 | + companion object { |
| 156 | + private const val GITHUB_API_HOST = "api.github.com" |
| 157 | + private const val DESCRIPTOR_EXTENSION = "json" |
| 158 | + private const val PROPERTIES_FILE = ".properties" |
| 159 | + private const val COMMIT_HASH_FILE = "commit_sha" |
| 160 | + |
| 161 | + fun getInstance( |
| 162 | + logger: Logger = LoggerFactory.getLogger(LibraryDescriptorsManager::class.java), |
| 163 | + exceptionsHandler: ExceptionsHandler = ExceptionsHandler.DEFAULT, |
| 164 | + ): LibraryDescriptorsManager { |
| 165 | + return LibraryDescriptorsManager( |
| 166 | + "Kotlin", |
| 167 | + "kotlin-jupyter-libraries", |
| 168 | + "", |
| 169 | + "libraries", |
| 170 | + "libraries", |
| 171 | + "libraries", |
| 172 | + exceptionsHandler, |
| 173 | + File(System.getProperty("user.home")).resolve(".jupyter_kotlin"), |
| 174 | + logger |
| 175 | + ) |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments