mirror of
https://github.com/vale981/emacs-ipython-notebook
synced 2025-03-04 16:51:38 -05:00
reduce package dependencies
This commit is contained in:
parent
a6e48d12cf
commit
c10a059b62
6 changed files with 25 additions and 216 deletions
|
@ -26,7 +26,6 @@
|
|||
(require 'ein-core)
|
||||
(require 'ein-notebooklist)
|
||||
(require 'ein-dev)
|
||||
(require 'ein-k8s)
|
||||
|
||||
(defcustom ein:jupyter-use-containers nil
|
||||
"Take EIN in a different direcsh."
|
||||
|
@ -122,17 +121,7 @@ with the call to the jupyter notebook."
|
|||
(defun ein:jupyter-process-lines (url-or-port command &rest args)
|
||||
"If URL-OR-PORT registered as a k8s url, preface COMMAND ARGS with `kubectl exec'."
|
||||
(condition-case err
|
||||
(cond ((-when-let* ((url-or-port url-or-port)
|
||||
(parsed-url (url-generic-parse-url url-or-port))
|
||||
(not-local (not (string= (url-host parsed-url)
|
||||
ein:url-localhost))))
|
||||
(string= url-or-port (ein:k8s-service-url-or-port)))
|
||||
(let ((pod-name (kubernetes-state-resource-name (ein:k8s-get-pod))))
|
||||
(apply #'process-lines kubernetes-kubectl-executable
|
||||
(nconc
|
||||
(split-string (format "exec %s -- %s" pod-name command))
|
||||
args))))
|
||||
(t (apply #'process-lines command args)))
|
||||
(apply #'process-lines command args)
|
||||
(error (ein:log 'info "ein:jupyter-process-lines: %s" (error-message-string err))
|
||||
nil)))
|
||||
|
||||
|
@ -238,20 +227,18 @@ our singleton jupyter server process here."
|
|||
;;;###autoload
|
||||
(defun ein:jupyter-crib-running-servers ()
|
||||
"Shell out to jupyter for running servers."
|
||||
(nconc
|
||||
(cl-loop for line in
|
||||
(apply #'ein:jupyter-process-lines nil
|
||||
ein:jupyter-server-command
|
||||
(split-string
|
||||
(format "%s%s %s"
|
||||
(aif ein:jupyter-server-use-subcommand
|
||||
(concat it " ") "")
|
||||
"list" "--json")))
|
||||
collecting (destructuring-bind
|
||||
(&key url &allow-other-keys)
|
||||
(ein:json-read-from-string line)
|
||||
(ein:url url)))
|
||||
(aif (ein:k8s-service-url-or-port) (list it))))
|
||||
(cl-loop for line in
|
||||
(apply #'ein:jupyter-process-lines nil
|
||||
ein:jupyter-server-command
|
||||
(split-string
|
||||
(format "%s%s %s"
|
||||
(aif ein:jupyter-server-use-subcommand
|
||||
(concat it " ") "")
|
||||
"list" "--json")))
|
||||
collecting (destructuring-bind
|
||||
(&key url &allow-other-keys)
|
||||
(ein:json-read-from-string line)
|
||||
(ein:url url))))
|
||||
|
||||
;;;###autoload
|
||||
(defun ein:jupyter-server-start (server-command
|
||||
|
|
171
lisp/ein-k8s.el
171
lisp/ein-k8s.el
|
@ -1,171 +0,0 @@
|
|||
;;; ein-k8s.el --- hooks to chrisbarrett's kubernetes*.el -*- lexical-binding: t; -*-
|
||||
|
||||
;; Copyright (C) 2019 The Authors
|
||||
|
||||
;; Authors: dickmao <github id: dickmao>
|
||||
|
||||
;; This file is NOT part of GNU Emacs.
|
||||
|
||||
;; ein-k8s.el is free software: you can redistribute it and/or modify
|
||||
;; it under the terms of the GNU General Public License as published by
|
||||
;; the Free Software Foundation, either version 3 of the License, or
|
||||
;; (at your option) any later version.
|
||||
|
||||
;; ein-k8s.el is distributed in the hope that it will be useful,
|
||||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
;; GNU General Public License for more details.
|
||||
|
||||
;; You should have received a copy of the GNU General Public License
|
||||
;; along with ein-k8s.el. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
;;; Commentary:
|
||||
|
||||
;;; Code:
|
||||
|
||||
(require 'kubernetes)
|
||||
|
||||
(defcustom ein:k8s-name-deployment nil
|
||||
"Regexp by which we recognize jupyter services."
|
||||
:type 'string
|
||||
:group 'ein)
|
||||
|
||||
(defcustom ein:k8s-name-service "jupyter-service"
|
||||
"Regexp by which we recognize jupyter services."
|
||||
:type 'string
|
||||
:group 'ein)
|
||||
|
||||
(defun ein:k8s-select-context (&optional query-p)
|
||||
(interactive "p")
|
||||
(when (or query-p
|
||||
(null (kubernetes-state-config (kubernetes-state))))
|
||||
(kubernetes-contexts-refresh-now)
|
||||
(if-let ((contexts (ein:k8s-get-contexts)))
|
||||
(let ((desired-context
|
||||
(ein:completing-read "Select context: " contexts nil t)))
|
||||
(message "Rereading state...")
|
||||
(kubernetes-state-clear)
|
||||
(let ((response
|
||||
(kubernetes-kubectl-await
|
||||
(apply-partially #'kubernetes-kubectl
|
||||
kubernetes-props
|
||||
(kubernetes-state)
|
||||
(split-string (format "config use-context %s"
|
||||
desired-context)))
|
||||
(lambda (buf)
|
||||
(with-current-buffer buf
|
||||
(string-match (rx bol "Switched to context \""
|
||||
(group (+? nonl)) "\"." (* space) eol)
|
||||
(buffer-string))
|
||||
(match-string 1 (buffer-string)))))))
|
||||
(if (string= response desired-context)
|
||||
(progn
|
||||
(kubernetes-state-update-config (kubernetes-kubectl-await-on-async
|
||||
kubernetes-props
|
||||
(kubernetes-state)
|
||||
#'kubernetes-kubectl-config-view))
|
||||
(let ((current-name (alist-get
|
||||
'name
|
||||
(kubernetes-state-current-context
|
||||
(kubernetes-state)))))
|
||||
(unless (string= current-name desired-context)
|
||||
(error "ein:k8s-select-context': could not update state for %s"
|
||||
desired-context))
|
||||
(if (kubernetes-nodes-refresh-now)
|
||||
(progn
|
||||
(mapc (lambda (resource)
|
||||
(when-let ((refresh-f
|
||||
(intern-soft (format "kubernetes-%s-refresh-now" resource))))
|
||||
(when (fboundp refresh-f)
|
||||
(funcall refresh-f))))
|
||||
(cl-remove-if (apply-partially #'eq 'nodes)
|
||||
(mapcar #'car kubernetes-overview-views-alist)))
|
||||
(message ""))
|
||||
(error "ein:k8s-select-context: %s is down" current-name))))
|
||||
(error "ein:k8s-select-context: use-context returned %s, expected %s"
|
||||
response desired-context))))
|
||||
(error "ein:k8s-select-context: No contexts found"))))
|
||||
|
||||
(defun ein:k8s-get-contexts ()
|
||||
(let ((response (kubernetes-kubectl-await-on-async kubernetes-props
|
||||
(kubernetes-state)
|
||||
#'kubernetes-kubectl-config-view)))
|
||||
(-let* [((&alist 'contexts contexts 'current-context current) response)
|
||||
(names (--map (alist-get 'name it) (append contexts nil)))]
|
||||
(when (member current names)
|
||||
(setq names (cons current (-remove-item current names))))
|
||||
names)))
|
||||
|
||||
(defun ein:k8s-get-deployment ()
|
||||
(-let* [(deployments (kubernetes-state-deployments (kubernetes-state)))
|
||||
((&alist 'items items) deployments)]
|
||||
(seq-some (lambda (it)
|
||||
(-let [(&alist 'metadata (&alist 'name)) it]
|
||||
(and (string= name ein:k8s-name-deployment) it)))
|
||||
items)))
|
||||
|
||||
(defun ein:k8s-get-pod ()
|
||||
(when-let ((deployment (ein:k8s-get-deployment)))
|
||||
(cl-first (kubernetes-overview--pods-for-deployment (kubernetes-state)
|
||||
deployment))))
|
||||
(defun ein:k8s-get-service ()
|
||||
(-let* [(services (kubernetes-state-services (kubernetes-state)))
|
||||
((&alist 'items items) services)]
|
||||
(seq-some (lambda (it)
|
||||
(-let [(&alist 'metadata (&alist 'name)) it]
|
||||
(and (string= name ein:k8s-name-service) it)))
|
||||
items)))
|
||||
|
||||
(defun ein:k8s-get-node ()
|
||||
(-when-let* ((pod (ein:k8s-get-pod))
|
||||
((&alist 'spec (&alist 'nodeName)) pod)
|
||||
(node (kubernetes-state-lookup-node nodeName (kubernetes-state)))
|
||||
((&alist 'metadata (&alist 'name)) node))
|
||||
node))
|
||||
|
||||
(defsubst ein:k8s-ensure ()
|
||||
(and ein:k8s-name-deployment (executable-find kubernetes-kubectl-executable)
|
||||
(condition-case err
|
||||
(progn
|
||||
(ein:k8s-select-context)
|
||||
(kubernetes-state-current-context (kubernetes-state)))
|
||||
(error (ein:log 'info "ein:k8s-ensure: %s" (error-message-string err))
|
||||
nil))))
|
||||
|
||||
(defsubst ein:k8s-in-cluster (addr)
|
||||
"Is ein client inside the k8s cluster?"
|
||||
(if-let ((ip-command (executable-find "ip")))
|
||||
(with-temp-buffer
|
||||
(apply #'call-process ip-command nil t nil
|
||||
(split-string (format "n ls %s" addr)))
|
||||
(goto-char (point-min))
|
||||
(search-forward addr nil t))
|
||||
;; hack if ip command not found
|
||||
(string= "minikube"
|
||||
(alist-get 'name (kubernetes-state-current-context
|
||||
(kubernetes-state))))))
|
||||
|
||||
(defun ein:k8s-service-url-or-port ()
|
||||
(ein:k8s-ensure)
|
||||
(-when-let* ((service (ein:k8s-get-service))
|
||||
((&alist 'spec (&alist 'ports [(&alist 'nodePort)])) service)
|
||||
(node (ein:k8s-get-node))
|
||||
((&alist 'status (&alist 'addresses)) node)
|
||||
(host-ip
|
||||
(seq-some (lambda (address)
|
||||
(when (string= (alist-get 'type address) "InternalIP")
|
||||
(alist-get 'address address)))
|
||||
addresses)))
|
||||
(if (ein:k8s-in-cluster host-ip)
|
||||
(ein:url (concat "http://" host-ip ":" (number-to-string nodePort)))
|
||||
(when-let ((ips (kubernetes-kubectl-await-command ingress
|
||||
(lambda (item)
|
||||
(-let* (((&alist 'status
|
||||
(&alist 'loadBalancer
|
||||
(&alist 'ingress
|
||||
[(&alist 'ip)])))
|
||||
item))
|
||||
ip)))))
|
||||
(ein:url (concat "http://" (car ips)))))))
|
||||
|
||||
(provide 'ein-k8s)
|
|
@ -395,11 +395,12 @@ This function is called via `ein:notebook-after-rename-hook'."
|
|||
(nbs (ein:nblist--sort-group (cdr (assoc "notebook" groups))
|
||||
sort-param
|
||||
sort-order))
|
||||
(files (ein:nblist--sort-group (-flatten-n 1 (-map #'cdr (-group-by
|
||||
#'(lambda (x) (car (last (s-split "\\." (plist-get x :name)))))
|
||||
(cdr (assoc "file" groups)))))
|
||||
sort-param
|
||||
sort-order)))
|
||||
(files (ein:nblist--sort-group
|
||||
(-flatten-n 1 (-map #'cdr (-group-by
|
||||
#'(lambda (x) (car (last (split-string (plist-get x :name) "\\."))))
|
||||
(cdr (assoc "file" groups)))))
|
||||
sort-param
|
||||
sort-order)))
|
||||
(-concat dirs nbs files)))
|
||||
|
||||
(defun render-header (url-or-port &rest args)
|
||||
|
|
|
@ -6,7 +6,5 @@
|
|||
(anaphora "20180618")
|
||||
(request "20200117.0")
|
||||
(deferred "0.5")
|
||||
(polymode "20190426.1729")
|
||||
(kubernetes "20200114.0")
|
||||
(dash "2.13.0")
|
||||
(s "1.11.0")))
|
||||
(polymode "20190714.0")
|
||||
(dash "2.13.0")))
|
||||
|
|
|
@ -29,7 +29,6 @@
|
|||
(require 'ein-jupyter)
|
||||
(require 'ein-file)
|
||||
(require 'ein-notebooklist)
|
||||
(require 'ein-k8s)
|
||||
|
||||
(defcustom ein:process-jupyter-regexp "\\(jupyter\\|ipython\\)\\(-\\|\\s-+\\)note"
|
||||
"Regexp by which we recognize notebook servers."
|
||||
|
@ -209,10 +208,9 @@ is used instead. BUFFER-CALLBACK is called after notebook opened."
|
|||
(defun ein:process-find-file-callback ()
|
||||
"A callback function for `find-file-hook' to open notebook."
|
||||
(interactive)
|
||||
(cl-letf (((symbol-function 'ein:k8s-select-context) #'ignore))
|
||||
(-when-let* ((filename buffer-file-name)
|
||||
(match-p (string-match-p "\\.ipynb$" filename)))
|
||||
(ein:process-open-notebook filename #'kill-buffer-if-not-modified))))
|
||||
(-when-let* ((filename buffer-file-name)
|
||||
(match-p (string-match-p "\\.ipynb$" filename)))
|
||||
(ein:process-open-notebook filename #'kill-buffer-if-not-modified)))
|
||||
|
||||
(provide 'ein-process)
|
||||
|
||||
|
|
|
@ -27,12 +27,10 @@
|
|||
|
||||
(require 'cc-mode)
|
||||
(require 'json)
|
||||
(require 's)
|
||||
(require 'dash)
|
||||
(require 'url)
|
||||
(require 'deferred)
|
||||
|
||||
|
||||
;;; Macros and core functions/variables
|
||||
|
||||
(defmacro ein:with-undo-disabled (&rest body)
|
||||
|
@ -210,8 +208,7 @@ at point, i.e. any word before then \"(\", if it is present."
|
|||
(setq url-or-port (format "http://localhost:%s" url-or-port)))
|
||||
(let ((parsed-url (url-generic-parse-url url-or-port)))
|
||||
(when (null (url-host parsed-url))
|
||||
(setq url-or-port (concat (if (string= url-or-port (ein:k8s-service-url-or-port))
|
||||
"http" "https") "://" url-or-port))
|
||||
(setq url-or-port (concat "https://" url-or-port))
|
||||
(setq parsed-url (url-generic-parse-url url-or-port)))
|
||||
(when (or (string= (url-host parsed-url) "localhost")
|
||||
(string= (url-host parsed-url) ein:url-localhost)
|
||||
|
@ -685,7 +682,6 @@ Use `ein:log' for debugging and logging."
|
|||
(when ein:enable-gc-adjust
|
||||
(setq gc-cons-threshold current-gc-cons-threshold))))
|
||||
|
||||
|
||||
;;; Git utilities
|
||||
|
||||
(defun ein:call-process (command &optional args)
|
||||
|
|
Loading…
Add table
Reference in a new issue