Skip to content

Commit

Permalink
refactor(scanner): Move downloadRecursively() to `ProvenanceDownloa…
Browse files Browse the repository at this point in the history
…der`

Being a download function that exclusively calls `download()` from
`ProvenanceDownloader`, this should be part of `ProvenanceDownloader`
rather than of `Scanner`. While at it, make it public for use by scanner
plugins.

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Jan 15, 2024
1 parent 233eb8b commit 972e24c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
19 changes: 1 addition & 18 deletions scanner/src/main/kotlin/Scanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ package org.ossreviewtoolkit.scanner

import java.io.File
import java.io.IOException
import java.nio.file.StandardCopyOption
import java.time.Instant

import kotlin.io.path.moveTo
import kotlin.time.measureTime

import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -705,7 +703,7 @@ class Scanner(

// runCatching has a bug with smart-cast, see https://youtrack.jetbrains.com/issue/KT-62938.
try {
dir = downloadRecursively(nestedProvenance)
dir = provenanceDownloader.downloadRecursively(nestedProvenance)
archiver.archive(dir, nestedProvenance.root)
} catch (e: IOException) {
controller.addIssue(
Expand All @@ -724,21 +722,6 @@ class Scanner(

logger.info { "Created file archives for ${provenancesWithMissingArchives.size} package(s) in $duration." }
}

private fun downloadRecursively(nestedProvenance: NestedProvenance): File {
// Use the provenanceDownloader to download each provenance from nestedProvenance separately, because they are
// likely already cached if a path scanner wrapper is used.

val root = provenanceDownloader.download(nestedProvenance.root)

nestedProvenance.subRepositories.forEach { (path, provenance) ->
val tempDir = provenanceDownloader.download(provenance)
val targetDir = root.resolve(path)
tempDir.toPath().moveTo(targetDir.toPath(), StandardCopyOption.ATOMIC_MOVE)
}

return root
}
}

/**
Expand Down
24 changes: 24 additions & 0 deletions scanner/src/main/kotlin/provenance/ProvenanceDownloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
package org.ossreviewtoolkit.scanner.provenance

import java.io.File
import java.nio.file.StandardCopyOption

import kotlin.io.path.copyToRecursively
import kotlin.io.path.moveTo

import kotlinx.coroutines.runBlocking

Expand All @@ -47,6 +49,28 @@ fun interface ProvenanceDownloader {
* Throws a [DownloadException] if the download fails.
*/
fun download(provenance: KnownProvenance): File

/**
* Download the source code specified by the provided [nestedProvenance] incl. sub-repositories and return the path
* to the temporary directory that contains the downloaded source code. The caller is responsible for deleting the
* directory.
*
* Throws a [DownloadException] if the download fails.
*/
fun downloadRecursively(nestedProvenance: NestedProvenance): File {
// Use the provenanceDownloader to download each provenance from nestedProvenance separately, because they are
// likely already cached if a path scanner wrapper is used.

val root = download(nestedProvenance.root)

nestedProvenance.subRepositories.forEach { (path, provenance) ->
val tempDir = download(provenance)
val targetDir = root.resolve(path)
tempDir.toPath().moveTo(targetDir.toPath(), StandardCopyOption.ATOMIC_MOVE)
}

return root
}
}

/**
Expand Down

0 comments on commit 972e24c

Please sign in to comment.