-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.lisp
331 lines (275 loc) · 10.6 KB
/
utils.lisp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
;;;; utils.lisp
(in-package #:quicklisp-controller)
(defvar *random-alphanumeric*
(concatenate 'string
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789"))
(defun random-element (vector)
(aref vector (random (length vector))))
(defun random-char ()
(random-element *random-alphanumeric*))
(defun random-string (length)
(map-into (make-string length) 'random-char))
(defun native (pathname)
(native-namestring (merge-pathnames pathname)))
(defun prefix-timestamp (&optional (time (get-universal-time)))
(multiple-value-bind (second minute hour day month year)
(decode-universal-time time 0)
(declare (ignore second minute hour))
(format nil "~4,'0D~2,'0D~2,'0D" year month day)))
(defvar *in-temporary-directory*)
(setf (documentation '*in-temporary-directory* 'variable)
"Bound to the current temporary directory of
CALL-IN-TEMPORARY-DIRECTORY for the duration of the call. Used
for checking that a pathname is being generated within a
directory that will be cleaned up.")
(defun temporary-pathname (pathname)
(unless (boundp '*in-temporary-directory*)
(error "Not in a temporary directory scope"))
(values (ensure-directories-exist (merge-pathnames pathname))))
(defvar *rm-rf-debug* nil)
(defun rm-rf (path)
(unless *rm-rf-debug*
(run "rm" "-rf" (native (translate-logical-pathname path)))))
(defun call-in-temporary-directory (template-pathname fun)
(flet ((random-temporary ()
(let* ((parts (pathname-directory template-pathname))
(last (first (last parts)))
(randomized (format nil "~A-~A" last (random-string 8))))
(make-pathname :directory (nconc (butlast parts) (list randomized))
:defaults template-pathname))))
(block nil
(tagbody
retry
(let* ((path (random-temporary))
(*in-temporary-directory* path))
(handler-case
(progn
(sb-posix:mkdir (native path) #o700)
(unwind-protect
(with-posix-cwd path
(return (funcall fun)))
(ignore-errors (rm-rf path))))
(sb-posix:syscall-error (condition)
(when (= (sb-posix:syscall-errno condition)
sb-posix:eexist)
(go retry))
(error condition))))))))
(defmacro in-temporary-directory (template-pathname &body body)
"Evaluate BODY with the POSIX working directory and
*default-pathname-defaults set to a temporary directory specified by
TEMPLATE-PATHNAME. The TEMPLATE-PATHNAME will be used to form a unique
name in the filesystem."
`(call-in-temporary-directory ,template-pathname (lambda () ,@body)))
(defmacro in-anonymous-directory (&body body)
"Like IN-TEMPORARY-DIRECTORY, but does not require specifying a
template pathname."
(let ((base (gensym)))
`(let ((,base #p "quicklisp-controller:tmp;anonymous;"))
(ensure-directories-exist ,base)
(with-posix-cwd ,base
(in-temporary-directory "anonymous/"
,@body)))))
(defmacro ensure-in-anonymous-directory (&body body)
`(if (boundp '*in-temporary-directory*)
(progn ,@body)
(in-anonymous-directory ,@body)))
(defun copy (from to)
(run "cp" (native (truename from)) (native to)))
(defun curl (url output)
(run "curl" "--location" url "--output" (native output)))
(defparameter *days* #("Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun"))
(defvar *months* #("Jan" "Feb" "Mar" "Apr" "May" "Jun"
"Jul" "Aug" "Sep" "Oct" "Nov" "Dec"))
(defun http-date-string (&optional (time (get-universal-time)))
"Return a HTTP-style date string."
(multiple-value-bind (second minute hour day month year day-of-week)
(decode-universal-time time 0)
(let ((*print-pretty* nil))
(format nil "~A, ~2,'0D ~A ~4,'0D ~2,'0D:~2,'0D:~2,'0D GMT"
(aref *days* day-of-week)
day
(aref *months* (1- month))
year
hour
minute
second))))
(defun string-digest (string)
(ironclad:byte-array-to-hex-string
(ironclad:digest-sequence :md5
(babel:string-to-octets string
:encoding :utf-8))))
(defun merge-logical (pathname logical)
(merge-pathnames pathname
(translate-logical-pathname logical)))
(defun parent-directory (pathname)
(make-pathname :directory (butlast (pathname-directory pathname))
:defaults pathname))
(defun split-spaces (line)
(ppcre:split " " line))
(defmacro destructure-line (lambda-list line &body body)
`(destructuring-bind ,lambda-list
(split-spaces ,line)
,@body))
(defun call-for-each-line (fun file)
(with-open-file (stream file)
(loop for line = (read-line stream nil)
while line do (funcall fun line))))
(defmacro for-each-line ((line file) &body body)
`(call-for-each-line (lambda (,line) ,@body) ,file))
(defun file-lines (file)
(let ((result '()))
(for-each-line (line file)
(push line result))
(nreverse result)))
(defun ignorable-line-p (line)
(or (zerop (length line))
(char= (char line 0) #\#)))
(defun config-file-lines (file)
(remove-if #'ignorable-line-p (file-lines file)))
(defun save-lines (lines file)
(with-open-file (*standard-output* file
:direction :output
:if-exists :supersede)
(map nil #'write-line lines))
(probe-file file))
(defun save-forms (forms file)
(with-open-file (*standard-output* file
:direction :output
:if-exists :supersede)
(map nil 'print forms))
(probe-file file))
(defun save-form (form file)
(save-forms (list form) file))
(defun tarball-contents (file)
(run-output-lines "tar" "tzf" file))
(defun tarball-prefix (file)
"For a tarball that unpacks into a subdirectory (e.g. 'foo/foo.asd',
'foo/package.asd', etc), extract the subdirectory string. Errors if
the subdirectory is absent or inconsistent."
(let ((contents (tarball-contents file)))
(let ((prefixes (loop for path in contents
collect
(subseq path 0 (position #\/ path)))))
(unless (every 'string= prefixes (rest prefixes))
(error "Inconsistent prefixes in ~A: ~S"
file
(remove-duplicates prefixes :test 'string=)))
(format nil "~A/" (first prefixes)))))
(defun tarball-canonical-name (file)
(string-right-trim "/" (tarball-prefix file)))
(defun repack (input-file new-prefix output-file)
(in-temporary-directory "repack/"
(let ((old-prefix (tarball-prefix input-file)))
(run "tar" "xf" input-file)
(unless (equal old-prefix new-prefix)
(run "mv" old-prefix new-prefix))
(run "tar" :owner 0 :group 0 "-cf" "repack.tar" new-prefix)
(run "gzip" "-n9" "repack.tar")
(copy "repack.tar.gz" output-file)))
(probe-file output-file))
(defun package-exported-symbols (package)
(let ((symbols '()))
(do-external-symbols (symbol package (sort symbols #'string<))
(push symbol symbols))))
(defun dump-symbols (prefix)
"Display a list of all external symbols in the image prefixed by PREFIX."
(let ((packages (sort (list-all-packages) #'string< :key #'package-name))
(ignored-packages (mapcar #'find-package '(:keyword :cl))))
(dolist (package packages)
(unless (member package ignored-packages)
(dolist (symbol (package-exported-symbols package))
(format t "~A ~A ~S~%" prefix (package-name package) symbol))))))
(defun make-string-table (&key case-sensitive)
(make-hash-table :test (if case-sensitive
'equal
'equalp)))
(defun first-line-of (file)
(with-open-file (stream file)
(read-line stream)))
(defun first-form-of (file)
(with-open-file (stream file)
(read stream)))
(defun file-size (file)
(with-open-file (stream file)
(file-length stream)))
(defun file-md5 (file)
(ironclad:byte-array-to-hex-string
(ironclad:digest-file :md5 file)))
(defun dist-string (&optional (timestamp (get-universal-time)))
(multiple-value-bind (second minute hour day month year)
(decode-universal-time timestamp 0)
(declare (ignore second minute hour))
(format nil "~4,'0D-~2,'0D-~2,'0D" year month day)))
(defgeneric base-directory (object)
(:method ((object pathname))
(merge-pathnames object))
(:method ((object string))
(base-directory (pathname object))))
(defgeneric relative-to (object pathname)
(:documentation "Merge PATHNAME with the BASE-DIRECTORY of OBJECT.")
(:method (object pathname)
(merge-pathnames pathname (base-directory object))))
(defun relative-to-system (pathname)
(merge-pathnames pathname
quicklisp-controller-config:*base-directory*))
(defun skip (&optional condition)
(declare (ignore condition))
(let ((restart (find-restart 'skip)))
(when restart
(invoke-restart restart))))
(defmacro with-skipping (&body body)
`(handler-bind ((error #'skip))
,@body))
(defun empty-file-p (file)
(zerop (file-size file)))
(defun escape-html (string)
(with-output-to-string (s)
(loop for char across string
do
(case char
(#\&
(write-string "&" s))
(#\<
(write-string "<" s))
(#\>
(write-string ">" s))
(#\Nul
(write-string "[nul]" s))
(t (write-char char s))))))
(defun gist-file (&key (description "No description") pathname (public t))
(unless pathname
(error "Pathname required"))
(flet ((js (&rest args)
(apply #'githappy:js args))
(table (&rest args)
(apply #'githappy:table args)))
(let ((value (alexandria:read-file-into-string pathname))
(key (file-namestring pathname)))
(githappy:create-gist :body
(githappy:js "description" description
"public" public
"files" (table key (table "content" value)))))))
(defun gist-string (&key (description "No description") string (public t))
(unless string
(error "string required"))
(flet ((js (&rest args)
(apply #'githappy:js args))
(table (&rest args)
(apply #'githappy:table args)))
(let ((value string)
(key "text.md"))
(githappy:create-gist :body
(githappy:js "description" description
"public" public
"files" (table key (table "content" value)))))))
(defun how-long-ago (days)
(cond ((< days 1)
"less than a day")
((< days 21)
(format nil "~:D day~:P" (truncate days)))
((< days 365)
(format nil "~:D week~:P" (truncate days 7.0)))
(t
(format nil "~:D year~:P" (truncate days 365.0)))))