Skip to content

Commit

Permalink
feat(pip): Detect the Python version from .python-version
Browse files Browse the repository at this point in the history
Resolves #8366.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Mar 1, 2024
1 parent 8d33760 commit 62e22bf
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion plugins/package-managers/python/src/main/kotlin/Pip.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Pip(
}

override fun resolveDependencies(definitionFile: File, labels: Map<String, String>): List<ProjectAnalyzerResult> {
val result = runPythonInspector(definitionFile)
val result = runPythonInspector(definitionFile) { detectPythonVersion(definitionFile.parentFile) }

val project = result.toOrtProject(managerName, analysisRoot, definitionFile)
val packages = result.packages.toOrtPackages()
Expand Down Expand Up @@ -130,4 +130,12 @@ class Pip(
}
}.getOrThrow()
}

private fun detectPythonVersion(workingDir: File): String? {
// While there seems to be no formal specification, a `.python-version` file seems to be supposed to just
// contain the plain version, see e.g. https://github.com/pyenv/pyenv/blob/21c2a3d/test/version.bats#L28.
val pythonVersionFile = workingDir.resolve(".python-version")
if (!pythonVersionFile.isFile) return null
return pythonVersionFile.readLines().firstOrNull()?.takeIf { it.firstOrNull()?.isDigit() == true }
}
}

0 comments on commit 62e22bf

Please sign in to comment.