Mercurial > rlm
view 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 source
1 (ns rlm.shell-write3 (:use4 [clojure.contrib5 [duck-streams :only [file-str]]6 command-line7 str-utils8 shell-out9 ])11 (:import (java.io File BufferedReader ByteArrayOutputStream InputStreamReader))12 (:import (org.apache.commons.exec PumpStreamHandler DefaultExecutor ExecuteWatchdog CommandLine)))15 (defn sw16 "same as sh but uses apache-commons to actually execute the process,17 and prints output as soon as the subprocess returns output. Prints all18 errors and normal ouput"19 [& commands+args]21 (let [[commands {:keys [dir]}] (split-with string? commands+args)]22 (let23 [24 parsed-commands (str (CommandLine/parse (apply str (interpose " " commands))))25 process (if dir26 (.exec (Runtime/getRuntime) parsed-commands27 (into-array String "") (file-str dir))28 (.exec (Runtime/getRuntime) parsed-commands))]29 (println parsed-commands)30 (let [reader31 (BufferedReader.32 (InputStreamReader. (.getInputStream process)))34 error-reader35 (BufferedReader.36 (InputStreamReader. (.getErrorStream process)))]38 ;output from program40 (loop []41 (let [line (.readLine reader )]42 (if (not (nil? line))43 (do (println line) (recur)) nil)))45 ;errors from program48 (let [err-str50 (loop [errors ""]51 (let [line (.readLine error-reader )]52 (if (not (nil? line))53 (recur (str errors line "\n"))54 errors)))]55 (if (> (.length err-str) 0)56 (do57 (println "******************** Error Stream ********************")58 (println err-str)59 (println "******************************************************"))))))))