/
stage.rkt
968 lines (924 loc) · 40.5 KB
/
stage.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
#lang racket/base
(require racket/format
racket/match
racket/list
racket/path
racket/file
racket/port
racket/string
setup/unpack
setup/dirs
net/url
file/untgz
file/unzip
file/private/check-path
openssl/sha1
json
net/git-checkout
"../name.rkt"
"../strip.rkt"
"catalog.rkt"
"download.rkt"
"print.rkt"
"path.rkt"
"dirs.rkt"
"desc.rkt"
"params.rkt"
"get-info.rkt"
"mod-paths.rkt"
"addl-installs.rkt"
"repo-path.rkt"
"orig-pkg.rkt"
"git.rkt"
"prefetch.rkt"
"checkout-credentials.rkt"
"network.rkt"
"github-url.rkt")
(provide (struct-out install-info)
remote-package-checksum
stage-package/info
pkg-stage
package-url->checksum
github-client_secret
github-client_id)
(struct install-info (name orig-pkg directory git-directory clean? checksum module-paths additional-installs))
(define (communication-type type)
(if (and (eq? type 'github)
use-git-for-github?)
'git
(if (eq? type 'git-url)
'git
type)))
(define (remote-package-checksum pkg download-printf pkg-name
#:type [type #f]
#:prefetch? [prefetch? #f]
#:prefetch-group [prefetch-group #f]
#:catalog-lookup-cache [catalog-lookup-cache #f]
#:remote-checksum-cache [remote-checksum-cache #f])
(define (lookup-normally download-printf)
(define checksum
(match pkg
[`(catalog ,pkg-name . ,_)
(define info (package-catalog-lookup pkg-name #f catalog-lookup-cache
download-printf
#:prefetch-group prefetch-group))
(hash-ref info 'checksum)]
[`(url ,pkg-url-str)
(package-url->checksum pkg-url-str
#:type type
#:download-printf download-printf
#:pkg-name pkg-name)]
[`(git ,pkg-url-str)
(package-url->checksum pkg-url-str
#:type (or type 'git-url)
#:download-printf download-printf
#:pkg-name pkg-name)]
[`(clone ,_ ,pkg-url-str)
(package-url->checksum pkg-url-str
#:type 'clone
#:download-printf download-printf
#:pkg-name pkg-name)]))
(when remote-checksum-cache
(hash-set! remote-checksum-cache pkg checksum))
checksum)
(when (and prefetch? (not (and catalog-lookup-cache
remote-checksum-cache
prefetch-group)))
(error "internal error: insufficient caches or group for prefetch of package checksum"))
;; Loop to combine cache lookup and prefetch dispatch:
(let loop ([prefetch? prefetch?] [download-printf download-printf])
(cond
[(and remote-checksum-cache
(hash-ref remote-checksum-cache pkg #f))
=> (lambda (checksum)
(if (and (prefetch-future? checksum)
(not prefetch?))
(prefetch-touch checksum prefetch-group download-printf)
checksum))]
[prefetch?
(make-prefetch-future/hash remote-checksum-cache
pkg
lookup-normally
prefetch-group
download-printf)]
[else
(lookup-normally download-printf)])))
;; Downloads a package (if needed) and unpacks it (if needed) into a
;; temporary directory.
(define (stage-package/info pkg
given-type
given-pkg-name
#:at-dir given-at-dir
#:given-checksum [given-checksum #f]
#:cached-url [cached-url #f]
#:use-cache? use-cache?
check-sums?
download-printf
metadata-ns
#:catalog-lookup-cache [catalog-lookup-cache #f]
#:remote-checksum-cache [remote-checksum-cache #f]
#:strip [strip-mode #f]
#:force-strip? [force-strip? #f]
#:in-place? [in-place? #f]
#:in-place-clean? [in-place-clean? #f]
#:link-dirs? [link-dirs? #f])
(define-values (inferred-pkg-name type)
(if (path? pkg)
(package-source->name+type (path->string pkg)
(or given-type
(if (directory-exists? pkg)
(if link-dirs?
'link
'dir)
'file))
#:must-infer-name? (not given-pkg-name)
#:complain (complain-about-source given-pkg-name))
(package-source->name+type pkg given-type
#:link-dirs? link-dirs?
#:must-infer-name? (not given-pkg-name)
#:complain (complain-about-source given-pkg-name))))
(define pkg-name (or given-pkg-name inferred-pkg-name))
(when (and type (not pkg-name))
(pkg-error (~a "could not infer package name from source\n"
" source: ~a")
pkg))
(cond
[(and (eq? type 'github)
(not (regexp-match? #rx"^git(?:hub)?://" pkg)))
;; Add "git://github.com/"
(stage-package/info (string-append "git://github.com/" pkg) type
pkg-name
#:at-dir given-at-dir
#:given-checksum given-checksum
#:use-cache? use-cache?
check-sums? download-printf
metadata-ns
#:strip strip-mode
#:force-strip? force-strip?)]
[(eq? type 'clone)
(define pkg-url (string->url pkg))
(define-values (transport host port repo branch path)
(split-git-or-hub-url pkg-url))
(define pkg-no-query (real-git-url pkg-url host port repo))
(define clone-dir (or given-at-dir
(current-directory)))
(define (status s) (download-printf "~a\n" s))
(define orig-pkg (desc->orig-pkg 'clone pkg given-at-dir))
(define checksum
(or given-checksum
(remote-package-checksum orig-pkg download-printf pkg-name
#:catalog-lookup-cache catalog-lookup-cache
#:remote-checksum-cache remote-checksum-cache)))
;; If the clone directory already exists, and if it already has
;; the target commit, then we use that directory. It may have
;; changes to package metadata (even uncommitted changes) that
;; we'd prefer to use
(define working-dir
(and (directory-exists? clone-dir)
(or (not checksum)
(let ([o (open-output-bytes)])
(and (parameterize ([current-directory clone-dir]
[current-error-port (open-output-bytes)]
[current-output-port o])
(git "log" "--pretty=%H" #:fail-mode 'status))
(regexp-match (~a "(?m:^" (regexp-quote checksum) ")")
(get-output-bytes o)))))
(if (null? path)
clone-dir
(apply build-path clone-dir path))))
(define tmp-dir (and (not working-dir)
(make-temporary-file
(string-append "~a-" pkg-name)
'directory)))
(define staged? #f)
(dynamic-wind
void
(λ ()
(unless (and (directory-exists? clone-dir)
(directory-exists? (build-path clone-dir ".git")))
(download-printf "Cloning remote repository ~a\n to ~a\n"
pkg-no-query
clone-dir)
(make-directory* clone-dir)
(parameterize ([current-directory clone-dir])
(apply git #:status status "clone"
(append
(if (eq? branch 'head) null (list "-b" branch))
(list pkg-no-query ".")))))
(unless working-dir
(parameterize ([current-directory clone-dir])
(download-printf "Fetching from remote repository ~a\n"
pkg-no-query)
(apply git #:status status "fetch"
(append
(list pkg-no-query)
(if (eq? branch 'head) null (list branch))))))
(cond
[tmp-dir
;; Make a clone of the [to-be-]linked checkout so that
;; we can check dependencies, etc., before changing
;; the checkout. By using `--shared` for the clone, all
;; the commits that we've fetched will be available to
;; checkout (even though they may be unreachable at this
;; point, since we haven't merged the fetched commits).
(download-printf "Cloning repository locally for staging\n")
(git #:status status "clone" "--shared" clone-dir tmp-dir)
(parameterize ([current-directory tmp-dir])
(apply git #:status status "checkout"
(cond
[checksum => list]
[(eq? branch 'head) null]
[else (list branch)])))
(lift-git-directory-content tmp-dir path)]
[else
(download-printf "Using clone directory directly for metadata\n")])
(begin0
(update-install-info-checksum
(update-install-info-orig-pkg
(update-install-info-git-dir
(stage-package/info (or working-dir tmp-dir)
'dir
pkg-name
#:at-dir given-at-dir
#:given-checksum checksum
#:cached-url pkg-url
#:use-cache? use-cache?
check-sums?
download-printf
metadata-ns
#:strip strip-mode
#:force-strip? force-strip?
#:in-place? #t
#:in-place-clean? (not working-dir))
(apply build-path clone-dir path))
orig-pkg)
checksum)
(set! staged? #t)))
(λ ()
(unless staged?
(when tmp-dir
(delete-directory/files tmp-dir)))))]
[(or (eq? type 'file-url)
(eq? type 'dir-url)
(eq? type 'github)
(eq? type 'git)
(eq? type 'git-url))
(define pkg-url-str (normalize-url type pkg (string->url pkg)))
(define pkg-url (string->url pkg-url-str))
(define scheme (url-scheme pkg-url))
(define orig-pkg (desc->orig-pkg type pkg-url-str #f))
(define found-checksum
;; If a checksum is given, use that. In the case of a non-github
;; source, we could try to get the checksum from the source, and
;; then check whether it matches the expected one, but we choose
;; to avoid an extra trip to the server.
(or given-checksum
(remote-package-checksum orig-pkg download-printf pkg-name
#:catalog-lookup-cache catalog-lookup-cache
#:remote-checksum-cache remote-checksum-cache)))
(when check-sums?
(check-checksum given-checksum found-checksum "unexpected" pkg #f))
(define checksum (or found-checksum given-checksum))
(define downloaded-info
(match (communication-type type)
['git
(when (equal? checksum "")
(pkg-error
(~a "cannot use empty checksum for Git repository package source\n"
" source: ~a")
pkg))
(define-values (transport host port repo branch path)
(split-git-or-hub-url pkg-url #:type type))
(define tmp-dir
(make-temporary-file
(string-append
"~a-"
(regexp-replace* #rx"[:/\\.~]" (format "~a.~a" repo branch) "_"))
'directory))
(define staged? #f)
(dynamic-wind
void
(λ ()
(download-repo! pkg-url transport host port repo tmp-dir checksum
#:use-cache? use-cache?
#:download-printf download-printf)
(lift-git-directory-content tmp-dir path)
(begin0
(stage-package/info tmp-dir
'dir
pkg-name
#:at-dir given-at-dir
#:given-checksum checksum
#:cached-url pkg-url
#:use-cache? use-cache?
check-sums?
download-printf
metadata-ns
#:strip strip-mode
#:force-strip? force-strip?
#:in-place? #t
#:in-place-clean? #t)
(set! staged? #t)))
(λ ()
(when (and use-cache? (not staged?))
(clean-cache pkg-url checksum))
(unless staged?
(delete-directory/files tmp-dir))))]
['github
(unless checksum
(pkg-error
(~a "could not find checksum for GitHub package source, which implies it doesn't exist\n"
" source: ~a")
pkg))
(when (equal? checksum "")
(pkg-error
(~a "cannot use empty checksum for GitHub package source\n"
" source: ~a")
pkg))
(match-define (list* user repo branch path)
(split-github-url pkg-url))
(define new-url
(url "https" #f "github.com" #f #t
(map (λ (x) (path/param x empty))
(list user repo "legacy.tar.gz" checksum))
empty
#f))
(define tmp.tgz
(make-temporary-file
(string-append
"~a-"
(format "~a.~a.tgz" repo branch))
#f))
(define tmp-dir
(make-temporary-file
(string-append
"~a-"
(format "~a.~a" repo branch))
'directory))
(dynamic-wind
void
(λ ()
(download-file! new-url tmp.tgz checksum
#:use-cache? use-cache?
#:download-printf download-printf)
(define staged? #f)
(dynamic-wind
void
(λ ()
(untar tmp.tgz tmp-dir #:strip-components 1)
(unless (null? path)
(unless (directory-exists? (apply build-path tmp-dir path))
(pkg-error
(~a "specified directory is not in GitHub respository archive\n"
" path: ~a")
(apply build-path path)))
(lift-directory-content tmp-dir path))
(begin0
(stage-package/info tmp-dir
'dir
pkg-name
#:at-dir given-at-dir
#:given-checksum checksum
#:cached-url new-url
#:use-cache? use-cache?
check-sums?
download-printf
metadata-ns
#:strip strip-mode
#:force-strip? force-strip?
#:in-place? #t
#:in-place-clean? #t)
(set! staged? #t)))
(λ ()
(when (and use-cache? (not staged?))
(clean-cache new-url checksum))
(unless staged?
(delete-directory/files tmp-dir)))))
(λ ()
(delete-directory/files tmp.tgz)))]
[_
(define url-last-component
(path/param-path (last (url-path pkg-url))))
(define url-looks-like-directory? (eq? type 'dir-url))
(define-values
(package-path download-type download-package!)
(cond
[url-looks-like-directory?
(define package-path
(make-temporary-file
(string-append
"~a-"
pkg-name)
'directory))
(define (path-like f)
(check-unpack-path 'MANIFEST f)
(build-path package-path f))
(define (url-like f)
(if (and (pair? (url-path pkg-url))
(equal? "" (path/param-path (last (url-path pkg-url)))))
;; normal relative path:
(combine-url/relative pkg-url f)
;; we're assuming that the last path element is
;; a directory, so just add f:
(struct-copy url pkg-url [path
(append
(url-path pkg-url)
(list (path/param f null)))])))
(values package-path
'dir
(λ ()
(download-printf "Cloning remote directory ~a\n"
(url->string pkg-url))
(make-directory* package-path)
(define manifest
(call/input-url+200
#:who 'download-manifest
(url-like "MANIFEST")
port->lines))
(unless manifest
(define suggest-git (suggested-git-path pkg-url given-type))
(pkg-error (~a "could not find MANIFEST for package source\n"
" source: ~a~a")
pkg
(if suggest-git
(~a "\n"
" possible solution:\n"
" If the URL is intended to refer to a Git repository, use\n"
" " suggest-git "\n"
" so that the URL ends in \".git\"")
"")))
(for ([f (in-list manifest)])
(download-file! (url-like f)
(path-like f)
#f
#:use-cache? use-cache?))))]
[else
(define package-path
(make-temporary-file
(string-append
"~a-"
url-last-component)
#f))
(delete-file package-path)
(values package-path
'file
(λ ()
(log-pkg-debug "\tAssuming URL names a file")
(download-file! pkg-url package-path checksum
#:use-cache? use-cache?
#:download-printf download-printf)))]))
(define staged? #f)
(dynamic-wind
void
(λ ()
(download-package!)
(log-pkg-debug "\tDownloading done, installing ~a as ~a"
package-path pkg-name)
(begin0
(stage-package/info package-path
download-type
pkg-name
#:at-dir given-at-dir
#:given-checksum checksum
#:cached-url pkg-url
#:use-cache? use-cache?
check-sums?
download-printf
metadata-ns
#:strip strip-mode
#:force-strip? force-strip?)
(set! staged? #t)))
(λ ()
(when (or (file-exists? package-path)
(directory-exists? package-path))
(when (and use-cache? (not staged?))
(clean-cache pkg-url checksum))
(delete-directory/files package-path))))]))
(define info (update-install-info-orig-pkg downloaded-info
orig-pkg))
(when (and check-sums?
(install-info-checksum info)
(not checksum))
(pkg-error (~a "remote package had no checksum\n"
" package: ~a")
pkg))
(when check-sums?
(check-checksum checksum (install-info-checksum info)
"mismatched"
pkg
(and use-cache? cached-url)))
(update-install-info-checksum
info
checksum)]
[(eq? type 'file)
(define pkg-path (if (path? pkg)
pkg
(package-source->path pkg type)))
(unless (file-exists? pkg-path)
(pkg-error "no such file\n path: ~a" pkg-path))
(define checksum-pth (format "~a.CHECKSUM" pkg-path))
(define expected-checksum
(and (file-exists? checksum-pth)
check-sums?
(file->string checksum-pth)))
(check-checksum given-checksum expected-checksum "unexpected" pkg-path #f)
(define actual-checksum
(with-input-from-file pkg-path
(λ ()
(sha1 (current-input-port)))))
(check-checksum expected-checksum actual-checksum "mismatched" pkg-path
(and use-cache? cached-url))
(define checksum
actual-checksum)
(define pkg-format (filename-extension pkg-path))
(define pkg-dir
(make-temporary-file (string-append "~a-" pkg-name)
'directory))
(define staged? #f)
(dynamic-wind
void
(λ ()
(make-directory* pkg-dir)
(match pkg-format
[#"tgz"
(untar pkg-path pkg-dir)
(remove-extra-directory-layer pkg-dir)]
[#"tar"
(untar pkg-path pkg-dir)
(remove-extra-directory-layer pkg-dir)]
[#"gz" ; assuming .tar.gz
(untar pkg-path pkg-dir)
(remove-extra-directory-layer pkg-dir)]
[#"zip"
(unzip pkg-path (make-filesystem-entry-reader #:dest pkg-dir)
#:preserve-attributes? #t
#:utc-timestamps? #t)
(remove-extra-directory-layer pkg-dir)]
[#"plt"
(make-directory* pkg-dir)
(unpack pkg-path pkg-dir
(lambda (x) (log-pkg-debug "~a" x))
(lambda () pkg-dir)
#f
(lambda (auto-dir main-dir file) pkg-dir))
(define info-path (build-path pkg-dir "info.rkt"))
(unless (file-exists? info-path)
;; Add in "info.rkt" file to make it multi-collection,
;; since a ".plt" archive is never single-collection. This
;; is needed for supporting old ".plt" archives as packages.
(call-with-output-file info-path
(lambda (o)
(fprintf o "#lang setup/infotab\n")
(write '(define collection 'multi) o)
(newline o))))]
[x
(pkg-error "invalid package format\n given: ~a" x)])
(begin0
(update-install-info-checksum
(update-install-info-orig-pkg
(stage-package/info pkg-dir
'dir
pkg-name
#:at-dir given-at-dir
#:given-checksum checksum
#:cached-url cached-url
#:use-cache? use-cache?
check-sums?
download-printf
metadata-ns
#:strip strip-mode
#:force-strip? force-strip?
#:in-place? (not strip-mode)
#:in-place-clean? #t)
`(file ,(simple-form-path* pkg-path)))
checksum)
(unless strip-mode
(set! staged? #t))))
(λ ()
(unless staged?
(delete-directory/files pkg-dir))))]
[(or (eq? type 'dir)
(eq? type 'link)
(eq? type 'static-link))
(define pkg-path (if (path? pkg)
pkg
(package-source->path pkg type)))
(unless (directory-exists? pkg-path)
(pkg-error "no such directory\n path: ~a" pkg-path))
(cond
[(or (eq? type 'link)
(eq? type 'static-link))
(install-info pkg-name
(desc->orig-pkg type pkg-path #f)
pkg-path
#f ; no git-dir
#f ; no clean?
given-checksum ; if a checksum is provided, just use it
(directory->module-paths pkg-path pkg-name metadata-ns)
(directory->additional-installs pkg-path pkg-name metadata-ns))]
[else
(define pkg-dir
(if in-place?
(if strip-mode
(cond
[in-place-clean?
(unless force-strip?
(check-strip-compatible strip-mode pkg-name pkg-path pkg-error))
(generate-stripped-directory strip-mode pkg-path pkg-path)
pkg-path]
[else
(pkg-error "cannot strip directory in place\n path: ~a" pkg-path)])
pkg-path)
(let ([pkg-dir (make-temporary-file "pkg~a" 'directory)])
(if strip-mode
(begin
(unless force-strip?
(check-strip-compatible strip-mode pkg-name pkg-path pkg-error))
(make-directory* pkg-dir)
(generate-stripped-directory strip-mode pkg-path pkg-dir))
(begin
(make-parent-directory* pkg-dir)
(delete-directory pkg-dir)
(copy-directory/files pkg-path pkg-dir #:keep-modify-seconds? #t)))
pkg-dir)))
(when (or (not in-place?)
in-place-clean?)
(drop-redundant-files pkg-dir))
(install-info pkg-name
`(dir ,(simple-form-path* pkg-path))
pkg-dir
#f ; no git-dir
(or (not in-place?) in-place-clean?)
given-checksum ; if a checksum is provided, just use it
(directory->module-paths pkg-dir pkg-name metadata-ns)
(directory->additional-installs pkg-dir pkg-name metadata-ns))])]
[(eq? type 'name)
(define catalog-info (package-catalog-lookup pkg #f catalog-lookup-cache
download-printf))
(log-pkg-debug "catalog response: ~s" catalog-info)
(define source (hash-ref catalog-info 'source))
(define checksum (hash-ref catalog-info 'checksum))
(define info (stage-package/info source
#f
pkg-name
#:at-dir given-at-dir
#:given-checksum checksum
#:use-cache? use-cache?
check-sums?
download-printf
metadata-ns
#:catalog-lookup-cache catalog-lookup-cache
#:remote-checksum-cache remote-checksum-cache
#:strip strip-mode
#:force-strip? force-strip?))
(when check-sums?
(check-checksum given-checksum checksum "unexpected" pkg #f)
(check-checksum checksum (install-info-checksum info) "incorrect" pkg #f))
(define-values (new-name new-type) (package-source->name+type source #f))
(define repo-url (and (or (eq? new-type 'git)
(eq? new-type 'git-url)
(eq? new-type 'github))
source))
(case new-type
[(link static-link clone)
;; The `source` must have been something like a `file://`
;; URL that embeds a special installation type. In that case,
;; we don't try to keep track of the catalog reference.
info]
[else
(update-install-info-orig-pkg
(update-install-info-checksum
info
checksum)
(desc->orig-pkg 'name pkg #f #:repo-url repo-url))])]
[else
(pkg-error "cannot infer package source type\n source: ~a" pkg)]))
(define (pkg-stage desc
#:namespace [metadata-ns (make-metadata-namespace)]
#:in-place? [in-place? #f]
#:strip [strip-mode #f]
#:force-strip? [force-strip? #f]
#:use-cache? [use-cache? #f]
#:quiet? [quiet? #t])
(define i (stage-package/info (pkg-desc-source desc)
(pkg-desc-type desc)
(pkg-desc-name desc)
#:at-dir (pkg-desc-extra-path desc)
#:given-checksum (pkg-desc-checksum desc)
#:use-cache? use-cache?
#t
(if quiet? void printf/flush)
metadata-ns
#:in-place? in-place?
#:strip strip-mode
#:force-strip? force-strip?))
(values (install-info-name i)
(install-info-directory i)
(install-info-checksum i)
(install-info-clean? i)
(install-info-module-paths i)))
;; ----------------------------------------
(define (package-url->checksum pkg-url-str [query empty]
#:type [given-type #f]
#:download-printf [download-printf void]
#:pkg-name [pkg-name "package"])
(define pkg-url
(string->url pkg-url-str))
(define type (if (eq? given-type 'clone)
(if (github-url? (string->url pkg-url-str))
'github
'git)
(or given-type
(let-values ([(name type) (package-source->name+type pkg-url-str given-type)])
type))))
(case (communication-type type)
[(git)
(define-values (transport host port repo branch path)
(split-git-or-hub-url pkg-url #:type type))
(download-printf "Querying Git references for ~a at ~a\n" pkg-name pkg-url-str)
(call-with-git-checkout-credentials
(lambda ()
(call-with-network-retries
(lambda ()
;; Supplying `#:dest-dir #f` means that we just resolve `branch`
;; to an ID:
(git-checkout host #:port port repo
#:dest-dir #f
#:ref branch
#:status-printf
(lambda (fmt . args)
(define (strip-ending-newline s)
(regexp-replace #rx"\n$" s ""))
(log-pkg-debug
(strip-ending-newline (apply format fmt args))))
#:initial-error
(lambda ()
(raise
;; This is a git error so that
;; call-with-git-checkout-credentials will retry
(exn:fail:git
(~a "pkg: Git checkout initial protocol failed;\n"
" the given URL might not refer to a Git repository\n"
" given URL: "
pkg-url-str)
(current-continuation-marks))))
#:transport transport)))))]
[(github)
(match-define (list* user repo url-branch path)
(split-github-url pkg-url))
(define (query-json path kind)
(define api-u
(url "https" #f "api.github.com" #f #t
(map (λ (x) (path/param x empty))
path)
(append query
(if (and (github-client_id)
(github-client_secret))
(list (cons 'client_id (github-client_id))
(cons 'client_secret (github-client_secret)))
empty))
#f))
(download-printf "Querying GitHub ~a for ~a\n" kind pkg-name)
(log-pkg-debug "Querying GitHub at ~a" (url->string api-u))
(define api-bs
(call/input-url+200
api-u port->bytes
#:who 'query-github
#:headers (list (format "User-Agent: raco-pkg/~a" (version)))))
(unless api-bs
(error 'package-url->checksum
"could not connect to GitHub\n URL: ~a"
(url->string
(struct-copy url api-u
[query query]))))
(read-json (open-input-bytes api-bs)))
(define branch (cond
[(eq? url-branch 'head)
(define info (query-json (list "repos" user repo) 'head))
(hash-ref info 'default_branch)]
[else url-branch]))
(or
(for/or ([kind '("branches" "tags")])
(define branches (query-json (list "repos" user repo kind) kind))
(unless (and (list? branches)
(andmap hash? branches)
(andmap (λ (b) (hash-has-key? b 'name)) branches)
(andmap (λ (b) (hash-has-key? b 'commit)) branches))
(error 'package-url->checksum
"Invalid response from Github: ~e"
branches))
(for/or ([b (in-list branches)])
(and (equal? (hash-ref b 'name) branch)
(hash-ref (hash-ref b 'commit) 'sha))))
;; no matching branch/tag found, so if `branch' matches the
;; syntax of a commit id, then assume that it refers to a commit
(and (regexp-match? #rx"[a-f0-9]+" branch)
branch))]
[else
(define u (string-append pkg-url-str ".CHECKSUM"))
(download-printf "Downloading checksum for ~a\n" pkg-name)
(log-pkg-debug "Downloading checksum as ~a" u)
(call/input-url+200 (string->url u)
port->string
#:who 'download-checksum)]))
(define (check-checksum given-checksum checksum what pkg-src cached-url)
(when (and given-checksum
checksum
(not (equal? given-checksum checksum)))
(clean-cache cached-url checksum)
(pkg-error (~a "~a checksum on package\n"
" package source: ~a\n"
" expected: ~e\n"
" got: ~e")
what
pkg-src
given-checksum
checksum)))
;; ----------------------------------------
;; Disambiguate `str` as needed to ensure that it will be parsed as
;; `type` in the future.
(define (normalize-url type str as-url)
(case type
[(git) ; not git-url, which should not be normalized by adding ".git"
(cond
[(equal? "git" (url-scheme as-url))
str]
[(equal? "github" (url-scheme as-url))
str]
[else
(define p (reverse (url-path as-url)))
(define skip (if (equal? "" (path/param-path (car p)))
cdr
values))
(define e (path/param-path (car (skip p))))
(cond
[(not (regexp-match? #rx"[.]git$" e))
(url->string (struct-copy url as-url
[path
(reverse
(cons (path/param (string-append e ".git")
(path/param-param (car (skip p))))
(cdr (skip p))))]))]
[else str])])]
[else str]))
;; ----------------------------------------
(define (update-install-info-orig-pkg if op)
(struct-copy install-info if
[orig-pkg op]))
(define (update-install-info-checksum if op)
(struct-copy install-info if
[checksum op]))
(define (update-install-info-git-dir if dir)
(struct-copy install-info if
[git-directory dir]))
;; ----------------------------------------
(define github-client_id (make-parameter #f))
(define github-client_secret (make-parameter #f))
(define (lift-git-directory-content tmp-dir path)
(unless (null? path)
(unless (directory-exists? (apply build-path tmp-dir path))
(pkg-error
(~a "specified directory is not in Git respository\n"
" path: ~a")
(apply build-path path)))
(lift-directory-content tmp-dir path)))
;; ----------------------------------------
(define (untar pkg pkg-dir #:strip-components [strip-components 0])
(make-directory* pkg-dir)
(untgz pkg #:dest pkg-dir #:strip-count strip-components))
;; ----------------------------------------
(define (drop-redundant-files pkg-dir)
;; Ad hoc space-saving rule: for an installation-wide package, remove
;; any redundant license files.
(when (and (eq? 'installation (current-pkg-scope))
(find-share-dir))
(for ([i (in-list '("COPYING.txt" "COPYING_LESSER.txt" "COPYRIGHT.txt"
"LICENSE-APACHE" "LICENSE-MIT"))])
(define pkg-file (build-path pkg-dir i))
(define share-file (build-path (find-share-dir) i))
(when (and (file-exists? pkg-file)
(file-exists? share-file)
(equal? (file->bytes pkg-file)
(file->bytes share-file)))
;; This file would be redundant, so drop it
(delete-file pkg-file)))))
;; ----------------------------------------
(define (suggested-git-path pkg-url given-type)
(define p (url-path pkg-url))
(define drop-n
(if (and ((length p) . >= . 1)
(equal? "" (path/param-path (last p))))
2
1))
(and (not given-type)
((length p) . >= . drop-n)
(let ([e (list-ref p (- (length p) drop-n))])
(url->string
(struct-copy url pkg-url [path (append
(drop-right p drop-n)
(list
(path/param
(string-append
(path/param-path e)
".git")
(path/param-param e))))])))))