Mercurial > rlm
annotate 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 |
rev | line source |
---|---|
rlm@0 | 1 (ns rlm.shell-write |
rlm@0 | 2 |
rlm@0 | 3 (:use |
rlm@0 | 4 [clojure.contrib |
rlm@0 | 5 [duck-streams :only [file-str]] |
rlm@0 | 6 command-line |
rlm@0 | 7 str-utils |
rlm@0 | 8 shell-out |
rlm@0 | 9 ]) |
rlm@0 | 10 |
rlm@0 | 11 (:import (java.io File BufferedReader ByteArrayOutputStream InputStreamReader)) |
rlm@0 | 12 (:import (org.apache.commons.exec PumpStreamHandler DefaultExecutor ExecuteWatchdog CommandLine))) |
rlm@0 | 13 |
rlm@0 | 14 |
rlm@0 | 15 (defn sw |
rlm@0 | 16 "same as sh but uses apache-commons to actually execute the process, |
rlm@0 | 17 and prints output as soon as the subprocess returns output. Prints all |
rlm@0 | 18 errors and normal ouput" |
rlm@0 | 19 [& commands+args] |
rlm@0 | 20 |
rlm@0 | 21 (let [[commands {:keys [dir]}] (split-with string? commands+args)] |
rlm@0 | 22 (let |
rlm@0 | 23 [ |
rlm@0 | 24 parsed-commands (str (CommandLine/parse (apply str (interpose " " commands)))) |
rlm@0 | 25 process (if dir |
rlm@0 | 26 (.exec (Runtime/getRuntime) parsed-commands |
rlm@0 | 27 (into-array String "") (file-str dir)) |
rlm@0 | 28 (.exec (Runtime/getRuntime) parsed-commands))] |
rlm@0 | 29 (println parsed-commands) |
rlm@0 | 30 (let [reader |
rlm@0 | 31 (BufferedReader. |
rlm@0 | 32 (InputStreamReader. (.getInputStream process))) |
rlm@0 | 33 |
rlm@0 | 34 error-reader |
rlm@0 | 35 (BufferedReader. |
rlm@0 | 36 (InputStreamReader. (.getErrorStream process)))] |
rlm@0 | 37 |
rlm@0 | 38 ;output from program |
rlm@0 | 39 |
rlm@0 | 40 (loop [] |
rlm@0 | 41 (let [line (.readLine reader )] |
rlm@0 | 42 (if (not (nil? line)) |
rlm@0 | 43 (do (println line) (recur)) nil))) |
rlm@0 | 44 |
rlm@0 | 45 ;errors from program |
rlm@0 | 46 |
rlm@0 | 47 |
rlm@0 | 48 (let [err-str |
rlm@0 | 49 |
rlm@0 | 50 (loop [errors ""] |
rlm@0 | 51 (let [line (.readLine error-reader )] |
rlm@0 | 52 (if (not (nil? line)) |
rlm@0 | 53 (recur (str errors line "\n")) |
rlm@0 | 54 errors)))] |
rlm@0 | 55 (if (> (.length err-str) 0) |
rlm@0 | 56 (do |
rlm@0 | 57 (println "******************** Error Stream ********************") |
rlm@0 | 58 (println err-str) |
rlm@0 | 59 (println "******************************************************")))))))) |
rlm@0 | 60 |
rlm@0 | 61 |
rlm@0 | 62 |
rlm@0 | 63 |
rlm@0 | 64 |
rlm@0 | 65 |
rlm@0 | 66 |
rlm@0 | 67 |
rlm@0 | 68 |
rlm@0 | 69 |
rlm@0 | 70 |