From ee8bd9a078b37dbfd79057e6337e1039f5f0145a Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Mon, 15 Apr 2024 18:07:09 +0200 Subject: [PATCH 1/4] fix(GenerateScopeExcludesCommand): Ensure that an input file is readable Signed-off-by: Sebastian Schuberth --- .../kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt b/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt index a74470ff5a45a..fc0f461f6ce72 100644 --- a/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt +++ b/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt @@ -45,7 +45,7 @@ internal class GenerateScopeExcludesCommand : CliktCommand( "--ort-file", "-i", help = "The ORT file to generate scope excludes for." ).convert { it.expandTilde() } - .file(mustExist = true, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = false) + .file(mustExist = true, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = true) .convert { it.absoluteFile.normalize() } .required() From 4bf0067525c55529cfc0d7b740ed850d11a201e5 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Mon, 15 Apr 2024 18:10:01 +0200 Subject: [PATCH 2/4] feat(GenerateScopeExcludesCommand): Do not require the repo config to exist Signed-off-by: Sebastian Schuberth --- .../repoconfig/GenerateScopeExcludesCommand.kt | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt b/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt index fc0f461f6ce72..afc79dcfb4671 100644 --- a/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt +++ b/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt @@ -53,7 +53,7 @@ internal class GenerateScopeExcludesCommand : CliktCommand( "--repository-configuration-file", help = "The repository configuration file to write the generated scope excludes to." ).convert { it.expandTilde() } - .file(mustExist = true, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = false) + .file(mustExist = false, canBeFile = true, canBeDir = false, mustBeWritable = false, mustBeReadable = false) .convert { it.absoluteFile.normalize() } .required() @@ -61,8 +61,13 @@ internal class GenerateScopeExcludesCommand : CliktCommand( val ortResult = readOrtResult(ortFile) val scopeExcludes = ortResult.generateScopeExcludes() - repositoryConfigurationFile - .readValue() + val repositoryConfiguration = if (repositoryConfigurationFile.isFile) { + repositoryConfigurationFile.readValue() + } else { + RepositoryConfiguration() + } + + repositoryConfiguration .replaceScopeExcludes(scopeExcludes) .sortScopeExcludes() .write(repositoryConfigurationFile) From be7aebe046bf172310b2ae683c2317a40d9184c5 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Mon, 15 Apr 2024 18:17:33 +0200 Subject: [PATCH 3/4] chore(GenerateScopeExcludesCommand): Log the scopes at info level Signed-off-by: Sebastian Schuberth --- .../commands/repoconfig/GenerateScopeExcludesCommand.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt b/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt index afc79dcfb4671..24ae990d65bba 100644 --- a/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt +++ b/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt @@ -25,6 +25,8 @@ import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.options.required import com.github.ajalt.clikt.parameters.types.file +import org.apache.logging.log4j.kotlin.logger + import org.ossreviewtoolkit.helper.utils.minimize import org.ossreviewtoolkit.helper.utils.readOrtResult import org.ossreviewtoolkit.helper.utils.replaceScopeExcludes @@ -79,6 +81,8 @@ private fun OrtResult.generateScopeExcludes(): List { dependencyNavigator.scopeNames(project) } + logger.info { "Found the following scopes: ${projectScopes.joinToString()}" } + return getProjects().flatMap { project -> getScopeExcludesForPackageManager(project.id.type) }.minimize(projectScopes) From 4d0b3ca5bef24fc07e5fcd83bab9f7a0f1d9ab3f Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Mon, 15 Apr 2024 18:18:38 +0200 Subject: [PATCH 4/4] perf(GenerateScopeExcludesCommand): Deduplicate scopes into sets This also avoids logging the same scope multiple times. Signed-off-by: Sebastian Schuberth --- .../kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt | 2 +- .../src/main/kotlin/commands/repoconfig/RemoveEntriesCommand.kt | 2 +- helper-cli/src/main/kotlin/utils/Extensions.kt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt b/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt index 24ae990d65bba..510c692bc5e5f 100644 --- a/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt +++ b/helper-cli/src/main/kotlin/commands/repoconfig/GenerateScopeExcludesCommand.kt @@ -77,7 +77,7 @@ internal class GenerateScopeExcludesCommand : CliktCommand( } private fun OrtResult.generateScopeExcludes(): List { - val projectScopes = getProjects().flatMap { project -> + val projectScopes = getProjects().flatMapTo(mutableSetOf()) { project -> dependencyNavigator.scopeNames(project) } diff --git a/helper-cli/src/main/kotlin/commands/repoconfig/RemoveEntriesCommand.kt b/helper-cli/src/main/kotlin/commands/repoconfig/RemoveEntriesCommand.kt index f1b01fbc7f703..5254b03861b34 100644 --- a/helper-cli/src/main/kotlin/commands/repoconfig/RemoveEntriesCommand.kt +++ b/helper-cli/src/main/kotlin/commands/repoconfig/RemoveEntriesCommand.kt @@ -94,7 +94,7 @@ internal class RemoveEntriesCommand : CliktCommand( val scopeExcludes = ortResult .getProjects() - .flatMap { project -> project.scopes.map { scope -> scope.name } } + .flatMapTo(mutableSetOf()) { project -> project.scopes.map { scope -> scope.name } } .let { projectScopes -> ortResult.getExcludes().scopes.minimize(projectScopes) } val licenseFindings = ortResult.getProjectLicenseFindings() diff --git a/helper-cli/src/main/kotlin/utils/Extensions.kt b/helper-cli/src/main/kotlin/utils/Extensions.kt index 006784703bd1f..1887319336f86 100644 --- a/helper-cli/src/main/kotlin/utils/Extensions.kt +++ b/helper-cli/src/main/kotlin/utils/Extensions.kt @@ -79,7 +79,7 @@ import org.ossreviewtoolkit.utils.spdx.SpdxSingleLicenseExpression * Return an approximated minimal sublist of [this] so that the result still matches the exact same entries of the given * [projectScopes]. */ -internal fun List.minimize(projectScopes: List): List { +internal fun List.minimize(projectScopes: Set): List { val scopeExcludes = associateWith { scopeExclude -> projectScopes.filter { scopeExclude.matches(it) }.toSet() }