[python] support of two ways of possible responses from Hatch (PY-60410)

(cherry picked from commit d47225d7606fca1d65339b919b8250204360593a)

GitOrigin-RevId: 7714ad3d3dd5ccded7ce7a236265b2554a397830
This commit is contained in:
Vitaly Legchilkin
2025-07-07 15:12:36 +02:00
committed by intellij-monorepo-bot
parent 8a19e2350d
commit c81b5051f4
2 changed files with 21 additions and 2 deletions

View File

@@ -135,7 +135,7 @@ class HatchEnv(runtime: HatchRuntime) : HatchCommand("env", runtime) {
return executeAndHandleErrors("create", *arguments) {
val actualEnvName = envName ?: DEFAULT_ENV_NAME
when {
it.exitCode == 0 && it.stderrString.startsWith("Creating environment") -> Result.success(CreateResult.Created)
it.isSuccessStop("Creating environment") -> Result.success(CreateResult.Created)
it.exitCode == 0 -> Result.success(CreateResult.AlreadyExists)
it.stderrString.startsWith("Environment `$actualEnvName` is not defined by project config") -> Result.success(CreateResult.NotDefinedInConfig)
else -> Result.failure(null)
@@ -188,7 +188,7 @@ class HatchEnv(runtime: HatchRuntime) : HatchCommand("env", runtime) {
return executeAndHandleErrors("remove", *arguments) {
val actualEnvName = envName ?: DEFAULT_ENV_NAME
when {
it.exitCode == 0 && it.stderrString.startsWith("Removing environment") -> Result.success(RemoveResult.Removed)
it.isSuccessStop("Removing environment") -> Result.success(RemoveResult.Removed)
it.exitCode == 0 && it.stderrString.isBlank() -> Result.success(RemoveResult.NotExists)
it.stderrString.startsWith("Environment `$actualEnvName` is not defined by project config") -> Result.success(RemoveResult.NotDefinedInConfig)
it.stderrString.startsWith("Cannot remove active environment") -> Result.success(RemoveResult.CantRemoveActiveEnvironment)

View File

@@ -0,0 +1,19 @@
package com.intellij.python.hatch.cli
import com.intellij.platform.eel.provider.utils.EelProcessExecutionResult
import com.intellij.platform.eel.provider.utils.stderrString
/**
* Hatch has two versions of messages in the stop function: "Expected prefix ..." (old_message) and "Finished expected prefix ..." (final_text)
* https://github.com/pypa/hatch/blob/4ebce0e1fe8bf0fcdef587a704c207a063d72575/src/hatch/cli/terminal.py#L65
*/
fun EelProcessExecutionResult.isSuccessStop(expectedPrefix: String): Boolean {
require(expectedPrefix.isNotEmpty()) { "Expected prefix can't be empty" }
if (exitCode != 0) return false
val response = stderrString
val result = response.startsWith(expectedPrefix) ||
response.startsWith("Finished ${expectedPrefix.first().lowercase()}${expectedPrefix.drop(1)}")
return result
}