|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2018 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a class to manage Python virtual environments. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 import sys |
|
14 import shutil |
|
15 |
|
16 from PyQt5.QtCore import pyqtSlot, QObject |
|
17 from PyQt5.QtWidgets import QDialog |
|
18 |
|
19 from E5Gui import E5MessageBox |
|
20 |
|
21 import Preferences |
|
22 |
|
23 |
|
24 class VirtualenvManager(QObject): |
|
25 """ |
|
26 Class implementing an object to manage Python virtual environments. |
|
27 """ |
|
28 def __init__(self, parent=None): |
|
29 """ |
|
30 Constructor |
|
31 |
|
32 @param parent reference to the parent object |
|
33 @type QWidget |
|
34 """ |
|
35 super(VirtualenvManager, self).__init__(parent) |
|
36 |
|
37 self.__ui = parent |
|
38 |
|
39 self.__virtualEnvironments = {} |
|
40 self.__virtualEnvironmentInterpreters = {} |
|
41 environments = Preferences.toDict(Preferences.Prefs.settings.value( |
|
42 "PyVenv/Environments", {})) |
|
43 interpreters = Preferences.toDict(Preferences.Prefs.settings.value( |
|
44 "PyVenv/Interpreters", {})) |
|
45 for venvName, venvExe in interpreters.items(): |
|
46 # remove all environments, that don't exist anymore |
|
47 if os.access(venvExe, os.X_OK): |
|
48 self.__virtualEnvironmentInterpreters[venvName] = venvExe |
|
49 self.__virtualEnvironments[venvName] = environments[venvName] |
|
50 |
|
51 defaultPy = sys.executable.replace("w.exe", ".exe") |
|
52 if defaultPy not in self.__virtualEnvironmentInterpreters.values(): |
|
53 self.__virtualEnvironmentInterpreters["<default>"] = defaultPy |
|
54 self.__virtualEnvironments["<default>"] = "" |
|
55 |
|
56 self.__updateSettings() |
|
57 |
|
58 def __updateSettings(self): |
|
59 """ |
|
60 Private slot to save the virtual environments. |
|
61 """ |
|
62 Preferences.Prefs.settings.setValue( |
|
63 "PyVenv/Environments", self.__virtualEnvironments) |
|
64 Preferences.Prefs.settings.setValue( |
|
65 "PyVenv/Interpreters", self.__virtualEnvironmentInterpreters) |
|
66 |
|
67 @pyqtSlot() |
|
68 def createVirtualEnv(self): |
|
69 """ |
|
70 Public slot to create a new virtual environment. |
|
71 """ |
|
72 from .VirtualenvConfigurationDialog import \ |
|
73 VirtualenvConfigurationDialog |
|
74 |
|
75 dlg = VirtualenvConfigurationDialog() |
|
76 if dlg.exec_() == QDialog.Accepted: |
|
77 (pyvenv, args, name, openTarget, createLog, createScript, |
|
78 targetDir, interpreter) = dlg.getData() |
|
79 |
|
80 # now do the call |
|
81 from .VirtualenvExecDialog import VirtualenvExecDialog |
|
82 dia = VirtualenvExecDialog(pyvenv, targetDir, name, openTarget, |
|
83 createLog, createScript, interpreter, |
|
84 self) |
|
85 dia.show() |
|
86 dia.start(args) |
|
87 dia.exec_() |
|
88 |
|
89 def addVirtualEnv(self, venvName, venvDirectory): |
|
90 """ |
|
91 Public method to add a virtual environment. |
|
92 |
|
93 @param venvName logical name for the virtual environment |
|
94 @type str |
|
95 @param venvDirectory directory of the virtual envoronment |
|
96 @type str |
|
97 """ |
|
98 if venvName in self.__virtualEnvironments: |
|
99 ok = E5MessageBox.yesNo( |
|
100 None, |
|
101 self.tr("Add Virtual Environment"), |
|
102 self.tr("""A virtual environment named <b>{0}</b> exists""" |
|
103 """ already. Shall it be replaced?""") |
|
104 .format(venvName), |
|
105 icon=E5MessageBox.Warning) |
|
106 if not ok: |
|
107 return |
|
108 |
|
109 from .VirtualenvInterpreterSelectionDialog import \ |
|
110 VirtualenvInterpreterSelectionDialog |
|
111 dlg = VirtualenvInterpreterSelectionDialog(venvName, venvDirectory) |
|
112 if dlg.exec_() == QDialog.Accepted: |
|
113 venvExe = dlg.getData() |
|
114 self.__virtualEnvironmentInterpreters[venvName] = venvExe |
|
115 self.__virtualEnvironments[venvName] = venvDirectory |
|
116 |
|
117 self.__updateSettings() |
|
118 |
|
119 def deleteVirtualEnv(self, venvName): |
|
120 """ |
|
121 Public method to delete a virtual environment from disk. |
|
122 |
|
123 @param venvName logical name for the virtual environment |
|
124 @type str |
|
125 """ |
|
126 if venvName in self.__virtualEnvironments: |
|
127 ok = E5MessageBox.yesNo( |
|
128 None, |
|
129 self.tr("Delete Virtual Environment"), |
|
130 self.tr("""Do you really want to delete the virtual""" |
|
131 """ environment <b>{0}</b>?<br>Path: {1}""") |
|
132 .format(venvName, self.__virtualEnvironments[venvName])) |
|
133 if ok: |
|
134 shutil.rmtree(self.__virtualEnvironments[venvName], True) |
|
135 del self.__virtualEnvironments[venvName] |
|
136 del self.__virtualEnvironmentInterpreters[venvName] |
|
137 |
|
138 self.__updateSettings() |