1 /* <lambda>null2 * Copyright 2019 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.jetbrains.dokka.Utilities 18 19 import okhttp3.OkHttpClient 20 import okhttp3.Request 21 import java.io.File 22 import java.io.FileOutputStream 23 24 object DownloadSamples { 25 26 /** HTTP Client to make requests **/ 27 val client = OkHttpClient() 28 29 /** 30 * Function that downloads samples based on the directory structure described in hashmap 31 */ 32 fun downloadSamples(): Boolean { 33 34 //loop through each directory of AOSP code in SamplesPathsToURLs.kt 35 filepathsToUrls.forEach { (filepath, url) -> 36 37 //build request using each URL 38 val request = Request.Builder() 39 .url(url) 40 .build() 41 42 val response = client.newCall(request).execute() 43 44 if (response.isSuccessful) { 45 46 //save .tar.gz file to filepath designated by map 47 val currentFile = File(filepath) 48 currentFile.mkdirs() 49 50 val fos = FileOutputStream("$filepath.tar.gz") 51 fos.write(response.body?.bytes()) 52 fos.close() 53 54 //Unzip, Untar, and delete compressed file after 55 extractFiles(filepath) 56 57 } else { 58 println("Error Downloading Samples: $response") 59 return false 60 } 61 } 62 63 println("Successfully completed download of samples.") 64 return true 65 66 } 67 68 /** 69 * Execute bash commands to extract file, then delete archive file 70 */ 71 private fun extractFiles(pathToFile: String) { 72 73 ProcessBuilder() 74 .command("tar","-zxf", "$pathToFile.tar.gz", "-C", pathToFile) 75 .redirectError(ProcessBuilder.Redirect.INHERIT) 76 .redirectOutput(ProcessBuilder.Redirect.INHERIT) 77 .start() 78 .waitFor() 79 80 ProcessBuilder() 81 .command("rm", "$pathToFile.tar.gz") 82 .redirectError(ProcessBuilder.Redirect.INHERIT) 83 .redirectOutput(ProcessBuilder.Redirect.INHERIT) 84 .start() 85 .waitFor() 86 } 87 88 }