Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Lemur suitable as a library #31

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
/tmp
/.idea
/lemur.iml
/.lein-repl-history
/.nrepl-port
2 changes: 1 addition & 1 deletion bin/lemur
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if [ -f "$BASE/resources/log4j.properties" ]; then
LEMUR_LOG_FILE="-Dlog4j.configuration=file:$BASE/resources/log4j.properties"
fi

cmd="$JAVA $LEMUR_LOG_FILE $JAVA_OPTS -cp $CLASSPATH lemur.core"
cmd="$JAVA $LEMUR_LOG_FILE $JAVA_OPTS -cp $CLASSPATH lemur.tool"
if [ "$1" == "dry-run" ]; then echo $cmd "$@"; fi

$cmd "$@"
7 changes: 3 additions & 4 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,20 @@
:libdir-path "lib"

:profiles {:dev {:plugins [[lein-midje "2.0.4"]]
:dependencies [[midje "1.4.0"]
[org.clojure/tools.trace "0.7.3"]]}
:dependencies [[midje "1.4.0"]]}
:1.3 {:dependencies [[org.clojure/clojure "1.3.0"]]}
:1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}}

:repl-init lemur.repl
:main ^:skip-aot lemur.repl
:min-lein-version "2.0.0"

:run-aliases {:lemur lemur.core}
:run-aliases {:lemur lemur.tool}

:test-selectors {:default (fn [v] (not (or (:integration v) (:manual v))))
:integration :integration
:manual :manual
:all (fn [v] (not (:manual v)))}

:aot [lemur.core]
:aot [lemur.tool]
)
11 changes: 10 additions & 1 deletion src/main/clj/lemur/command_line.clj
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,22 @@
command-spec)
(doto (DumperOptions.) (.setDefaultScalarStyle DumperOptions$ScalarStyle/PLAIN))))

(defn quit [& {:keys [msg cmdspec exception exit-code] :or {exit-code 0}}]
(defn- quit* [{:keys [msg cmdspec exception exit-code] :or {exit-code 0}}]
(if cmdspec (print-usage cmdspec))
(if (and msg (= 0 exit-code)) (println msg "\n"))
(if (and msg (not= 0 exit-code)) (println (format "ERROR%n%n%s%n" msg)))
(if exception (print-stack-trace exception))
(System/exit exit-code))

(defn quit [& args]
(quit* args))

(defn quit-by-error [ex]
(quit* (:data (ex-data ex))))

(defn error [& {:keys [msg] :as args}]
(throw (ex-info msg {:data args})))

(defn- defaults-from-cmd-spec
"cmd-spec is a coll of tuples. Extract the default values (i.e. the optional
third arg of each tuple) and return as a map."
Expand Down
14 changes: 7 additions & 7 deletions src/main/clj/lemur/common.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
are not relied upon by core lemur (except for the core base), but may be used in
your own jobdefs."
(:use
[lemur.core :only [lfn]]
[lemur.command-line :only [extract-cl-args quit]])
[lemur.core :only [lfn]])
(:require
[clojure.string :as s]
[clojure.java.io :as io]
[clojure.tools.logging :as log]
[com.climate.shell :as sh]
[com.climate.io :as ccio]
[lemur.command-line :as cli]
[lemur.util :as util]
[com.climate.services.aws.s3 :as s3]
[com.climate.services.aws.emr :as emr])
Expand Down Expand Up @@ -167,9 +167,9 @@
:or {mini-spec [] min 0 max Integer/MAX_VALUE empty false required []}}
args]
(when (and (seq required) (empty? mini-spec))
(quit :exit-code 1 :msg "(val-remaining) call has a :required clause, but not a :mini-spec"))
(cli/error :exit-code 1 :msg "(val-remaining) call has a :required clause, but not a :mini-spec"))
(lfn [remaining]
(let [[remaining-opts remaining-args] (extract-cl-args mini-spec remaining)
(let [[remaining-opts remaining-args] (cli/extract-cl-args mini-spec remaining)
results (vector
(if-not (>= (count remaining-args) min)
(err remaining-opts remaining-args "Must contain at least %s elements" min))
Expand Down Expand Up @@ -220,13 +220,13 @@
file))]
(sh/sh "diff" "-u" (.getPath leftf) (.getPath rightf) :out :pass)))

(defn- quit-on-failure
(defn- error-on-failure
"First arg is the result of com.climate.shell/sh (a map). If the sh result
indicates a failure, via it's :exit value, then quit with the given msg for
output. The exit-code of the process will be the :exit value from the map."
[sh-result msg]
(when-not (zero? (:exit sh-result))
(quit :msg msg :exit-code (:exit sh-result))))
(cli/error :msg msg :exit-code (:exit sh-result))))

(defn diff-test-data
"Validate test data against known expected results. Creates a fn suitable for
Expand All @@ -252,7 +252,7 @@
[& tests]
(fn [eopts _]
(doseq [[name path] tests]
(quit-on-failure
(error-on-failure
(diff-parts
(str (:test-uri eopts) "/expected/" path)
(str (:data-uri eopts) "/" path))
Expand Down
Loading