[#31] respect PIPE signal
This commit is contained in:
parent
05f408e3af
commit
cdb973365c
5 changed files with 51 additions and 25 deletions
2
sci
2
sci
|
|
@ -1 +1 @@
|
|||
Subproject commit cca49acbd2c7b2ca02f75167d18c4475a56e60bb
|
||||
Subproject commit 225dfe2314f2789b3050be2a70f10c123c13d277
|
||||
16
src/babashka/impl/pipe_signal_handler.clj
Normal file
16
src/babashka/impl/pipe_signal_handler.clj
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
(ns babashka.impl.pipe-signal-handler
|
||||
{:no-doc true}
|
||||
(:import [sun.misc Signal]
|
||||
[sun.misc SignalHandler]))
|
||||
|
||||
(def pipe-state (volatile! nil))
|
||||
|
||||
(defn pipe-signal-received? []
|
||||
(identical? :PIPE @pipe-state))
|
||||
|
||||
(defn handle-pipe! []
|
||||
(Signal/handle
|
||||
(Signal. "PIPE")
|
||||
(reify SignalHandler
|
||||
(handle [_ _]
|
||||
(vreset! pipe-state :PIPE)))))
|
||||
|
|
@ -1,12 +1,15 @@
|
|||
(ns babashka.main
|
||||
{:no-doc true}
|
||||
(:require
|
||||
[babashka.impl.File :as File]
|
||||
[babashka.impl.pipe-signal-handler :refer [handle-pipe! pipe-signal-received?]]
|
||||
[clojure.edn :as edn]
|
||||
[clojure.java.io :as io]
|
||||
[clojure.java.shell :as shell]
|
||||
[clojure.string :as str]
|
||||
[sci.core :as sci]
|
||||
[babashka.impl.File :as File])
|
||||
[sci.core :as sci])
|
||||
(:import [sun.misc Signal]
|
||||
[sun.misc SignalHandler])
|
||||
(:gen-class))
|
||||
|
||||
(set! *warn-on-reflection* true)
|
||||
|
|
@ -142,22 +145,13 @@
|
|||
(edn/read {;;:readers *data-readers*
|
||||
:eof ::EOF} *in*))
|
||||
|
||||
#_(defn set-ssl []
|
||||
(let [home (System/getProperty "user.home")
|
||||
bb-lib-dir (io/file home ".babashka" "lib")
|
||||
lib-path (System/getProperty "java.library.path")
|
||||
ca-certs-dir (io/file bb-lib-dir "security")
|
||||
ca-certs (.getPath (io/file ca-certs-dir "cacerts"))]
|
||||
(System/setProperty "java.library.path" (str (.getPath bb-lib-dir) ":" lib-path))
|
||||
(System/setProperty "javax.net.ssl.trustStore" ca-certs)
|
||||
(System/setProperty "javax.net.ssl.tru stAnchors" ca-certs)))
|
||||
|
||||
(defn load-file* [ctx file]
|
||||
(let [s (slurp file)]
|
||||
(sci/eval-string s ctx)))
|
||||
|
||||
(defn main
|
||||
[& args]
|
||||
(handle-pipe!)
|
||||
#_(binding [*out* *err*]
|
||||
(prn ">> args" args))
|
||||
(let [t0 (System/currentTimeMillis)
|
||||
|
|
@ -165,13 +159,15 @@
|
|||
:help? :file :command-line-args
|
||||
:expression :stream? :time?] :as _opts}
|
||||
(parse-opts args)
|
||||
read-next #(if stream?
|
||||
(if raw-in (or (read-line) ::EOF)
|
||||
(read-edn))
|
||||
(delay (let [in (slurp *in*)]
|
||||
(if raw-in
|
||||
(parse-shell-string in)
|
||||
(edn/read-string in)))))
|
||||
read-next #(if (pipe-signal-received?)
|
||||
::EOF
|
||||
(if stream?
|
||||
(if raw-in (or (read-line) ::EOF)
|
||||
(read-edn))
|
||||
(delay (let [in (slurp *in*)]
|
||||
(if raw-in
|
||||
(parse-shell-string in)
|
||||
(edn/read-string in))))))
|
||||
env (atom {})
|
||||
ctx {:bindings (assoc bindings '*command-line-args* command-line-args)
|
||||
:env env}
|
||||
|
|
@ -199,7 +195,8 @@
|
|||
(let [res (sci/eval-string expr ctx)]
|
||||
(if raw-out
|
||||
(if (coll? res)
|
||||
(doseq [l res]
|
||||
(doseq [l res
|
||||
:while (not (pipe-signal-received?))]
|
||||
(println l))
|
||||
(println res))
|
||||
((if println? println? prn) res)))) 0]]
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
(ns babashka.main-test
|
||||
(:require
|
||||
[clojure.test :as test :refer [deftest is testing]]
|
||||
[babashka.test-utils :as test-utils]
|
||||
[babashka.main :as main]
|
||||
[babashka.test-utils :as test-utils]
|
||||
[clojure.edn :as edn]
|
||||
[clojure.string :as str]))
|
||||
[clojure.java.shell :refer [sh]]
|
||||
[clojure.string :as str]
|
||||
[clojure.test :as test :refer [deftest is testing]]))
|
||||
|
||||
(defn bb [input & args]
|
||||
(edn/read-string (apply test-utils/bb (str input) (map str args))))
|
||||
|
|
@ -114,3 +115,12 @@
|
|||
(deftest io-test
|
||||
(is (true? (bb nil "(.exists (io/file \"README.md\"))")))
|
||||
(is (true? (bb nil "(.canWrite (io/file \"README.md\"))"))))
|
||||
|
||||
(deftest pipe-test
|
||||
(when test-utils/native?
|
||||
(let [out (:out (sh "bash" "-c" "./bb -o '(range)' |
|
||||
./bb --stream '(* *in* *in*)' |
|
||||
head -n10"))
|
||||
out (str/split-lines out)
|
||||
out (map edn/read-string out)]
|
||||
(is (= out (take 10 (map #(* % %) (range))))))))
|
||||
|
|
|
|||
|
|
@ -33,6 +33,9 @@
|
|||
"native" #'bb-native
|
||||
#'bb-jvm))
|
||||
|
||||
(if (= bb #'bb-jvm)
|
||||
(def jvm? (= bb #'bb-jvm))
|
||||
(def native? (not jvm?))
|
||||
|
||||
(if jvm?
|
||||
(println "==== Testing JVM version")
|
||||
(println "==== Testing native version"))
|
||||
|
|
|
|||
Loading…
Reference in a new issue