Python: refactoring before interpreters service.

Extract various classes to the shared modules, `ExecutablePython` now has `env` map as it can be used by conda.

GitOrigin-RevId: eb45ea29f49132fa4f91c979f71453e6a2ade344
This commit is contained in:
Ilya.Kazakevich
2025-06-08 01:34:29 +02:00
committed by intellij-monorepo-bot
parent 088eb4ef09
commit 107201d413
16 changed files with 63 additions and 38 deletions

View File

@@ -1,21 +1,27 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.python.community.execService.python.advancedApi
import com.intellij.python.community.execService.python.advancedApi.ExecutablePython.Companion.vanillaExecutablePython
import com.jetbrains.python.PythonBinary
import java.nio.file.Path
/**
* Something that can execute python code (vanilla cpython, conda).
* `[binary] [args]` <python-args-go-here> (i.e `-m foo.py`).
* For [VanillaExecutablePython] it is `python` without arguments, but for conda it might be `conda run` etc
* [env] are added to the environment
* For [vanillaExecutablePython] it is `python` without arguments, but for conda it might be `conda run` etc
*/
interface ExecutablePython {
val binary: Path
val args: List<String>
data class ExecutablePython(
val binary: Path,
val args: List<String>,
val env: Map<String, String>,
) {
companion object {
class VanillaExecutablePython(override val binary: PythonBinary) : ExecutablePython {
override val args: List<String> = emptyList()
}
/**
* Plain python that doesn't have args nor envs
*/
fun vanillaExecutablePython(binary: PythonBinary): ExecutablePython =
ExecutablePython(binary, emptyList(), emptyMap())
}
}

View File

@@ -25,7 +25,8 @@ suspend fun <T> ExecService.executePythonAdvanced(
executeAdvanced(python.binary, {
addArgs(*python.args.toTypedArray())
argsBuilder()
}, options, processInteractiveHandler)
// TODO: Merge PATH
}, options.copy(env = options.env + python.env), processInteractiveHandler)
/**

View File

@@ -5,7 +5,7 @@ import com.intellij.python.community.execService.ExecOptions
import com.intellij.python.community.execService.ExecService
import com.intellij.python.community.execService.PyProcessListener
import com.intellij.python.community.execService.ZeroCodeStdoutTransformer
import com.intellij.python.community.execService.python.advancedApi.ExecutablePython.Companion.VanillaExecutablePython
import com.intellij.python.community.execService.python.advancedApi.ExecutablePython
import com.intellij.python.community.execService.python.advancedApi.executeHelperAdvanced
import com.intellij.python.community.execService.python.advancedApi.validatePythonAndGetVersion
import com.jetbrains.python.PythonBinary
@@ -25,8 +25,7 @@ suspend fun ExecService.executeHelper(
options: ExecOptions = ExecOptions(),
procListener: PyProcessListener? = null,
): PyExecResult<String> =
executeHelperAdvanced(VanillaExecutablePython(python), helper, args, options, procListener, ZeroCodeStdoutTransformer)
executeHelperAdvanced(ExecutablePython.vanillaExecutablePython(python), helper, args, options, procListener, ZeroCodeStdoutTransformer)
/**
* Ensures that this python is executable and returns its version. Error if python is broken.
@@ -36,6 +35,6 @@ suspend fun ExecService.executeHelper(
*/
@ApiStatus.Internal
suspend fun ExecService.validatePythonAndGetVersion(python: PythonBinary): PyResult<LanguageLevel> =
validatePythonAndGetVersion(VanillaExecutablePython(python))
validatePythonAndGetVersion(ExecutablePython.vanillaExecutablePython(python))
suspend fun PythonBinary.validatePythonAndGetVersion(): PyResult<LanguageLevel> = ExecService().validatePythonAndGetVersion(this)