-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.lisp
415 lines (376 loc) · 18.7 KB
/
main.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
(in-package #:org.shirakumo.redist)
(defun envvar (var)
(let ((val (uiop:getenv var)))
(when (and val (string/= "" val))
val)))
(defmacro with-envvar ((val var) &body body)
`(let ((,val (envvar ,var)))
(when ,val ,@body)))
(defun create-update-script (&key (file (merge-pathnames "redist.lisp" (storage-file))) (if-exists :supersede))
(ensure-directories-exist file)
(with-open-file (stream file :direction :output :if-exists if-exists)
(with-standard-io-syntax
(let ((*package* #.*package*)
(*print-case* :downcase)
(*print-right-margin* 80)
(*print-readably* NIL))
(format stream "~
#| Dist update script compiled automatically
exec ~a --noinform --load \"$0\" -- \"$@\"
#|
#-quicklisp (load ~s)
(ql:quickload :redist :silent T)
(setf org.shirakumo.redist:*storage-file* ~s)
(setf org.shirakumo.redist:*default-output-directory* ~s)
(setf org.shirakumo.redist:*default-source-directory* ~s)
(org.shirakumo.redist:main)"
(pathname-utils:native-namestring (truename (first (uiop:raw-command-line-arguments))))
(ql-impl-util::quicklisp-init-file-form)
(storage-file)
(default-output-directory)
(default-source-directory)))))
file)
(defun self ()
(pathname-utils:native-namestring (truename (first (uiop:raw-command-line-arguments)))))
(defun create-systemd-service (&key (file "/etc/systemd/system/redist.service") (if-exists :supersede) (binary (self)) (interval "monthly") enable)
(with-open-file (stream file :direction :output :if-exists if-exists)
(format stream "~
[Unit]
Description=Redist Distribution Compilation
After=network.target
[Service]
Type=oneshot
ExecStart=~a compile -vuj 4
WorkingDirectory=~a
User=~a
Group=~a
Environment=DIST_SOURCE_DIR=~a
Environment=DIST_OUTPUT_DIR=~a
Environment=STORAGE_FILE=~a"
binary
(pathname-utils:native-namestring (uiop:getcwd))
#-sbcl "redist" #+sbcl (sb-posix:passwd-name (sb-posix:getpwuid (sb-posix:stat-uid (sb-posix:stat binary))))
#-sbcl "redist" #+sbcl (sb-posix:group-name (sb-posix:getgrgid (sb-posix:stat-uid (sb-posix:stat binary))))
(pathname-utils:native-namestring (default-source-directory))
(pathname-utils:native-namestring (default-output-directory))
(pathname-utils:native-namestring (storage-file))))
(with-open-file (stream (make-pathname :type "timer" :defaults file) :direction :output :if-exists if-exists)
(format stream "~
[Unit]
Description=Redist Releases
[Timer]
OnCalendar=~a
Persistent=true
[Install]
WantedBy=timers.target"
interval))
(when enable
(uiop:run-program (list "systemctl" "enable" "redist.timer")))
file)
(defun main/help ()
(format T "Commands:
compile Compile a dist release
--version version Specify the version to compile
-d --dist dist Specify the dist to compile. Can be specified
multiple times. If unspecified, all dists are
compiled
-u --update If specified will update associated projects
first
-v --verbose If specified shows updates about the progress
-f --force If specified compiles the dist version even
if it exists already
-x --overwrite Overwrite the last release. Cannot be used
together with version. Implies --force
-j --jobs N Make the compilation threaded.
update Update local project checkouts
--version version Specify the version to update to. If
unspecified, updates to latest
-p --project project Specify the project to update. Can be
specified multiple times. If unspecified, all
projects are updated
-v --verbose If specified shows updates about the progress
-j --jobs Make the compilation threaded.
list List known objects
thing The thing to list. Can be projects, dists, or
releases. Defaults to releases of all dists.
-p --project project The project to list releases of
-d --dist dist The dist to list releases of
describe Describe information about an object
-p --project project The project to describe
-d --dist dist The dist to describe
--version version The version to describe
add Add a new project or add a project to a dist
url The url of the project's primary remote.
-t --type type The type of the remote source. Defaults to
GIT
-n --name name The name of the project. If unspecified is
derived from the url
-d --dist dist The dist to add the project to. Can be
specified multiple times. If unspecified,
adds to all dists
-v --verbose To print verbose output about the progress
add-dist Add a new dist
name The name of the dist to create
--url url The canonical URL at which the dist resides
--versioning scheme The scheme to use to create new dist version
identifiers. Can be one of:
integer
timestamp (default)
date
-v --verbose To print verbose output about the progress
remove Remove a project from dists
name The name of the project to remove
-d --dist dist The dist to remove the project from. Can be
specified multiple times. If unspecified,
removes from all dists
replicate Create a new mirror of another dist
url The URL of the distinfo file to replicate
-n --name name The name of the dist. If unspecified is
taken from the dist metadata.
-v --verbose To print verbose output about the progress
-l --latest-only To only download the latest version, rather
than the default of all available versions
-s --skip-archives Don't download the release archives
archive Update the release archives
-p --project project Update all archives of the project
-d --dist dist Update all archives of projects in the dist
--version version Update only the given version
-v --verbose To print verbose output about the progress
test Test a dist release
name The name of the dist to test a release of
-u --update If specified will update associated projects
first and create a new release to run the
tests on. The release won't be published.
-v --verbose If specified shows the output of the tests
-j --jobs N Make the testing threaded. This does *not*
pair well with -v.
install Install a Systemd service. Requires root.
-i --interval interval Set the timer interval. Defaults to monthly
-e --enable Enable the service
help Shows this help listing
Environment Variables:
REDIST_DEBUG When set, will enter the debugger on error
DIST_SOURCE_DIR The base directory of the project sources. If
unspecified, defaults to
DISTINFO_FILE/sources/
DIST_OUTPUT_DIR The base directory of the compiled
output. You'll want to point your webserver at
this. If unspecified, defaults to
DISTINFO_FILE/releases/
STORAGE_FILE The file that contains all dist storage info.
If unspecified tries in order:
DIST_SOURCE_DIR/../distinfo.db
DIST_SOURCE_DIR/../distinfo.sexp
DIST_OUTPUT_DIR/../distinfo.db
DIST_OUTPUT_DIR/../distinfo.sexp
~~/dist/distinfo.db
~~/dist/distinfo.sexp
distinfo.db
distinfo.sexp
Database Info:
Redist keeps a database of all the dists, projects, and releases. This
database can be stored as plaintext or as an sqlite database. This is
informed by the STORAGE_FILE. Redist loads this on startup, and will
persist any changes made during your operations to it again. If no
database is present, a basic plaintext storage is created for you.
Please see https://shirakumo.org/projects/redist for more information.
"))
(defun main/compile (&key version update dist verbose force overwrite jobs)
(when overwrite
(when version (error "Cannot specify version alongside overwrite."))
(unless force (setf force T)))
(let ((args (list* :if-exists :supersede :force force :update update :verbose verbose
(if version (list :version version)))))
(with-kernel (when jobs (parse-integer jobs))
(do-list* (dist (or (enlist dist) (list-dists)))
(if overwrite
(apply #'compile dist :version (version (first (releases dist))) args)
(apply #'compile dist args))))))
(defun main/update (&key version project verbose jobs)
(with-kernel (when jobs (parse-integer jobs))
(do-list* (project (or (enlist project) (list-projects)))
(unless (disabled-p project)
(update project :version version :verbose verbose)))))
(defun main/list (&optional (thing "releases") &key project dist)
(cond ((string-equal thing "projects")
(dolist (project (list-projects))
(format T "~&~a~%" (name project))))
((string-equal thing "dists")
(dolist (dist (list-dists))
(format T "~&~a~%" (name dist))))
((or (string-equal thing "releases")
(string-equal thing "versions"))
(dolist (release (cond (project (releases (project project)))
(dist (releases (dist dist)))
(T (loop for dist in (list-dists) append (releases dist)))))
(format T "~&~a~%" (version release))))
((string-equal thing "sources")
(labels ((rec (class)
(dolist (class (c2mop:class-direct-subclasses class))
(format T "~&~a~%" (class-name class))
(rec class))))
(rec (find-class 'source-manager))))
(T
(error "Don't know how to list ~a." thing))))
(defun main/describe (&key dist project version)
(cond (dist
(let ((dist (or (dist dist) (error "No dist named ~s" dist))))
(describe (if version
(find-release version dist)
dist))))
(project
(let ((project (or (project project) (error "No project named ~s" project))))
(describe (if version
(find-release version project)
project))))
(T
(format *error-output* "~
Sources:~12t~a
Output:~12t~a
Storage:~12t~a
Dists:"
(default-source-directory)
(default-output-directory)
(storage-file))
(loop for dist in (list-dists)
do (format *error-output* "~12t~a ~a~%" (version dist) (name dist)))
(format *error-output* "Projects:")
(loop for project in (list-projects)
do (format *error-output* "~12t~a ~a~%" (version project) (name project))))))
(defun main/add (url &key type name dist verbose)
(ensure-storage)
(let* ((name (or name (url-extract-name url)))
(type (intern (string-upcase (or type "git")) "KEYWORD"))
(project (or (project name)
(progn (when verbose (verbose "Creating new project ~a" name))
(make-instance 'project :name name :sources `((,type ,url)))))))
(dolist (dist (or (enlist dist) (list-dists)) (setf (project name) project))
(when verbose (verbose "Adding project to dist ~a" (name dist)))
(add-project project dist))))
(defun main/add-dist (name &key url verbose (versioning "timestamp"))
(ensure-storage)
(let ((name (intern (string-upcase name) #.*package*)))
(when (dist name)
(error "A dist with this name already exists."))
(when (or (null url) (string= "" url))
(error "A canonical dist URL is required."))
(when verbose (verbose "Creating new dist ~a" name))
(setf (dist name) (make-instance (cond ((string= "integer" versioning) 'integer-versioned-dist)
((string= "timestamp" versioning) 'timestamp-versioned-dist)
((string= "date" versioning) 'date-versioned-dist)
(T (error "Unknown versioning scheme ~s" versioning)))
:name name
:url url))))
(defun main/remove (name &key dist verbose)
(let ((project (or (project name)
(error "No project named ~s" name))))
(dolist (dist (or (enlist dist) (list-dists)))
(when verbose (verbose "Removing project from dist ~a" (name dist)))
(remove-project project dist))))
(defun main/replicate (url &key name verbose latest-only skip-archives)
(ensure-storage)
(replicate-dist url :name name
:verbose verbose
:current-version-only latest-only
:download-archives (not skip-archives)))
(defun main/archive (&key project dist version verbose)
(labels ((package (release)
(compile release :force T :verbose verbose))
(process (project)
(if version
(let ((release (find-release version project)))
(when release (package release)))
(mapc #'package (releases project)))))
(when project
(process (or (project project)
(error "No project named ~s" project))))
(when dist
(mapc #'process (projects (or (dist dist)
(error "No dist named ~s" dist)))))))
(defun main/install (&key enable (interval "monthly"))
(ensure-storage)
(create-systemd-service :enable enable :interval interval))
(defun main/test (dist &key update verbose jobs)
(when update
(with-kernel (when jobs (parse-integer jobs))
(dolist (project (projects dist))
(update project))))
(with-kernel (when jobs (parse-integer jobs))
(test T dist :verbose (if verbose :full T)
:on-error :continue
:use-latest-release (not update))))
(defun parse-args (args &key flags chars)
(let ((kargs ())
(pargs ()))
(loop for arg = (pop args)
while arg
do (labels ((next-arg (arg)
(if args
(pop args)
(error "Missing value for ~a" arg)))
(handle-argument (arg)
(setf (getf kargs arg) (cond ((find arg flags :test #'string-equal)
T)
((null (getf kargs arg))
(next-arg arg))
((consp (getf kargs arg))
(list* (next-arg arg) (getf kargs arg)))
(T
(list (next-arg arg) (getf kargs arg)))))))
(cond ((string= "--" arg :end2 2)
(handle-argument (find-symbol (string-upcase (subseq arg 2)) "KEYWORD")))
((string= "-" arg :end2 1)
(loop for char across (subseq arg 1)
for arg = (getf chars char)
do (cond (arg
(handle-argument arg))
(T
(error "No such argument ~a" char)))))
(T
(push arg pargs)))))
(append (nreverse pargs) kargs)))
(defun main (&optional (args (uiop:command-line-arguments)))
#+sbcl (sb-ext:disable-debugger)
(let ((args (or args '("help"))))
(setf *here* (pathname-utils:to-directory (first (uiop:raw-command-line-arguments))))
(handler-bind ((error
(lambda (e)
(cond ((uiop:getenv "REDIST_DEBUG")
#+sbcl (sb-ext:enable-debugger)
(invoke-debugger e))
(T
(format *error-output* "~&ERROR: ~a~%" e)
(lparallel:end-kernel)
(uiop:print-condition-backtrace e)
(uiop:quit 2)))))
(warning
(lambda (e)
(format *error-output* "~&WARNING: ~a~%" e)
(muffle-warning e)))
#+sbcl
(sb-sys:interactive-interrupt
(lambda (e)
(format *error-output* "~&Interactive interrupt~%")
(lparallel:end-kernel)
(uiop:quit 1))))
(destructuring-bind (command . args) args
(let ((cmdfun (find-symbol (format NIL "~a/~:@(~a~)" 'main command) #.*package*)))
(unless cmdfun
(error "No command named ~s." command))
(with-envvar (val "DIST_SOURCE_DIR")
(setf *default-source-directory* (pathname-utils:parse-native-namestring val :as :directory)))
(with-envvar (val "DIST_OUTPUT_DIR")
(setf *default-output-directory* (pathname-utils:parse-native-namestring val :as :directory)))
(with-envvar (val "STORAGE_FILE")
(setf *storage-file* (pathname-utils:parse-native-namestring val :as :file)))
(try-open-storage)
(apply #'funcall cmdfun (parse-args args :flags '(:verbose :update :force :overwrite :latest-only :skip-archives :enable :versioning)
:chars '(#\v :verbose #\u :update #\f :force
#\d :dist #\p :project #\n :version
#\n :name #\t :type #\x :overwrite
#\j :jobs #\l :latest-only
#\s :skip-archives
#\i :interval #\e :enable)))
(when *storage*
(store T T T)))))
(uiop:quit)))