Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to deactivate package managers #4962

Merged
merged 3 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ supported:
* [Cargo](https://doc.rust-lang.org/cargo/)
* Scala
* [SBT](https://www.scala-sbt.org/)
* Unmanged
* This is a special "package manager" that mananges all files that cannot be associated to any of the other package
managers.

<a name="analyzer-for-spdx-documents"></a>

Expand Down
6 changes: 4 additions & 2 deletions advisor/src/main/kotlin/Advisor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ class Advisor(
private val LOADER = ServiceLoader.load(AdviceProviderFactory::class.java)!!

/**
* The list of all available [AdviceProvider]s in the classpath.
* The set of all available [advice provider factories][AdviceProviderFactory] in the classpath, sorted by name.
*/
val ALL by lazy { LOADER.iterator().asSequence().toList().sortedBy { it.providerName } }
val ALL: Set<AdviceProviderFactory> by lazy {
LOADER.iterator().asSequence().toSortedSet(compareBy { it.providerName })
}
}

@JvmOverloads
Expand Down
4 changes: 2 additions & 2 deletions analyzer/src/funTest/kotlin/managers/SbtFunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SbtFunTest : StringSpec({
Git().run(projectDir, "clean", "-fd")

val ortResult = Analyzer(DEFAULT_ANALYZER_CONFIGURATION).run {
analyze(findManagedFiles(projectDir, listOf(Sbt.Factory())))
analyze(findManagedFiles(projectDir, setOf(Sbt.Factory())))
}

val expectedResult = readOrtResult(expectedOutputFile)
Expand All @@ -72,7 +72,7 @@ class SbtFunTest : StringSpec({
Git().run(projectDir, "clean", "-fd")

val ortResult = Analyzer(DEFAULT_ANALYZER_CONFIGURATION).run {
analyze(findManagedFiles(projectDir, listOf(Sbt.Factory())))
analyze(findManagedFiles(projectDir, setOf(Sbt.Factory())))
}

val expectedResult = yamlMapper.readValue<OrtResult>(
Expand Down
7 changes: 4 additions & 3 deletions analyzer/src/main/kotlin/Analyzer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Analyzer(private val config: AnalyzerConfiguration, private val labels: Ma
@JvmOverloads
fun findManagedFiles(
absoluteProjectPath: File,
packageManagers: List<PackageManagerFactory> = PackageManager.ALL,
packageManagers: Set<PackageManagerFactory> = PackageManager.ALL,
repositoryConfiguration: RepositoryConfiguration = RepositoryConfiguration()
): ManagedFileInfo {
require(absoluteProjectPath.isAbsolute)
Expand Down Expand Up @@ -81,8 +81,9 @@ class Analyzer(private val config: AnalyzerConfiguration, private val labels: Ma
it.parentFile.absoluteFile == absoluteProjectPath
}

if (factoryFiles.isEmpty() || !hasDefinitionFileInRootDirectory) {
Unmanaged.Factory().create(absoluteProjectPath, config, repositoryConfiguration).let {
val unmanagedFactory = packageManagers.find { it is Unmanaged.Factory }
if (unmanagedFactory != null && (factoryFiles.isEmpty() || !hasDefinitionFileInRootDirectory)) {
unmanagedFactory.create(absoluteProjectPath, config, repositoryConfiguration).let {
managedFiles[it] = listOf(absoluteProjectPath)
}
}
Expand Down
9 changes: 5 additions & 4 deletions analyzer/src/main/kotlin/PackageManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ abstract class PackageManager(
private val LOADER = ServiceLoader.load(PackageManagerFactory::class.java)!!

/**
* The list of all available package managers in the classpath.
* The set of all available [package manager factories][PackageManagerFactory] in the classpath, sorted by name.
*/
val ALL by lazy { LOADER.iterator().asSequence().toList().sortedBy { it.managerName } }
val ALL: Set<PackageManagerFactory> by lazy {
LOADER.iterator().asSequence().toSortedSet(compareBy { it.managerName })
}

private val PACKAGE_MANAGER_DIRECTORIES = listOf(
// Ignore intermediate build system directories.
Expand All @@ -93,8 +95,7 @@ abstract class PackageManager(
* Recursively search the [directory] for files managed by any of the [packageManagers]. The search is performed
* depth-first so that root project files are found before any subproject files for a specific manager.
*/
fun findManagedFiles(directory: File, packageManagers: List<PackageManagerFactory> = ALL):
ManagedProjectFiles {
fun findManagedFiles(directory: File, packageManagers: Set<PackageManagerFactory> = ALL): ManagedProjectFiles {
require(directory.isDirectory) {
"The provided path is not a directory: ${directory.absolutePath}"
}
Expand Down
2 changes: 1 addition & 1 deletion analyzer/src/main/kotlin/managers/Pub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class Pub(
// first to generate the local.properties file by using `flutter pub get`.
val gradleFactory = Gradle.Factory()
val gradleDefinitionFiles =
findManagedFiles(analysisRoot, listOf(gradleFactory)).getOrDefault(gradleFactory, emptyList())
findManagedFiles(analysisRoot, setOf(gradleFactory)).getOrDefault(gradleFactory, emptyList())

if (gradleDefinitionFiles.isNotEmpty()) {
log.info { "Found ${gradleDefinitionFiles.size} ${gradleFactory.managerName} project(s) at:" }
Expand Down
3 changes: 3 additions & 0 deletions analyzer/src/main/kotlin/managers/Unmanaged.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class Unmanaged(
repoConfig: RepositoryConfiguration
) : PackageManager(name, analysisRoot, analyzerConfig, repoConfig) {
class Factory : AbstractPackageManagerFactory<Unmanaged>("Unmanaged") {
// The empty list returned here deliberately causes this special package manager to never be considered in
// PackageManager.findManagedFiles(). Instead, it will only be explicitly instantiated as part of
// Analyzer.findManagedFiles().
override val globsForDefinitionFiles = emptyList<String>()

override fun create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ org.ossreviewtoolkit.analyzer.managers.Pub$Factory
org.ossreviewtoolkit.analyzer.managers.Sbt$Factory
org.ossreviewtoolkit.analyzer.managers.SpdxDocumentFile$Factory
org.ossreviewtoolkit.analyzer.managers.Stack$Factory
org.ossreviewtoolkit.analyzer.managers.Unmanaged$Factory
org.ossreviewtoolkit.analyzer.managers.Yarn$Factory
6 changes: 3 additions & 3 deletions analyzer/src/test/kotlin/PackageManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class PackageManagerTest : WordSpec({

// The test project contains at least one file per package manager, so the result should also contain an
// entry for each package manager.
managedFiles.keys shouldContainExactlyInAnyOrder PackageManager.ALL
managedFiles.keys shouldContainExactlyInAnyOrder PackageManager.ALL.filterNot { it is Unmanaged.Factory }

// The keys in expected and actual maps of definition files are different instances of package manager
// factories. So to compare values use the package manager names as keys instead.
Expand Down Expand Up @@ -132,7 +132,7 @@ class PackageManagerTest : WordSpec({
"find only files for active package managers" {
val managedFiles = PackageManager.findManagedFiles(
projectDir,
listOf(Gradle.Factory(), Pip.Factory(), Sbt.Factory())
setOf(Gradle.Factory(), Pip.Factory(), Sbt.Factory())
)

managedFiles.size shouldBe 3
Expand All @@ -155,7 +155,7 @@ class PackageManagerTest : WordSpec({
}

"find no files if no package managers are active" {
val managedFiles = PackageManager.findManagedFiles(projectDir, emptyList())
val managedFiles = PackageManager.findManagedFiles(projectDir, emptySet())

managedFiles.size shouldBe 0
}
Expand Down
18 changes: 14 additions & 4 deletions cli/src/main/kotlin/commands/AnalyzerCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,23 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende
"times. For example: --label distribution=external"
).associate()

private val packageManagers by option(
private val activatedPackageManagers by option(
"--package-managers", "-m",
help = "The comma-separated package managers to activate, any of ${allPackageManagersByName.keys}."
help = "The comma-separated package managers to activate, any of ${allPackageManagersByName.keys}. Note that " +
"deactivation overrides activation."
).convert { name ->
allPackageManagersByName[name]
?: throw BadParameterValue("Package managers must be one or more of ${allPackageManagersByName.keys}.")
}.split(",").default(PackageManager.ALL)
}.split(",").default(PackageManager.ALL.toList())

private val deactivatedPackageManagers by option(
"--not-package-managers", "-n",
help = "The comma-separated package managers to deactivate, any of ${allPackageManagersByName.keys}. Note " +
"that deactivation overrides activation."
).convert { name ->
allPackageManagersByName[name]
?: throw BadParameterValue("Package managers must be one or more of ${allPackageManagersByName.keys}.")
}.split(",").default(emptyList())

private val globalOptionsForSubcommands by requireObject<GlobalOptions>()

Expand All @@ -174,7 +184,7 @@ class AnalyzerCommand : CliktCommand(name = "analyze", help = "Determine depende
println("The following configuration files and directories are used:")
println("\t" + configurationFiles.joinToString("\n\t"))

val distinctPackageManagers = packageManagers.distinct()
val distinctPackageManagers = activatedPackageManagers.toSet() - deactivatedPackageManagers.toSet()
println("The following package managers are activated:")
println("\t" + distinctPackageManagers.joinToString())

Expand Down
7 changes: 5 additions & 2 deletions downloader/src/main/kotlin/VersionControlSystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ abstract class VersionControlSystem {
private val LOADER = ServiceLoader.load(VersionControlSystem::class.java)!!

/**
* The (prioritized) list of all available Version Control Systems in the classpath.
* The set of all available [Version Control Systems][VersionControlSystem] in the classpath, sorted by
* priority.
*/
val ALL by lazy { LOADER.iterator().asSequence().toList().sortedByDescending { it.priority } }
val ALL: Set<VersionControlSystem> by lazy {
LOADER.iterator().asSequence().toSortedSet(compareByDescending { it.priority })
}

/**
* Return the applicable VCS for the given [vcsType], or null if none is applicable.
Expand Down
6 changes: 4 additions & 2 deletions reporter/src/main/kotlin/Reporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ interface Reporter {
private val LOADER = ServiceLoader.load(Reporter::class.java)!!

/**
* The list of all available reporters in the classpath.
* The set of all available [reporters][Reporter] in the classpath, sorted by name.
*/
val ALL by lazy { LOADER.iterator().asSequence().toList().sortedBy { it.reporterName } }
val ALL: Set<Reporter> by lazy {
LOADER.iterator().asSequence().toSortedSet(compareBy { it.reporterName })
}
}

/**
Expand Down
6 changes: 4 additions & 2 deletions scanner/src/main/kotlin/Scanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,11 @@ abstract class Scanner(
private val LOADER = ServiceLoader.load(ScannerFactory::class.java)!!

/**
* The list of all available scanners in the classpath.
* The set of all available [scanner factories][ScannerFactory] in the classpath, sorted by name.
*/
val ALL by lazy { LOADER.iterator().asSequence().toList().sortedBy { it.scannerName } }
val ALL: Set<ScannerFactory> by lazy {
LOADER.iterator().asSequence().toSortedSet(compareBy { it.scannerName })
}
}

/**
Expand Down