Skip to content

Commit 6a18f96

Browse files
committed
uv integration
1 parent aaabb68 commit 6a18f96

5 files changed

Lines changed: 278 additions & 4 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ a.out
2020
*.iml
2121
.lsp
2222
.clj-kondo
23-
pregen-ffi-test
23+
pregen-ffi-test
24+
.calva

deps.edn

Lines changed: 175 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
{:paths ["src"]
2-
:deps {org.clojure/clojure {:mvn/version "1.11.1" :scope "provided"}
2+
:deps {org.clojure/clojure {:mvn/version "1.12.0" }
33
cnuernber/dtype-next {:mvn/version "10.111"}
44
net.java.dev.jna/jna {:mvn/version "5.12.1"}
55
org.clojure/data.json {:mvn/version "1.0.0"}
6+
cheshire/cheshire {:mvn/version "5.13.0"}
7+
scicloj/clojisr {:mvn/version "1.0.0"}
68
;;Replace me with caffeine...
79
com.google.guava/guava {:mvn/version "31.1-jre"}}
10+
11+
812

913
:aliases {:dev
10-
{:extra-deps {criterium/criterium {:mvn/version"0.4.5"}
14+
{:extra-deps {criterium/criterium {:mvn/version "0.4.5"}
1115
ch.qos.logback/logback-classic {:mvn/version "1.1.3"}}}
1216
:fastcall
1317
{:jvm-opts ["-Dlibpython_clj.manual_gil=true"]}
@@ -77,4 +81,172 @@
7781
:exec-args {:installer :local
7882
:artifact "target/libpython-clj.jar"}}
7983

80-
}}
84+
}
85+
86+
:python-version "3.13.7"
87+
:python-deps [
88+
"openai==1.58.1"
89+
]
90+
91+
:renv-deps {:R
92+
{:Version "4.4.1",
93+
:Repositories [{:Name "CRAN", :URL "https://cloud.r-project.org"}]},
94+
:Packages
95+
{:tidyselect
96+
{:Package "tidyselect",
97+
:Version "1.2.1",
98+
:Source "Repository",
99+
:Repository "CRAN",
100+
:Requirements
101+
["R" "cli" "glue" "lifecycle" "rlang" "vctrs" "withr"],
102+
:Hash "829f27b9c4919c16b593794a6344d6c0"},
103+
:withr
104+
{:Package "withr",
105+
:Version "3.0.1",
106+
:Source "Repository",
107+
:Repository "CRAN",
108+
:Requirements ["R" "grDevices" "graphics"],
109+
:Hash "07909200e8bbe90426fbfeb73e1e27aa"},
110+
:utf8
111+
{:Package "utf8",
112+
:Version "1.2.4",
113+
:Source "Repository",
114+
:Repository "CRAN",
115+
:Requirements ["R"],
116+
:Hash "62b65c52671e6665f803ff02954446e9"},
117+
:rlang
118+
{:Package "rlang",
119+
:Version "1.1.4",
120+
:Source "Repository",
121+
:Repository "CRAN",
122+
:Requirements ["R" "utils"],
123+
:Hash "3eec01f8b1dee337674b2e34ab1f9bc1"},
124+
:glue
125+
{:Package "glue",
126+
:Version "1.7.0",
127+
:Source "Repository",
128+
:Repository "CRAN",
129+
:Requirements ["R" "methods"],
130+
:Hash "e0b3a53876554bd45879e596cdb10a52"},
131+
:vctrs
132+
{:Package "vctrs",
133+
:Version "0.6.5",
134+
:Source "Repository",
135+
:Repository "CRAN",
136+
:Requirements ["R" "cli" "glue" "lifecycle" "rlang"],
137+
:Hash "c03fa420630029418f7e6da3667aac4a"},
138+
:tibble
139+
{:Package "tibble",
140+
:Version "3.2.1",
141+
:Source "Repository",
142+
:Repository "CRAN",
143+
:Requirements
144+
["R"
145+
"fansi"
146+
"lifecycle"
147+
"magrittr"
148+
"methods"
149+
"pillar"
150+
"pkgconfig"
151+
"rlang"
152+
"utils"
153+
"vctrs"],
154+
:Hash "a84e2cc86d07289b3b6f5069df7a004c"},
155+
:magrittr
156+
{:Package "magrittr",
157+
:Version "2.0.3",
158+
:Source "Repository",
159+
:Repository "CRAN",
160+
:Requirements ["R"],
161+
:Hash "7ce2733a9826b3aeb1775d56fd305472"},
162+
:lifecycle
163+
{:Package "lifecycle",
164+
:Version "1.0.4",
165+
:Source "Repository",
166+
:Repository "CRAN",
167+
:Requirements ["R" "cli" "glue" "rlang"],
168+
:Hash "b8552d117e1b808b09a832f589b79035"},
169+
:dplyr
170+
{:Package "dplyr",
171+
:Version "1.1.4",
172+
:Source "Repository",
173+
:Repository "CRAN",
174+
:Requirements
175+
["R"
176+
"R6"
177+
"cli"
178+
"generics"
179+
"glue"
180+
"lifecycle"
181+
"magrittr"
182+
"methods"
183+
"pillar"
184+
"rlang"
185+
"tibble"
186+
"tidyselect"
187+
"utils"
188+
"vctrs"],
189+
:Hash "fedd9d00c2944ff00a0e2696ccf048ec"},
190+
:pkgconfig
191+
{:Package "pkgconfig",
192+
:Version "2.0.3",
193+
:Source "Repository",
194+
:Repository "CRAN",
195+
:Requirements ["utils"],
196+
:Hash "01f28d4278f15c76cddbea05899c5d6f"},
197+
:fansi
198+
{:Package "fansi",
199+
:Version "1.0.6",
200+
:Source "Repository",
201+
:Repository "CRAN",
202+
:Requirements ["R" "grDevices" "utils"],
203+
:Hash "962174cf2aeb5b9eea581522286a911f"},
204+
:renv
205+
{:Package "renv",
206+
:Version "1.0.7",
207+
:Source "Repository",
208+
:Repository "CRAN",
209+
:Requirements ["utils"],
210+
:Hash "397b7b2a265bc5a7a06852524dabae20"},
211+
:pillar
212+
{:Package "pillar",
213+
:Version "1.9.0",
214+
:Source "Repository",
215+
:Repository "CRAN",
216+
:Requirements
217+
["cli" "fansi" "glue" "lifecycle" "rlang" "utf8" "utils" "vctrs"],
218+
:Hash "15da5a8412f317beeee6175fbc76f4bb"},
219+
:generics
220+
{:Package "generics",
221+
:Version "0.1.3",
222+
:Source "Repository",
223+
:Repository "CRAN",
224+
:Requirements ["R" "methods"],
225+
:Hash "15e9634c0fcd294799e9b2e929ed1b86"},
226+
:cli
227+
{:Package "cli",
228+
:Version "3.6.3",
229+
:Source "Repository",
230+
:Repository "CRAN",
231+
:Requirements ["R" "utils"],
232+
:Hash "b21916dd77a27642b447374a5d30ecf3"},
233+
:R6
234+
{:Package "R6",
235+
:Version "2.5.1",
236+
:Source "Repository",
237+
:Repository "CRAN",
238+
:Requirements ["R"],
239+
:Hash "470851b6d5d0ac559e9d01bb352b4021"}
240+
:Rserve
241+
{:Package "Rserve",
242+
:Version "1.8-13",
243+
:Source "Repository",
244+
:Repository "CRAN",
245+
:Requirements ["R"],
246+
:Hash "589ebd25cffe93ab5ab61e66936d2678"}
247+
}}
248+
249+
250+
251+
252+
}

