fix poetry package manager running poetry update upon reloading the package list; #PY-78077 Fixed

(cherry picked from commit b0f73f88f39a62b4351fb6189fe5d9afb79ef6d3)

GitOrigin-RevId: 8e78ddecb0298826c8bc8dcf55e5b105954bd269
This commit is contained in:
Aleksandr Sorotskii
2025-01-08 23:03:44 +01:00
committed by intellij-monorepo-bot
parent 170ad83859
commit 400fb17865
2 changed files with 21 additions and 21 deletions

View File

@@ -18,6 +18,7 @@ import com.intellij.util.SystemProperties
import com.jetbrains.python.PyBundle import com.jetbrains.python.PyBundle
import com.jetbrains.python.packaging.PyExecutionException import com.jetbrains.python.packaging.PyExecutionException
import com.jetbrains.python.packaging.PyPackageManager import com.jetbrains.python.packaging.PyPackageManager
import com.jetbrains.python.packaging.common.PythonPackage
import com.jetbrains.python.packaging.management.PythonPackageManager import com.jetbrains.python.packaging.management.PythonPackageManager
import com.jetbrains.python.pathValidation.PlatformAndRoot import com.jetbrains.python.pathValidation.PlatformAndRoot
import com.jetbrains.python.pathValidation.ValidationRequest import com.jetbrains.python.pathValidation.ValidationRequest
@@ -184,10 +185,25 @@ suspend fun poetryInstallPackage(sdk: Sdk, pkg: String, extraArgs: List<String>)
suspend fun poetryUninstallPackage(sdk: Sdk, pkg: String): Result<String> = runPoetryWithSdk(sdk, "remove", pkg) suspend fun poetryUninstallPackage(sdk: Sdk, pkg: String): Result<String> = runPoetryWithSdk(sdk, "remove", pkg)
@Internal @Internal
suspend fun poetryReloadPackages(sdk: Sdk): Result<String> { suspend fun poetryShowPackages(sdk: Sdk): Result<List<PythonPackage>> {
runPoetryWithSdk(sdk, "update").onFailure { return Result.failure(it) } val output = runPoetryWithSdk(sdk, "show").getOrElse {
runPoetryWithSdk(sdk, "install", "--no-root").onFailure { return Result.failure(it) } return Result.failure(it)
return runPoetryWithSdk(sdk, "show") }
return parsePoetryShow(output).let { Result.success(it) }
}
@Internal
fun parsePoetryShow(input: String): List<PythonPackage> {
val result = mutableListOf<PythonPackage>()
input.split("\n").forEach { line ->
if (line.isNotBlank()) {
val packageInfo = line.trim().split(" ").map { it.trim() }.filter { it.isNotBlank() && it != "(!)"}
result.add(PythonPackage(packageInfo[0], packageInfo[1], false))
}
}
return result
} }
/** /**

View File

@@ -27,8 +27,7 @@ class PoetryPackageManager(project: Project, sdk: Sdk) : PythonPackageManager(pr
override suspend fun uninstallPackageCommand(pkg: PythonPackage): Result<String> = poetryUninstallPackage(sdk, pkg.name) override suspend fun uninstallPackageCommand(pkg: PythonPackage): Result<String> = poetryUninstallPackage(sdk, pkg.name)
override suspend fun reloadPackagesCommand(): Result<List<PythonPackage>> { override suspend fun reloadPackagesCommand(): Result<List<PythonPackage>> {
val output = poetryReloadPackages(sdk).getOrElse { return Result.failure(it) } return poetryShowPackages(sdk)
return Result.success(parsePoetryShow(output))
} }
override suspend fun reloadPackages(): Result<List<PythonPackage>> { override suspend fun reloadPackages(): Result<List<PythonPackage>> {
@@ -57,21 +56,6 @@ class PoetryPackageManager(project: Project, sdk: Sdk) : PythonPackageManager(pr
/** /**
* Parses the output of `poetry show` into a list of packages. * Parses the output of `poetry show` into a list of packages.
*/ */
private fun parsePoetryShow(input: String): List<PythonPackage> {
val result = mutableListOf<PythonPackage>()
input.split("\n").forEach { line ->
if (line.isNotBlank()) {
val packageInfo = line.trim().split(" ").map { it.trim() }.filter { it.isNotBlank() }
result.add(PythonPackage(packageInfo[0], packageInfo[1], false))
}
}
return result
}
@TestOnly
fun parsePoetryShowTest(input: String): List<PythonPackage> {
return parsePoetryShow(input)
}
@TestOnly @TestOnly
fun parsePoetryShowOutdatedTest(input: String): Map<String, PythonOutdatedPackage> { fun parsePoetryShowOutdatedTest(input: String): Map<String, PythonOutdatedPackage> {