kunwp1 commented on code in PR #4484: URL: https://github.com/apache/texera/pull/4484#discussion_r3174460513
########## amber/src/main/scala/org/apache/texera/web/resource/pythonvirtualenvironment/PveManager.scala: ########## @@ -0,0 +1,231 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.texera.web.resource.pythonvirtualenvironment + +import java.nio.file.{Files, Path, Paths} +import java.util.concurrent.BlockingQueue +import scala.collection.mutable.Map +import scala.jdk.CollectionConverters._ +import scala.sys.process._ +import java.util.Comparator +import org.apache.texera.amber.config.UdfConfig + +/** + * PveManager is responsible for managing Python Virtual Environments (PVEs) + * for each Computing Unit + * + * It supports: + * - Creating and initializing isolated Python environments + * - Installing and uninstalling Python packages + * - Tracking system vs user-installed packages via metadata files + * - Streaming pip output logs back to the caller + * + * Each PVE is stored under: + * /tmp/texera-pve/venvs/{cuid}/{pveName}/ + * + * The structure includes: + * - pve/ -> actual virtual environment + * - metadata/ -> package tracking (system + user) + */ + +object PveManager { + + private val VenvRoot: Path = Paths.get("/tmp/texera-pve/venvs") + + private def cuidDir(cuid: Int, pveName: String): Path = { + VenvRoot.resolve(cuid.toString).resolve(pveName) + } + + private def pveDir(cuid: Int, pveName: String): Path = + cuidDir(cuid, pveName).resolve("pve") + + private def pythonBinPath(cuid: Int, pveName: String): Path = + pveDir(cuid, pveName).resolve("bin").resolve("python") + + private def metadataDir(cuid: Int, pveName: String): Path = + pveDir(cuid, pveName).resolve("metadata") + + private def pipEnv: Map[String, String] = + Map( + "PYTHONUNBUFFERED" -> "1", + "PIP_PROGRESS_BAR" -> "off", + "PIP_DISABLE_PIP_VERSION_CHECK" -> "1", + "PIP_NO_INPUT" -> "1" + ) + + def getSystemPackages(): Seq[String] = { + val pythonPath = UdfConfig.pythonPath.trim + val python = if (pythonPath.isEmpty) "python3" else pythonPath Review Comment: I saw your latest change. I suggest you to add a new file something like `PythonUtils.scala` and put the logic there because this piece of code is also being used in `PythonWorkflowWorker.scala` and we shouldn't have unnecessary duplicates. Otherwise, it's very hard to manage those copies. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