python.edn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{:python-executable ".venv/bin/python"
2+
:pre-initialize-fn libpython-clj2.python.uv/setup!}
3+

src/libpython_clj2/python.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ user> (py/py. np linspace 2 3 :num 10)
113113
(let [python-edn-opts (-> (try (slurp "python.edn")
114114
(catch java.io.FileNotFoundException _ "{}"))
115115
clojure.edn/read-string)
116+
_ (log/infof "Pre-initialize-fn %s" (some-> python-edn-opts :pre-initialize-fn))
116117
_ (some-> python-edn-opts :pre-initialize-fn requiring-resolve (apply []))
117118
options (merge python-edn-opts options)
118119
info (py-info/detect-startup-info options)

src/libpython_clj2/python/uv.clj

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
(ns libpython-clj2.python.uv
2+
(:require
3+
[clojure.edn :as edn]
4+
[clojure.java.process :as process]
5+
[clojure.string :as str]
6+
[cheshire.core :as json]
7+
))
8+
9+
(defn write-pyproject-toml! [deps-edn]
10+
11+
(let [
12+
13+
python-deps
14+
(:python-deps deps-edn)
15+
16+
;python-version (:python-version deps-edn)
17+
18+
py-project-header-lines ["[project]"
19+
"name = \"temp\""
20+
"version = \"0.0\""
21+
;(format "requires-python = \"==%s\"" python-version)
22+
]
23+
python-deps-lines
24+
(map
25+
(fn [dep]
26+
(format "\"%s\"," dep))
27+
python-deps)
28+
29+
py-project-lines
30+
(concat
31+
py-project-header-lines
32+
[ "dependencies = ["]
33+
python-deps-lines
34+
"]\n")
35+
]
36+
37+
(spit "pyproject.toml"
38+
(str/join "\n" py-project-lines))))
39+
40+
(defn char-seq
41+
[^java.io.Reader rdr]
42+
(let [chr (.read rdr)]
43+
(if (>= chr 0)
44+
(do
45+
;(def chr chr)
46+
(print (char (Integer. chr)))
47+
(flush)
48+
(cons chr (lazy-seq (char-seq rdr)))))))
49+
50+
51+
(defn start-and-print! [process-args]
52+
(let [args
53+
(concat [{:env { "RENV_CONFIG_INSTALL_VERBOSE" "TRUE"}}]
54+
process-args)
55+
p
56+
(apply process/start args)]
57+
58+
(with-open [in-rdr (java.io.InputStreamReader. (.getInputStream p))
59+
err-rdr (java.io.InputStreamReader. (.getErrorStream p))]
60+
61+
62+
63+
(dorun (char-seq in-rdr))
64+
(dorun (char-seq err-rdr)))))
65+
66+
67+
(defn setup-python! []
68+
(println "Synchronize python venv with uv sync. This might take a few minutes")
69+
(let [deps-edn
70+
(->
71+
(slurp "deps.edn")
72+
edn/read-string)
73+
74+
]
75+
(write-pyproject-toml! deps-edn)
76+
(start-and-print! [ "unbuffer" "uv" "sync" "--python" (-> deps-edn :python-version)]))
77+
)
78+
79+
(defn setup-r! []
80+
(let [renv-lock
81+
(->
82+
(slurp "deps.edn")
83+
edn/read-string
84+
:renv-deps
85+
(json/generate-string {:pretty true}))]
86+
(spit "renv.lock" renv-lock))
87+
88+
(start-and-print! ["rm" "-rf" "renv"])
89+
(start-and-print! ["rm" ".Rprofile"])
90+
(start-and-print! ["Rscript" "-e" "renv::init()"])
91+
(start-and-print! ["Rscript" "-e" "renv::restore()"]))
92+
93+
(defn setup! [& args]
94+
(setup-python!)
95+
(setup-r!)
96+
)
97+

0 commit comments

Comments
 (0)