MCPcopy Index your code
hub / github.com/celer-pkg/celer / writePythonVenvConfig

Method writePythonVenvConfig

configs/toolchain_file.go:290–364  ·  view source on GitHub ↗

writePythonVenvConfig generates Python virtual environment configuration if Python tool is available.

(toolchain *strings.Builder)

Source from the content-addressed store, hash-verified

288
289// writePythonVenvConfig generates Python virtual environment configuration if Python tool is available.
290func (c *Celer) writePythonVenvConfig(toolchain *strings.Builder) error {
291 // Skip if no Python tool detected.
292 if buildtools.PythonTool == nil || buildtools.PythonTool.Path == "" {
293 return nil
294 }
295
296 // Get python version of current project.
297 pythonVersion := buildtools.GetDefaultPythonVersion()
298 pythonConfig := c.PythonConfig()
299 if pythonConfig != nil && pythonConfig.GetVersion() != "" {
300 pythonVersion = pythonConfig.GetVersion()
301 }
302
303 // Normalize version to minor version format (e.g., 3.10.5 -> 3.10)
304 minorVersion := pythonVersion
305 if strings.Count(pythonVersion, ".") > 1 {
306 parts := strings.Split(pythonVersion, ".")
307 minorVersion = parts[0] + "." + parts[1]
308 }
309
310 // Detect Python major version (2 or 3)
311 majorVersion := "3"
312 if strings.HasPrefix(pythonVersion, "2") {
313 majorVersion = "2"
314 }
315
316 venvFolder := fmt.Sprintf("venv-%s@%s", minorVersion, c.Project().GetName())
317 venvDirAbs := filepath.Join(dirs.WorkspaceDir, "installed", venvFolder)
318
319 // Check if venv directory exists
320 if !fileio.PathExists(venvDirAbs) {
321 return nil
322 }
323
324 venvDir := filepath.Join("${WORKSPACE_ROOT}", "installed", venvFolder)
325
326 // Write Python virtual environment configuration
327 fmt.Fprintf(toolchain, "\n# ============== Python %s virtual environment ============== #\n", majorVersion)
328 fmt.Fprintf(toolchain, "# Python venv location.\n")
329 fmt.Fprintf(toolchain, "set(PYTHON_VENV_DIR %q)\n\n", venvDir)
330
331 if majorVersion == "3" {
332 // Python3 configuration using modern Find(Python3)
333 fmt.Fprintf(toolchain, "# Python3 executable and libraries\n")
334 fmt.Fprintf(toolchain, "set(Python3_EXECUTABLE \"${PYTHON_VENV_DIR}/bin/python3\" CACHE FILEPATH \"Python3 executable\")\n")
335 fmt.Fprintf(toolchain, "set(Python3_ROOT_DIR \"${PYTHON_VENV_DIR}\" CACHE PATH \"Python3 root directory\")\n")
336 fmt.Fprintf(toolchain, "\n# For CMake find_package(Python3) to correctly locate the venv Python.\n")
337 fmt.Fprintf(toolchain, "list(APPEND CMAKE_PREFIX_PATH \"${PYTHON_VENV_DIR}\")\n")
338 } else {
339 // Python2 configuration using legacy Find(PythonLibs)
340 fmt.Fprintf(toolchain, "# Python2 executable and libraries\n")
341 fmt.Fprintf(toolchain, "set(PYTHON_EXECUTABLE \"${PYTHON_VENV_DIR}/bin/python\" CACHE FILEPATH \"Python executable\")\n")
342 fmt.Fprintf(toolchain, "\n# For CMake find_package(PythonLibs) to correctly locate the venv Python2.\n")
343 fmt.Fprintf(toolchain, "list(APPEND CMAKE_PREFIX_PATH \"${PYTHON_VENV_DIR}\")\n")
344 }
345
346 fmt.Fprintf(toolchain, "\n# Set environment variables for Python package discovery.\n")
347 siteRel := fmt.Sprintf("lib/python%s/site-packages", minorVersion)

Callers 1

GenerateToolchainFileMethod · 0.95

Calls 8

PythonConfigMethod · 0.95
ProjectMethod · 0.95
GetDefaultPythonVersionFunction · 0.92
PathExistsFunction · 0.92
SprintfMethod · 0.80
LdLibraryPathMethod · 0.80
GetVersionMethod · 0.65
GetNameMethod · 0.65

Tested by

no test coverage detected