Skip to content

Commit

Permalink
jupyter-runtime-directory: Don't call shell commands at load time
Browse files Browse the repository at this point in the history
* jupyter-env.el (jupyter-runtime-directory): Remove custom status.
New function definition.

* jupyter-kernel-manager.el (jupyter-write-connection-file):
Use new function.

* ob-jupyter.el (org-babel-jupyter--run-repl):
Remove setting of `jupyter-runtime-directory`.

* test/jupyter-test.el (jupyter-runtime-directory): New test.
  • Loading branch information
nnicandro committed Jul 9, 2019
1 parent 4f944d3 commit 7b78ca7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
31 changes: 25 additions & 6 deletions jupyter-env.el
Expand Up @@ -32,6 +32,14 @@
(require 'jupyter-base)
(eval-when-compile (require 'subr-x))

(defvar jupyter-runtime-directory nil
"The Jupyter runtime directory.
When a new kernel is started through `jupyter-start-kernel', this
directory is where kernel connection files are written to.
This variable should not be used. To obtain the runtime directory
call the function `jupyter-runtime-directory'.")

(defun jupyter-command (&rest args)
"Run a Jupyter shell command synchronously, return its output.
The shell command run is
Expand All @@ -44,12 +52,23 @@ return nil."
(when (zerop (apply #'process-file "jupyter" nil t nil args))
(string-trim-right (buffer-string)))))

(defcustom jupyter-runtime-directory (jupyter-command "--runtime-dir")
"The Jupyter runtime directory.
When a new kernel is started through `jupyter-start-kernel', this
directory is where kernel connection files are written to."
:group 'jupyter
:type 'string)
(defun jupyter-runtime-directory ()
"Return the runtime directory used by Jupyter.
Create the directory if necessary. If `default-directory' is a
remote directory, return the runtime directory on that remote.
As a side effect, the variable `jupyter-runtime-directory' is set
to the local runtime directory if it is nil."
(unless jupyter-runtime-directory
(setq jupyter-runtime-directory
(let ((default-directory user-emacs-directory))
(jupyter-command "--runtime-dir"))))
(let ((dir (if (file-remote-p default-directory)
(concat (file-remote-p default-directory)
(jupyter-command "--runtime-dir"))
jupyter-runtime-directory)))
(prog1 dir
(make-directory dir 'parents))))

(defun jupyter-locate-python ()
"Return the path to the python executable in use by Jupyter.
Expand Down
3 changes: 1 addition & 2 deletions jupyter-kernel-manager.el
Expand Up @@ -241,8 +241,7 @@ garbage collected. The file is also deleted when Emacs exits if
it hasn't been already."
(cl-check-type session jupyter-session)
(cl-check-type obj jupyter-finalized-object)
(make-directory jupyter-runtime-directory 'parents)
(let* ((temporary-file-directory jupyter-runtime-directory)
(let* ((temporary-file-directory (jupyter-runtime-directory))
(json-encoding-pretty-print t)
(file (make-temp-file "emacs-kernel-" nil ".json"))
(kill-hook (lambda () (when (and file (file-exists-p file))
Expand Down
4 changes: 1 addition & 3 deletions ob-jupyter.el
Expand Up @@ -206,9 +206,7 @@ variables in PARAMS."
(let ((remote (file-remote-p session)))
(when (and remote (zerop (length (file-local-name session))))
(error "No remote session name"))
(let* ((default-directory (or remote default-directory))
(runtime-directory (jupyter-command "--runtime-dir"))
(jupyter-runtime-directory (concat remote runtime-directory)))
(let ((default-directory (or remote default-directory)))
(jupyter-run-repl kernel nil nil 'jupyter-org-client))))

(defun org-babel-jupyter-initiate-session-by-key (session params)
Expand Down
21 changes: 21 additions & 0 deletions test/jupyter-test.el
Expand Up @@ -683,6 +683,27 @@
(jupyter-stop-channels jupyter-current-client)))
(jupyter-shutdown-kernel manager))))))

;;; Environment

(ert-deftest jupyter-runtime-directory ()
:tags '(env)
(let (dir-created jupyter-runtime-directory)
(cl-letf (((symbol-function #'jupyter-command)
(lambda (&rest _) "foo"))
((symbol-function #'make-directory)
(lambda (&rest _)
(setq dir-created t))))
(jupyter-runtime-directory)
(should dir-created)
(setq dir-created nil)
(should (equal jupyter-runtime-directory "foo"))
(let ((default-directory "/ssh:foo:~"))
(should (equal (jupyter-runtime-directory) "/ssh:foo:foo"))
(ert-info ("Variable definition is always local")
(setq jupyter-runtime-directory nil)
(jupyter-runtime-directory)
(should (equal jupyter-runtime-directory "foo")))))))

;;; Client

;; TODO: Different values of the session argument
Expand Down

0 comments on commit 7b78ca7

Please sign in to comment.