diff --git a/jupyter-env.el b/jupyter-env.el index 3104dd9f..9453eff7 100644 --- a/jupyter-env.el +++ b/jupyter-env.el @@ -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 @@ -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. diff --git a/jupyter-kernel-manager.el b/jupyter-kernel-manager.el index d7765ba9..babd5b1e 100644 --- a/jupyter-kernel-manager.el +++ b/jupyter-kernel-manager.el @@ -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)) diff --git a/ob-jupyter.el b/ob-jupyter.el index 21dc7c7b..85de1a3a 100644 --- a/ob-jupyter.el +++ b/ob-jupyter.el @@ -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) diff --git a/test/jupyter-test.el b/test/jupyter-test.el index 6276d7d8..d4cac192 100644 --- a/test/jupyter-test.el +++ b/test/jupyter-test.el @@ -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