Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

file type inspection

  • Loading branch information...
commit 6019a6df5b369d3b4822914425011fc59d2fb691 1 parent aec2cb3
@timmc authored
View
32 clj/src/kpawebgen/util.clj
@@ -1,6 +1,7 @@
(ns kpawebgen.util
"Common utilities."
- (:require [clojure.java.io :as io])
+ (:require [clojure.java.io :as io]
+ clojure.java.shell)
(:import (java.io File)
(java.util.regex Pattern Matcher)))
@@ -12,9 +13,34 @@
(if (.isAbsolute f2) (io/file p) (io/file f p))))
(defn file
- "Concatenate one or more paths. Absolute paths are respected. Concatenating
-a/b/c with /d/e/f results in /d/e/f."
+ "Concatenate one or more paths. Absolute paths overrule all previous paths.
+Concatenating a/b/c with /d/e/f results in /d/e/f."
[path & more]
{:pre [path], :post [(cast File %)]}
(let [f (io/file path)]
(if (empty? more) (io/file f) (reduce path-cat f more))))
+
+(defn sh-file
+ "Get the shell result of calling the UNIX `file` command on a File."
+ [f]
+ {:pre [(cast File f)]}
+ (clojure.java.shell/sh "file" "-b" "-L" "--" (.getPath f)))
+
+(defn file-type-match-any?
+ "Check if a file f's type matches (via re-find) any regex supplied in
+re-list, returning logical boolean. File may be a path or java.io.File.
+Always false if error occurs when attempting to stat or read file.
+opts is a map of options:
+
+* :ok-missing? [false] Return true if file is missing
+* :ok-empty? [false] Return true if file is empty"
+ [f re-list & opts]
+ (let [f (file f)
+ {:keys [ok-missing? ok-empty?]
+ :or {ok-missing? false, ok-empty? false}} opts]
+ (if (.exists f)
+ (let [{:keys [exit out]} (sh-file f)]
+ (and (= exit 0)
+ (some #(re-find % out)
+ (if ok-empty? (cons #"^empty\n$" re-list) re-list))))
+ ok-missing?)))
View
37 clj/test/kpawebgen/test/util.clj
@@ -1,6 +1,7 @@
(ns kpawebgen.test.util
- (:use [kpawebgen.util])
- (:use [clojure.test]))
+ (:use [kpawebgen.util]
+ [clojure.test])
+ (require [clojure.java.io :as io]))
(deftest paths
(are [a e] (= (.getPath a)
@@ -13,3 +14,35 @@
(file "a/b/" "/etc/init.d" "c/d") "/etc/init.d/c/d"
(file "/a" "/b" "/c" "/d" "/e") "/e"))
+(defn mapply
+ [f & args]
+ (apply f (apply concat (butlast args) (last args))))
+
+(defn maybe-real
+ "Resolve a file resource if real, or just make a File somehow."
+ [path]
+ (if-let [res (io/resource path)]
+ (io/as-file res)
+ (java.io.File. path)))
+
+(deftest file-types
+ (are [f res opts exp]
+ (= (boolean (mapply file-type-match-any? (maybe-real f) res opts))
+ exp)
+ ;; basic explicit behavior
+ "." [#"directory"] {:ok-empty? false, :ok-missing? false} true
+ ;; some, not every
+ "." [#"xxx" #"dir"] {:ok-empty? false, :ok-missing? false} true
+ ;; default: missing is not OK
+ "/fnurble" [#"whatever"] {} false
+ "/fnurble" [#"whatever"] {:ok-missing? true} true
+ ;; default: empty is not OK
+ "kpawebgen/util/filetype/blank.txt" [#"xxx"] {} false
+ "kpawebgen/util/filetype/blank.txt" [#"empty"] {} true
+ ;; no conflation of empty with missing
+ "kpawebgen/util/filetype/blank.txt" [#"xxx"] {:ok-missing? true} false
+ "kpawebgen/util/filetype/blank.txt" [#"xxx"] {:ok-empty? true} true
+ ;; don't even need matchers
+ "kpawebgen/util/filetype/blank.txt" [] {:ok-empty? true} true
+ ;; basic matching on a normal file
+ "kpawebgen/util/filetype/img.png" [#"image"] {} true))
View
0  clj/test/kpawebgen/util/filetype/blank.txt
No changes.
View
BIN  clj/test/kpawebgen/util/filetype/img.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Please sign in to comment.
Something went wrong with that request. Please try again.