-
Notifications
You must be signed in to change notification settings - Fork 1
/
with-venv.el
294 lines (242 loc) · 10.1 KB
/
with-venv.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
;;; with-venv.el --- Execute with Python virtual environment activated -*- lexical-binding: t; -*-
;; Author: 10sr <8.slashes [at] gmail [dot] com>
;; URL: https://github.com/10sr/with-venv-el
;; Version: 0.0.2
;; Keywords: processes python venv
;; Package-Requires: ((cl-lib "0.5") (emacs "24.4"))
;; This file is not part of GNU Emacs.
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;; `with-venv' macro executes BODY with Python virtual environment activated:
;; (with-venv
;; (executable-find "python"))
;; This macro search for suitable venv directory for current evironment:
;; by default it supports `pipenv`, `poetry`, and directories named
;; `".venv"` and `"venv"`.
;; You can modify `with-venv-find-venv-dir-functions' to add or remove
;; these supports.
;; The automatic search result will be cached as a buffer-local variable, so
;; `with-venv' try to find venv dir only at the first time it is used after
;; visiting file.
;; To explicitly update this cache (without re-visiting file) after you
;; created/changed a virtual environment, invoke M-x `with-venv-find-venv-dir'
;; manually.
;; You can also set buffer-local vairable `with-venv-venv-dir' explicitly
;; to specify venv directory for `with-venv' macro.
;; In this case, the automatic search will be totally disabled for that buffer.
;; If you want to always enable `with-venv' for certain functions,
;; `with-venv-advice-add' can be used for that purpose:
;; (with-venv-advice-add 'blacken-buffer)
;; Adviced functions are always wrapped with `with-venv' macro when called.
;; Call `with-venv-advice-remove' to remove these advices.
;;; Code:
(require 'cl-lib)
(require 'nadvice)
(defvar-local with-venv-venv-dir
nil
"Venv directory path.
This variable is intended to be explicitly set by user.
When nil, `with-venv' tries to find suitable venv dir.
When empty string (\"\"), it means that venv is not available for this buffer.
When this variable is set to non-empty string, use this value without checking
if it is a valid python environment.")
;;;###autoload
(defmacro with-venv-dir (dir &rest body)
"Set python environment to DIR and execute BODY.
This macro does not check if DIR is a valid python environemnt.
If dir is nil or empty string (\"\"), execute BODY as usual."
(declare (indent 1) (debug t))
(let ((dirval (cl-gensym)))
`(let ((,dirval ,dir)
(--with-venv-process-environment-orig (cl-copy-list process-environment))
(--with-venv-exec-path-orig (cl-copy-list exec-path)))
(unwind-protect
(progn
(when (and ,dirval
(not (string= ,dirval
"")))
(let* ((dir (file-name-as-directory ,dirval))
(bin (expand-file-name "bin" dir)))
;; Do the same thing that bin/activate does
(setq exec-path
(cons bin
exec-path))
(setenv "VIRTUAL_ENV" dir)
(setenv "PATH" (if (string= ""
(or (getenv "PATH") ""))
bin
(concat bin ":" (getenv "PATH"))))
(setenv "PYTHONHOME")))
,@body)
(setq process-environment
--with-venv-process-environment-orig)
(setq exec-path
--with-venv-exec-path-orig)))))
(defvar-local with-venv--venv-dir-found nil
"Previously used venv dir path.
Set by `with-venv-find-venv-dir' using `with-venv-find-venv-dir-functions'.
Default value nil means that venv search has not done for this buffer yet.
When empty string (\"\"), it means that venv is not available for this buffer.
To force search venv again, run `with-venv-find-venv-dir' manually.
")
(defvar-local with-venv-found-type nil
"`with-venv' directory type of current buffer.
Used by `with-venv-info-mode'.")
(defvar with-venv--last-found-type nil
"Last found type.")
;;;###autoload
(defmacro with-venv (&rest body)
"Execute BODY with venv enabled.
This function tries to find suitable venv dir, or run BODY as usual when no
suitable environment was found.
This function calls `with-venv-find-venv-dir' with no-refresh enabled to
search venv dir for current buffer.
The result will be cached so this search won't be done any more for current
session unless you explicitly invoke `with-venv-find-venv-dir' command manually."
(declare (indent 0) (debug t))
`(with-venv-dir
;; If set explicitly use it
(or with-venv-venv-dir
;; Check previously used directory
(with-venv-find-venv-dir t))
,@body))
(defun with-venv-find-venv-dir (&optional no-refresh)
"Search for venv dir and set it to `with-venv--venv-dir-found'.
If optional arg NO-REFRESH is non-nil and `with-venv--venv-dir-found' is
already set, do not search for venv dir again.
If suitable dir not found, set the value to empty string (\"\").
Return value of `with-venv--venv-dir-found'."
(interactive)
(unless (and with-venv--venv-dir-found
no-refresh)
(setq with-venv--venv-dir-found (or (with-venv--find-venv-dir)
""))
;; FIXME: Not work when called in parallel
(setq with-venv-found-type
with-venv--last-found-type)
(setq with-venv--last-found-type
nil)
)
with-venv--venv-dir-found)
(defcustom with-venv-find-venv-dir-functions
nil
"Functions to find venv dir.
See `with-venv-find-venv-dir' how this variable is used."
:type 'hook
:group 'with-venv)
(add-hook 'with-venv-find-venv-dir-functions
'with-venv-find-venv-dir-pipenv
t)
(add-hook 'with-venv-find-venv-dir-functions
'with-venv-find-venv-dir-poetry
t)
(add-hook 'with-venv-find-venv-dir-functions
'with-venv-find-venv-dir-dot-venv
t)
(add-hook 'with-venv-find-venv-dir-functions
'with-venv-find-venv-dir-venv
t)
(defun with-venv--find-venv-dir (&optional dir)
"Try to find venv dir for DIR.
If none found return nil.
This function processes `with-venv-find-venv-dir-functions' with
`run-hook-with-args-until-success'."
(with-temp-buffer
(when dir
(cd dir))
(run-hook-with-args-until-success 'with-venv-find-venv-dir-functions)))
(defun with-venv-find-venv-dir-pipenv ()
"Try to find venv dir via pipenv."
(when (executable-find "pipenv")
(with-temp-buffer
(let ((status (call-process "pipenv" nil t nil "--venv")))
(when (eq status 0)
(setq with-venv--last-found-type "Pipenv")
(goto-char (point-min))
(buffer-substring-no-properties (point-at-bol)
(point-at-eol)))))))
(defun with-venv-find-venv-dir-poetry ()
"Try to find venv dir via poetry."
(when (executable-find "poetry")
(with-temp-buffer
(let ((status (call-process "poetry" nil t nil "env" "info" "--path")))
(when (eq status 0)
(setq with-venv--last-found-type "Poetry")
(goto-char (point-min))
(buffer-substring-no-properties (point-at-bol)
(point-at-eol)))))))
(defun with-venv-find-venv-dir-poetry-legacy ()
"Try to find venv dir via poetry debug:info command."
(when (executable-find "poetry")
(with-temp-buffer
(let ((status (call-process "poetry" nil t nil "debug:info")))
(when (eq status 0)
(goto-char (point-min))
(save-match-data
(when (re-search-forward "^ \\* Path: *\\(.*\\)$")
(setq with-venv--last-found-type "Poetry")
(match-string 1))))))))
(defun with-venv-find-venv-dir-dot-venv ()
"Try to find venv dir by its name."
(let ((dir (locate-dominating-file default-directory
;; OK on windows?
".venv/bin/python")))
(when dir
(setq with-venv--last-found-type ".venv/")
(expand-file-name ".venv"
dir))))
(defun with-venv-find-venv-dir-venv ()
"Try to find venv dir by its name."
(let ((dir (locate-dominating-file default-directory
;; OK on windows?
"venv/bin/python")))
(when dir
(setq with-venv--last-found-type "venv/")
(expand-file-name "venv"
dir))))
;;;###autoload
(defun with-venv-advice-add (func)
"Setup advice so that `with-venv' macro is always applied to FUNC."
(advice-add func
:around
'with-venv--advice-around))
;;;###autoload
(defun with-venv-advice-remove (func)
"Remove advice of FUNC added by `with-venv-advice-add'."
(advice-remove func
'with-venv--advice-around))
(defun with-venv--advice-around (orig-func &rest args)
"Function to be used to advice functions with `with-venv-advice-add'.
When a function is adviced with this function, it is wrapped with `with-venv'.
ORIG-FUNC is the target function, and ARGS is the argument when it is called."
(with-venv
(apply orig-func args)))
;; with-venv-info-mode
(defun with-venv-info-lighter ()
"Genarete status of `with-venv-info-mode'."
(let ((type (if with-venv-venv-dir
"Given"
(if (string= ""
with-venv--venv-dir-found)
"-"
(or with-venv-found-type
"?")))))
(format " W/V[%s]"
type)))
;;;###autoload
(define-minor-mode with-venv-info-mode
"Minor-mode to show info about current `with-venv' activated directory."
:lighter (:eval (with-venv-info-lighter))
nil)
(provide 'with-venv)
;;; with-venv.el ends here