/
link_actions.clj
85 lines (77 loc) · 3.19 KB
/
link_actions.clj
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
(ns clojupyter.install.conda.link-actions
(:gen-class)
(:require [clojupyter.install.conda.env :as env]
[clojupyter.install.filemap :as fm]
[clojupyter.install.conda.conda-specs :as csp]
[clojupyter.install.local-specs :as lsp]
[clojupyter.kernel.version :as ver]
[clojupyter.util-actions :as u!]
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.spec.alpha :as s]
[io.simplect.compose :refer [C def- p]]
[me.raynes.fs :as fs]))
(def DEPEND [csp/DEPEND-DUMMY])
(def- classpath-urls
(C #(java.lang.ClassLoader/getSystemClassLoader)
#(.getURLs %)
vec))
(def- classpath-clojupyter-jarfiles
(C classpath-urls
(p filter (C str (p re-find lsp/CONDA-JARNAME-RE)))
vec))
;;; ----------------------------------------------------------------------------------------------------
;;; EXTERNAL
;;; ----------------------------------------------------------------------------------------------------
(defn conda-clojupyter-kernel-dir
"Action returning the directory to be used as kernel directory for the Conda-installed Clojupyter
kernel."
[prefix]
(-> prefix
(str "/share/jupyter/kernels/conda-clojupyter")
io/file))
(defn conda-ensure-dir!
"Action to ensure existence of `dir`."
[dir]
(let [dir (io/file dir)]
(fs/mkdirs dir)
(when-not (and (fs/directory? dir) (fs/writeable? dir))
(throw (Exception. (str "Failed to create clojupyter conda install dir: " (or dir "nil") "."))))))
(defn conda-link-environment
"Action return the data about the install environment needed to calculate how to Conda-install
Clojupyter."
([] (conda-link-environment {}))
([{:keys [jarfile prefix]}]
(let [build-num (env/PKG_BUILDNUM)
ident (str "clojupyter=" (ver/version-string) "=" (or build-num "?"))
prefix (or prefix (env/PREFIX))
jarfiles (classpath-clojupyter-jarfiles)
jarfile (or jarfile (when (-> jarfiles count (= 1))
(-> jarfiles first io/file)))
items (if jarfile
(->> jarfile fs/parent file-seq (filter fs/file?)
(filter (C str (p re-find #"\.(png|jar)$")))
(into #{}))
#{})
destdir (conda-clojupyter-kernel-dir prefix)
env {:conda-link/destdir destdir
:conda-link/ident ident
:conda-link/items items
:conda-link/item-filemap (fm/filemap items destdir)
:conda-link/prefix prefix}]
(if (s/valid? :conda-link/env env)
env
(u!/throw-info "conda-link-environment: internal error"
{:env env, :explain-str (s/explain-str :conda-link/env env)})))))
(defn conda-verify-install
"Action to check if the Clojupyter kernel appears to have been installed correctly."
[destdir]
(let [files (->> destdir
file-seq
(filter fs/file?)
(map #(.getName ^java.io.File %))
(into #{}))]
(set/subset? #{lsp/KERNEL-JSON
(->> lsp/LOGO-ASSET io/file .getName)
lsp/DEFAULT-TARGET-JARNAME}
files)))