1
|
|
|
import os |
2
|
|
|
import subprocess |
3
|
|
|
import sys |
4
|
|
|
|
5
|
|
|
import json |
6
|
|
|
|
7
|
|
|
import sublime |
8
|
|
|
import sublime_plugin |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class CondaCommand(sublime_plugin.WindowCommand): |
12
|
|
|
"""Contains all of the attributes that will be inherited by other commands.""" |
13
|
|
|
|
14
|
|
|
@property |
15
|
|
|
def settings(self): |
16
|
|
|
"""Load the plugin settings for commands to use. |
17
|
|
|
|
18
|
|
|
For Unix systems, the plugin will first try to load the old settings |
19
|
|
|
file with the base name 'conda.sublime-settings'. If this file can't |
20
|
|
|
be accessed the plugin will set the settings file to |
21
|
|
|
the new settings file named 'Conda.sublime-settings'. |
22
|
|
|
""" |
23
|
|
|
if sys.platform.startswith('win'): |
24
|
|
|
return sublime.load_settings('Conda (Windows).sublime-settings') |
25
|
|
|
else: |
26
|
|
|
try: |
27
|
|
|
settings = sublime.load_settings('conda.sublime-settings') |
28
|
|
|
# sublime text doesn't tell us if the settings file exists unless we try to access it |
29
|
|
|
os.path.expanduser(settings.get('environment_directory')) |
30
|
|
|
except AttributeError: |
31
|
|
|
settings = sublime.load_settings('Conda.sublime-settings') |
32
|
|
|
|
33
|
|
|
return settings |
34
|
|
|
|
35
|
|
|
@property |
36
|
|
|
def executable(self): |
37
|
|
|
"""Retrieve the python executable path from settings.""" |
38
|
|
|
return os.path.expanduser(self.settings.get('executable')) |
39
|
|
|
|
40
|
|
|
@property |
41
|
|
|
def configuration(self): |
42
|
|
|
"""Retrieve the conda configuration file from settings.""" |
43
|
|
|
return os.path.expanduser(self.settings.get('configuration')) |
44
|
|
|
|
45
|
|
|
@property |
46
|
|
|
def root_directory(self): |
47
|
|
|
"""Retrieve the directory of conda's root environment.""" |
48
|
|
|
if sys.platform == 'win32': |
49
|
|
|
root_directory = os.path.dirname(self.executable) |
50
|
|
|
else: |
51
|
|
|
root_directory = os.path.dirname(self.executable).replace('bin', '') |
52
|
|
|
|
53
|
|
|
return root_directory |
54
|
|
|
|
55
|
|
|
@property |
56
|
|
|
def conda_environments(self): |
57
|
|
|
"""Find all conda environments in the specified directory.""" |
58
|
|
|
try: |
59
|
|
|
directory = os.path.expanduser(self.settings.get('environment_directory')) |
60
|
|
|
|
61
|
|
|
environments = [['root', self.root_directory]] |
62
|
|
|
environments.extend([[environment, os.path.join(directory, environment)] |
63
|
|
|
for environment in os.listdir(directory)]) |
64
|
|
|
|
65
|
|
|
return environments |
66
|
|
|
|
67
|
|
|
except FileNotFoundError: |
68
|
|
|
return [['root', self.root_directory]] |
69
|
|
|
|
70
|
|
|
@property |
71
|
|
|
def project_data(self): |
72
|
|
|
"""Retrieve the project data to be used in the current window.""" |
73
|
|
|
if self.window.project_data() is None: |
74
|
|
|
return {} |
75
|
|
|
else: |
76
|
|
|
return self.window.project_data() |
77
|
|
|
|
78
|
|
|
@property |
79
|
|
|
def startupinfo(self): |
80
|
|
|
"""Property used to hide command prompts when on Windows platforms.""" |
81
|
|
|
startupinfo = None |
82
|
|
|
|
83
|
|
|
if sys.platform == 'win32': |
84
|
|
|
startupinfo = subprocess.STARTUPINFO() |
85
|
|
|
|
86
|
|
|
if sys.version_info.major == 3: |
87
|
|
|
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
88
|
|
|
else: |
89
|
|
|
startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW |
90
|
|
|
|
91
|
|
|
return startupinfo |
92
|
|
|
|
93
|
|
|
def retrieve_environment_name(self, path): |
94
|
|
|
"""Retrieve the environment name from the active environment path. |
95
|
|
|
|
96
|
|
|
If the active environment is the root environment, 'root' must be |
97
|
|
|
returned instead of the basename from the environment path. |
98
|
|
|
""" |
99
|
|
|
if path == self.root_directory: |
100
|
|
|
return 'root' |
101
|
|
|
else: |
102
|
|
|
return os.path.basename(path) |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
class CreateCondaEnvironmentCommand(CondaCommand): |
106
|
|
|
"""Contains the methods needed to create a conda environment.""" |
107
|
|
|
|
108
|
|
|
def run(self): |
109
|
|
|
"""Display 'Conda: Create' in Sublime Text's command palette. |
110
|
|
|
|
111
|
|
|
When 'Conda: Create' is clicked by the user, Sublime's text input |
112
|
|
|
box will show allowing the user to input the name of environment. |
113
|
|
|
This environment name is then passed to the create_environment |
114
|
|
|
method. |
115
|
|
|
""" |
116
|
|
|
self.window.show_input_panel('Conda Environment Name:', '', |
117
|
|
|
self.retrieve_python_version, None, None) |
118
|
|
|
|
119
|
|
|
def retrieve_python_version(self, environment): |
120
|
|
|
"""Display a list of available Python versions for the environment. |
121
|
|
|
|
122
|
|
|
Forcing the user to select the Python version allows conda to create |
123
|
|
|
a new Python executable inside the environment directory. |
124
|
|
|
""" |
125
|
|
|
self.environment = environment |
126
|
|
|
|
127
|
|
|
python_versions = ['Python 2.7', 'Python 3.5', 'Python 3.6'] |
128
|
|
|
|
129
|
|
|
self.window.show_quick_panel(python_versions, self.create_environment) |
130
|
|
|
|
131
|
|
|
def create_environment(self, index): |
132
|
|
|
"""Create a conda environment in the envs directory.""" |
133
|
|
|
if index != -1: |
134
|
|
|
if index == 0: |
135
|
|
|
python_version = 'python=2.7' |
136
|
|
|
elif index == 1: |
137
|
|
|
python_version = 'python=3.5' |
138
|
|
|
else: |
139
|
|
|
python_version = 'python=3.6' |
140
|
|
|
|
141
|
|
|
cmd = [self.executable, '-m', 'conda', 'create', |
142
|
|
|
'--name', self.environment, python_version, '-y', '-q'] |
143
|
|
|
|
144
|
|
|
self.window.run_command('exec', {'cmd': cmd}) |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
class RemoveCondaEnvironmentCommand(CondaCommand): |
148
|
|
|
"""Contains the methods needed to remove a conda environment.""" |
149
|
|
|
|
150
|
|
|
def run(self): |
151
|
|
|
"""Display 'Conda: Remove' in Sublime Text's command palette. |
152
|
|
|
|
153
|
|
|
When 'Conda: Removed' is clicked by the user, the command |
154
|
|
|
palette whill show all conda environments available for removal. |
155
|
|
|
The index of the selected environment is then passed to the |
156
|
|
|
remove_environment method" |
157
|
|
|
""" |
158
|
|
|
self.window.show_quick_panel(self.conda_environments, |
159
|
|
|
self.remove_environment) |
160
|
|
|
|
161
|
|
|
def remove_environment(self, index): |
162
|
|
|
"""Remove a conda environment from the envs directory.""" |
163
|
|
|
if index != -1: |
164
|
|
|
environment = self.conda_environments[index][0] |
165
|
|
|
|
166
|
|
|
cmd = [self.executable, '-m', 'conda', 'remove', |
167
|
|
|
'--name', environment, '--all', '-y', '-q'] |
168
|
|
|
|
169
|
|
|
self.window.run_command('exec', {'cmd': cmd}) |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
class ListCondaEnvironmentCommand(CondaCommand): |
173
|
|
|
"""Contains the methods needed to list available conda environments.""" |
174
|
|
|
|
175
|
|
|
def run(self): |
176
|
|
|
"""Display 'Conda: List' in Sublime Text's command palette. |
177
|
|
|
|
178
|
|
|
When 'Conda: List' is clicked by the user, the command |
179
|
|
|
palette will show all available conda environments. |
180
|
|
|
""" |
181
|
|
|
self.window.show_quick_panel(self.conda_environments, |
182
|
|
|
None) |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
class ActivateCondaEnvironmentCommand(CondaCommand): |
186
|
|
|
"""Contains the methods needed to activate a conda environment.""" |
187
|
|
|
|
188
|
|
|
def run(self): |
189
|
|
|
"""Display 'Conda: Activate' in Sublime Text's command palette. |
190
|
|
|
|
191
|
|
|
When 'Conda: Activate' is clicked by the user, the command |
192
|
|
|
palette will show all available conda environments. The |
193
|
|
|
clicked environment will be activated as the current environment. |
194
|
|
|
""" |
195
|
|
|
self.window.show_quick_panel(self.conda_environments, |
196
|
|
|
self.activate_environment) |
197
|
|
|
|
198
|
|
|
def activate_environment(self, index): |
199
|
|
|
"""Activate the environment selected from the command palette.""" |
200
|
|
|
if index != -1: |
201
|
|
|
project_data = self.project_data |
202
|
|
|
|
203
|
|
|
project_data['conda_environment'] = self.conda_environments[index][1] |
204
|
|
|
|
205
|
|
|
self.window.set_project_data(project_data) |
206
|
|
|
|
207
|
|
|
sublime.status_message('Activated conda environment: {}' |
208
|
|
|
.format(self.conda_environments[index][0])) |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
class DeactivateCondaEnvironmentCommand(CondaCommand): |
212
|
|
|
"""Contains the methods needed to deactivate a conda environment.""" |
213
|
|
|
|
214
|
|
|
def run(self): |
215
|
|
|
"""Display 'Conda: Deactivate' in Sublime Text's command palette. |
216
|
|
|
|
217
|
|
|
When 'Conda: Deactivate' is clicked by the user, the command |
218
|
|
|
palette will show all available conda environments. The |
219
|
|
|
clicked environment will be deactivated. |
220
|
|
|
""" |
221
|
|
|
self.window.show_quick_panel(self.active_environment, |
222
|
|
|
self.deactivate_environment) |
223
|
|
|
|
224
|
|
|
@property |
225
|
|
|
def active_environment(self): |
226
|
|
|
"""Retrieve the active conda environment.""" |
227
|
|
|
try: |
228
|
|
|
environment_path = self.project_data['conda_environment'] |
229
|
|
|
environment_name = self.retrieve_environment_name(environment_path) |
230
|
|
|
|
231
|
|
|
return [[environment_name, os.path.dirname(environment_path)]] |
232
|
|
|
|
233
|
|
|
except KeyError: |
234
|
|
|
return ['No Active Conda Environment'] |
235
|
|
|
|
236
|
|
|
def deactivate_environment(self, index): |
237
|
|
|
"""Deactivate the environment selected in the command palette.""" |
238
|
|
|
if index != -1: |
239
|
|
|
try: |
240
|
|
|
project_data = self.project_data |
241
|
|
|
|
242
|
|
|
del project_data['conda_environment'] |
243
|
|
|
|
244
|
|
|
self.window.set_project_data(project_data) |
245
|
|
|
|
246
|
|
|
sublime.status_message('Deactivated conda environment: {}' |
247
|
|
|
.format(self.conda_environments[index][0])) |
248
|
|
|
except KeyError: |
249
|
|
|
sublime.status_message('No active conda environment') |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
class ListCondaPackageCommand(CondaCommand): |
253
|
|
|
"""Contains all of the methods needed to list all installed packages.""" |
254
|
|
|
|
255
|
|
|
def run(self): |
256
|
|
|
"""Display 'Conda: List' in Sublime Text's command palette. |
257
|
|
|
|
258
|
|
|
When 'Conda: List' is clicked by the user, the build output |
259
|
|
|
displays all packages installed in the current environment. |
260
|
|
|
""" |
261
|
|
|
self.window.show_quick_panel(self.environment_packages, None) |
262
|
|
|
|
263
|
|
View Code Duplication |
@property |
|
|
|
|
264
|
|
|
def environment_packages(self): |
265
|
|
|
"""List each package name and version installed in the environment.""" |
266
|
|
|
try: |
267
|
|
|
environment_path = self.project_data['conda_environment'] |
268
|
|
|
environment = self.retrieve_environment_name(environment_path) |
269
|
|
|
|
270
|
|
|
package_data = subprocess.check_output([self.executable, '-m', 'conda', 'list', |
271
|
|
|
'--name', environment], |
272
|
|
|
startupinfo=self.startupinfo, universal_newlines=True) |
273
|
|
|
|
274
|
|
|
packages = package_data.splitlines()[2:] |
275
|
|
|
package_names = [packages[i].split()[0] for i, _ in enumerate(packages)] |
276
|
|
|
|
277
|
|
|
return package_names |
278
|
|
|
|
279
|
|
|
except KeyError: |
280
|
|
|
return ['No Active Conda Environment'] |
281
|
|
|
|
282
|
|
|
|
283
|
|
|
class InstallCondaPackageCommand(CondaCommand): |
284
|
|
|
"""Contains all of the methods needed to install a conda package.""" |
285
|
|
|
|
286
|
|
|
def run(self): |
287
|
|
|
"""Display an input box allowing the user to input a package name.""" |
288
|
|
|
self.window.show_input_panel('Package Name:', '', self.install_package, |
289
|
|
|
None, None) |
290
|
|
|
|
291
|
|
|
def install_package(self, package): |
292
|
|
|
"""Install the given package name via conda.""" |
293
|
|
|
try: |
294
|
|
|
environment_path = self.project_data['conda_environment'] |
295
|
|
|
environment = self.retrieve_environment_name(environment_path) |
296
|
|
|
cmd = [self.executable, '-m', 'conda', 'install', package, |
297
|
|
|
'--name', environment, '-y', '-q'] |
298
|
|
|
self.window.run_command('exec', {'cmd': cmd}) |
299
|
|
|
|
300
|
|
|
except KeyError: |
301
|
|
|
sublime.status_message('No active conda environment.') |
302
|
|
|
|
303
|
|
|
|
304
|
|
|
class RemoveCondaPackageCommand(CondaCommand): |
305
|
|
|
"""Contains all of the methods needed to remove a conda package.""" |
306
|
|
|
|
307
|
|
|
def run(self): |
308
|
|
|
"""Display an input box allowing the user to pick a package to remove.""" |
309
|
|
|
self.window.show_quick_panel(self.environment_packages, self.remove_package) |
310
|
|
|
|
311
|
|
View Code Duplication |
@property |
|
|
|
|
312
|
|
|
def environment_packages(self): |
313
|
|
|
"""List each package name and version installed in the environment. |
314
|
|
|
|
315
|
|
|
This property had to be duplicated as the attribute from |
316
|
|
|
ListCondaPackageCommand could not be inherited properly. |
317
|
|
|
""" |
318
|
|
|
try: |
319
|
|
|
environment_path = self.project_data['conda_environment'] |
320
|
|
|
environment = self.retrieve_environment_name(environment_path) |
321
|
|
|
|
322
|
|
|
package_data = subprocess.check_output([self.executable, '-m', 'conda', 'list', |
323
|
|
|
'--name', environment], |
324
|
|
|
startupinfo=self.startupinfo, universal_newlines=True) |
325
|
|
|
|
326
|
|
|
packages = package_data.splitlines()[2:] |
327
|
|
|
package_names = [packages[i].split()[0] for i, _ in enumerate(packages)] |
328
|
|
|
|
329
|
|
|
return package_names |
330
|
|
|
|
331
|
|
|
except KeyError: |
332
|
|
|
return ['No Active Conda Environment'] |
333
|
|
|
|
334
|
|
|
def remove_package(self, index): |
335
|
|
|
"""Remove the given package name via conda.""" |
336
|
|
|
if index != -1: |
337
|
|
|
package_to_remove = self.environment_packages[index] |
338
|
|
|
|
339
|
|
|
environment_path = self.project_data['conda_environment'] |
340
|
|
|
|
341
|
|
|
environment = self.retrieve_environment_name(environment_path) |
342
|
|
|
|
343
|
|
|
cmd = [self.executable, '-m', 'conda', 'remove', package_to_remove, |
344
|
|
|
'--name', environment, '-y', '-q'] |
345
|
|
|
|
346
|
|
|
self.window.run_command('exec', {'cmd': cmd}) |
347
|
|
|
|
348
|
|
|
|
349
|
|
|
class ListCondaChannelsCommand(CondaCommand): |
350
|
|
|
"""Contains all of the methods needed to display conda's channel sources.""" |
351
|
|
|
|
352
|
|
|
def run(self): |
353
|
|
|
"""Display 'Conda: List Channel Sources' in Sublime Text's command palette. |
354
|
|
|
|
355
|
|
|
When 'Conda: List Channel Sources' is clicked by the user, |
356
|
|
|
the command palette displays all of the channel sources found |
357
|
|
|
in the condarc configuration file. |
358
|
|
|
""" |
359
|
|
|
self.window.show_quick_panel(self.channel_sources, None) |
360
|
|
|
|
361
|
|
|
@property |
362
|
|
|
def channel_sources(self): |
363
|
|
|
"""List each channel source found in the condarc configuration file.""" |
364
|
|
|
sources = subprocess.check_output([self.executable, '-m', 'conda', 'config', |
365
|
|
|
'--show-sources', '--json'], |
366
|
|
|
startupinfo=self.startupinfo) |
367
|
|
|
sources = json.loads(sources.decode()) |
368
|
|
|
|
369
|
|
|
try: |
370
|
|
|
return sources[self.configuration]['channels'] |
371
|
|
|
|
372
|
|
|
except KeyError: |
373
|
|
|
return ['No Channel Sources Available'] |
374
|
|
|
|
375
|
|
|
|
376
|
|
|
class SearchCondaPackageCommand(CondaCommand): |
377
|
|
|
"""Contains all of the methods needed to search for a conda package.""" |
378
|
|
|
|
379
|
|
|
def run(self): |
380
|
|
|
"""Display an input box allowing the user to input a package name.""" |
381
|
|
|
self.window.show_input_panel('Package Name:', '', self.search_package, |
382
|
|
|
None, None) |
383
|
|
|
|
384
|
|
|
def search_package(self, package): |
385
|
|
|
"""Search for a package included in the defaults channel.""" |
386
|
|
|
cmd = [self.executable, '-m', 'conda', 'search', package] |
387
|
|
|
self.window.run_command('exec', {'cmd': cmd}) |
388
|
|
|
|
389
|
|
|
|
390
|
|
|
class AddCondaChannelCommand(CondaCommand): |
391
|
|
|
"""Contains all of the methods needed to add a conda channel source.""" |
392
|
|
|
|
393
|
|
|
def run(self): |
394
|
|
|
"""Display 'Conda: Add Channel Source' in Sublime Text's command palette. |
395
|
|
|
|
396
|
|
|
When 'Conda: Add Channel Source' is clicked by the user, |
397
|
|
|
an input box will show allowing the user to type the name |
398
|
|
|
of the channel to add. |
399
|
|
|
""" |
400
|
|
|
self.window.show_input_panel('Conda Channel Name:', '', |
401
|
|
|
self.add_channel, None, None) |
402
|
|
|
|
403
|
|
|
def add_channel(self, channel): |
404
|
|
|
"""Add the given channel to the condarc configuration file.""" |
405
|
|
|
cmd = [self.executable, '-m', 'conda', 'config', '--add', |
406
|
|
|
'channels', channel] |
407
|
|
|
|
408
|
|
|
self.window.run_command('exec', {'cmd': cmd}) |
409
|
|
|
|
410
|
|
|
|
411
|
|
|
class RemoveCondaChannelCommand(CondaCommand): |
412
|
|
|
"""Contains all of the methods needed to remove a conda channel source.""" |
413
|
|
|
|
414
|
|
|
def run(self): |
415
|
|
|
"""Display 'Conda: Remove Channel Source' in Sublime Text's command palette. |
416
|
|
|
|
417
|
|
|
When 'Conda: Remove Channel Source' is clicked by the user, |
418
|
|
|
the command palette will show a list of channel sources |
419
|
|
|
available to be removed by the user. |
420
|
|
|
""" |
421
|
|
|
self.window.show_quick_panel(self.channel_sources, self.remove_channel) |
422
|
|
|
|
423
|
|
|
@property |
424
|
|
|
def channel_sources(self): |
425
|
|
|
"""List each channel source found in the condarc configuration file. |
426
|
|
|
|
427
|
|
|
This property had to be duplicated as the attribute from |
428
|
|
|
ListCondaChannelCommand could not be inherited properly. |
429
|
|
|
""" |
430
|
|
|
sources = subprocess.check_output([self.executable, '-m', 'conda', 'config', |
431
|
|
|
'--show-sources', '--json'], |
432
|
|
|
startupinfo=self.startupinfo) |
433
|
|
|
sources = json.loads(sources.decode()) |
434
|
|
|
|
435
|
|
|
try: |
436
|
|
|
return sources[self.configuration]['channels'] |
437
|
|
|
|
438
|
|
|
except KeyError: |
439
|
|
|
return ['No Channel Sources Available'] |
440
|
|
|
|
441
|
|
|
def remove_channel(self, index): |
442
|
|
|
"""Remove a channel from the condarc configuration file.""" |
443
|
|
|
if index != -1: |
444
|
|
|
channel = self.channel_sources[index] |
445
|
|
|
|
446
|
|
|
cmd = [self.executable, '-m', 'conda', 'config', '--remove', |
447
|
|
|
'channels', channel] |
448
|
|
|
|
449
|
|
|
self.window.run_command('exec', {'cmd': cmd}) |
450
|
|
|
|
451
|
|
|
|
452
|
|
|
class ExecuteCondaEnvironmentCommand(CondaCommand): |
453
|
|
|
"""Override Sublime Text's default ExecCommand with a targeted build.""" |
454
|
|
|
|
455
|
|
|
def run(self, **kwargs): |
456
|
|
|
"""Run the current Python file with the conda environment's Python executable. |
457
|
|
|
|
458
|
|
|
The activated conda environment is retrieved from the Sublime Text |
459
|
|
|
window project data. The Python executable found in the conda |
460
|
|
|
environment's bin directory is used to build the file. |
461
|
|
|
""" |
462
|
|
|
try: |
463
|
|
|
environment = self.project_data['conda_environment'] |
464
|
|
|
|
465
|
|
|
if sys.platform == 'win32': |
466
|
|
|
executable_path = '{}\\python' .format(environment) |
467
|
|
|
else: |
468
|
|
|
executable_path = '{}/bin/python' .format(environment) |
469
|
|
|
|
470
|
|
|
kwargs['cmd'][0] = os.path.normpath(executable_path) |
471
|
|
|
|
472
|
|
|
except KeyError: |
473
|
|
|
pass |
474
|
|
|
|
475
|
|
|
self.window.run_command('exec', kwargs) |
476
|
|
|
|