Skip to content

Instantly share code, notes, and snippets.

@CraZySacX
Created May 7, 2014 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CraZySacX/88ffce3ff09aa3d1188e to your computer and use it in GitHub Desktop.
Save CraZySacX/88ffce3ff09aa3d1188e to your computer and use it in GitHub Desktop.
Sorting by int value rather than String value in a mixed sequence
(ns wgetjava.handler
(:require [clojure.java.io :as io]
[clojure.string :as str]
[org.ozias.cljlibs.shell.shell :refer :all]
[org.ozias.cljlibs.utils.core :refer :all])
(:import (clojure.lang PersistentArrayMap PersistentVector)))
(def ^{:private true :doc "cookies temp file path"}
cookies-path (to-path (tmpdir) "cookies.txt"))
(def ^{:private true :doc "wget shell command vector"}
wget ["wget" "-c" "--progress=bar:force" "--load-cookies" cookies-path "-P"])
(def ^{:private true :doc "base oracle Java download url"}
baseurl "http://download.oracle.com/otn-pub/java/jdk/")
(def ^{:private true :doc "filename prefix map"}
fprem
{:jdk "jdk-"
:jre "jre-"})
(def ^{:private true :doc "filename suffix map"}
fsufm
{:lin {:32 {:rpm "-linux-i586.rpm"
:tgz "-linux-i586.tar.gz"}
:64 {:rpm "-linux-x64.rpm"
:tgz "-linux-x64.tar.gz"}}
:mac {:64 {:dmg "-macosx-x64.dmg"}}
:win {:32 {:exe "-windows-i586.exe"}
:64 {:exe "-windows-x64.exe"}}})
(def ^{:private true :doc "url suffix map"}
usufm
{:7 {:0 ""
:1 "08"
:2 "13"
:3 "04"
:4 "20"
:5 "06"
:6 "24"
:7 "10"
:9 "05"
:10 "18"
:11 "21"
:13 "20"
:15 "03"
:17 "02"
:21 "11"
:25 "15"
:40 "43"
:45 "18"
:51 "13"
:55 "13"}
:8 {:0 "132"
:5 "13"}})
(defn build-url
"Build the download URL from the configuration.
Will evaluate to nil if the configuration is not supported."
[& {:keys [type ver pr os arch ft]
:or {type :jdk ver :8 pr :5 os :lin arch :64 ft :tgz}}]
(let [prn (name pr)
vn (name ver)
vs (if (= "0" prn) vn (str vn "u" prn))]
(if-let [suf (-> usufm ver pr)]
(let [usuf (str vs (if-not (= "" suf) (str "-b" suf "/") "/"))]
(if-let [pre (type fprem)]
(if-let [sufm (-> fsufm os arch ft)]
(str baseurl usuf pre vs sufm)))))))
(defn- out
"Used to print characters from conch output buffer"
[c _]
(print c)
(flush))
(defn- smash-maps
"Smash the url maps into a map of all valid key combinations"
[]
(into {} (for [[k _] fprem]
[k (into {} (for [[k1 v] usufm]
[k1 (into {} (for [[k2 _] v]
[k2 fsufm]))]))])))
(defn- conf-comparator
"Compare alphanumeric normally, numeric inverted (desc)"
[x y]
(let [fx (first x)
fy (first y)]
(if (= fx fy)
(conf-comparator (rest x) (rest y))
(if (and (number? fx) (number? fy))
(compare fy fx)
(compare fx fy)))))
(defmulti convert-types
"Convert the strings that are ints into Integers"
class)
(defmethod convert-types PersistentVector [[a b c d e f]]
[a (Integer/parseInt b) (Integer/parseInt c) d (Integer/parseInt e) f])
(defmethod convert-types PersistentArrayMap [m]
(assoc m
:ver (Integer/parseInt (:ver m))
:pr (Integer/parseInt (:pr m))
:arch (Integer/parseInt (:arch m))))
(defn valid-keys
"Display the valid keys that can be given to the download function"
[]
(->> (flatten-map (smash-maps) "-")
(keys)
(map #(str/split (name %) #"-"))
(into [])
(map convert-types)
(sort conf-comparator)
(map reverse)
(map #(zipmap [:ft :arch :os :pr :ver :type] %))))
(defn- setup-cookies
"Setup the proper cookies.txt to be used with wget"
[type ver]
(let [f (str "cookies_" (name ver) "_" (name type) ".txt")]
(spit cookies-path (slurp (io/resource f)))))
(defn download
"Download a Java JRE/JDK
The following keys control what to download:
:type - :jre, :jdk (Default: :jdk)
:ver - :7, :8 (Default: :8)
:pr - :<num> (Default: :5)
:os - :lin, :mac, :win (Default :lin)
:arch - :32, :64 (Default: :64)
:ft - :rpm, :tgz, :dmg, :exe (Default: :tgz)
:dd - download directory (Default: tmpdir)
(download) => Will download jdk-8u5-linux-x64.tar.gz to your system dependent
temporary directory.
(download :type :jre) => Will download jre-8u5-linux-x64.tar.gz to your system
dependent temporary directory."
[& {:keys [type ver pr os arch ft dd]
:or {type :jdk ver :8 pr :5 os :lin arch :64 ft :tgz dd (tmpdir)}}]
(let [wget (conj wget dd)]
(when-let [url (build-url :type type :ver ver :pr pr
:os os :arch arch :ft ft)]
(setup-cookies type ver)
(if (-> wget
(conj url)
(shell-cmd :out out :err out :seq true :buffer 80) successful?)
[0 ["Successfully downloaded the file at:" \newline url]]
[1 ["Unable to download the requested file"]]))))
(defn- filter-by-os
"Filter the given map by :os value"
[m]
(filter #(cond
(linux?) (= "lin" (:os %))
(mac?) (= "mac" (:os %))
(windows?) (= "win" (:os %))) m))
(defn- unsupported
"Generate the unsupported configuration message"
[conf]
[1 (into ["Unsupported Java Download requested" \newline \newline
"Supplied Configuration:" \newline
; conf \newline \newline
(->> conf
(remove (comp nil? val))
(fmapv name)
(reverse)
(into {})
(convert-types)) \newline \newline
"Supported Configurations for your OS:" \newline]
(interpose \newline (filter-by-os (valid-keys))))])
(defn handle-args
"Handle your program arguments here. You may support zero or more.
Ensure that you use the exit function when processing is complete."
[config]
(let [keys [:type :ver :pr :os :arch :ft]
conf (assoc (zipmap keys (map #(keyword (-> config :options %)) keys))
:dd (-> config :options :dir))
args (->> conf
(remove (comp nil? val))
(into [])
(flatten))
url (apply build-url args)
file (if url (last (str/split url #"/")) "")
supported (vec (valid-keys))]
(cond
(-> config :options :dry-run) [0 [file]]
(-> config :options :supported) [0 (interpose \newline supported)]
:else (if url
(apply download args)
(unsupported conf)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment