Skip to content

Instantly share code, notes, and snippets.

@skuro
Last active June 11, 2018 19:27
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 skuro/38677b99b494ed2071f963ff2b7008d2 to your computer and use it in GitHub Desktop.
Save skuro/38677b99b494ed2071f963ff2b7008d2 to your computer and use it in GitHub Desktop.
Fugly but works
(ns fossa-parser.core
(:require [clojure.java.io :as io]
[clojure.string :as string]))
(defn maybe-license
"If a license header is found, returns the license"
[[pre lic post]]
(let [thick-line #"^=+$"]
(when (and (re-matches thick-line pre)
(re-matches thick-line post))
(string/trim lic))))
(defn update-license
"Adds lib to the libs list for license"
[licenses->libs license lib]
(update licenses->libs license (fnil conj #{}) lib))
(defn parse
"Parses fossa outputs such as https://gist.github.com/maoo/1bae6b420d79cc54df8563ef0b8c13fa"
[fossa-file]
(let [no-more? (complement seq)
lines (line-seq (io/reader fossa-file))]
(loop [current-license nil
licenses->libs {}
remaining-lines lines]
(if (no-more? remaining-lines)
;; we're done here
licenses->libs
;; time to parse lines
(if-let [license (maybe-license remaining-lines)]
;; found a license header, update current license and move ahead 3 lines
(recur license
licenses->libs
(drop 3 remaining-lines))
(let [lib-line #"^- ([^(]+) .*$"
;; re-matches gives back the full matching string then all matched groups
[_ lib] (re-matches lib-line (first remaining-lines))]
(if (some? lib)
;; found a lib, take note of the license
(recur current-license
(update-license licenses->libs current-license lib)
(rest remaining-lines))
;; else just skip the line and move ahead
(recur current-license
licenses->libs
(rest remaining-lines)))))))))
(defn libs->licenses
"An inverted index to discover libs distributed with more than one license"
[licenses->libs]
(let [inverted-index (reduce (fn [result [lic libs]]
(reduce (fn [acc lib]
(update acc lib (fnil conj []) lic))
result
libs))
{}
licenses->libs)]
(into {} (filter (fn [[_ licenses]]
(< 1 (count licenses))) inverted-index))))
fossa-parser.core> (parse "/Users/skuro/Downloads/gistfile1.txt")
{"" #{"jersey-core-client" "jersey-core-common"},
"BSD-style" #{"JDOM"},
"Eclipse Public License 1.0" #{"JUnit"},
"BSD-3-Clause" #{"MiG Base64" "SnakeYAML"},
"BSD-2-Clause" #{"Hamcrest Core"},
"Apache-2.0"
#{"Apache HttpClient" "Bean Validation API"
"Guava: Google Core Libraries for Java" "Jackson-core"
"Apache Commons Codec" "Jackson-module-JAXB-annotations"
"Apache Commons Logging" "Jackson datatype: JSR310" "Gson" "javax.ws.rs-api"
"Javassist" "Jackson-JAXRS-JSON" "Jackson-JAXRS-base" "SnakeYAML"
"swagger-annotations" "Apache Log4j" "Apache Commons Lang"
"jackson-databind" "Apache HttpCore" "rome" "Jackson-dataformat-YAML"
"rome-utils" "javax.inject" "Jackson-annotations"},
"MIT License (Expat)" #{"Hamcrest Core"},
"BSD 3-Clause License (Revised)" #{"Hamcrest Core"},
"CDDL"
#{"HK2 API module" "OSGi resource locator" "jersey-media-json-jackson"
"jersey-media-multipart" "MIME streaming extension" "javax.annotation-api"
"jersey-ext-entity-filtering"},
"GPL-2.0"
#{"HK2 API module" "javax.ws.rs-api" "OSGi resource locator"
"aopalliance-repackaged" "jersey-media-json-jackson"
"jersey-media-multipart" "MIME streaming extension"
"jersey-connectors-apache" "javax.annotation-api" "SnakeYAML"
"jersey-ext-entity-filtering" "ServiceLocator Default Implementation"
"jersey-inject-hk2" "HK2 Implementation Utilities" "javax.inject"},
"LGPL-2.1" #{"Javassist" "SnakeYAML"},
"Common Public License 1.0 (CPL-1.0)" #{"Hamcrest Core" "JUnit"},
"EPL-1.0" #{"SnakeYAML" "JUnit"},
"CDDL-1.0"
#{"javax.ws.rs-api" "aopalliance-repackaged" "jersey-connectors-apache"
"ServiceLocator Default Implementation" "jersey-inject-hk2"
"HK2 Implementation Utilities" "javax.inject"},
"Apache License 2.0 (Apache-2.0)" #{"Hamcrest Core"},
"GPL-2.0-with-classpath-exception"
#{"javax.ws.rs-api" "aopalliance-repackaged" "jersey-connectors-apache"
"ServiceLocator Default Implementation" "jersey-inject-hk2"
"HK2 Implementation Utilities" "javax.inject"},
"CDDL-1.1" #{"javax.ws.rs-api" "jersey-connectors-apache"},
"Apache" #{"JDOM"},
"Public-domain" #{"Guava: Google Core Libraries for Java"},
"MIT" #{"jsoup" "YahooFinanceAPI"},
"MPL-1.1" #{"Javassist"},
"GPL-3.0" #{"javax.ws.rs-api"},
"MPL-2.0" #{"Javassist"},
"MIT-style" #{"SLF4J API Module" "SLF4J LOG4J-12 Binding"},
"Apache 2.0" #{"swagger-models" "swagger-core"}}
fossa-parser.core>
fossa-parser.core>
fossa-parser.core> (libs->licenses (parse "/Users/skuro/Downloads/gistfile1.txt"))
{"Guava: Google Core Libraries for Java" ["Apache-2.0" "Public-domain"],
"JDOM" ["BSD-style" "Apache"],
"HK2 API module" ["CDDL" "GPL-2.0"],
"javax.ws.rs-api"
["Apache-2.0"
"GPL-2.0"
"CDDL-1.0"
"GPL-2.0-with-classpath-exception"
"CDDL-1.1"
"GPL-3.0"],
"Javassist" ["Apache-2.0" "LGPL-2.1" "MPL-1.1" "MPL-2.0"],
"OSGi resource locator" ["CDDL" "GPL-2.0"],
"aopalliance-repackaged"
["GPL-2.0" "CDDL-1.0" "GPL-2.0-with-classpath-exception"],
"jersey-media-json-jackson" ["CDDL" "GPL-2.0"],
"jersey-media-multipart" ["CDDL" "GPL-2.0"],
"MIME streaming extension" ["CDDL" "GPL-2.0"],
"jersey-connectors-apache"
["GPL-2.0" "CDDL-1.0" "GPL-2.0-with-classpath-exception" "CDDL-1.1"],
"Hamcrest Core"
["BSD-2-Clause"
"MIT License (Expat)"
"BSD 3-Clause License (Revised)"
"Common Public License 1.0 (CPL-1.0)"
"Apache License 2.0 (Apache-2.0)"],
"javax.annotation-api" ["CDDL" "GPL-2.0"],
"SnakeYAML" ["BSD-3-Clause" "Apache-2.0" "GPL-2.0" "LGPL-2.1" "EPL-1.0"],
"jersey-ext-entity-filtering" ["CDDL" "GPL-2.0"],
"ServiceLocator Default Implementation"
["GPL-2.0" "CDDL-1.0" "GPL-2.0-with-classpath-exception"],
"jersey-inject-hk2" ["GPL-2.0" "CDDL-1.0" "GPL-2.0-with-classpath-exception"],
"HK2 Implementation Utilities"
["GPL-2.0" "CDDL-1.0" "GPL-2.0-with-classpath-exception"],
"javax.inject"
["Apache-2.0" "GPL-2.0" "CDDL-1.0" "GPL-2.0-with-classpath-exception"],
"JUnit"
["Eclipse Public License 1.0" "Common Public License 1.0 (CPL-1.0)" "EPL-1.0"]}
fossa-parser.core>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment