Skip to content

Commit e422eaa

Browse files
authored
Merge pull request #53 from pmonks/dev
Release 2.0.292
2 parents d31fc24 + 706efa9 commit e422eaa

File tree

7 files changed

+73
-23
lines changed

7 files changed

+73
-23
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This library leverages, and is inspired by, the *excellent* [SPDX project](https
1515

1616
## Disclaimer
1717

18-
**The author and contributors to `lice-comb` are not lawyers, and neither they nor `lice-comb` itself provides legal advice. This is nothing more than logic to assist in finding license information.**
18+
**The author and contributors to `lice-comb` are not lawyers, and neither they nor `lice-comb` itself provides legal advice. This is nothing more than logic to assist in finding license information.** If you need a primer on the legal aspects of open source software, the author has found the [Blue Oak Council](https://blueoakcouncil.org/) to be a useful resource.
1919

2020
## System Requirements
2121

deps.edn

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
hato/hato {:mvn/version "0.9.0"}
2727
dev.weavejester/medley {:mvn/version "1.7.0"}
2828
miikka/clj-base62 {:mvn/version "0.1.1"}
29-
com.github.pmonks/clj-spdx {:mvn/version "1.0.145"}
29+
com.github.pmonks/clj-spdx {:mvn/version "1.0.152"}
3030
com.github.pmonks/rencg {:mvn/version "1.0.51"}
3131
com.github.pmonks/embroidery {:mvn/version "0.1.20"}}
3232
:aliases

src/lice_comb/files.clj

+22-13
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@
4545
(defn probable-license-files
4646
"Returns all probable license files in the given directory, recursively, as a
4747
set of java.io.File objects. dir may be a String or a java.io.File, either of
48-
which must refer to a readable directory."
49-
[dir]
50-
(when (lciu/readable-dir? dir)
51-
(some-> (seq (filter #(and (.isFile ^java.io.File %) (probable-license-file? %)) (file-seq (io/file dir))))
52-
set)))
48+
which must refer to a readable directory. By default ignores hidden
49+
directories."
50+
([dir] (probable-license-files dir false))
51+
([dir include-hidden-dirs?]
52+
(when (lciu/readable-dir? dir)
53+
(some-> (lciu/filter-file-only-seq (io/file dir)
54+
(fn [^java.io.File d] (and (not= (.getCanonicalFile d) (.getCanonicalFile (io/file (lcmvn/local-maven-repo)))) ; Make sure to exclude the Maven local repo, just in case it happens to be nested within dir
55+
(or include-hidden-dirs? (not (.isHidden d)))))
56+
probable-license-file?)
57+
set))))
5358

5459
(defn file->expressions-info
5560
"Returns an expressions-info map for the given file (an InputStream or
@@ -123,14 +128,18 @@
123128
(defn- zip-compressed-files
124129
"Returns a set of all probable ZIP compressed files (Files) in the given
125130
directory, recursively, or nil if there are none. dir may be a String or a
126-
java.io.File, and must refer to a readable directory."
127-
[dir]
128-
(when (lciu/readable-dir? dir)
129-
(some-> (seq (filter #(and (.isFile ^java.io.File %)
130-
(or (s/ends-with? (str %) ".zip")
131-
(s/ends-with? (str %) ".jar")))
132-
(file-seq (io/file dir))))
133-
set)))
131+
java.io.File, and must refer to a readable directory. By default ignores
132+
hidden directories."
133+
([dir] (zip-compressed-files dir false))
134+
([dir include-hidden-dirs?]
135+
(when (lciu/readable-dir? dir)
136+
(some-> (lciu/filter-file-only-seq (io/file dir)
137+
(fn [^java.io.File d] (or include-hidden-dirs? (not (.isHidden d))))
138+
(fn [^java.io.File f]
139+
(let [lname (s/lower-case (.getName f))]
140+
(or (s/ends-with? lname ".zip")
141+
(s/ends-with? lname ".jar")))))
142+
set))))
134143

135144
(defn dir->expressions-info
136145
"Returns an expressions-info map for the given dir (a String or a File,

src/lice_comb/impl/utils.clj

+40
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,46 @@
252252
[_]
253253
(throw (ex-info "Cannot determine filename of an InputStream - did you forget to provide it separately?" {})))
254254

255+
(defn filter-file-seq*
256+
"As for clojure.core/file-seq, but with support for filtering. pred must be
257+
a predicate that accepts one argument of type java.io.File. Files that don't
258+
meet pred will not be included in the result, and directories that don't meet
259+
pred will not be recursed into (so pred must be able to handle both distinct
260+
cases).
261+
262+
Note also that dir is always returned, even if it does not meet pred."
263+
[^java.io.File dir pred]
264+
(let [pred (or pred (constantly true))
265+
filter (reify java.io.FileFilter (accept [_ f] (boolean (pred (.getCanonicalFile ^java.io.File f)))))] ; Use the canonical file, otherwise we will get tripped up by "." being "hidden" according to the JVM when running on a Unix 🤡
266+
(tree-seq
267+
(fn [^java.io.File f] (.isDirectory f))
268+
(fn [^java.io.File d] (seq (.listFiles d filter)))
269+
dir)))
270+
271+
(defn filter-file-seq
272+
"As for clojure.core/file-seq, but with support for filtering. dir-pred
273+
controls which directories will be included in the result and recursed into.
274+
file-pred controls which files will be included in the result. Both must be
275+
a predicate of one argument of type java.io.File.
276+
277+
Note also that dir is always returned, even if it does not meet dir-pred."
278+
[dir dir-pred file-pred]
279+
(let [dir-pred (or dir-pred (constantly true))
280+
file-pred (or file-pred (constantly true))
281+
pred (fn [^java.io.File f]
282+
(or (and (.isDirectory f) (dir-pred f))
283+
(file-pred f)))]
284+
(filter-file-seq* dir pred)))
285+
286+
(defn filter-file-only-seq
287+
"As for clojure.core/file-seq, with support for filtering and only returns
288+
files (but not any directories that were traversed during the seq). dir-pred
289+
controls which directories will be recursed into. file-pred controls which
290+
files will be included in the result. Both must be a predicate of one
291+
argument of type java.io.File."
292+
[dir dir-pred file-pred]
293+
(seq (filter #(.isFile ^java.io.File %) (filter-file-seq dir dir-pred file-pred))))
294+
255295
(defn getenv
256296
"Obtain the given environment variable, returning default (or nil, if default
257297
is not provided) if it isn't set."

src/lice_comb/maven.clj

+3-3
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@
119119
"Attempts to determine the license(s) (a map) from a POM license name/URL
120120
pair. Returns nil if no matches were found."
121121
[{:keys [name url]}]
122-
; 1. Look in the name field(s)
122+
; 1. Look in the name field
123123
(let [name-expressions (lcm/name->expressions-info name)]
124124
(if (or (empty? name-expressions)
125125
(and (= 1 (count name-expressions)) (lcm/unidentified? (first (keys name-expressions)))))
126-
; 2. If the names didn't give us any identified licenses, look in the url field(s) (this can be slower and less accurate, which is why it has lower priority)
126+
; 2. If the name didn't give us any identified licenses, look in the url field (this can be slower and less accurate, which is why it has lower priority)
127127
(let [uri-expressions (lcm/uri->expressions-info url)]
128128
(if (or (empty? uri-expressions)
129129
(and (= 1 (count uri-expressions)) (lcm/unidentified? (first (keys uri-expressions)))))
@@ -258,7 +258,7 @@
258258
version (if (release-version? version) (ga-release-version group-id artifact-id) version)
259259
file-version (resolve-snapshot-version group-id artifact-id version)
260260
gav-path (str (s/replace group-id "." "/") "/" artifact-id "/" version "/" artifact-id "-" file-version ".pom")
261-
local-pom (io/file (str @local-maven-repo-a separator (s/replace gav-path "/" separator)))]
261+
local-pom (io/file (str @local-maven-repo-a separator (s/replace gav-path "/" separator)))]
262262
(if (and (.exists local-pom)
263263
(.isFile local-pom))
264264
(.toURI local-pom)

test/lice_comb/data/CC-BY-4.0/LICENSE

+1
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,4 @@ the avoidance of doubt, this paragraph does not form part of the
393393
public licenses.
394394

395395
Creative Commons may be contacted at creativecommons.org.
396+

test/lice_comb/files_test.clj

+5-5
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@
9292
(testing "Handed a directory"
9393
(is (nil? (file->expressions "."))))
9494
(testing "Files on disk"
95-
; (is (= #{"CC-BY-4.0"} (file->expressions (str test-data-path "/CC-BY-4.0/LICENSE")))) ; Failing due to https://github.com/spdx/license-list-XML/issues/1960
96-
(is (valid= #{"MPL-2.0"} (file->expressions (str test-data-path "/MPL-2.0/LICENSE")))))
95+
; (is (= #{"CC-BY-4.0"} (file->expressions (str test-data-path "/CC-BY-4.0/LICENSE")))) ; Failing due to https://github.com/spdx/Spdx-Java-Library/issues/233
96+
(is (valid= #{"MPL-2.0"} (file->expressions (str test-data-path "/MPL-2.0/LICENSE")))))
9797
(testing "URLs"
9898
(is (valid= #{"Apache-2.0"} (file->expressions "https://www.apache.org/licenses/LICENSE-2.0.txt")))
9999
(is (valid= #{"Apache-2.0"} (file->expressions (io/as-url "https://www.apache.org/licenses/LICENSE-2.0.txt")))))
100100
(testing "InputStreams"
101101
(is (thrown? clojure.lang.ExceptionInfo (with-open [is (io/input-stream "https://www.apache.org/licenses/LICENSE-2.0.txt")] (file->expressions is))))
102-
(is (valid= #{"Apache-2.0"} (with-open [is (io/input-stream "https://www.apache.org/licenses/LICENSE-2.0.txt")] (file->expressions is "LICENSE_2.0.txt")))))
102+
(is (valid= #{"Apache-2.0"} (with-open [is (io/input-stream "https://www.apache.org/licenses/LICENSE-2.0.txt")] (file->expressions is "LICENSE_2.0.txt")))))
103103
(testing "POM files"
104104
(is (valid= #{"Apache-2.0"} (file->expressions (str test-data-path "/simple.pom"))))
105105
(is (valid= #{"BSD-3-Clause"} (file->expressions (str test-data-path "/no-xml-ns.pom"))))
@@ -136,10 +136,10 @@
136136
(is (nil? (dir->expressions "this_directory_does_not_exist")))
137137
(is (nil? (dir->expressions "deps.edn"))))
138138
(testing "Valid directory"
139-
(is (valid= ;#{"GPL-2.0-only WITH Classpath-exception-2.0" "BSD-3-Clause" "Apache-2.0" "Unlicense AND CC0-1.0" "MIT" "MPL-2.0" "CC-BY-4.0"} ; CC-BY-4.0 failing due to https://github.com/spdx/license-list-XML/issues/1960
139+
(is (valid= ;#{"GPL-2.0-only WITH Classpath-exception-2.0" "BSD-3-Clause" "Apache-2.0" "Unlicense AND CC0-1.0" "MIT" "MPL-2.0" "CC-BY-4.0"} ; Failing due to https://github.com/spdx/Spdx-Java-Library/issues/233
140140
#{"GPL-2.0-only WITH Classpath-exception-2.0" "BSD-3-Clause" "Apache-2.0" "Unlicense AND CC0-1.0" "MIT" "MPL-2.0"}
141141
(dir->expressions "."))))
142142
(testing "Valid directory - include ZIP compressed files"
143-
(is (valid= ;#{"GPL-2.0-only WITH Classpath-exception-2.0" "BSD-3-Clause" "Apache-2.0" "Unlicense AND CC0-1.0" "MIT" "MPL-2.0" "CC-BY-4.0" "AGPL-3.0-or-later"} ; CC-BY-4.0 failing due to https://github.com/spdx/license-list-XML/issues/1960
143+
(is (valid= ;#{"GPL-2.0-only WITH Classpath-exception-2.0" "BSD-3-Clause" "Apache-2.0" "Unlicense AND CC0-1.0" "MIT" "MPL-2.0" "CC-BY-4.0" "AGPL-3.0-or-later"} ; Failing due to https://github.com/spdx/Spdx-Java-Library/issues/233
144144
#{"GPL-2.0-only WITH Classpath-exception-2.0" "BSD-3-Clause" "Apache-2.0" "Unlicense AND CC0-1.0" "MIT" "MPL-2.0" "AGPL-3.0-or-later"}
145145
(dir->expressions "." {:include-zips? true})))))

0 commit comments

Comments
 (0)