-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathGatherLicenses.scala
338 lines (307 loc) · 11.7 KB
/
GatherLicenses.scala
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import sbt.Keys._
import sbt._
import complete.DefaultParsers._
import org.apache.ivy.core.resolve.IvyNode
import src.main.scala.licenses.backend.{
CombinedBackend,
GatherCopyrights,
GatherNotices,
GithubHeuristic
}
import src.main.scala.licenses.frontend.SbtLicenses
import src.main.scala.licenses.report._
import src.main.scala.licenses.{
DependencySummary,
DistributionDescription,
ReviewedSummary
}
import scala.collection.JavaConverters._
import scala.sys.process._
/** The task and configuration for automatically gathering license information.
*/
object GatherLicenses {
val distributions = taskKey[Seq[DistributionDescription]](
"Defines descriptions of distributions."
)
val configurationRoot = settingKey[File]("Path to review configuration.")
val licenseConfigurations =
settingKey[Set[String]]("The ivy configurations we consider in the review.")
private val stateFileName = "report-state"
/** The task that performs the whole license gathering process. */
def run = Def.inputTask {
val names: Seq[String] = spaceDelimited("<arg>").parsed
val log = state.value.log
val targetRoot = target.value
log.info(
"Gathering license files and copyright notices. " +
"This task may take a long time."
)
val configRoot = configurationRoot.value
val namesToProcess: Set[String] = names.toSet
val knownDistributions = distributions.value
val distributionsToProcess =
if (names.isEmpty) knownDistributions
else
knownDistributions.filter(distribution =>
namesToProcess.contains(distribution.artifactName)
)
val unrecognizedDistributions =
namesToProcess -- distributionsToProcess.map(_.artifactName).toSet
if (unrecognizedDistributions.nonEmpty) {
val message =
s"Unrecognized distribution names: $unrecognizedDistributions."
log.error(message)
throw new IllegalArgumentException(message)
}
log.info(
s"Found following distributions to process: ${distributionsToProcess.map(_.artifactName)}"
)
val reports = distributionsToProcess.map { distribution =>
log.info(s"Processing the ${distribution.artifactName} distribution")
val projectNames = distribution.sbtComponents.map(_.name)
log.info(
s"It consists of the following sbt project roots:" +
s" ${projectNames.mkString(", ")}"
)
val (sbtInfo, sbtDiagnostics) =
SbtLicenses.analyze(distribution.sbtComponents, log)
val allInfo = sbtInfo // TODO [RW] add Rust frontend result here (#1187)
log.info(s"${allInfo.size} unique dependencies discovered")
val defaultBackend = CombinedBackend(GatherNotices, GatherCopyrights)
val processed = allInfo.map { dependency =>
log.debug(
s"Processing ${dependency.moduleInfo} (${dependency.license}) -> " +
s"${dependency.url}"
)
val defaultAttachments = defaultBackend.run(dependency.sources)
val WithDiagnostics(attachments, attachmentDiagnostics) =
if (defaultAttachments.nonEmpty) WithDiagnostics(defaultAttachments)
else GithubHeuristic(dependency, log).run()
(dependency, attachments, attachmentDiagnostics)
}
val forSummary = processed.map(t => (t._1, t._2))
val processingDiagnostics = processed.flatMap(_._3)
val summary = DependencySummary(forSummary)
val distributionRoot = configRoot / distribution.artifactName
val WithDiagnostics(processedSummary, summaryDiagnostics) =
Review(distributionRoot, summary).run()
val allDiagnostics =
sbtDiagnostics ++ processingDiagnostics ++ summaryDiagnostics
val reportDestination =
targetRoot / s"${distribution.artifactName}-report.html"
val (warnings: Seq[Diagnostic.Warning], errors: Seq[Diagnostic.Error]) =
Diagnostic.partition(allDiagnostics)
if (warnings.nonEmpty) {
log.warn(s"Found ${warnings.size} non-fatal warnings in the report:")
warnings.foreach(notice => log.warn(notice.message))
}
if (errors.isEmpty) {
log.info("No fatal errors found in the report.")
} else {
log.error(s"Found ${errors.size} fatal errors in the report:")
errors.foreach(problem => log.error(problem.message))
}
Report.writeHTML(
distribution,
processedSummary,
allDiagnostics,
reportDestination
)
log.info(
s"Written the report for the ${distribution.artifactName} to " +
s"`$reportDestination`."
)
val packagePath = distribution.packageDestination
PackageNotices.create(distribution, processedSummary, packagePath)
ReportState.write(
distributionRoot / stateFileName,
distribution,
errors.size
)
log.info(s"Re-generated distribution notices at `$packagePath`.")
if (errors.nonEmpty) {
log.warn(
"The distribution notices were regenerated, but there are " +
"not-reviewed issues within the report. The notices are probably " +
"incomplete."
)
}
(distribution, processedSummary)
}
log.warn(
"Finished gathering license information. " +
"This is an automated process, make sure that its output is reviewed " +
"by a human to ensure that all licensing requirements are met."
)
reports
}
private def verifyReportStatus(
log: Logger,
targetRoot: File,
distributionDescription: DistributionDescription,
distributionConfig: File
): Unit = {
val name = distributionDescription.artifactName
def warnAndThrow(exceptionMessage: String): Nothing = {
log.error(exceptionMessage)
log.warn(
"Please make sure to run `enso / gatherLicenses` " +
s"and review the reports generated at $targetRoot, " +
"ensuring that the legal review is complete and there are no warnings."
)
log.warn(
"See docs/distribution/licenses.md#review-process for a more detailed " +
"explanation."
)
throw LegalReviewException(exceptionMessage)
}
ReportState.read(distributionConfig / stateFileName, log) match {
case Some(reviewState) =>
val currentInputHash =
ReportState.computeInputHash(distributionDescription)
if (currentInputHash != reviewState.inputHash) {
log.info("Input hash computed from build.sbt: " + currentInputHash)
log.info("Input hash stored in metadata: " + reviewState.inputHash)
warnAndThrow(
s"Report for the $name is not up to date - " +
s"it seems that some dependencies were added or removed."
)
}
if (reviewState.warningsCount > 0) {
warnAndThrow(
s"Report for the $name has ${reviewState.warningsCount} warnings."
)
}
log.info(s"Report for $name is reviewed.")
case None =>
warnAndThrow(s"Report for $name has not been generated.")
}
}
private def verifyPackage(
log: Logger,
distributionConfig: File,
packageDestination: File
): Unit = {
val reportState = ReportState
.read(distributionConfig / stateFileName, log)
.getOrElse(
throw LegalReviewException(
s"Report at $distributionConfig is not available. " +
s"Make sure to run `enso/gatherLicenses` or `openLegalReviewReport`."
)
)
val currentOutputHash = ReportState.computeOutputHash(packageDestination)
if (currentOutputHash != reportState.outputHash) {
log.info("Output hash computed from build.sbt: " + currentOutputHash)
log.info("Output hash stored in metadata: " + reportState.outputHash)
log.error(
s"Generated package at $packageDestination seems to be not up-to-date."
)
log.warn(
"Re-run `enso/gatherLicenses` and make sure that all files " +
"from the notice package are committed, no unexpected files " +
"have been added and the package is created in a consistent way."
)
throw LegalReviewException(
s"Package $packageDestination has different content than expected."
)
} else {
log.info(s"Package $packageDestination is up-to-date.")
}
}
/** The task that verifies if the report has been generated and is up-to-date.
*/
lazy val verifyReports = Def.task {
val configRoot = configurationRoot.value
val log = streams.value.log
val targetRoot = target.value
for (distribution <- distributions.value) {
val distributionConfig = configRoot / distribution.artifactName
verifyReportStatus(
log = log,
targetRoot = targetRoot,
distributionDescription = distribution,
distributionConfig = distributionConfig
)
verifyPackage(
log = log,
distributionConfig = distributionConfig,
packageDestination = distribution.packageDestination
)
}
}
/** A task that verifies if contents of the provided package directory are
* up-to-date with the review state.
*
* It takes two arguments:
* - an artifact name identifying the distribution
* - a path to the generated packages.
*/
lazy val verifyGeneratedPackage = Def.inputTask {
val configRoot = configurationRoot.value
val log = streams.value.log
val args: Seq[String] = spaceDelimited("<arg>").parsed
val (distributionName, packagePathString) = args match {
case Seq(distribution, path) => (distribution, path)
case _ =>
throw new IllegalArgumentException(
"The task expects exactly 2 arguments."
)
}
val packageDestination = file(packagePathString)
verifyPackage(
log = log,
distributionConfig = configRoot / distributionName,
packageDestination = packageDestination
)
}
case class LegalReviewException(string: String)
extends RuntimeException(string)
/** Launches a server that allows to easily review the generated report.
*
* Requires `npm` to be on the system PATH.
*/
def runReportServer(): Unit = {
Seq("npm", "install").!
Process(Seq("npm", "start"), file("tools/legal-review-helper"))
.run(connectInput = true)
.exitValue()
}
/** A task that prints which sub-projects use a dependency and what
* dependencies use it (so that one can track where dependencies come from).
*/
lazy val analyzeDependency = Def.inputTask {
val args: Seq[String] = spaceDelimited("<arg>").parsed
val evaluatedDistributions = distributions.value
val log = streams.value.log
for (arg <- args) {
for (distribution <- evaluatedDistributions) {
for (sbtComponent <- distribution.sbtComponents) {
val ivyDeps =
sbtComponent.licenseReport.orig.getDependencies.asScala
.map(_.asInstanceOf[IvyNode])
for (dep <- sbtComponent.licenseReport.licenses) {
if (dep.module.name.contains(arg)) {
val module = dep.module
log.info(
s"${distribution.artifactName} distribution, project ${sbtComponent.name} " +
s"contains $module"
)
val node = ivyDeps.find(n =>
SbtLicenses.safeModuleInfo(n) == Some(dep.module)
)
node match {
case None =>
log.warn(s"IvyNode for $module not found.")
case Some(ivyNode) =>
val callers =
ivyNode.getAllCallers.toSeq.map(_.toString).distinct
log.info(s"Callers: $callers")
}
}
}
}
}
}
}
}