VirtualEnv/VirtualenvManager.py

changeset 6362
ec32d1d7f525
parent 6349
17b3c75913de
child 6381
37f23590dbbc
diff -r 53f6bd7fb238 -r ec32d1d7f525 VirtualEnv/VirtualenvManager.py
--- a/VirtualEnv/VirtualenvManager.py	Fri Jun 22 18:18:23 2018 +0200
+++ b/VirtualEnv/VirtualenvManager.py	Sat Jun 23 15:14:48 2018 +0200
@@ -56,11 +56,15 @@
         #                   (empty for a global environment)
         #   interpreter:    the path of the Python interpreter
         #   variant:        Python variant (2 or 3)
+        #   is_global:      a flag indicating a global environment
         #
         for venvName in environments:
             interpreter = environments[venvName]["interpreter"]
             if os.access(interpreter, os.X_OK):
-                self.__virtualEnvironments[venvName] = environments[venvName]
+                environment = environments[venvName]
+                if "is_global" not in environment:
+                    environment["is_global"] = environment["path"] == ""
+                self.__virtualEnvironments[venvName] = environment
         
         # check, if the interpreter used to run eric is in the environments
         defaultPy = sys.executable.replace("w.exe", ".exe")
@@ -76,6 +80,7 @@
                 "path": "",
                 "interpreter": defaultPy,
                 "variant": sys.version_info[0],
+                "is_global": True,
             }
         
         self.__saveSettings()
@@ -112,7 +117,7 @@
             dia.exec_()
     
     def addVirtualEnv(self, venvName, venvDirectory, venvInterpreter="",
-                      venvVariant=3):
+                      venvVariant=3, isGlobal=False):
         """
         Public method to add a virtual environment.
         
@@ -124,6 +129,8 @@
         @type str
         @param venvVariant Python variant of the virtual environment
         @type int
+        @param isGlobal flag indicating a global environment
+        @type bool
         """
         if venvName in self.__virtualEnvironments:
             ok = E5MessageBox.yesNo(
@@ -142,12 +149,14 @@
             dlg = VirtualenvInterpreterSelectionDialog(venvName, venvDirectory)
             if dlg.exec_() == QDialog.Accepted:
                 venvInterpreter, venvVariant = dlg.getData()
+                isGlobal = True
         
         if venvInterpreter:
             self.__virtualEnvironments[venvName] = {
                 "path": venvDirectory,
                 "interpreter": venvInterpreter,
                 "variant": venvVariant,
+                "is_global": isGlobal,
             }
             
             self.__saveSettings()
@@ -156,7 +165,7 @@
                 self.__virtualenvManagerDialog.refresh()
     
     def setVirtualEnv(self, venvName, venvDirectory, venvInterpreter,
-                      venvVariant):
+                      venvVariant, isGlobal):
         """
         Public method to change a virtual environment.
         
@@ -168,6 +177,8 @@
         @type str
         @param venvVariant Python variant of the virtual environment
         @type int
+        @param isGlobal flag indicating a global environment
+        @type bool
         """
         if venvName not in self.__virtualEnvironments:
             E5MessageBox.yesNo(
@@ -183,6 +194,7 @@
             "path": venvDirectory,
             "interpreter": venvInterpreter,
             "variant": venvVariant,
+            "is_global": isGlobal,
         }
         
         self.__saveSettings()
@@ -191,7 +203,7 @@
             self.__virtualenvManagerDialog.refresh()
     
     def renameVirtualEnv(self, oldVenvName, venvName, venvDirectory,
-                         venvInterpreter, venvVariant):
+                         venvInterpreter, venvVariant, isGlobal):
         """
         Public method to substitute a virtual environment entry with a new
         name.
@@ -206,6 +218,8 @@
         @type str
         @param venvVariant Python variant of the virtual environment
         @type int
+        @param isGlobal flag indicating a global environment
+        @type bool
         """
         if oldVenvName not in self.__virtualEnvironments:
             E5MessageBox.yesNo(
@@ -219,7 +233,7 @@
         
         del self.__virtualEnvironments[oldVenvName]
         self.addVirtualEnv(venvName, venvDirectory, venvInterpreter,
-                           venvVariant)
+                           venvVariant, isGlobal)
     
     def deleteVirtualEnvs(self, venvNames):
         """
@@ -246,8 +260,7 @@
             )
             if dlg.exec_() == QDialog.Accepted:
                 for venvName in venvNames:
-                    if venvName in self.__virtualEnvironments and \
-                            bool(self.__virtualEnvironments[venvName]["path"]):
+                    if self.__isEnvironmentDeleteable(venvName):
                         shutil.rmtree(
                             self.__virtualEnvironments[venvName]["path"], True)
                         del self.__virtualEnvironments[venvName]
@@ -257,6 +270,26 @@
                 if self.__virtualenvManagerDialog:
                     self.__virtualenvManagerDialog.refresh()
     
+    def __isEnvironmentDeleteable(self, venvName):
+        """
+        Private method to check, if a virtual environment can be deleted from
+        disk.
+        
+        @param venvName name of the virtual environment
+        @type str
+        @return flag indicating it can be deleted
+        @rtype bool
+        """
+        ok = False
+        if venvName in self.__virtualEnvironments:
+            ok = True
+            ok &= bool(self.__virtualEnvironments[venvName]["path"])
+            ok &= not self.__virtualEnvironments[venvName]["is_global"]
+            ok &= os.access(self.__virtualEnvironments[venvName]["path"],
+                            os.W_OK)
+        
+        return ok
+    
     def removeVirtualEnvs(self, venvNames):
         """
         Public method to delete virtual environment from the list.
@@ -386,3 +419,14 @@
                 environments.append(venvName)
         
         return environments
+    
+    def isGlobalEnvironment(self, venvName):
+        """
+        Public method to test, if a given environment is a global one.
+        
+        @param venvName logical name of the virtual environment
+        @type str
+        @return flag indicating a global environment
+        @rtype bool
+        """
+        return self.__virtualEnvironments[venvName]["is_global"] 

eric ide

mercurial