Skip to content

Commit 2175e69

Browse files
committed
Pip: Make the operating system configurable
By default, the python-inspector resolves the dependencies for a linux installation. Add a configuration option to configure which operating system should be used. Signed-off-by: Martin Nonnenmacher <[email protected]>
1 parent 8a92c75 commit 2175e69

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

analyzer/src/main/kotlin/managers/Pip.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import org.ossreviewtoolkit.model.ProjectAnalyzerResult
4040
import org.ossreviewtoolkit.model.Scope
4141
import org.ossreviewtoolkit.model.VcsInfo
4242
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
43+
import org.ossreviewtoolkit.model.config.PackageManagerConfiguration
4344
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
4445
import org.ossreviewtoolkit.utils.common.CommandLineTool
4546
import org.ossreviewtoolkit.utils.common.Os
@@ -115,10 +116,18 @@ object PythonVersion : CommandLineTool, Logging {
115116
}
116117
}
117118

119+
private const val OPTION_OPERATING_SYSTEM = "operatingSystem"
120+
private const val OPTION_OPERATING_SYSTEM_DEFAULT = "linux"
121+
private val OPERATING_SYSTEMS = listOf("linux", "mac", "windows")
122+
118123
/**
119124
* The [PIP](https://pip.pypa.io/) package manager for Python. Also see
120125
* [install_requires vs requirements files](https://packaging.python.org/discussions/install-requires-vs-requirements/)
121126
* and [setup.py vs. requirements.txt](https://caremad.io/posts/2013/07/setup-vs-requirement/).
127+
*
128+
* This package manager supports the following [options][PackageManagerConfiguration.options]:
129+
* - *operatingSystem*: The name of the operating system to resolve dependencies for. One of "linux", "mac", or
130+
* "windows". Defaults to "linux".
122131
*/
123132
@Suppress("TooManyFunctions")
124133
class Pip(
@@ -141,6 +150,13 @@ class Pip(
141150
) = Pip(managerName, analysisRoot, analyzerConfig, repoConfig)
142151
}
143152

153+
private val operatingSystemOption = (getOptions()[OPTION_OPERATING_SYSTEM] ?: OPTION_OPERATING_SYSTEM_DEFAULT)
154+
.also { os ->
155+
require(os.isEmpty() || os in OPERATING_SYSTEMS) {
156+
"The 'operatingSystem' option must be one of ${OPERATING_SYSTEMS.joinToString { "'$it'" }}."
157+
}
158+
}
159+
144160
override fun command(workingDir: File?) = "pip"
145161

146162
override fun transformVersion(output: String) = output.removePrefix("pip ").substringBefore(' ')
@@ -284,15 +300,17 @@ class Pip(
284300
}
285301

286302
logger.info {
287-
"Resolving dependencies for '${definitionFile.absolutePath}' with Python version '$pythonVersion'."
303+
"Resolving dependencies for '${definitionFile.absolutePath}' with Python version '$pythonVersion' and " +
304+
"operating system '$operatingSystemOption'."
288305
}
289306

290307
val pythonInspectorResult = runCatching {
291308
try {
292309
PythonInspector.run(
293310
workingDir = workingDir,
294311
definitionFile = definitionFile,
295-
pythonVersion = pythonVersion.replace(".", "")
312+
pythonVersion = pythonVersion.replace(".", ""),
313+
operatingSystem = operatingSystemOption
296314
)
297315
} finally {
298316
workingDir.resolve(".cache").safeDeleteRecursively(force = true)

analyzer/src/main/kotlin/managers/utils/PythonInspector.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,17 @@ internal object PythonInspector : CommandLineTool {
5454
workingDir: File,
5555
definitionFile: File,
5656
pythonVersion: String = "38",
57+
operatingSystem: String = "linux"
5758
): PythonInspectorResult {
5859
val outputFile = createOrtTempFile(prefix = "python-inspector", suffix = ".json")
5960

6061
val commandLineOptions = buildList {
6162
add("--python-version")
6263
add(pythonVersion)
6364

65+
add("--operating-system")
66+
add(operatingSystem)
67+
6468
add("--json-pdt")
6569
add(outputFile.absolutePath)
6670

0 commit comments

Comments
 (0)