-
Notifications
You must be signed in to change notification settings - Fork 28
/
virtualenvwrapper.el
538 lines (464 loc) · 19.8 KB
/
virtualenvwrapper.el
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
;;; virtualenvwrapper.el --- a featureful virtualenv tool for Emacs
;; Copyright (C) 2013 - 2015 James J Porter and [contributors](https://github.com/porterjamesj/virtualenvwrapper.el/graphs/contributors)
;; Author: James J Porter <porterjamesj@gmail.com>
;; URL: http://github.com/porterjamesj/virtualenvwrapper.el
;; Version: 20151123
;; Keywords: python, virtualenv, virtualenvwrapper
;; Package-Requires: ((dash "1.5.0") (s "1.6.1"))
;;; Commentary:
;; A featureful virtualenv tool for Emacs. Emulates much of the
;; functionality of Doug Hellmann's
;; [virtualenvwrapper](https://bitbucket.org/dhellmann/virtualenvwrapper/)
;; See documentation at
;; https://github.com/porterjamesj/virtualenvwrapper.el for more details.
;;; Code:
(require 'dash)
(require 's)
;; needed to set gud-pdb-command-name
(require 'gud)
;; customizable variables
(defgroup virtualenvwrapper nil
"Virtualenvwrapper for Emacs."
:group 'python)
(defcustom venv-location
(expand-file-name (or (getenv "WORKON_HOME") "~/.virtualenvs/"))
"The location(s) of your virtualenvs. This
can be either a string, which indicates a single directory in which
you keep all your virtualenvs, or a list of strings, in which case it
specifies disparate locations in which all your virtualenvs are kept.
The default location is ~/.virtualenvs/, which is where your virtualenvs
are stored if you use virtualenvwrapper in the shell."
:group 'virtualenvwrapper)
(defcustom venv-dirlookup-names
'(".venv" "venv")
"Virtualenvs to search in the projectile-project-root
to activate when one of them is found."
:type '(repeat file)
:group 'virtualenvwrapper)
;; hooks
(defvar venv-premkvirtualenv-hook nil
"Hook run before creating a new virtualenv.")
(defvar venv-postmkvirtualenv-hook nil
"Hook run after creating a new virtualenv.")
(defvar venv-prermvirtualenv-hook nil
"Hook run before deleting a virtualenv.")
(defvar venv-postrmvirtualenv-hook nil
"Hook run after deleting a virtualenv.")
(defvar venv-preactivate-hook nil
"Hook run before a virtualenv is activated.")
(defvar venv-postactivate-hook nil
"Hook run after a virtualenv is activated.")
(defvar venv-predeactivate-hook nil
"Hook run before a virtualenv is deactivated.")
(defvar venv-postdeactivate-hook nil
"Hook run after a virtualenv is deactivated.")
;; internal variables that you probably shouldn't mess with
(defvar venv-history nil "The history of venvs we have worked on.")
(defvar venv-current-name nil "Name of current virtualenv.")
(defvar venv-current-dir nil "Directory of current virtualenv.")
(defvar venv-system-gud-pdb-command-name gud-pdb-command-name
"Whatever `gud-pdb-command-name' is (usually \\[pdb]).")
;; copy from virtualenv.el
(defvar venv-executables-dir
(if (eq system-type 'windows-nt) "Scripts" "bin")
"The name of the directory containing executables. It is system dependent.")
(defun venv-projectile-auto-workon ()
"If a venv in the projetile root exists, activates it.
Set your common venvs names in `venv-dirlookup-names'"
(let ((path (--first
(file-exists-p it)
(--map (concat (projectile-project-root) it)
venv-dirlookup-names))))
(when path
(setq venv-current-name path) ;; there's really nothing that feels good to do here ;_;
(venv--activate-dir path))))
;; internal utility functions
(defun venv--set-venv-gud-pdb-command-name ()
"When in a virtual env, call pdb as \\[python -m pdb]."
(setq gud-pdb-command-name "python -m pdb"))
(defun venv--set-system-gud-pdb-command-name ()
"Set the system \\[pdb] command."
(setq gud-pdb-command-name venv-system-gud-pdb-command-name))
(defun venv-clear-history ()
(setq venv-history nil))
(defun venv-dir-to-name (dir)
"Extract the name of a virtualenv from a path."
(car (last (--filter (not (s-blank? it))
(s-split "/" dir)))))
(defun venv-name-to-dir (name)
"Given the name of a virtualenv, translate it
to the directory where that virtualenv is located."
(file-name-as-directory
(let ((potential-dir
(if (stringp venv-location)
(concat (file-name-as-directory
(expand-file-name venv-location)) name)
(car (-filter
(lambda (d)
(s-equals? name (venv-dir-to-name d)))
venv-location)))))
(if (and potential-dir
(file-exists-p
(concat (file-name-as-directory
(expand-file-name potential-dir)) venv-executables-dir)))
(file-name-as-directory
(expand-file-name potential-dir))
(error (concat "No such virtualenv: " name))))))
(defun venv-get-candidates ()
"Wrapper to call get-candidates-list or
get-candidates-string depending on which
is appropriate for how venv-location is
specified."
(let ((candidates
(if (stringp venv-location)
(venv-get-candidates-dir venv-location)
(venv-get-candidates-list venv-location))))
(when (not (eq (length (-distinct candidates))
(length candidates)))
(error "Some virtualenvs have the same name!"))
candidates))
(defun venv-get-candidates-list (list)
"Given LIST of virtualenv directories,
return a list of names that can be used in, e.g.
a completing read. This trusts the caller to only
pass directories with are actually virtualenvs."
(-map (lambda (dir)
(car (last (-filter (lambda (s) (not (s-blank? s)))
(s-split "/" dir)))))
(-filter
(lambda (s) (car (file-attributes
(concat (file-name-as-directory
(expand-file-name s)) venv-executables-dir))))
list)))
(defun venv-get-candidates-dir (dir)
"Given a directory DIR containing virtualenvs, return a list
of names that can be used in the completing read."
(let ((proper-dir (file-name-as-directory (expand-file-name dir))))
(-filter (lambda (s)
(let ((subdir (concat proper-dir s)))
(car (file-attributes
(concat (file-name-as-directory subdir) venv-executables-dir)))))
(directory-files proper-dir nil "^[^.]"))))
(defun venv-get-stripped-path (path)
"Return what the PATH would look like if we weren't in a
virtualenv. PATH should be a list of strings specifiying directories."
(-filter
(lambda (s) (not (s-equals? s (concat venv-current-dir venv-executables-dir))))
path))
(defun venv--purge-history (candidates)
"Remove history candidates that are not present in the list CANDIDATES"
(setq venv-history (-filter (lambda (s) (-contains? candidates s))
venv-history)))
(defun venv-is-valid (name)
"Test if NAME is a valid virtualenv specifier"
(-contains? (venv-get-candidates) name))
(defun venv-read-name (prompt)
"Do a completing read to get the name of a candidate,
prompting the user with the string PROMPT"
(let ((candidates (venv-get-candidates)))
;; purge history of no longer existant candidates first
(venv--purge-history candidates)
(completing-read prompt
candidates nil t nil
'venv-history
(or (car venv-history)
(car candidates)))))
(defun venv-list-virtualenvs ()
(s-join "\n" (venv-get-candidates)))
(defun venv--activate-dir (dir)
"Given a directory corresponding to a virtualenv, activate it"
(run-hooks 'venv-preactivate-hook)
(setq venv-current-dir dir)
;; setup the python shell
(setq python-shell-virtualenv-path venv-current-dir)
;; setup emacs exec-path
(add-to-list 'exec-path (concat venv-current-dir venv-executables-dir))
;; setup the environment for subprocesses
(setenv "PATH" (concat venv-current-dir venv-executables-dir path-separator (getenv "PATH")))
;; keep eshell path in sync
(setq eshell-path-env (getenv "PATH"))
(setenv "VIRTUAL_ENV" venv-current-dir)
(venv--set-venv-gud-pdb-command-name)
(run-hooks 'venv-postactivate-hook))
;; potentially interactive user-exposed functions
;;;###autoload
(defun venv-deactivate ()
"Deactivate the current venv."
(interactive)
(run-hooks 'venv-predeactivate-hook)
(setq python-shell-virtualenv-path nil)
(setq exec-path (venv-get-stripped-path exec-path))
(setenv "PATH" (s-join path-separator
(venv-get-stripped-path
(s-split path-separator (getenv "PATH")))))
(setenv "VIRTUAL_ENV" nil)
(setq venv-current-name nil)
(setq venv-current-dir nil)
(setq eshell-path-env (getenv "PATH"))
(venv--set-system-gud-pdb-command-name)
(run-hooks 'venv-postdeactivate-hook)
(when (called-interactively-p 'interactive)
(message "virtualenv deactivated")))
;;;###autoload
(defun venv-set-location (&optional location)
"Set where to look for virtual environments to LOCATION.
This is useful e.g. when using tox."
(interactive)
(when (not location)
(setq location (read-directory-name "New virtualenv: ")))
(venv-deactivate)
(setq venv-location location)
(when (called-interactively-p 'interactive)
(message (concat "Virtualenv location: " location))))
;;;###autoload
(defun venv-workon (&optional name)
"Interactively switch to virtualenv NAME. Prompts for name if called
interactively."
(interactive)
;; if without argument, read from user
(unless name
(setq name (venv-read-name
(if venv-current-name
(format "Choose a virtualenv (currently %s): " venv-current-name)
"Choose a virtualenv: "))))
;; validate name
(when (not (venv-is-valid name))
(error (format "Invalid virtualenv %s specified!" name)))
;; then deactivate
(venv-deactivate)
;; then switch
(setq venv-current-name name)
;; push it onto the history
(add-to-list 'venv-history venv-current-name)
;; actually activate it
(venv--activate-dir (venv-name-to-dir venv-current-name))
(when (called-interactively-p 'interactive)
(message (concat "Switched to virtualenv: " venv-current-name))))
;; for hilarious reasons to do with bytecompiling, this has to be here
;; instead of below
(defmacro venv-with-virtualenv (name &rest forms)
"Evaluate FORMS with venv NAME active. NAME must be a string
identifying a virtualenv."
`(progn
(let ((prev-dir default-directory)
(prev-env venv-current-name))
(venv-workon ,name) ;; switch it up
(cd venv-current-dir)
(unwind-protect
(progn
,@forms) ;; evalulate forms
(if prev-env ;; switch back
(venv-workon prev-env)
(venv-deactivate))
(cd prev-dir)))))
(defun venv--check-executable ()
"Verify that there is a virtualenv executable available,
throwing an error if not"
(unless (executable-find "virtualenv")
(error "There doesn't appear to be a virtualenv executable on
your exec path. Ensure that you have virtualenv installed and
that the exec-path variable is set such that virtualenv can
be found. A common cause of problems like this is GUI Emacs
not having environment variables set up like the shell. Check
out https://github.com/purcell/exec-path-from-shell for a
robust solution to this problem.")))
(defun venv-get-python-executable ()
"Do a completing read for a python executable to use in mkvirtualenv"
)
;;;###autoload
(defun venv-mkvirtualenv (&rest names)
"Create new virtualenvs NAMES. If venv-location is a single
directory, the new virtualenvs are made there; if it is a list of
directories, the new virtualenvs are made in the current
default-directory."
(interactive)
(venv--check-executable)
(let ((parent-dir (if (stringp venv-location)
(file-name-as-directory
(expand-file-name venv-location))
default-directory))
(python-exe-arg (when current-prefix-arg
(concat "--python="
(read-string "Python executable: " "python"))))
(names (if names names
(list (read-from-minibuffer "New virtualenv: ")))))
;; map over all the envs we want to make
(--each names
;; error if this env already exists
(when (-contains? (venv-get-candidates) it)
(error "A virtualenv with this name already exists!"))
(run-hooks 'venv-premkvirtualenv-hook)
(shell-command (concat "virtualenv " python-exe-arg " " parent-dir it))
(when (listp venv-location)
(add-to-list 'venv-location (concat parent-dir it)))
(venv-with-virtualenv it
(run-hooks 'venv-postmkvirtualenv-hook))
(when (called-interactively-p 'interactive)
(message (concat "Created virtualenv: " it))))
;; workon the last venv we made
(venv-workon (car (last names)))))
;;;###autoload
(defun venv-rmvirtualenv (&rest names)
"Delete virtualenvs NAMES."
(interactive)
;; deactivate first
(venv-deactivate)
;; check validity and read names if necessary
(if names
(--map (when (not (venv-is-valid it))
(error "Invalid virtualenv specified!"))
names)
(setq names (list (venv-read-name "Virtualenv to delete: "))))
;; map over names, deleting the appropriate directory
(--each names
(run-hooks 'venv-prermvirtualenv-hook)
(delete-directory (venv-name-to-dir it) t)
;; get it out of the history so it doesn't show up in completing reads
(setq venv-history (-filter
(lambda (s) (not (s-equals? s it))) venv-history))
;; if location is a list, delete it from the list
(when (listp venv-location)
(setq venv-location
(-filter (lambda (locs) (not (s-equals?
it
(venv-dir-to-name locs))))
venv-location)))
(run-hooks 'venv-postrmvirtualenv-hook)
(when (called-interactively-p)
(message (concat "Deleted virtualenv: " it)))))
;;;###autoload
(defun venv-lsvirtualenv ()
"List all available virtualenvs in a temp buffer."
(interactive)
(with-output-to-temp-buffer
"*Virtualenvs*"
(princ (venv-list-virtualenvs))))
;;;###autoload
(defun venv-cdvirtualenv (&optional subdir)
"Change to the directory of current virtualenv. If
SUBDIR is passed, append that to the path such that
we are immediately in that directory."
(interactive)
(if venv-current-dir
(let ((going-to (concat (file-name-as-directory
(expand-file-name venv-current-dir))
subdir)))
(cd going-to)
(when (called-interactively-p 'interactive)
(message (concat "Now in directory: " going-to))))
(error "No virtualenv is currently active.")))
;;;###autoload
(defun venv-cpvirtualenv (&optional name newname)
"Copy virtualenv NAME to NEWNAME. Any arguments not passed will be
prompted for This comes with the same caveat as cpvirtualenv in the
original virtualenvwrapper, which is that is far from guarenteed to
work well. Many packages hardcode absolute paths in various places an
will break if moved to a new location. Use with caution. If used with
a single virtualenv directory, behaves just like cpvirtualenv in
virtualenvwrapper.sh. If used with virtualenvs spread around the
filesystem, creates the new virtualenv in the current default
directory."
(interactive)
(let ((parent-dir (if (stringp venv-location)
(file-name-as-directory
(expand-file-name venv-location))
default-directory)))
(when (not name) (setq name (venv-read-name "Virtualenv to copy from: ")))
(when (not newname) (setq newname
(read-from-minibuffer "Virtualenv to copy to: ")))
;; throw an error if newname already exists
(when (file-exists-p (concat parent-dir newname))
(error "A virtualenv with the proposed name already exists!"))
;; make the copy
(copy-directory (venv-name-to-dir name)
(concat parent-dir newname))
;; if the location specifier is a list, add to it.
(when (listp venv-location)
(add-to-list 'venv-location (concat parent-dir newname)))
(when (called-interactively-p 'interactive)
(message (format "Copied virtualenv %s to %s" name newname)))
(venv-workon newname)))
;; macros and functions supporting executing elisp or
;; shell commands in a particular venv
(defmacro venv-allvirtualenv (&rest forms)
"For each virtualenv, activate it, switch to its directory,
and then evaluate FORMS."
`(progn
(--each (venv-get-candidates)
(venv-with-virtualenv it
,@forms))))
(defun venv-with-virtualenv-shell-command (name command)
"Execute the string COMMAND in virtualenv NAME."
(venv-with-virtualenv name
(shell-command command)))
(defun venv-allvirtualenv-shell-command (&optional command)
"Just like venv-allvirtulenv, but executes a shell
command (COMMAND) rather than elisp forms."
(interactive)
(when (not command)
(setq command (read-from-minibuffer "Shell command to execute: ")))
(-map (lambda (name)
(venv-with-virtualenv-shell-command name command))
(venv-get-candidates))
(message (concat "Executed " command " in all virtualenvs")))
;; Code for setting up interactive shell and eshell
;; interactive shell
;;;###autoload
(defun venv-shell-init (process)
"Activate the current virtualenv in a newly opened shell."
(comint-send-string
process
(concat "if command -v workon >/dev/null 2>&1; then workon "
venv-current-name
"; else source "
venv-current-dir venv-executables-dir
"/activate; fi \n")))
;;;###autoload
(defun venv-initialize-interactive-shells ()
"Configure interactive shells for use with
virtualenvwrapper.el."
(defadvice shell (around strip-env ())
"Use the environment without the venv to start up a new shell."
(let* ((buffer-name (or buffer "*shell*"))
(buffer-exists-already (get-buffer buffer-name)))
(if (or buffer-exists-already (not venv-current-name))
ad-do-it
(progn (setenv "PATH" (s-join path-separator (venv-get-stripped-path
(s-split path-separator (getenv "PATH")))))
(setenv "VIRTUAL_ENV" nil)
ad-do-it
(venv-shell-init buffer-name)
(setenv "PATH" (concat venv-current-dir venv-executables-dir path-separator (getenv "PATH")))
(setenv "VIRTUAL_ENV" venv-current-dir)))))
(ad-activate 'shell))
;; eshell
(eval-and-compile
(defun venv--gen-fun (command)
`(defun ,(intern (format "pcomplete/eshell-mode/%s" command)) ()
(pcomplete-here* (venv-get-candidates)))))
(defmacro venv--make-pcompletions (commands)
`(progn ,@(-map #'venv--gen-fun commands)))
;;;###autoload
(defun venv-initialize-eshell ()
"Configure eshell for use with virtualenvwrapper.el."
;; make emacs and eshell share an environment
(setq eshell-modify-global-environment t)
;; set eshell path
(setq eshell-path-env (getenv "PATH"))
;; alias functions
(defun eshell/workon (arg) (venv-workon arg))
(defun eshell/deactivate () (venv-deactivate))
(defun eshell/rmvirtualenv (&rest args) (apply #'venv-rmvirtualenv args))
(defun eshell/mkvirtualenv (&rest args) (apply #'venv-mkvirtualenv args))
(defun eshell/cpvirtualenv (&rest args) (apply #'venv-cpvirtualenv args))
(defun eshell/cdvirtualenv (&optional arg) (venv-cdvirtualenv arg))
(defun eshell/lsvirtualenv () (venv-list-virtualenvs))
(defun eshell/allvirtualenv (&rest command)
(venv-allvirtualenv-shell-command
(s-join " " (eshell-stringify-list command))))
;; make completions work
(venv--make-pcompletions ("workon" "rmvirtualenv"
"cdvirtualenv" "cpvirtualenv"))
(message "Eshell virtualenv support initialized."))
(provide 'virtualenvwrapper)
;;; virtualenvwrapper.el ends here