-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathcheck.clj
executable file
·59 lines (49 loc) · 1.77 KB
/
check.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env bb
(ns check
(:require [babashka.fs :as fs]
[clojure.data.csv :as csv]
[clojure.java.io :as io]
clojure.string
clojure.pprint))
(def multiaddr-dir (.getParent (io/file (.getParent (io/file *file*)))))
(def multicodec-csv-file
(cond
(fs/exists? (io/file multiaddr-dir "../multicodec")) (io/file multiaddr-dir "../multicodec/table.csv")
(fs/exists? (io/file multiaddr-dir "./multicodec")) (io/file multiaddr-dir "./multicodec/table.csv")
:else
(do
(println "Can't find multicodec repo")
(System/exit 1))))
(defn parseHex [h]
(-> h
(clojure.string/split #"0x")
second
(Integer/parseInt 16)))
(defn parse-csv [reader code-parser]
(let [data (csv/read-csv reader)
headers (map (comp keyword clojure.string/trim) (first data))
body (map #(map clojure.string/trim %) (rest data))]
(doall
(->>
body
(map (partial zipmap headers))
(map #(update % :code code-parser))))))
(def multicodec-contents
(with-open [reader (io/reader multicodec-csv-file)]
(parse-csv reader parseHex)))
(def multiaddr-contents
(with-open [reader (io/reader (io/file multiaddr-dir "protocols.csv"))]
(parse-csv reader #(Integer/parseInt %))))
(defn to-codec-map [table] (reduce #(assoc %1 (:code %2) %2) {} table))
(def missing-multicodecs
(let [multicodec-map (to-codec-map multicodec-contents)]
(reduce
(fn [acc multiaddr]
(if-not (contains? multicodec-map (:code multiaddr))
(conj acc multiaddr)
acc))
[] multiaddr-contents)))
(when (> (count missing-multicodecs) 0)
(println "Some protocols in the multiadddr table are not registered with multicodecs:")
(clojure.pprint/print-table missing-multicodecs)
(System/exit 1))