forked from oss-review-toolkit/ort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAskalono.kt
168 lines (137 loc) · 6.06 KB
/
Askalono.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (C) 2017-2019 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package org.ossreviewtoolkit.scanner.scanners
import com.fasterxml.jackson.databind.JsonNode
import java.io.File
import java.io.IOException
import java.net.HttpURLConnection
import java.time.Instant
import kotlin.io.path.createTempDirectory
import okhttp3.Request
import org.ossreviewtoolkit.model.EMPTY_JSON_NODE
import org.ossreviewtoolkit.model.LicenseFinding
import org.ossreviewtoolkit.model.Provenance
import org.ossreviewtoolkit.model.ScanResult
import org.ossreviewtoolkit.model.ScanSummary
import org.ossreviewtoolkit.model.TextLocation
import org.ossreviewtoolkit.model.config.ScannerConfiguration
import org.ossreviewtoolkit.model.yamlMapper
import org.ossreviewtoolkit.scanner.AbstractScannerFactory
import org.ossreviewtoolkit.scanner.LocalScanner
import org.ossreviewtoolkit.scanner.ScanException
import org.ossreviewtoolkit.spdx.calculatePackageVerificationCode
import org.ossreviewtoolkit.utils.ORT_NAME
import org.ossreviewtoolkit.utils.OkHttpClientHelper
import org.ossreviewtoolkit.utils.Os
import org.ossreviewtoolkit.utils.ProcessCapture
import org.ossreviewtoolkit.utils.log
import org.ossreviewtoolkit.utils.unpackZip
class Askalono(name: String, config: ScannerConfiguration) : LocalScanner(name, config) {
class Factory : AbstractScannerFactory<Askalono>("Askalono") {
override fun create(config: ScannerConfiguration) = Askalono(scannerName, config)
}
override val expectedVersion = "0.4.3"
override val configuration = ""
override val resultFileExt = "txt"
override fun command(workingDir: File?) =
listOfNotNull(workingDir, if (Os.isWindows) "askalono.exe" else "askalono").joinToString(File.separator)
override fun transformVersion(output: String) =
// "askalono --version" returns a string like "askalono 0.2.0-beta.1", so simply remove the prefix.
output.removePrefix("askalono ")
override fun bootstrap(): File {
val platform = when {
Os.isLinux -> "Linux"
Os.isMac -> "macOS"
Os.isWindows -> "Windows"
else -> throw IllegalArgumentException("Unsupported operating system.")
}
val archive = "askalono-$platform.zip"
val url = "https://github.com/amzn/askalono/releases/download/$expectedVersion/$archive"
log.info { "Downloading $scannerName from $url... " }
val request = Request.Builder().get().url(url).build()
return OkHttpClientHelper.execute(request).use { response ->
val body = response.body
if (response.code != HttpURLConnection.HTTP_OK || body == null) {
throw IOException("Failed to download $scannerName from $url.")
}
if (response.cacheResponse != null) {
log.info { "Retrieved $scannerName from local cache." }
}
val unpackDir = createTempDirectory("$ORT_NAME-$scannerName-$expectedVersion").toFile().apply {
deleteOnExit()
}
log.info { "Unpacking '$archive' to '$unpackDir'... " }
body.bytes().unpackZip(unpackDir)
unpackDir
}
}
override fun scanPathInternal(path: File, resultsFile: File): ScanResult {
val startTime = Instant.now()
val process = ProcessCapture(
scannerPath.absolutePath,
"crawl", path.absolutePath
)
val endTime = Instant.now()
if (process.stderr.isNotBlank()) {
log.debug { process.stderr }
}
with(process) {
if (isSuccess) {
stdoutFile.copyTo(resultsFile)
val result = getRawResult(resultsFile)
val summary = generateSummary(startTime, endTime, path, result)
return ScanResult(Provenance(), details, summary)
} else {
throw ScanException(errorMessage)
}
}
}
override fun getRawResult(resultsFile: File): JsonNode {
if (!resultsFile.isFile || resultsFile.length() == 0L) return EMPTY_JSON_NODE
val yamlNodes = resultsFile.readLines().chunked(3) { (path, license, score) ->
val licenseNoOriginalText = license.substringBeforeLast(" (original text)")
val yamlString = listOf("Path: $path", licenseNoOriginalText, score).joinToString("\n")
yamlMapper.readTree(yamlString)
}
return yamlMapper.createArrayNode().apply { addAll(yamlNodes) }
}
private fun generateSummary(startTime: Instant, endTime: Instant, scanPath: File, result: JsonNode): ScanSummary {
val licenseFindings = sortedSetOf<LicenseFinding>()
result.mapTo(licenseFindings) {
val filePath = File(it["Path"].textValue())
LicenseFinding(
license = getSpdxLicenseIdString(it["License"].textValue()),
location = TextLocation(
// Turn absolute paths in the native result into relative paths to not expose any information.
relativizePath(scanPath, filePath),
TextLocation.UNKNOWN_LINE
)
)
}
return ScanSummary(
startTime = startTime,
endTime = endTime,
fileCount = result.size(),
packageVerificationCode = calculatePackageVerificationCode(scanPath),
licenseFindings = licenseFindings,
copyrightFindings = sortedSetOf(),
issues = mutableListOf()
)
}
}