24 from eric7.UI.DeleteFilesConfirmationDialog import DeleteFilesConfirmationDialog |
24 from eric7.UI.DeleteFilesConfirmationDialog import DeleteFilesConfirmationDialog |
25 |
25 |
26 from .VirtualenvMeta import VirtualenvMetaData |
26 from .VirtualenvMeta import VirtualenvMetaData |
27 |
27 |
28 |
28 |
29 # TODO: introduce 'eric-ide Server' environment definitions |
|
30 class VirtualenvManager(QObject): |
29 class VirtualenvManager(QObject): |
31 """ |
30 """ |
32 Class implementing an object to manage Python virtual environments. |
31 Class implementing an object to manage Python virtual environments. |
33 |
32 |
34 @signal virtualEnvironmentAdded() emitted to indicate the addition of |
33 @signal virtualEnvironmentAdded() emitted to indicate the addition of |
83 # interpreter: the path of the Python interpreter |
82 # interpreter: the path of the Python interpreter |
84 # variant: Python variant (always 3) |
83 # variant: Python variant (always 3) |
85 # is_global: a flag indicating a global environment |
84 # is_global: a flag indicating a global environment |
86 # is_conda: a flag indicating an Anaconda environment |
85 # is_conda: a flag indicating an Anaconda environment |
87 # is_remote: a flag indicating a remotely accessed environment |
86 # is_remote: a flag indicating a remotely accessed environment |
|
87 # is_eric_server a flag indicating an eric IDE server environment |
88 # exec_path: a string to be prefixed to the PATH environment |
88 # exec_path: a string to be prefixed to the PATH environment |
89 # setting |
89 # setting |
90 # description a description of the environment |
90 # description a description of the environment |
91 # |
91 # |
92 for venvName in environments: |
92 for venvName in environments: |
152 Preferences.syncPreferences() |
152 Preferences.syncPreferences() |
153 self.__loadSettings() |
153 self.__loadSettings() |
154 |
154 |
155 def __cleanEnvironments(self): |
155 def __cleanEnvironments(self): |
156 """ |
156 """ |
157 Private method to delete all non-existent local environments. |
157 Private method to delete all non-existent local or eric IDE server environments. |
158 """ |
158 """ |
159 removed = False |
159 removed = False |
160 |
160 |
161 for venvName in list(self.__virtualEnvironments): |
161 for venvName in list(self.__virtualEnvironments): |
162 venvItem = self.__virtualEnvironments[venvName] |
162 venvItem = self.__virtualEnvironments[venvName] |
163 if not venvItem.is_remote: |
163 if not venvItem.is_remote: |
164 # It is a local environment; check it is still valid. |
|
165 venvPath = venvItem.path |
164 venvPath = venvItem.path |
166 if venvPath and not os.path.exists(venvPath): |
165 if venvPath: |
167 del self.__virtualEnvironments[venvName] |
166 if venvItem.is_eric_server: |
168 removed = True |
167 # It is an eric IDE server environment; check it is still valid. |
|
168 ericServer = ericApp().getObject("EricServer") |
|
169 if ( |
|
170 ericServer.isServerConnected() |
|
171 and ericServer.getHost() == venvItem.eric_server |
|
172 and not ericServer.getServiceInterface("FileSystem").exists( |
|
173 venvPath |
|
174 ) |
|
175 ): |
|
176 del self.__virtualEnvironments[venvName] |
|
177 removed = True |
|
178 else: |
|
179 # It is a local environment; check it is still valid. |
|
180 if not os.path.exists(venvPath): |
|
181 del self.__virtualEnvironments[venvName] |
|
182 removed = True |
169 if removed: |
183 if removed: |
170 self.__saveSettings() |
184 self.__saveSettings() |
171 self.virtualEnvironmentRemoved.emit() |
185 self.virtualEnvironmentRemoved.emit() |
172 self.virtualEnvironmentsListChanged.emit() |
186 self.virtualEnvironmentsListChanged.emit() |
173 |
187 |
440 if venvName in self.__virtualEnvironments: |
454 if venvName in self.__virtualEnvironments: |
441 ok = True |
455 ok = True |
442 ok &= bool(self.__virtualEnvironments[venvName].path) |
456 ok &= bool(self.__virtualEnvironments[venvName].path) |
443 ok &= not self.__virtualEnvironments[venvName].is_global |
457 ok &= not self.__virtualEnvironments[venvName].is_global |
444 ok &= not self.__virtualEnvironments[venvName].is_remote |
458 ok &= not self.__virtualEnvironments[venvName].is_remote |
|
459 ok &= not self.__virtualEnvironments[venvName].is_eric_server |
445 ok &= os.access(self.__virtualEnvironments[venvName].path, os.W_OK) |
460 ok &= os.access(self.__virtualEnvironments[venvName].path, os.W_OK) |
446 |
461 |
447 return ok |
462 return ok |
448 |
463 |
449 def removeVirtualEnvs(self, venvNames): |
464 def removeVirtualEnvs(self, venvNames): |
625 @param venvName logical name of the virtual environment |
640 @param venvName logical name of the virtual environment |
626 @type str |
641 @type str |
627 @return flag indicating a global environment |
642 @return flag indicating a global environment |
628 @rtype bool |
643 @rtype bool |
629 """ |
644 """ |
630 if venvName in self.__virtualEnvironments: |
645 try: |
631 return self.__virtualEnvironments[venvName].is_global |
646 return self.__virtualEnvironments[venvName].is_global |
632 else: |
647 except KeyError: |
633 return False |
648 return False |
634 |
649 |
635 def isCondaEnvironment(self, venvName): |
650 def isCondaEnvironment(self, venvName): |
636 """ |
651 """ |
637 Public method to test, if a given environment is an Anaconda |
652 Public method to test, if a given environment is an Anaconda |
640 @param venvName logical name of the virtual environment |
655 @param venvName logical name of the virtual environment |
641 @type str |
656 @type str |
642 @return flag indicating an Anaconda environment |
657 @return flag indicating an Anaconda environment |
643 @rtype bool |
658 @rtype bool |
644 """ |
659 """ |
645 if venvName in self.__virtualEnvironments: |
660 try: |
646 return self.__virtualEnvironments[venvName].is_conda |
661 return self.__virtualEnvironments[venvName].is_conda |
647 else: |
662 except KeyError: |
648 return False |
663 return False |
649 |
664 |
650 def isRemoteEnvironment(self, venvName): |
665 def isRemoteEnvironment(self, venvName): |
651 """ |
666 """ |
652 Public method to test, if a given environment is a remotely accessed |
667 Public method to test, if a given environment is a remotely accessed |
655 @param venvName logical name of the virtual environment |
670 @param venvName logical name of the virtual environment |
656 @type str |
671 @type str |
657 @return flag indicating a remotely accessed environment |
672 @return flag indicating a remotely accessed environment |
658 @rtype bool |
673 @rtype bool |
659 """ |
674 """ |
660 if venvName in self.__virtualEnvironments: |
675 try: |
661 return self.__virtualEnvironments[venvName].is_remote |
676 return self.__virtualEnvironments[venvName].is_remote |
662 else: |
677 except KeyError: |
|
678 return False |
|
679 |
|
680 def isEricServerEnvironment(self, venvName): |
|
681 """ |
|
682 Public method to test, if a given environment is an environment accessed |
|
683 through an eric IDE server. |
|
684 |
|
685 @param venvName logical name of the virtual environment |
|
686 @type str |
|
687 @return flag indicating a remotely accessed environment |
|
688 @rtype bool |
|
689 """ |
|
690 try: |
|
691 return self.__virtualEnvironments[venvName].is_eric_server |
|
692 except KeyError: |
663 return False |
693 return False |
664 |
694 |
665 def getVirtualenvExecPath(self, venvName): |
695 def getVirtualenvExecPath(self, venvName): |
666 """ |
696 """ |
667 Public method to get the search path prefix of a virtual environment. |
697 Public method to get the search path prefix of a virtual environment. |
669 @param venvName logical name for the virtual environment |
699 @param venvName logical name for the virtual environment |
670 @type str |
700 @type str |
671 @return search path prefix |
701 @return search path prefix |
672 @rtype str |
702 @rtype str |
673 """ |
703 """ |
674 if venvName in self.__virtualEnvironments: |
704 try: |
675 return self.__virtualEnvironments[venvName].exec_path |
705 return self.__virtualEnvironments[venvName].exec_path |
676 else: |
706 except KeyError: |
677 return "" |
707 return "" |
678 |
708 |
679 def setVirtualEnvironmentsBaseDir(self, baseDir): |
709 def setVirtualEnvironmentsBaseDir(self, baseDir): |
680 """ |
710 """ |
681 Public method to set the base directory for the virtual environments. |
711 Public method to set the base directory for the virtual environments. |