File tree Expand file tree Collapse file tree 7 files changed +103
-37
lines changed Expand file tree Collapse file tree 7 files changed +103
-37
lines changed Original file line number Diff line number Diff line change
1
+ Basic scripting
2
+ * Entrypoints are defined as tasks in bb.edn.
3
+
4
+ ```
5
+ bb nrepl-server 1667
6
+ ```
7
+
8
+ Nice walkthrough of conjure:
9
+ ```
10
+ :ConjureSchool
11
+ ```
Original file line number Diff line number Diff line change 1
1
{:paths [" " ]
2
- :tasks {
3
- default (prn " Hello, World!" )
4
- findenv hello/venv-test
5
- }
6
- }
2
+ :tasks {venv-find-activate-script dot.cli/venv-find-activate-script
3
+ venv-find dot.cli/venv-find}}
Original file line number Diff line number Diff line change
1
+ function source-env {
2
+ local from_dir=${1:- $PWD }
3
+ local activate_file=" $( bb venv-find-activate-script ${from_dir} ) "
4
+ if [[ -f $activate_file ]]; then
5
+ echo " Sourcing ${activate_file} "
6
+ source $activate_file
7
+ else
8
+ echo " No virtualenv found"
9
+ fi
10
+ }
Original file line number Diff line number Diff line change
1
+ (ns dot.cli
2
+ (:require [clojure.tools.cli :refer [parse-opts]]
3
+ [clojure.pprint]
4
+ [dot.venv]))
5
+
6
+ ; ; CLI adapters
7
+ (defn venv-find-activate-script [& args]
8
+ (let [path (first args)]
9
+ (-> path dot.venv/find-env :activate-path print)))
10
+
11
+ (defn venv-find [& args]
12
+ (let [path (first args)]
13
+ (-> path dot.venv/find-env clojure.pprint/pprint)))
14
+
15
+ (def cli-options
16
+ ; ; An option with a required argument
17
+ [[" -p" " --port PORT" " Port number"
18
+ :default 80
19
+ :parse-fn #(Integer/parseInt %)
20
+ :validate [#(< 0 % 0x10000 ) " Must be a number between 0 and 65536" ]]
21
+ ; ; A non-idempotent option (:default is applied first)
22
+ [" -v" nil " Verbosity level"
23
+ :id :verbosity
24
+ :default 0
25
+ :update-fn inc] ; Prior to 0.4.1, you would have to use:
26
+ ; ; :assoc-fn (fn [m k _] (update-in m [k] inc))
27
+ ; ; A boolean option defaulting to nil
28
+ [" -h" " --help" ]])
29
+
30
+ (defn -main [& args]
31
+ (let [opts (parse-opts args cli-options)]
32
+ (print opts)))
Original file line number Diff line number Diff line change
1
+ (ns dot.util
2
+ (:require [babashka.fs :as fs]))
3
+
4
+ (defn pwd []
5
+ (System/getProperty " user.dir" ))
6
+
7
+ (defn ancestors-and-self [path]
8
+ (if (nil? path)
9
+ []
10
+ (lazy-seq (cons (str path) (ancestors-and-self (fs/parent path))))))
Original file line number Diff line number Diff line change
1
+ (ns dot.venv
2
+ (:require [dot.util :refer [pwd ancestors-and-self]]
3
+ [babashka.fs :as fs]
4
+ [babashka.process :refer [shell]]))
5
+
6
+ (defn- resolve-path [path]
7
+ (if (nil? path) (pwd ) path))
8
+
9
+ (defn dir-candidates [dirseq]
10
+ (for [dir dirseq
11
+ env-dir-name [" .venv" " venv" " env" ]]
12
+ (str dir " /" env-dir-name)))
13
+
14
+ (defn env-binary-path [env-path binary]
15
+ (str env-path " /bin/" binary))
16
+
17
+ (defn python-site-packages-dir [python-exe]
18
+ (let [python-exe (if (nil? python-exe) " python" python-exe)
19
+ python-code " import site; print(site.getsitepackages()[-1])"
20
+ process (shell {:out :string :err :string } python-exe " -c" python-code)]
21
+ (:out process)))
22
+
23
+ (defn find-env [path]
24
+ (let [path (resolve-path path)
25
+ dirseq (ancestors-and-self path)
26
+ env (->> dirseq
27
+ (dir-candidates )
28
+ (filter #(fs/exists? (env-binary-path % " activate" )))
29
+ first)]
30
+ (if env
31
+ {:root (str (fs/parent env))
32
+ :venv-path env
33
+ :activate-path (env-binary-path env " activate" )
34
+ :python-exe-path (env-binary-path env " python" )
35
+ :site-packages-dir (python-site-packages-dir (env-binary-path env " python" ))}
36
+ nil )))
37
+
38
+ ; ; (python-site-packages-dir nil)
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments