-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.clj
219 lines (188 loc) · 6.23 KB
/
io.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
(ns komcrad-utils.io
(:gen-class)
(:require [clojure.java.io :refer [output-stream]]
[komcrad-utils.wait :refer [wait-for]]))
(defn file [s]
(clojure.java.io/file s))
(defn existing-file
"returns a file object based on path s.
returns nil if file doesn't exist"
[s]
(let [file (file s)]
(when (.exists file) file)))
(defn tmp-file
"returns a tmp file File object"
[]
(. java.io.File createTempFile "komcrad-utils-" ".tmp"))
(defn tmp-folder
"returns a tmp folder File object"
[]
(let [file (tmp-file)]
(.delete file)
(.mkdir file)
file))
(defn resource-as-file
"returns a temp file containing resource"
[resource]
(let [res (clojure.java.io/resource resource)
tmp (tmp-file)]
(with-open [in (clojure.java.io/input-stream res)]
(clojure.java.io/copy in tmp) tmp)))
(defn tempify
"returns a file object inside a tmp folder.
retains original name of file"
[filename]
(file (str (.getCanonicalPath (tmp-folder)) "/"
(.getName (file filename)))))
(defn as-tmp [filename]
(let [in-file (file filename)
tmp (tempify filename)]
(when (.exists in-file)
(with-open [in (clojure.java.io/input-stream in-file)]
(clojure.java.io/copy in tmp)))
tmp))
(defn resource-as-folder-child
"returns a file (in a tmp folder) with the same name as resource"
[resource]
(let [res (clojure.java.io/resource resource)
tmp (tmp-folder)
new-file (file (str (.getCanonicalPath tmp) (. java.io.File separator)
(last (clojure.string/split (.getPath res) #"/"))))]
(with-open [in (clojure.java.io/input-stream res)]
(clojure.java.io/copy in new-file)) new-file))
(defn file-list
"returns a vector of files found in file (a folder)"
[file]
(vec (.listFiles (clojure.java.io/file file))))
(defn files-partial-match
"given a vector of files, returns a vector of files whos names match filename"
[files filename]
(filter #(.contains (.getName %) filename) files))
(defn wait-file
"waits for a file described by path s. returns file or nil"
([s timeout]
(wait-for #(existing-file s) timeout 50))
([s]
(wait-file s 5000)))
(defn delete-file
"deletes file recursively"
[file]
(loop [files (vec [(clojure.java.io/file file)])]
(when (not (empty? files))
(let [f (first files)]
(if (and (.isDirectory f) (> (count (.listFiles f)) 0))
(recur (concat (vec (.listFiles f)) files))
(do (.delete f)
(recur (rest files))))))))
(defn parent
"return parent of file"
[file]
(.getParent file))
(defn delete-file-parent
"calls delete-file on parent of file"
[file]
(delete-file (parent file)))
(defn smart-delete
"calls delete-file on parent of file if it matches ^komcrad-utils"
[file]
(if (re-find #"^komcrad-utils" (.getName (.getParentFile file)))
(delete-file-parent file)
(delete-file file)))
(defn file-move
[input-file output-file]
(clojure.java.io/copy input-file output-file)
(if (.exists output-file)
(do (delete-file (.getPath input-file)) true)))
(defn file-move-tmp [input-file]
(let [tmp-dir (tmp-folder)
tmp-file (file (str (.getPath tmp-dir) (. java.io.File separator)
(.getName input-file)))]
(file-move input-file tmp-file) tmp-file))
(defmacro with-tmp-file
"executes body and insures the file is deleted"
[file & body]
`(let [file# ~file]
(try
~@body
(finally (delete-file file#)))))
(defmacro with-tmp-files
"executes body and insures the files are deleted"
[files & body]
`(let [files# ~files]
(try
~@body
(finally (doseq [x# files#] (delete-file x#))))))
(defmacro with-tmps
"Executes body and insures the files are deleted.
Syntax similar to let:
(with-tmps [file1 (tmp-file) file2 (resource-as-file \"resource\")]
(println file1 file2))"
[bindings & body]
`((fn [~@(take-nth 2 bindings)]
(try
~@body
(finally
(doseq [x# [~@(take-nth 2 bindings)]]
(komcrad-utils.io/delete-file x#)))))
~@(take-nth 2 (rest bindings))))
(defmacro with-tmp-folder-children
"executes body and insures the files are deleted
along with their parents"
[files & body]
`(let [files# ~files]
(try
~@body
(finally (doseq [x# files#] (delete-file (parent x#)))))))
(defmacro with-tf
"binds the given symbol to a tmpfile, executes body with access
to the tmpfile and insures the file gets deleted
Example: (with-tf [my-file]
(spit my-file \"hello\")
(slurp my-file))"
[[file] & body]
`(let [~file (komcrad-utils.io/tmp-file)]
(try
~@body
(finally (delete-file ~file)))))
(defn host-port-listening?
"returns true if a host at ip is listing on port n"
[ip n]
(try
(let [s (new java.net.Socket)]
(.connect s (new java.net.InetSocketAddress ip n) 5000)
(.close s) true)
(catch Exception e false)))
(defn available-port? [n]
(try
(let [socket (java.net.ServerSocket. n)]
(.close socket) true)
(catch Exception e false)))
(defn available-port []
(let [socket (java.net.ServerSocket. 0)]
(.close socket)
(.getLocalPort socket)))
(defn local-ip []
(.getHostAddress (java.net.InetAddress/getLocalHost)))
(defn get-local-interfaces []
(let [interfaces (. java.net.NetworkInterface getNetworkInterfaces)]
(if (.hasMoreElements interfaces)
(loop [interface (.nextElement interfaces) result []]
(if (.hasMoreElements interfaces)
(recur (.nextElement interfaces) (conj result interface))
(conj result interface))))))
(defn filter-interfaces [s]
(vec (filter (fn [x] (.contains (.getName x) s)) (get-local-interfaces))))
(defn ipv4-from-interface
[^java.net.NetworkInterface interface]
(.getHostAddress (.getAddress (first (filter #(= 32 (.getNetworkPrefixLength %))
(.getInterfaceAddresses interface))))))
(defn touch-tmp
"creates a file named name in a tmp dir"
[name]
(let [parent (tmp-folder)]
(file (str (.getCanonicalPath parent) (. java.io.File separator) name))))
(defn zero-fill
"adds n bytes to file"
[file n]
(with-open [out (output-stream file)]
(.write out (byte-array n))) file)