SarahAsad23 commented on code in PR #4484:
URL: https://github.com/apache/texera/pull/4484#discussion_r3159012379


##########
amber/src/main/scala/org/apache/texera/web/resource/pythonvirtualenvironment/PveManager.scala:
##########
@@ -0,0 +1,282 @@
+/*
+ * 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, StandardOpenOption}
+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 systemPackagesPath(cuid: Int, pveName: String): Path =
+    metadataDir(cuid, pveName).resolve("system-packages.txt")
+
+  private def writeMetadata(path: Path, lines: Seq[String]): Unit = {
+    if (path.getParent != null) {
+      Files.createDirectories(path.getParent)
+    }
+    Files.write(
+      path,
+      lines.asJava,
+      StandardOpenOption.CREATE,
+      StandardOpenOption.TRUNCATE_EXISTING,
+      StandardOpenOption.WRITE
+    )
+  }
+
+  private def readMetadataList(path: Path): List[String] = {
+    if (!Files.exists(path)) return Nil
+    Files.readAllLines(path).asScala.map(_.trim).filter(_.nonEmpty).toList
+  }
+
+  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(cuid: Int, pveName: String): (Seq[String]) = {
+    val sys = readMetadataList(systemPackagesPath(cuid, pveName))

Review Comment:
   I addressed (1) by ensuring system packages are visible even when no 
environment has been created. For (2), I revised the design to avoid storing 
system metadata and instead rely on pip freeze, as suggested.



-- 
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]

Reply via email to