eric6/VirtualEnv/VirtualenvManager.py

changeset 7635
0cdead130a81
parent 7628
f904d0eef264
child 7726
b1ade4fcf05f
equal deleted inserted replaced
7634:8c3d033e5044 7635:0cdead130a81
66 self.__virtualEnvironments = {} 66 self.__virtualEnvironments = {}
67 # each environment entry is a dictionary: 67 # each environment entry is a dictionary:
68 # path: the directory of the virtual environment 68 # path: the directory of the virtual environment
69 # (empty for a global environment) 69 # (empty for a global environment)
70 # interpreter: the path of the Python interpreter 70 # interpreter: the path of the Python interpreter
71 # variant: Python variant (2 or 3) 71 # variant: Python variant (always 3)
72 # is_global: a flag indicating a global environment 72 # is_global: a flag indicating a global environment
73 # is_conda: a flag indicating an Anaconda environment 73 # is_conda: a flag indicating an Anaconda environment
74 # is_remote: a flag indicating a remotely accessed environment 74 # is_remote: a flag indicating a remotely accessed environment
75 # exec_path: a string to be prefixed to the PATH environment 75 # exec_path: a string to be prefixed to the PATH environment
76 # setting 76 # setting
77 # 77 #
78 envsToDelete = []
78 for venvName in environments: 79 for venvName in environments:
79 environment = environments[venvName] 80 environment = environments[venvName]
81 if environment["variant"] == 2:
82 # Python2 environment are not supported anymore, delete them
83 envsToDelete.append(venvName)
84 continue
85
80 if ( 86 if (
81 ("is_remote" in environment and environment["is_remote"]) or 87 ("is_remote" in environment and environment["is_remote"]) or
82 os.access(environment["interpreter"], os.X_OK) 88 os.access(environment["interpreter"], os.X_OK)
83 ): 89 ):
84 if "is_global" not in environment: 90 if "is_global" not in environment:
89 environment["is_remote"] = False 95 environment["is_remote"] = False
90 if "exec_path" not in environment: 96 if "exec_path" not in environment:
91 environment["exec_path"] = "" 97 environment["exec_path"] = ""
92 self.__virtualEnvironments[venvName] = environment 98 self.__virtualEnvironments[venvName] = environment
93 99
100 # now remove unsupported environments
101 for venvName in envsToDelete:
102 del environments[venvName]
103
94 # check, if the interpreter used to run eric is in the environments 104 # check, if the interpreter used to run eric is in the environments
95 defaultPy = sys.executable.replace("w.exe", ".exe") 105 defaultPy = sys.executable.replace("w.exe", ".exe")
96 found = False 106 found = False
97 for venvName in self.__virtualEnvironments: 107 for venvName in self.__virtualEnvironments:
98 if (defaultPy == 108 if (defaultPy ==
102 if not found: 112 if not found:
103 # add an environment entry for the default interpreter 113 # add an environment entry for the default interpreter
104 self.__virtualEnvironments[VirtualenvManager.DefaultKey] = { 114 self.__virtualEnvironments[VirtualenvManager.DefaultKey] = {
105 "path": "", 115 "path": "",
106 "interpreter": defaultPy, 116 "interpreter": defaultPy,
107 "variant": sys.version_info[0], 117 "variant": 3,
108 "is_global": True, 118 "is_global": True,
109 "is_conda": False, 119 "is_conda": False,
110 "is_remote": False, 120 "is_remote": False,
111 "exec_path": "", 121 "exec_path": "",
112 } 122 }
184 dia.show() 194 dia.show()
185 dia.start(resultDict["arguments"]) 195 dia.start(resultDict["arguments"])
186 dia.exec_() 196 dia.exec_()
187 197
188 def addVirtualEnv(self, venvName, venvDirectory, venvInterpreter="", 198 def addVirtualEnv(self, venvName, venvDirectory, venvInterpreter="",
189 venvVariant=3, isGlobal=False, isConda=False, 199 isGlobal=False, isConda=False, isRemote=False,
190 isRemote=False, execPath=""): 200 execPath=""):
191 """ 201 """
192 Public method to add a virtual environment. 202 Public method to add a virtual environment.
193 203
194 @param venvName logical name for the virtual environment 204 @param venvName logical name for the virtual environment
195 @type str 205 @type str
196 @param venvDirectory directory of the virtual environment 206 @param venvDirectory directory of the virtual environment
197 @type str 207 @type str
198 @param venvInterpreter interpreter of the virtual environment 208 @param venvInterpreter interpreter of the virtual environment
199 @type str 209 @type str
200 @param venvVariant Python variant of the virtual environment
201 @type int
202 @param isGlobal flag indicating a global environment 210 @param isGlobal flag indicating a global environment
203 @type bool 211 @type bool
204 @param isConda flag indicating an Anaconda virtual environment 212 @param isConda flag indicating an Anaconda virtual environment
205 @type bool 213 @type bool
206 @param isRemote flag indicating a remotely accessed environment 214 @param isRemote flag indicating a remotely accessed environment
231 from .VirtualenvInterpreterSelectionDialog import ( 239 from .VirtualenvInterpreterSelectionDialog import (
232 VirtualenvInterpreterSelectionDialog 240 VirtualenvInterpreterSelectionDialog
233 ) 241 )
234 dlg = VirtualenvInterpreterSelectionDialog(venvName, venvDirectory) 242 dlg = VirtualenvInterpreterSelectionDialog(venvName, venvDirectory)
235 if dlg.exec_() == QDialog.Accepted: 243 if dlg.exec_() == QDialog.Accepted:
236 venvInterpreter, venvVariant = dlg.getData() 244 venvInterpreter = dlg.getData()
237 245
238 if venvInterpreter: 246 if venvInterpreter:
239 self.__virtualEnvironments[venvName] = { 247 self.__virtualEnvironments[venvName] = {
240 "path": venvDirectory, 248 "path": venvDirectory,
241 "interpreter": venvInterpreter, 249 "interpreter": venvInterpreter,
242 "variant": venvVariant, 250 "variant": 3, # always 3
243 "is_global": isGlobal, 251 "is_global": isGlobal,
244 "is_conda": isConda, 252 "is_conda": isConda,
245 "is_remote": isRemote, 253 "is_remote": isRemote,
246 "exec_path": execPath, 254 "exec_path": execPath,
247 } 255 }
251 self.virtualEnvironmentAdded.emit() 259 self.virtualEnvironmentAdded.emit()
252 if self.__virtualenvManagerDialog: 260 if self.__virtualenvManagerDialog:
253 self.__virtualenvManagerDialog.refresh() 261 self.__virtualenvManagerDialog.refresh()
254 262
255 def setVirtualEnv(self, venvName, venvDirectory, venvInterpreter, 263 def setVirtualEnv(self, venvName, venvDirectory, venvInterpreter,
256 venvVariant, isGlobal, isConda, isRemote, 264 isGlobal, isConda, isRemote, execPath):
257 execPath):
258 """ 265 """
259 Public method to change a virtual environment. 266 Public method to change a virtual environment.
260 267
261 @param venvName logical name of the virtual environment 268 @param venvName logical name of the virtual environment
262 @type str 269 @type str
263 @param venvDirectory directory of the virtual environment 270 @param venvDirectory directory of the virtual environment
264 @type str 271 @type str
265 @param venvInterpreter interpreter of the virtual environment 272 @param venvInterpreter interpreter of the virtual environment
266 @type str 273 @type str
267 @param venvVariant Python variant of the virtual environment
268 @type int
269 @param isGlobal flag indicating a global environment 274 @param isGlobal flag indicating a global environment
270 @type bool 275 @type bool
271 @param isConda flag indicating an Anaconda virtual environment 276 @param isConda flag indicating an Anaconda virtual environment
272 @type bool 277 @type bool
273 @param isRemote flag indicating a remotely accessed environment 278 @param isRemote flag indicating a remotely accessed environment
287 return 292 return
288 293
289 self.__virtualEnvironments[venvName] = { 294 self.__virtualEnvironments[venvName] = {
290 "path": venvDirectory, 295 "path": venvDirectory,
291 "interpreter": venvInterpreter, 296 "interpreter": venvInterpreter,
292 "variant": venvVariant, 297 "variant": 3, # always 3
293 "is_global": isGlobal, 298 "is_global": isGlobal,
294 "is_conda": isConda, 299 "is_conda": isConda,
295 "is_remote": isRemote, 300 "is_remote": isRemote,
296 "exec_path": execPath, 301 "exec_path": execPath,
297 } 302 }
301 self.virtualEnvironmentChanged.emit(venvName) 306 self.virtualEnvironmentChanged.emit(venvName)
302 if self.__virtualenvManagerDialog: 307 if self.__virtualenvManagerDialog:
303 self.__virtualenvManagerDialog.refresh() 308 self.__virtualenvManagerDialog.refresh()
304 309
305 def renameVirtualEnv(self, oldVenvName, venvName, venvDirectory, 310 def renameVirtualEnv(self, oldVenvName, venvName, venvDirectory,
306 venvInterpreter, venvVariant, isGlobal, isConda, 311 venvInterpreter, isGlobal, isConda,
307 isRemote, execPath): 312 isRemote, execPath):
308 """ 313 """
309 Public method to substitute a virtual environment entry with a new 314 Public method to substitute a virtual environment entry with a new
310 name. 315 name.
311 316
315 @type str 320 @type str
316 @param venvDirectory directory of the virtual environment 321 @param venvDirectory directory of the virtual environment
317 @type str 322 @type str
318 @param venvInterpreter interpreter of the virtual environment 323 @param venvInterpreter interpreter of the virtual environment
319 @type str 324 @type str
320 @param venvVariant Python variant of the virtual environment
321 @type int
322 @param isGlobal flag indicating a global environment 325 @param isGlobal flag indicating a global environment
323 @type bool 326 @type bool
324 @param isConda flag indicating an Anaconda virtual environment 327 @param isConda flag indicating an Anaconda virtual environment
325 @type bool 328 @type bool
326 @param isRemote flag indicating a remotely accessed environment 329 @param isRemote flag indicating a remotely accessed environment
339 icon=E5MessageBox.Warning) 342 icon=E5MessageBox.Warning)
340 return 343 return
341 344
342 del self.__virtualEnvironments[oldVenvName] 345 del self.__virtualEnvironments[oldVenvName]
343 self.addVirtualEnv(venvName, venvDirectory, venvInterpreter, 346 self.addVirtualEnv(venvName, venvDirectory, venvInterpreter,
344 venvVariant, isGlobal, isConda, isRemote, 347 isGlobal, isConda, isRemote, execPath)
345 execPath)
346 348
347 def deleteVirtualEnvs(self, venvNames): 349 def deleteVirtualEnvs(self, venvNames):
348 """ 350 """
349 Public method to delete virtual environments from the list and disk. 351 Public method to delete virtual environments from the list and disk.
350 352
537 environments = [name for name in environments 539 environments = [name for name in environments
538 if not self.isRemoteEnvironment(name)] 540 if not self.isRemoteEnvironment(name)]
539 541
540 return environments 542 return environments
541 543
542 def getVirtualenvNamesForVariant(self, variant):
543 """
544 Public method to get a list of virtual environments for a given
545 Python variant.
546
547 @param variant Python variant (2 or 3)
548 @type int
549 @return list of defined virtual environments
550 @rtype list of str
551 """
552 environments = []
553 for venvName in self.__virtualEnvironments:
554 if self.__virtualEnvironments[venvName]["variant"] == variant:
555 environments.append(venvName)
556
557 return environments
558
559 def getVirtualenvVariant(self, venvName):
560 """
561 Public method to get the variant of a virtual environment.
562
563 @param venvName logical name for the virtual environment
564 @type str
565 @return Python variant of the environment
566 @rtype str
567 """
568 if venvName in self.__virtualEnvironments:
569 return "Python{0}".format(
570 self.__virtualEnvironments[venvName]["variant"])
571 else:
572 return ""
573
574 def isGlobalEnvironment(self, venvName): 544 def isGlobalEnvironment(self, venvName):
575 """ 545 """
576 Public method to test, if a given environment is a global one. 546 Public method to test, if a given environment is a global one.
577 547
578 @param venvName logical name of the virtual environment 548 @param venvName logical name of the virtual environment

eric ide

mercurial