VirtualEnv/VirtualenvManager.py

changeset 6362
ec32d1d7f525
parent 6349
17b3c75913de
child 6381
37f23590dbbc
equal deleted inserted replaced
6361:53f6bd7fb238 6362:ec32d1d7f525
54 # each environment entry is a dictionary: 54 # each environment entry is a dictionary:
55 # path: the directory of the virtual environment 55 # path: the directory of the virtual environment
56 # (empty for a global environment) 56 # (empty for a global environment)
57 # interpreter: the path of the Python interpreter 57 # interpreter: the path of the Python interpreter
58 # variant: Python variant (2 or 3) 58 # variant: Python variant (2 or 3)
59 # is_global: a flag indicating a global environment
59 # 60 #
60 for venvName in environments: 61 for venvName in environments:
61 interpreter = environments[venvName]["interpreter"] 62 interpreter = environments[venvName]["interpreter"]
62 if os.access(interpreter, os.X_OK): 63 if os.access(interpreter, os.X_OK):
63 self.__virtualEnvironments[venvName] = environments[venvName] 64 environment = environments[venvName]
65 if "is_global" not in environment:
66 environment["is_global"] = environment["path"] == ""
67 self.__virtualEnvironments[venvName] = environment
64 68
65 # check, if the interpreter used to run eric is in the environments 69 # check, if the interpreter used to run eric is in the environments
66 defaultPy = sys.executable.replace("w.exe", ".exe") 70 defaultPy = sys.executable.replace("w.exe", ".exe")
67 found = False 71 found = False
68 for venvName in self.__virtualEnvironments: 72 for venvName in self.__virtualEnvironments:
74 # add an environment entry for the default interpreter 78 # add an environment entry for the default interpreter
75 self.__virtualEnvironments["<default>"] = { 79 self.__virtualEnvironments["<default>"] = {
76 "path": "", 80 "path": "",
77 "interpreter": defaultPy, 81 "interpreter": defaultPy,
78 "variant": sys.version_info[0], 82 "variant": sys.version_info[0],
83 "is_global": True,
79 } 84 }
80 85
81 self.__saveSettings() 86 self.__saveSettings()
82 87
83 def __saveSettings(self): 88 def __saveSettings(self):
110 dia.show() 115 dia.show()
111 dia.start(args) 116 dia.start(args)
112 dia.exec_() 117 dia.exec_()
113 118
114 def addVirtualEnv(self, venvName, venvDirectory, venvInterpreter="", 119 def addVirtualEnv(self, venvName, venvDirectory, venvInterpreter="",
115 venvVariant=3): 120 venvVariant=3, isGlobal=False):
116 """ 121 """
117 Public method to add a virtual environment. 122 Public method to add a virtual environment.
118 123
119 @param venvName logical name for the virtual environment 124 @param venvName logical name for the virtual environment
120 @type str 125 @type str
122 @type str 127 @type str
123 @param venvInterpreter interpreter of the virtual environment 128 @param venvInterpreter interpreter of the virtual environment
124 @type str 129 @type str
125 @param venvVariant Python variant of the virtual environment 130 @param venvVariant Python variant of the virtual environment
126 @type int 131 @type int
132 @param isGlobal flag indicating a global environment
133 @type bool
127 """ 134 """
128 if venvName in self.__virtualEnvironments: 135 if venvName in self.__virtualEnvironments:
129 ok = E5MessageBox.yesNo( 136 ok = E5MessageBox.yesNo(
130 None, 137 None,
131 self.tr("Add Virtual Environment"), 138 self.tr("Add Virtual Environment"),
140 from .VirtualenvInterpreterSelectionDialog import \ 147 from .VirtualenvInterpreterSelectionDialog import \
141 VirtualenvInterpreterSelectionDialog 148 VirtualenvInterpreterSelectionDialog
142 dlg = VirtualenvInterpreterSelectionDialog(venvName, venvDirectory) 149 dlg = VirtualenvInterpreterSelectionDialog(venvName, venvDirectory)
143 if dlg.exec_() == QDialog.Accepted: 150 if dlg.exec_() == QDialog.Accepted:
144 venvInterpreter, venvVariant = dlg.getData() 151 venvInterpreter, venvVariant = dlg.getData()
152 isGlobal = True
145 153
146 if venvInterpreter: 154 if venvInterpreter:
147 self.__virtualEnvironments[venvName] = { 155 self.__virtualEnvironments[venvName] = {
148 "path": venvDirectory, 156 "path": venvDirectory,
149 "interpreter": venvInterpreter, 157 "interpreter": venvInterpreter,
150 "variant": venvVariant, 158 "variant": venvVariant,
159 "is_global": isGlobal,
151 } 160 }
152 161
153 self.__saveSettings() 162 self.__saveSettings()
154 163
155 if self.__virtualenvManagerDialog: 164 if self.__virtualenvManagerDialog:
156 self.__virtualenvManagerDialog.refresh() 165 self.__virtualenvManagerDialog.refresh()
157 166
158 def setVirtualEnv(self, venvName, venvDirectory, venvInterpreter, 167 def setVirtualEnv(self, venvName, venvDirectory, venvInterpreter,
159 venvVariant): 168 venvVariant, isGlobal):
160 """ 169 """
161 Public method to change a virtual environment. 170 Public method to change a virtual environment.
162 171
163 @param venvName logical name of the virtual environment 172 @param venvName logical name of the virtual environment
164 @type str 173 @type str
166 @type str 175 @type str
167 @param venvInterpreter interpreter of the virtual environment 176 @param venvInterpreter interpreter of the virtual environment
168 @type str 177 @type str
169 @param venvVariant Python variant of the virtual environment 178 @param venvVariant Python variant of the virtual environment
170 @type int 179 @type int
180 @param isGlobal flag indicating a global environment
181 @type bool
171 """ 182 """
172 if venvName not in self.__virtualEnvironments: 183 if venvName not in self.__virtualEnvironments:
173 E5MessageBox.yesNo( 184 E5MessageBox.yesNo(
174 None, 185 None,
175 self.tr("Change Virtual Environment"), 186 self.tr("Change Virtual Environment"),
181 192
182 self.__virtualEnvironments[venvName] = { 193 self.__virtualEnvironments[venvName] = {
183 "path": venvDirectory, 194 "path": venvDirectory,
184 "interpreter": venvInterpreter, 195 "interpreter": venvInterpreter,
185 "variant": venvVariant, 196 "variant": venvVariant,
197 "is_global": isGlobal,
186 } 198 }
187 199
188 self.__saveSettings() 200 self.__saveSettings()
189 201
190 if self.__virtualenvManagerDialog: 202 if self.__virtualenvManagerDialog:
191 self.__virtualenvManagerDialog.refresh() 203 self.__virtualenvManagerDialog.refresh()
192 204
193 def renameVirtualEnv(self, oldVenvName, venvName, venvDirectory, 205 def renameVirtualEnv(self, oldVenvName, venvName, venvDirectory,
194 venvInterpreter, venvVariant): 206 venvInterpreter, venvVariant, isGlobal):
195 """ 207 """
196 Public method to substitute a virtual environment entry with a new 208 Public method to substitute a virtual environment entry with a new
197 name. 209 name.
198 210
199 @param oldVenvName old name of the virtual environment 211 @param oldVenvName old name of the virtual environment
204 @type str 216 @type str
205 @param venvInterpreter interpreter of the virtual environment 217 @param venvInterpreter interpreter of the virtual environment
206 @type str 218 @type str
207 @param venvVariant Python variant of the virtual environment 219 @param venvVariant Python variant of the virtual environment
208 @type int 220 @type int
221 @param isGlobal flag indicating a global environment
222 @type bool
209 """ 223 """
210 if oldVenvName not in self.__virtualEnvironments: 224 if oldVenvName not in self.__virtualEnvironments:
211 E5MessageBox.yesNo( 225 E5MessageBox.yesNo(
212 None, 226 None,
213 self.tr("Rename Virtual Environment"), 227 self.tr("Rename Virtual Environment"),
217 icon=E5MessageBox.Warning) 231 icon=E5MessageBox.Warning)
218 return 232 return
219 233
220 del self.__virtualEnvironments[oldVenvName] 234 del self.__virtualEnvironments[oldVenvName]
221 self.addVirtualEnv(venvName, venvDirectory, venvInterpreter, 235 self.addVirtualEnv(venvName, venvDirectory, venvInterpreter,
222 venvVariant) 236 venvVariant, isGlobal)
223 237
224 def deleteVirtualEnvs(self, venvNames): 238 def deleteVirtualEnvs(self, venvNames):
225 """ 239 """
226 Public method to delete virtual environments from the list and disk. 240 Public method to delete virtual environments from the list and disk.
227 241
244 """ environments?"""), 258 """ environments?"""),
245 venvMessages 259 venvMessages
246 ) 260 )
247 if dlg.exec_() == QDialog.Accepted: 261 if dlg.exec_() == QDialog.Accepted:
248 for venvName in venvNames: 262 for venvName in venvNames:
249 if venvName in self.__virtualEnvironments and \ 263 if self.__isEnvironmentDeleteable(venvName):
250 bool(self.__virtualEnvironments[venvName]["path"]):
251 shutil.rmtree( 264 shutil.rmtree(
252 self.__virtualEnvironments[venvName]["path"], True) 265 self.__virtualEnvironments[venvName]["path"], True)
253 del self.__virtualEnvironments[venvName] 266 del self.__virtualEnvironments[venvName]
254 267
255 self.__saveSettings() 268 self.__saveSettings()
256 269
257 if self.__virtualenvManagerDialog: 270 if self.__virtualenvManagerDialog:
258 self.__virtualenvManagerDialog.refresh() 271 self.__virtualenvManagerDialog.refresh()
272
273 def __isEnvironmentDeleteable(self, venvName):
274 """
275 Private method to check, if a virtual environment can be deleted from
276 disk.
277
278 @param venvName name of the virtual environment
279 @type str
280 @return flag indicating it can be deleted
281 @rtype bool
282 """
283 ok = False
284 if venvName in self.__virtualEnvironments:
285 ok = True
286 ok &= bool(self.__virtualEnvironments[venvName]["path"])
287 ok &= not self.__virtualEnvironments[venvName]["is_global"]
288 ok &= os.access(self.__virtualEnvironments[venvName]["path"],
289 os.W_OK)
290
291 return ok
259 292
260 def removeVirtualEnvs(self, venvNames): 293 def removeVirtualEnvs(self, venvNames):
261 """ 294 """
262 Public method to delete virtual environment from the list. 295 Public method to delete virtual environment from the list.
263 296
384 for venvName in self.__virtualEnvironments: 417 for venvName in self.__virtualEnvironments:
385 if self.__virtualEnvironments[venvName]["variant"] == variant: 418 if self.__virtualEnvironments[venvName]["variant"] == variant:
386 environments.append(venvName) 419 environments.append(venvName)
387 420
388 return environments 421 return environments
422
423 def isGlobalEnvironment(self, venvName):
424 """
425 Public method to test, if a given environment is a global one.
426
427 @param venvName logical name of the virtual environment
428 @type str
429 @return flag indicating a global environment
430 @rtype bool
431 """
432 return self.__virtualEnvironments[venvName]["is_global"]

eric ide

mercurial