Mercurial > rlm
diff src/rlm/shell_write.clj @ 0:78a630e650d2
initial import
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Oct 2011 00:57:08 -0700 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/rlm/shell_write.clj Tue Oct 18 00:57:08 2011 -0700 1.3 @@ -0,0 +1,70 @@ 1.4 +(ns rlm.shell-write 1.5 + 1.6 + (:use 1.7 + [clojure.contrib 1.8 + [duck-streams :only [file-str]] 1.9 + command-line 1.10 + str-utils 1.11 + shell-out 1.12 + ]) 1.13 + 1.14 + (:import (java.io File BufferedReader ByteArrayOutputStream InputStreamReader)) 1.15 + (:import (org.apache.commons.exec PumpStreamHandler DefaultExecutor ExecuteWatchdog CommandLine))) 1.16 + 1.17 + 1.18 +(defn sw 1.19 + "same as sh but uses apache-commons to actually execute the process, 1.20 + and prints output as soon as the subprocess returns output. Prints all 1.21 + errors and normal ouput" 1.22 + [& commands+args] 1.23 + 1.24 + (let [[commands {:keys [dir]}] (split-with string? commands+args)] 1.25 + (let 1.26 + [ 1.27 + parsed-commands (str (CommandLine/parse (apply str (interpose " " commands)))) 1.28 + process (if dir 1.29 + (.exec (Runtime/getRuntime) parsed-commands 1.30 + (into-array String "") (file-str dir)) 1.31 + (.exec (Runtime/getRuntime) parsed-commands))] 1.32 + (println parsed-commands) 1.33 + (let [reader 1.34 + (BufferedReader. 1.35 + (InputStreamReader. (.getInputStream process))) 1.36 + 1.37 + error-reader 1.38 + (BufferedReader. 1.39 + (InputStreamReader. (.getErrorStream process)))] 1.40 + 1.41 + ;output from program 1.42 + 1.43 + (loop [] 1.44 + (let [line (.readLine reader )] 1.45 + (if (not (nil? line)) 1.46 + (do (println line) (recur)) nil))) 1.47 + 1.48 + ;errors from program 1.49 + 1.50 + 1.51 + (let [err-str 1.52 + 1.53 + (loop [errors ""] 1.54 + (let [line (.readLine error-reader )] 1.55 + (if (not (nil? line)) 1.56 + (recur (str errors line "\n")) 1.57 + errors)))] 1.58 + (if (> (.length err-str) 0) 1.59 + (do 1.60 + (println "******************** Error Stream ********************") 1.61 + (println err-str) 1.62 + (println "******************************************************")))))))) 1.63 + 1.64 + 1.65 + 1.66 + 1.67 + 1.68 + 1.69 + 1.70 + 1.71 + 1.72 + 1.73 +