This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcli.rkt
516 lines (461 loc) · 20.1 KB
/
cli.rkt
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#lang racket/base
; Define primary entry point for the program.
(provide launch-denxi!)
(require racket/match
racket/path
racket/sequence
"cli-flag.rkt"
"cmdline.rkt"
"codec.rkt"
"crypto.rkt"
"dig.rkt"
"format.rkt"
"input.rkt"
"integrity.rkt"
"l10n.rkt"
"state.rkt"
"subprogram.rkt"
"message.rkt"
"monad.rkt"
"notary.rkt"
"package.rkt"
"pkgdef/static.rkt"
"port.rkt"
"printer.rkt"
"query.rkt"
"racket-module.rkt"
"security.rkt"
"setting.rkt"
"signature.rkt"
"source.rkt"
"string.rkt"
"transaction.rkt")
(module+ main (launch-denxi!))
(define (launch-denxi! #:arguments [args (current-command-line-arguments)]
#:format-message [format-message (get-message-formatter)]
#:handle-exit [handle-exit exit])
(run-entry-point! args
format-message
top-level-cli
handle-exit))
(define (top-level-cli args)
(cli #:program "denxi"
#:arg-help-strings '("action" "args")
#:help-suffix-string-key 'top-level-cli-help
#:args args
#:flags
(make-cli-flag-table
++envvar
++trust-executable
++trust-host-executable
++trust-cert
--workspace
--fasl-output
--memory-limit
--reader-friendly-output
--time-limit
--trust-any-exe
--trust-any-host
--verbose)
(λ (flags action . remaining-args)
(define-values (restrict? name proc)
(match action
["do" (values #t "do" do-command)]
["show" (values #t "show" show-command)]
["gc" (values #t "gc" gc-command)]
["mkint" (values #t "mkint" mkint-command)]
["fetch" (values #t "fetch" fetch-command)]
[_ (values ""
(λ _ (values null
(λ (halt)
(halt 1 ($cli:undefined-command action))))))]))
(define-values (subflags planned) (proc remaining-args))
(values (append flags subflags)
(λ (halt)
(if restrict?
(restrict halt
planned
#:memory-limit (DENXI_MEMORY_LIMIT_MB)
#:time-limit (DENXI_TIME_LIMIT_S)
#:trusted-executables (DENXI_TRUST_EXECUTABLES)
#:allowed-envvars (DENXI_ALLOW_ENV)
#:trust-unverified-host? (DENXI_TRUST_UNVERIFIED_HOST)
#:trust-any-executable? (DENXI_TRUST_ANY_EXECUTABLE)
#:trust-certificates (DENXI_TRUST_CERTIFICATES)
#:implicitly-trusted-host-executables (DENXI_TRUST_HOST_EXECUTABLES)
#:workspace (DENXI_WORKSPACE)
#:gc-period 30
#:name name)
(planned halt)))))))
(define (do-command args)
(cli #:program "do"
#:args args
#:arg-help-strings '()
#:flags
(make-cli-flag-table ++install-source
++install-abbreviated
++install-default
++install-link
++trust-public-key
++trust-chf
++input-override
--fetch-total-size
--fetch-buffer-size
--fetch-pkgdef-size
--max-redirects
--trust-any-digest
--trust-any-pubkey
--trust-bad-signature
--trust-unsigned
--assume-support)
(λ (flags)
(values flags
(λ (halt)
(define actions
(fold-transaction-actions
flags
(hasheq DENXI_INSTALL_ABBREVIATED_SOURCES
(match-lambda [source
(install #f #f source)])
DENXI_INSTALL_DEFAULT_SOURCES
(match-lambda [(list link-path source)
(install link-path #f source)])
DENXI_INSTALL_SOURCES
(match-lambda [(list link-path output-name source)
(install link-path output-name source)])
DENXI_INSTALL_ARTIFACTS
(match-lambda [(list link-path plinth)
(install-found-artifact plinth link-path)]))))
(if (null? actions)
(halt 0 null)
(let-values ([(commit rollback) (start-transaction!)])
(transact actions
(λ (messages) (commit) (halt 0 messages))
(λ (messages) (rollback) (halt 1 messages))))))))))
(define (gc-command args)
(cli #:program "gc"
#:args args
#:arg-help-strings '()
(λ (flags)
(values flags
(λ (halt)
(halt 0 ($finished-collecting-garbage (denxi-collect-garbage))))))))
(define (show-command args)
(cli #:args args
#:help-suffix-string-key 'show-command-help
#:program "show"
#:arg-help-strings '("what")
(λ (flags what)
(values flags
(λ (halt)
(match what
["installed"
(halt 0
(sequence->list
(sequence-map
(match-lambda*
[(list _ provider _ package _ edition _ revision _ output _ path)
($show-datum (list
(format-parsed-package-query
(parsed-package-query provider
package
edition
(~a revision)
(~a revision)
"ii"))
output
(~a (file-name-from-path path))))])
(in-all-installed))))]
["log"
(let loop ([next (read)])
(unless (eof-object? next)
(if ($message? next)
(write-message next)
(writeln next))
(loop (read))))
(halt 0 null)]
["links"
(halt 0
(sequence->list
(sequence-map (λ (link-path target-path)
($show-datum (list link-path target-path)))
(in-issued-links))))]
[_
(halt 1 ($cli:undefined-command what))]))))))
(define (mkint-command args)
(cli #:args args
#:program "mkint"
#:arg-help-strings '("algorithm" "encoding" "file")
(λ (flags algorithm-str encoding-str file-or-stdin)
(values flags
(λ (halt)
(with-handlers ([exn? (λ (e) (raise-user-error 'mkint (exn-message e)))])
(let ([algo (string->symbol algorithm-str)]
[encoding (string->symbol encoding-str)]
[port
(if (equal? "-" file-or-stdin)
(current-input-port)
(open-input-file file-or-stdin))])
(halt 0
($show-datum
`(integrity
',algo
(,(if (member encoding '(hex colon-separated-hex))
'hex
encoding)
,(coerce-string
(encode encoding (dynamic-wind void
(λ ()
(make-digest port algo))
(λ ()
(close-input-port port))))))))))))))))
(define-namespace-anchor cli-namespace-anchor)
(define (fetch-command args)
(cli #:args args
#:program "fetch"
#:arg-help-strings '("source-expr")
#:flags
(make-cli-flag-table --fetch-total-size
--fetch-buffer-size
--fetch-pkgdef-size
--fetch-timeout
--max-redirects)
(λ (flags source-expr-string)
(define display-name
(~s (~a #:max-width 60 #:limit-marker "..." source-expr-string)))
(define (copy-to-stdout in est-size)
(transfer in
(current-output-port)
#:on-status
(λ (status-message)
(write-message status-message
(current-message-formatter)
(current-error-port)))
#:transfer-name display-name
#:max-size (mebibytes->bytes (DENXI_FETCH_TOTAL_SIZE_MB))
#:buffer-size (mebibytes->bytes (DENXI_FETCH_BUFFER_SIZE_MB))
#:timeout-ms (DENXI_FETCH_TIMEOUT_MS)
#:est-size est-size))
(values flags
(λ (halt)
(define unnormalized-datum
(with-handlers ([exn?
(λ (e)
((error-display-handler) (exn-message e) e)
(halt 1 null))])
(string->value source-expr-string)))
(define datum
(cond [(symbol? unnormalized-datum)
(coerce-source (~a unnormalized-datum))]
[(string? unnormalized-datum)
(coerce-source unnormalized-datum)]
[else unnormalized-datum]))
(define program
(mdo source :=
(eval-untrusted-source-expression
datum
(namespace-anchor->namespace cli-namespace-anchor))
(subprogram-fetch display-name source copy-to-stdout)))
(define-values (result messages) (run-subprogram program))
(parameterize ([current-output-port (current-error-port)])
(write-message-log messages (current-message-formatter)))
(halt (if (eq? result FAILURE) 1 0) null))))))
; Functional tests follow. Use to detect changes in the interface and
; verify high-level impact.
(module+ test
(provide
(all-from-out racket
racket/runtime-path
rackunit)
(contract-out
[test-cli
(-> string?
(or/c (listof string?)
(vectorof string?))
(-> exit-code/c
program-log/c
bytes?
bytes?
any)
any)]
[check-cli
(-> (or/c (listof string?)
(vectorof string?))
(-> exit-code/c
program-log/c
bytes?
bytes?
any)
any)]
[get-checked-links
(-> (hash/c path-string? path-string?))]
[get-checked-installed-outputs
(-> list?)]
[check-garbage-collection
(-> predicate/c void?)]
[functional-test/install-all
(-> racket-module-input-variant/c
void?)]
[functional-test/install-one
(->* (racket-module-input-variant/c)
(string?)
void?)]))
(require racket
racket/runtime-path
rackunit
"file.rkt"
"racket-module.rkt"
"pkgdef/static.rkt"
(submod "state.rkt" test))
(define mkflag shortest-cli-flag)
(define (check-cli args continue)
(define messages null)
(define formatter (get-message-formatter))
(define stdout (open-output-bytes))
(define stderr (open-output-bytes))
(call-with-snake-oil-cipher-trust
(λ ()
(call-with-values
(λ ()
(parameterize ([current-output-port stdout]
[current-error-port stderr])
(launch-denxi! #:arguments (coerce-command-line-argument-list args)
#:format-message
(λ (m)
(set! messages (cons m messages))
(formatter m))
#:handle-exit
(λ (status)
(values status
(reverse messages)
(get-output-bytes stdout #t)
(get-output-bytes stderr #t))))))
continue))))
(define (split-buffer-lines buf)
(string-split (bytes->string/utf-8 buf) "\n"))
(define (test-cli msg args continue)
(test-case msg (check-cli args continue)))
(define (read-test-definition definition-datum)
(define read-program
(read-package-definition definition-datum))
(define-values (maybe-definition messages)
(run-subprogram read-program))
(when (eq? maybe-definition FAILURE)
(raise-user-error (~s messages)))
maybe-definition)
(define (make-available-link-name v path-prefix)
(let ([candidate (format "test-~a-~a" path-prefix v)])
(if (link-exists? candidate)
(make-available-link-name (add1 v) path-prefix)
candidate)))
(define (check-no-garbage-collected)
(define before-garbage-collection (get-checked-links))
(check-garbage-collection zero?)
(check-equal? before-garbage-collection (get-checked-links))
before-garbage-collection)
(define (functional-test/install-all definition-datum)
(define definition (read-test-definition definition-datum))
(define stripped (strip definition))
(define outputs (get-static-outputs (strip definition)))
(for ([output (in-list outputs)])
(define output-name (cadr output))
(test-installation definition
stripped
output-name)))
(define (functional-test/install-one definition-datum [output-name DEFAULT_STRING])
(define definition (read-test-definition definition-datum))
(test-installation definition
(strip definition)
output-name))
; Installs, then uninstalls an output in a new workspace.
(define (test-installation definition stripped output-name)
(define expected-link-name
(make-available-link-name 0 output-name))
(define expected-exact-query
(get-static-exact-package-query stripped))
(test-workspace (format "query: ~s, output: ~s, link: ~s"
(abbreviate-exact-package-query expected-exact-query)
output-name
expected-link-name)
(define-values (messages stdout stderr)
(parameterize ([current-string->source
(λ (_) (text-source (~s (syntax->datum definition))))])
(check-cli (list "do"
--DENXI_INSTALL_SOURCES
expected-link-name
output-name
"_")
(λ (exit-code messages stdout stderr)
(if (zero? exit-code)
(values messages
stdout
stderr)
(fail (string-join (map (get-message-formatter)
messages)
"\n")))))))
(define installed
(get-checked-installed-outputs))
; Search installed outputs for the one we just requested.
(check-true
(for/or ([entry (in-list installed)])
(match-define (list actual-exact-query
actual-output-name
actual-object-directory-name)
entry)
(and (equal? actual-exact-query
expected-exact-query)
(equal? actual-output-name
output-name)
(linked? expected-link-name
(build-object-path actual-object-directory-name)))))
(define currently-issued-links (check-no-garbage-collected))
(delete-file expected-link-name)
(check-equal? (get-checked-links) currently-issued-links)
(denxi-collect-garbage)
(check-true (hash-empty? (get-checked-links)))))
(define (get-checked-links)
(define (normalize path)
(if (complete-path? path)
path
(build-workspace-path path)))
(check-cli '("show" "links")
(λ (exit-code messages stdout stderr)
(check-equal? exit-code 0)
(for/hash ([entry (in-list messages)] #:when ($show-datum? entry))
(match-define ($show-datum (list link-path obj-path)) entry)
(when (link-exists? (normalize link-path))
(check-pred something-exists? (normalize obj-path)))
(values link-path obj-path)))))
(define (get-checked-installed-outputs)
(check-cli (list "show" "installed")
(λ (exit-code messages stdout stderr)
(check-equal? exit-code 0)
(for/list ([entry (in-list messages)])
(match-define ($show-datum (list exact-query output-name dirname)) entry)
(define output-directory (build-workspace-path "objects" dirname))
(define parsed (parse-package-query exact-query))
(check-pred directory-exists? output-directory)
(check-pred exact-package-query? parsed)
(list parsed output-name dirname)))))
(define (check-garbage-collection ok?)
(check-cli (list "gc")
(λ (exit-code messages stdout stderr)
(check-equal? exit-code 0)
(match messages
[(list ($finished-collecting-garbage r)) (check-pred ok? r)]
[_ (fail (format (~a "Garbage collection command "
"returned unexpected messages:~n~s")
messages))]))))
(test-cli "Fetch from user-provided sources"
'("fetch" "(byte-source #\"abcdef\")")
(λ (exit-code messages stdout stderr)
(check-equal? stdout #"abcdef")
(check-equal? exit-code 0)
(check-true (> (bytes-length stderr) 0))))
(test-case "Echo logs"
(parameterize ([current-input-port (open-input-bytes #"1 #s(($show-string $message 0) \"a\") 2")])
(check-cli (list "show" "log")
(λ (exit-code messages stdout stderr)
(check-equal? exit-code 0)
(check-equal? stderr #"")
(check-equal? stdout #"1\na\n2\n"))))))