-
Notifications
You must be signed in to change notification settings - Fork 17
/
commands.py
315 lines (229 loc) · 10.3 KB
/
commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
"""Sublime Text commands for virtualenv management."""
import logging
import os.path
import shlex
import shutil
import sys
import sublime
import sublime_plugin
import Default as sublime_default
from . import virtualenv_lib as virtualenv
logger = logging.getLogger(__name__)
SETTINGS = "Virtualenv.sublime-settings"
def settings():
"""Return the settings of the plugin."""
return sublime.load_settings(SETTINGS)
def save_settings():
"""Save plugin settings to disk."""
sublime.save_settings(SETTINGS)
settings.save = save_settings
class InvalidVirtualenv(Exception):
"""Exception raised when the current virtualenv is missing or corrupt."""
def __init__(self, venv):
"""Construct automatic error message from virtualenv path."""
message = (
"Virtualenv at \"{}\" is missing, corrupt or has been deleted.".format(venv))
super(InvalidVirtualenv, self).__init__(message)
self.message = message
class VirtualenvCommand(sublime_plugin.WindowCommand):
"""Base command with virtualenv management functionality."""
@property
def virtualenv_exec(self):
"""Virtualenv executable as specified in the settings.
Returns a list with the command split by shlex.split().
"""
return shlex.split(settings().get('executable'))
@property
def virtualenv_directories(self):
"""The list of directory paths specified in the settings."""
return [os.path.expanduser(path)
for path in settings().get('virtualenv_directories')]
def get_virtualenv(self, validate=False, **kwargs):
"""Get current virtualenv from project data.
(It will work even if the project has not been saved.
The data is stored internally anyway.)
Takes an optional flag 'validate'. If True, the virtualenv
will be checked and if the validation fails 'InvalidVirtualenv'
is raised.
"""
venv = kwargs.pop('virtualenv', "")
if not venv:
project_data = self.window.project_data() or {}
venv = project_data.get('virtualenv', "")
venv = os.path.expanduser(venv)
if validate and venv and not virtualenv.is_valid(venv):
self.set_virtualenv(None)
raise InvalidVirtualenv(venv)
return venv
def set_virtualenv(self, venv):
"""Update the current virtualenv in project data.
If the passed venv in None, remove virtualenv data from project.
"""
project_data = self.window.project_data() or {}
if venv:
project_data['virtualenv'] = venv
sublime.status_message("({}) ACTIVATED".format(os.path.basename(venv)))
else:
try:
del project_data['virtualenv']
sublime.status_message("DEACTIVATED")
except KeyError:
pass
self.window.set_project_data(project_data)
logger.info("Current virtualenv set to \"{}\".".format(venv))
def find_virtualenvs(self):
"""Return a list of (basename, path) tuples of the found virtualenvs.
Searches in open folders and paths defined in the settings.
The result is valid as input for Sublime's quick panel.
"""
search_dirs = self.window.folders() + self.virtualenv_directories
venvs = virtualenv.find_virtualenvs(search_dirs)
return [[os.path.basename(venv), venv] for venv in venvs]
def find_pythons(self):
"""Find python executables in the system.
Searches in os.environ and additional directories defined
in the settings.
"""
extra_paths = tuple(settings().get('extra_paths', []))
return virtualenv.find_pythons(extra_paths=extra_paths)
class VirtualenvExecCommand(sublime_default.exec.ExecCommand, VirtualenvCommand):
"""Extends the default exec command adapting the build parameters."""
def run(self, **kwargs):
"""Exec the command with virtualenv.
If a virtualenv is active and valid update the build parameters
as needed and call the built-in command.
Else, if no virtualenv is active, do nothing and call the built-in
command.
Else, if the active virtualenv is invalid or corrupt display an error
message and cancel execution.
"""
try:
venv = self.get_virtualenv(validate=True, **kwargs)
except InvalidVirtualenv as error:
sublime.error_message(str(error) + " Execution cancelled!")
else:
if venv:
kwargs = self.update_exec_kwargs(venv, **kwargs)
logger.info("Command executed with virtualenv \"{}\".".format(venv))
super(VirtualenvExecCommand, self).run(**kwargs)
def update_exec_kwargs(self, venv, **kwargs):
"""Modify exec kwargs to make use of the virtualenv."""
postactivate = virtualenv.activate(venv)
kwargs['path'] = postactivate['path']
kwargs['env'] = dict(kwargs.get('env', {}), **postactivate['env'])
kwargs['env'].pop('PYTHONHOME', None)
# On OS X, avoid being run in a login shell, to preserve the virtualenv-ized PATH
if sys.platform == 'darwin' and 'shell_cmd' in kwargs:
kwargs['cmd'] = ['/bin/bash', '-c', kwargs['shell_cmd']]
del kwargs['shell_cmd']
return kwargs
class ActivateVirtualenvCommand(VirtualenvCommand):
"""Command for selecting active virtualenv."""
def run(self, **kwargs):
"""Display available virtualenvs in quick panel."""
self.available_venvs = self.find_virtualenvs()
self.window.show_quick_panel(self.available_venvs, self._set_virtualenv)
def _set_virtualenv(self, index):
if index != -1:
venv = self.available_venvs[index][1]
self.set_virtualenv(venv)
class DeactivateVirtualenvCommand(VirtualenvCommand):
""""Commmand for deactivating the virtualenv."""
def run(self, **kwargs):
"""Just delete the virtualenv entry from project data."""
self.set_virtualenv(None)
def is_enabled(self):
"""Only if the virtualenv entry is set."""
return bool(self.get_virtualenv())
class NewVirtualenvCommand(VirtualenvCommand):
"""Command for creating a new virtualenv."""
def run(self, **kwargs):
"""Show input panel requesting virtualenv destination."""
self.window.show_input_panel(
"Virtualenv Path:", self.virtualenv_directories[0] + os.path.sep,
self.get_python, None, None)
def get_python(self, venv):
"""Show available python binaries in quick panel.
Callback called with virtualenv destination.
"""
if not venv:
return
self.venv = os.path.expanduser(os.path.normpath(venv))
self.found_pythons = self.find_pythons()
self.window.show_quick_panel(self.found_pythons, self.create_virtualenv)
def create_virtualenv(self, python_index):
"""Execute the command and create the virtualenv.
Callback called with selected python.
Constructs the appropriate command with the virtualenv command,
the selected python and the destination.
Delegates command execution to the built-in exec command, so
the process can be killed.
It will also activate the created virtualenv.
"""
cmd = self.virtualenv_exec
if python_index != -1:
python = self.found_pythons[python_index]
cmd += ['-p', python]
cmd += [self.venv]
self.window.run_command('exec', {'cmd': cmd})
self.set_virtualenv(self.venv)
class NewBuiltinVirtualenvCommand(NewVirtualenvCommand):
"""Command for creating a new virtualenv with built-in venv module."""
def find_pythons(self):
"""With venv module available."""
extra_paths = tuple(settings().get('extra_paths', []))
return virtualenv.find_pythons(
extra_paths=extra_paths, req_modules=('venv', ))
def create_virtualenv(self, python_index):
"""With the venv module installed in the selected python."""
if python_index == -1:
return
python = self.found_pythons[python_index]
cmd = [python, "-m", "venv", self.venv]
self.window.run_command('exec', {'cmd': cmd})
self.set_virtualenv(self.venv)
class RemoveVirtualenvCommand(VirtualenvCommand):
"""Command for deleting virtualenv directories."""
def run(self, **kwargs):
"""Display quick panel with found virtualenvs."""
self.available_venvs = self.find_virtualenvs()
self.window.show_quick_panel(self.available_venvs, self.remove_virtualenv)
def remove_virtualenv(self, index):
"""Request confirmation and delete the directory tree.
Also set current virtualenv to None.
"""
if index == -1:
return
venv = self.available_venvs[index][1]
confirmed = sublime.ok_cancel_dialog(
"Please confirm deletion of virtualenv at:\n\"{}\".".format(venv))
if confirmed:
try:
shutil.rmtree(venv)
logger.info("\"{}\" deleted.".format(venv))
except (IOError, OSError):
logger.error("Could not delete \"{}\".".format(venv))
else:
if venv == self.get_virtualenv():
self.set_virtualenv(None)
class AddVirtualenvDirectoryCommand(VirtualenvCommand):
"""Shortcut command for adding paths to the 'virtualenv_directories' setting."""
def run(self, **kwargs):
"""Shot input panel requesting path to user."""
self.window.show_input_panel(
"Directory path:", os.path.expanduser("~") + os.path.sep,
self.add_directory, None, None)
def add_directory(self, directory):
"""Add given directory to the list.
If the path is not a directory show error dialog.
"""
if not directory:
return
directory = os.path.expanduser(os.path.normpath(directory))
if not os.path.isdir(directory):
sublime.error_message("\"{}\" is not a directory.".format(directory))
return
directories = self.virtualenv_directories
directories.append(directory)
settings().set('virtualenv_directories', directories)
settings.save()