Skip to content

Commit

Permalink
fix poetry package manager running poetry update upon reloading the p…
Browse files Browse the repository at this point in the history
…ackage list; #PY-78077 Fixed

GitOrigin-RevId: b0f73f88f39a62b4351fb6189fe5d9afb79ef6d3
  • Loading branch information
asorotsky authored and intellij-monorepo-bot committed Jan 8, 2025
1 parent d7283df commit 29545de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.intellij.platform.ide.progress.withBackgroundProgress
import com.intellij.util.SystemProperties
import com.jetbrains.python.PyBundle
import com.jetbrains.python.packaging.PyPackageManager
import com.jetbrains.python.packaging.common.PythonPackage
import com.jetbrains.python.packaging.management.PythonPackageManager
import com.jetbrains.python.pathValidation.PlatformAndRoot
import com.jetbrains.python.pathValidation.ValidationRequest
Expand Down Expand Up @@ -182,10 +183,25 @@ suspend fun poetryInstallPackage(sdk: Sdk, pkg: String, extraArgs: List<String>)
suspend fun poetryUninstallPackage(sdk: Sdk, pkg: String): Result<String> = runPoetryWithSdk(sdk, "remove", pkg)

@Internal
suspend fun poetryReloadPackages(sdk: Sdk): Result<String> {
runPoetryWithSdk(sdk, "update").onFailure { return Result.failure(it) }
runPoetryWithSdk(sdk, "install", "--no-root").onFailure { return Result.failure(it) }
return runPoetryWithSdk(sdk, "show")
suspend fun poetryShowPackages(sdk: Sdk): Result<List<PythonPackage>> {
val output = runPoetryWithSdk(sdk, "show").getOrElse {
return Result.failure(it)
}

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
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 reloadPackagesCommand(): Result<List<PythonPackage>> {
val output = poetryReloadPackages(sdk).getOrElse { return Result.failure(it) }
return Result.success(parsePoetryShow(output))
return poetryShowPackages(sdk)
}

override suspend fun reloadPackages(): Result<List<PythonPackage>> {
Expand Down Expand Up @@ -57,21 +56,6 @@ class PoetryPackageManager(project: Project, sdk: Sdk) : PythonPackageManager(pr
/**
* 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
fun parsePoetryShowOutdatedTest(input: String): Map<String, PythonOutdatedPackage> {
Expand Down

0 comments on commit 29545de

Please sign in to comment.