forked from melpa/melpa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package-build.el
1442 lines (1282 loc) · 63.3 KB
/
package-build.el
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
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; package-build.el --- Tools for assembling a package archive
;; Copyright (C) 2011-2013 Donald Ephraim Curtis <[email protected]>
;; Copyright (C) 2012-2014 Steve Purcell <[email protected]>
;; Copyright (C) 2009 Phil Hagelberg <[email protected]>
;; Author: Donald Ephraim Curtis <[email protected]>
;; Created: 2011-09-30
;; Version: 0.1
;; Keywords: tools
;; Package-Requires: ((cl-lib "0.5"))
;; This file is not (yet) part of GNU Emacs.
;; However, it is distributed under the same license.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; This file allows a curator to publish an archive of Emacs packages.
;; The archive is generated from a set of recipes which describe elisp
;; projects and repositories from which to get them. The term
;; "package" here is used to mean a specific version of a project that
;; is prepared for download and installation.
;;; Code:
(require 'cl-lib)
(require 'package)
(require 'lisp-mnt)
(require 'json)
(defconst package-build--this-dir (file-name-directory (or load-file-name (buffer-file-name))))
(defgroup package-build nil
"Facilities for building package.el-compliant packages from upstream source code."
:group 'development)
(defcustom package-build-working-dir (expand-file-name "working/" package-build--this-dir)
"Directory in which to keep checkouts."
:group 'package-build
:type 'string)
(defcustom package-build-archive-dir (expand-file-name "packages/" package-build--this-dir)
"Directory in which to keep compiled archives."
:group 'package-build
:type 'string)
(defcustom package-build-recipes-dir (expand-file-name "recipes/" package-build--this-dir)
"Directory containing recipe files."
:group 'package-build
:type 'string)
(defcustom package-build-verbose t
"When non-nil, `package-build' feels free to print information about its progress."
:group 'package-build
:type 'boolean)
(defcustom package-build-stable nil
"When non-nil, `package-build' tries to build packages from versions-tagged code."
:group 'package-build
:type 'boolean)
(defcustom package-build-timeout-executable
(let ((prog (or (executable-find "timeout")
(executable-find "gtimeout"))))
(when (and prog
(string-match-p "^ *-k" (shell-command-to-string (concat prog " --help"))))
prog))
"Path to a GNU coreutils \"timeout\" command if available.
This must be a version which supports the \"-k\" option."
:group 'package-build
:type '(file :must-match t))
(defcustom package-build-timeout-secs 600
"Wait this many seconds for external processes to complete.
If an external process takes longer than
`package-build-timeout-secs' seconds to complete, the process is
terminated. The `package-build-timeout-secs' variable will only
have an effect if `package-build-timeout-executable' is not nil."
:group 'package-build
:type 'number)
(defcustom package-build-tar-executable
(or (executable-find "gtar")
(executable-find "tar"))
"Path to a (preferably GNU) tar command.
Certain package names (e.g. \"@\") may not work properly with a BSD tar."
:group 'package-build
:type '(file :must-match t))
(defcustom package-build-write-melpa-badge-images nil
"When non-nil, write MELPA badge images alongside packages, for use on github pages etc."
:group 'package-build
:type 'boolean)
;;; Internal Variables
(defvar package-build--recipe-alist nil
"Internal list of package build specs.
Do not use this directly. Use `package-build-recipe-alist'
function.")
(defvar package-build--recipe-alist-initialized nil
"Determines if `package-build--recipe-alist` has been initialized.")
(defvar package-build--archive-alist nil
"Internal list of already-built packages, in the standard package.el format.
Do not use this directly. Use `package-build-archive-alist'
function for access to this function")
(defvar package-build--archive-alist-initialized nil
"Determines if package-build--archive-alist has been initialized.")
(defconst package-build-default-files-spec
'("*.el" "*.el.in" "dir"
"*.info" "*.texi" "*.texinfo"
"doc/dir" "doc/*.info" "doc/*.texi" "doc/*.texinfo"
(:exclude ".dir-locals.el" "test.el" "tests.el" "*-test.el" "*-tests.el"))
"Default value for :files attribute in recipes.")
(defun package-build--message (format-string &rest args)
"Log a message using FORMAT-STRING and ARGS as per `message'."
(when package-build-verbose
(apply 'message format-string args)))
(defun package-build--slurp-file (file-name)
"Return the contents of FILE-NAME as a string, or nil if no such file exists."
(when (file-exists-p file-name)
(with-temp-buffer
(insert-file-contents file-name)
(buffer-substring-no-properties (point-min) (point-max)))))
(defun package-build--string-rtrim (str)
"Remove trailing whitespace from `STR'."
(replace-regexp-in-string "[ \t\n]*$" "" str))
(defun package-build--parse-time (str)
"Parse STR as a time, and format as a YYYYMMDD.HHMM string."
;; We remove zero-padding the HH portion, as it is lost
;; when stored in the archive-contents
(let* ((s (substring-no-properties str))
(time (date-to-time
(if (string-match "^\\([0-9]\\{4\\}\\)/\\([0-9]\\{2\\}\\)/\\([0-9]\\{2\\}\\) \\([0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}\\)$" str)
(concat (match-string 1 str) "-" (match-string 2 str) "-"
(match-string 3 str) " " (match-string 4 str))
str))))
(concat (format-time-string "%Y%m%d." time)
(format "%d" (or (string-to-number (format-time-string "%H%M" time)) 0)))))
(defun package-build--string-match-all (regex str &rest groups)
"Find every match for `REGEX' within `STR'.
Return a list containing the full match string and match for
groups `GROUPS'. The return list is of the form
((FULL GROUP1 GROUP2 ...) ...)
where FULL is the complete regexp match and
GROUP1, GROUP2, ... are the regex groups specified by the
`GROUPS' argument. If `GROUPS' is nil then FULL and GROUP1 will
be identical."
(let (result
(pos 0)
(groups (or groups '(0))))
(while (string-match regex str pos)
(push (cons (match-string 0 str) (mapcar
(lambda (group)
(match-string group str))
groups))
result)
(setq pos (match-end 0)))
result))
(defun package-build--find-parse-time (regex &optional bound)
"Find REGEX in current buffer and format as a time version, optionally looking only as far as BOUND."
(package-build--parse-time (progn (re-search-backward regex bound)
(match-string-no-properties 1))))
(defun package-build--valid-version-string (str)
"Return true if STR is a valid version, otherwise return nil."
(ignore-errors (version-to-list str)))
(defun package-build--find-tag-version-newest (regex &optional bound &rest additional-groups)
"Find the newest version matching REGEX after point, maybe stopping at BOUND.
The first capture group 1 is examined, together with any ADDITIONAL-GROUPS."
(let* ((text (buffer-substring-no-properties
(or bound (point-min)) (point)))
(tags (cl-remove-if-not
(lambda (tag-version)
(package-build--valid-version-string (cadr tag-version)))
(apply 'package-build--string-match-all regex text 1 additional-groups))))
(car (nreverse (sort tags (lambda (v1 v2)
(version< (cadr v1) (cadr v2))))))))
(defun package-build--find-parse-time-latest (regex &optional bound)
"Find the latest timestamp matching REGEX, optionally looking only as far as BOUND."
(let* ((text (buffer-substring-no-properties
(or bound (point-min)) (point)))
(times (mapcar 'package-build--parse-time
(mapcar 'cadr (package-build--string-match-all regex text 1)))))
(car (nreverse (sort times 'string<)))))
(defun package-build--run-process (dir command &rest args)
"In DIR (or `default-directory' if unset) run COMMAND with ARGS.
Output is written to the current buffer."
(let* ((default-directory (file-name-as-directory (or dir default-directory)))
(timeout (number-to-string package-build-timeout-secs))
(argv (append
(unless (eq system-type 'windows-nt)
'("env" "LC_ALL=C"))
(if package-build-timeout-executable
(append (list package-build-timeout-executable "-k" "60" timeout command) args)
(cons command args)))))
(unless (file-directory-p default-directory)
(error "Can't run process in non-existent directory: %s" default-directory))
(let ((exit-code (apply 'process-file (car argv) nil (current-buffer) t (cdr argv))))
(or (zerop exit-code)
(error "Command '%s' exited with non-zero status %d: %s"
argv exit-code (buffer-string))))))
(defun package-build--run-process-match (regex dir prog &rest args)
"Find match for REGEX when - in DIR, or `default-directory' if unset - we run PROG with ARGS."
(with-temp-buffer
(apply 'package-build--run-process dir prog args)
(goto-char (point-min))
(re-search-forward regex)
(match-string-no-properties 1)))
(defun package-build-checkout (package-name config working-dir)
"Check out source for PACKAGE-NAME with CONFIG under WORKING-DIR.
In turn, this function uses the :fetcher option in the CONFIG to
choose a source-specific fetcher function, which it calls with
the same arguments.
Returns a last-modification timestamp for the :files listed in
CONFIG, if any, or `package-build-default-files-spec' otherwise."
(let ((repo-type (plist-get config :fetcher)))
(package-build--message "Fetcher: %s" (symbol-name repo-type))
(unless (eq 'wiki repo-type)
(package-build--message "Source: %s\n" (or (plist-get config :repo) (plist-get config :url))))
(funcall (intern (format "package-build--checkout-%s" (symbol-name repo-type)))
package-name config (file-name-as-directory working-dir))))
(defvar package-build--last-wiki-fetch-time 0
"The time at which an emacswiki URL was last requested.
This is used to avoid exceeding the rate limit of 1 request per 2
seconds; the server cuts off after 10 requests in 20 seconds.")
(defvar package-build--wiki-min-request-interval 3
"The shortest permissible interval between successive requests for Emacswiki URLs.")
(defmacro package-build--with-wiki-rate-limit (&rest body)
"Rate-limit BODY code passed to this macro to match EmacsWiki's rate limiting."
(let ((now (cl-gensym))
(elapsed (cl-gensym)))
`(let* ((,now (float-time))
(,elapsed (- ,now package-build--last-wiki-fetch-time)))
(when (< ,elapsed package-build--wiki-min-request-interval)
(let ((wait (- package-build--wiki-min-request-interval ,elapsed)))
(package-build--message "Waiting %.2f secs before hitting Emacswiki again" wait)
(sleep-for wait)))
(unwind-protect
(progn ,@body)
(setq package-build--last-wiki-fetch-time (float-time))))))
(require 'mm-decode)
(defvar url-http-response-status)
(defvar url-http-end-of-headers)
(defun package-build--url-copy-file (url newname &optional ok-if-already-exists)
"Copy URL to NEWNAME. Both args must be strings.
Like `url-copy-file', but it produces an error if the http response is not 200.
Signals a `file-already-exists' error if file NEWNAME already exists,
unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.
A number as third arg means request confirmation if NEWNAME already exists."
(if (and (file-exists-p newname)
(not ok-if-already-exists))
(error "Opening output file: File already exists, %s" newname))
(let ((buffer (url-retrieve-synchronously url))
(handle nil))
(if (not buffer)
(error "Opening input file: No such file or directory, %s" url))
(with-current-buffer buffer
(unless (= 200 url-http-response-status)
(error "HTTP error %s fetching %s" url-http-response-status url))
(setq handle (mm-dissect-buffer t)))
(mm-save-part-to-file handle newname)
(kill-buffer buffer)
(mm-destroy-parts handle)))
(defun package-build--grab-wiki-file (filename)
"Download FILENAME from emacswiki, returning its last-modified time."
(let* ((download-url
(format "http://www.emacswiki.org/emacs/download/%s" filename))
(wiki-url
(format "http://www.emacswiki.org/emacs/%s" filename)))
(package-build--with-wiki-rate-limit
(package-build--url-copy-file download-url filename t))
(when (zerop (nth 7 (file-attributes filename)))
(error "Wiki file %s was empty - has it been removed?" filename))
;; The Last-Modified response header for the download is actually
;; correct for the file, but we have no access to that
;; header. Instead, we must query the non-raw emacswiki page for
;; the file.
;; Since those Emacswiki lookups are time-consuming, we maintain a
;; foo.el.stamp file containing ("SHA1" . "PARSED_TIME")
(let* ((new-content-hash (secure-hash 'sha1 (package-build--slurp-file filename)))
(stamp-file (concat filename ".stamp"))
(stamp-info (package-build--read-from-file stamp-file))
(prev-content-hash (car stamp-info)))
(if (and prev-content-hash
(string-equal new-content-hash prev-content-hash))
;; File has not changed, so return old timestamp
(progn
(package-build--message "%s is unchanged" filename)
(cdr stamp-info))
(package-build--message "%s has changed - checking mod time" filename)
(let ((new-timestamp
(with-current-buffer (package-build--with-wiki-rate-limit
(url-retrieve-synchronously wiki-url))
(unless (= 200 url-http-response-status)
(error "HTTP error %s fetching %s" url-http-response-status wiki-url))
(goto-char (point-max))
(package-build--find-parse-time
"Last edited \\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [0-9]\\{2\\}:[0-9]\\{2\\} [A-Z]\\{3\\}\\)"
url-http-end-of-headers))))
(package-build--dump (cons new-content-hash new-timestamp) stamp-file)
new-timestamp)))))
(defun package-build--checkout-wiki (name config dir)
"Checkout package NAME with config CONFIG from the EmacsWiki into DIR."
(unless package-build-stable
(with-current-buffer (get-buffer-create "*package-build-checkout*")
(unless (file-exists-p dir)
(make-directory dir))
(let ((files (or (plist-get config :files)
(list (format "%s.el" name))))
(default-directory dir))
(car (nreverse (sort (mapcar 'package-build--grab-wiki-file files) 'string-lessp)))))))
(defun package-build--darcs-repo (dir)
"Get the current darcs repo for DIR."
(package-build--run-process-match "Default Remote: \\(.*\\)" dir "darcs" "show" "repo"))
(defun package-build--checkout-darcs (name config dir)
"Check package NAME with config CONFIG out of darcs into DIR."
(unless package-build-stable
(let ((repo (plist-get config :url)))
(with-current-buffer (get-buffer-create "*package-build-checkout*")
(cond
((and (file-exists-p (expand-file-name "_darcs" dir))
(string-equal (package-build--darcs-repo dir) repo))
(package-build--princ-exists dir)
(package-build--run-process dir "darcs" "pull"))
(t
(when (file-exists-p dir)
(delete-directory dir t))
(package-build--princ-checkout repo dir)
(package-build--run-process nil "darcs" "get" repo dir)))
(apply 'package-build--run-process dir "darcs" "changes" "--max-count" "1"
(package-build--expand-source-file-list dir config))
(package-build--find-parse-time
"\\([a-zA-Z]\\{3\\} [a-zA-Z]\\{3\\} \\( \\|[0-9]\\)[0-9] [0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\} [A-Za-z]\\{3\\} [0-9]\\{4\\}\\)")))))
(defun package-build--fossil-repo (dir)
"Get the current fossil repo for DIR."
(package-build--run-process-match "\\(.*\\)" dir "fossil" "remote-url"))
(defun package-build--checkout-fossil (name config dir)
"Check package NAME with config CONFIG out of fossil into DIR."
(unless package-build-stable
(let ((repo (plist-get config :url)))
(with-current-buffer (get-buffer-create "*package-build-checkout*")
(cond
((and (or (file-exists-p (expand-file-name ".fslckout" dir))
(file-exists-p (expand-file-name "_FOSSIL_" dir)))
(string-equal (package-build--fossil-repo dir) repo))
(package-build--princ-exists dir)
(package-build--run-process dir "fossil" "update"))
(t
(when (file-exists-p dir)
(delete-directory dir t))
(package-build--princ-checkout repo dir)
(make-directory dir)
(package-build--run-process dir "fossil" "clone" repo "repo.fossil")
(package-build--run-process dir "fossil" "open" "repo.fossil")))
(package-build--run-process dir "fossil" "timeline" "-n" "1" "-t" "ci")
(or (package-build--find-parse-time
"=== \\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ===\n[0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}\\) ")
(error "No valid timestamps found!"))))))
(defun package-build--svn-repo (dir)
"Get the current svn repo for DIR."
(package-build--run-process-match "URL: \\(.*\\)" dir "svn" "info"))
(defun package-build--trim (str &optional chr)
"Return a copy of STR without any trailing CHR (or space if unspecified)."
(if (equal (elt str (1- (length str))) (or chr ? ))
(substring str 0 (1- (length str)))
str))
(defun package-build--princ-exists (dir)
"Print a message that the contents of DIR will be updated."
(package-build--message "Updating %s" dir))
(defun package-build--princ-checkout (repo dir)
"Print a message that REPO will be checked out into DIR."
(package-build--message "Cloning %s to %s" repo dir))
(defun package-build--checkout-svn (name config dir)
"Check package NAME with config CONFIG out of svn into DIR."
(unless package-build-stable
(with-current-buffer (get-buffer-create "*package-build-checkout*")
(let ((repo (package-build--trim (plist-get config :url) ?/))
(bound (goto-char (point-max))))
(cond
((and (file-exists-p (expand-file-name ".svn" dir))
(string-equal (package-build--svn-repo dir) repo))
(package-build--princ-exists dir)
(package-build--run-process dir "svn" "up"))
(t
(when (file-exists-p dir)
(delete-directory dir t))
(package-build--princ-checkout repo dir)
(package-build--run-process nil "svn" "checkout" repo dir)))
(apply 'package-build--run-process dir "svn" "info"
(package-build--expand-source-file-list dir config))
(or (package-build--find-parse-time-latest "Last Changed Date: \\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}\\( [+-][0-9]\\{4\\}\\)?\\)" bound)
(error "No valid timestamps found!"))))))
(defun package-build--cvs-repo (dir)
"Get the current CVS root and repository for DIR.
Return a cons cell whose `car' is the root and whose `cdr' is the repository."
(apply 'cons
(mapcar (lambda (file)
(package-build--string-rtrim (package-build--slurp-file (expand-file-name file dir))))
'("CVS/Root" "CVS/Repository"))))
(defun package-build--checkout-cvs (name config dir)
"Check package NAME with config CONFIG out of cvs into DIR."
(unless package-build-stable
(with-current-buffer (get-buffer-create "*package-build-checkout*")
(let ((root (package-build--trim (plist-get config :url) ?/))
(repo (or (plist-get config :module) (symbol-name name)))
(bound (goto-char (point-max))))
(cond
((and (file-exists-p (expand-file-name "CVS" dir))
(equal (package-build--cvs-repo dir) (cons root repo)))
(package-build--princ-exists dir)
(package-build--run-process dir "cvs" "update" "-dP"))
(t
(when (file-exists-p dir)
(delete-directory dir t))
(package-build--princ-checkout (format "%s from %s" repo root) dir)
;; CVS insists on relative paths as target directory for checkout (for
;; whatever reason), and puts "CVS" directories into every subdirectory
;; of the current working directory given in the target path. To get CVS
;; to just write to DIR, we need to execute CVS from the parent
;; directory of DIR, and specific DIR as relative path. Hence all the
;; following mucking around with paths. CVS is really horrid.
(let* ((dir (directory-file-name dir))
(working-dir (file-name-directory dir))
(target-dir (file-name-nondirectory dir)))
(package-build--run-process working-dir "env" "TZ=UTC" "cvs" "-z3" "-d" root "checkout"
"-d" target-dir repo))))
(apply 'package-build--run-process dir "cvs" "log"
(package-build--expand-source-file-list dir config))
(or (package-build--find-parse-time-latest "date: \\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}\\( [+-][0-9]\\{4\\}\\)?\\)" bound)
(package-build--find-parse-time-latest "date: \\([0-9]\\{4\\}/[0-9]\\{2\\}/[0-9]\\{2\\} [0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}\\);" bound)
(error "No valid timestamps found!"))
))))
(defun package-build--git-repo (dir)
"Get the current git repo for DIR."
(package-build--run-process-match
"Fetch URL: \\(.*\\)" dir "git" "remote" "show" "-n" "origin"))
(defun package-build--git-head-branch (dir)
"Get the current git repo for DIR."
(or (ignore-errors
(package-build--run-process-match
"HEAD branch: \\(.*\\)" dir "git" "remote" "show" "origin"))
"master"))
(defun package-build--checkout-git (name config dir)
"Check package NAME with config CONFIG out of git into DIR."
(let ((repo (plist-get config :url))
(commit (or (plist-get config :commit)
(let ((branch (plist-get config :branch)))
(when branch
(concat "origin/" branch))))))
(with-current-buffer (get-buffer-create "*package-build-checkout*")
(goto-char (point-max))
(cond
((and (file-exists-p (expand-file-name ".git" dir))
(string-equal (package-build--git-repo dir) repo))
(package-build--princ-exists dir)
(package-build--run-process dir "git" "remote" "update"))
(t
(when (file-exists-p dir)
(delete-directory dir t))
(package-build--princ-checkout repo dir)
(package-build--run-process nil "git" "clone" repo dir)))
(if package-build-stable
(let* ((bound (goto-char (point-max)))
(tag-version (and (package-build--run-process dir "git" "tag")
(or (package-build--find-tag-version-newest
"^\\(?:v[.-]?\\)?\\([0-9]+[^ \t\n]*\\)$" bound)
(error
"No valid stable versions found for %s"
name)))))
;; Using reset --hard here to comply with what's used for
;; unstable, but maybe this should be a checkout?
(package-build--run-process dir "git" "reset" "--hard" (concat "tags/" (car tag-version)))
(package-build--run-process dir "git" "submodule" "update" "--init" "--recursive")
(cadr tag-version))
(package-build--run-process dir "git" "reset" "--hard"
(or commit (concat "origin/" (package-build--git-head-branch dir))))
(package-build--run-process dir "git" "submodule" "update" "--init" "--recursive")
(apply 'package-build--run-process dir "git" "log" "--first-parent" "-n1" "--pretty=format:'\%ci'"
(package-build--expand-source-file-list dir config))
(package-build--find-parse-time
"\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}\\( [+-][0-9]\\{4\\}\\)?\\)")))))
(defun package-build--checkout-github (name config dir)
"Check package NAME with config CONFIG out of github into DIR."
(let* ((url (format "git://github.com/%s.git" (plist-get config :repo))))
(package-build--checkout-git name (plist-put (copy-sequence config) :url url) dir)))
(defun package-build--checkout-gitlab (name config dir)
"Check package NAME with config CONFIG out of gitlab into DIR."
(let* ((url (format "https://gitlab.com/%s.git" (plist-get config :repo))))
(package-build--checkout-git name (plist-put (copy-sequence config) :url url) dir)))
(defun package-build--bzr-expand-repo (repo)
"Get REPO expanded name."
(package-build--run-process-match "\\(?:branch root\\|repository branch\\): \\(.*\\)" nil "bzr" "info" repo))
(defun package-build--bzr-repo (dir)
"Get the current bzr repo for DIR."
(package-build--run-process-match "parent branch: \\(.*\\)" dir "bzr" "info"))
(defun package-build--checkout-bzr (name config dir)
"Check package NAME with config CONFIG out of bzr into DIR."
(unless package-build-stable
(let ((repo (package-build--bzr-expand-repo (plist-get config :url))))
(with-current-buffer (get-buffer-create "*package-build-checkout*")
(goto-char (point-max))
(cond
((and (file-exists-p (expand-file-name ".bzr" dir))
(string-equal (package-build--bzr-repo dir) repo))
(package-build--princ-exists dir)
(package-build--run-process dir "bzr" "merge"))
(t
(when (file-exists-p dir)
(delete-directory dir t))
(package-build--princ-checkout repo dir)
(package-build--run-process nil "bzr" "branch" repo dir)))
(apply 'package-build--run-process dir "bzr" "log" "-l1"
(package-build--expand-source-file-list dir config))
(package-build--find-parse-time
"\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}\\( [+-][0-9]\\{4\\}\\)?\\)")))))
(defun package-build--hg-repo (dir)
"Get the current hg repo for DIR."
(package-build--run-process-match "default = \\(.*\\)" dir "hg" "paths"))
(defun package-build--checkout-hg (name config dir)
"Check package NAME with config CONFIG out of hg into DIR."
(let ((repo (plist-get config :url)))
(with-current-buffer (get-buffer-create "*package-build-checkout*")
(goto-char (point-max))
(cond
((and (file-exists-p (expand-file-name ".hg" dir))
(string-equal (package-build--hg-repo dir) repo))
(package-build--princ-exists dir)
(package-build--run-process dir "hg" "pull")
(package-build--run-process dir "hg" "update"))
(t
(when (file-exists-p dir)
(delete-directory dir t))
(package-build--princ-checkout repo dir)
(package-build--run-process nil "hg" "clone" repo dir)))
(if package-build-stable
(let* ((bound (goto-char (point-max)))
(tag-version (and (package-build--run-process dir "hg" "tags")
(or (package-build--find-tag-version-newest
"^\\(?:v[.-]?\\)?\\([0-9]+[^ \t\n]*\\)[ \t]*[0-9]+:\\([[:xdigit:]]+\\)$"
bound
2)
(error
"No valid stable versions found for %s"
name)))))
(package-build--run-process dir "hg" "update" (nth 2 tag-version))
(cadr tag-version))
(apply 'package-build--run-process dir "hg" "log" "--style" "compact" "-l1"
(package-build--expand-source-file-list dir config))
(package-build--find-parse-time
"\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [0-9]\\{2\\}:[0-9]\\{2\\}\\( [+-][0-9]\\{4\\}\\)?\\)")))))
(defun package-build--dump (data file &optional pretty-print)
"Write DATA to FILE as a Lisp sexp.
Optionally PRETTY-PRINT the data."
(with-temp-file file
(package-build--message "File: %s" file)
(if pretty-print
(pp data (current-buffer))
(print data (current-buffer)))))
(defun package-build--write-pkg-file (pkg-file pkg-info)
"Write PKG-FILE containing PKG-INFO."
(with-temp-file pkg-file
(pp
`(define-package
,(aref pkg-info 0)
,(aref pkg-info 3)
,(aref pkg-info 2)
',(mapcar
(lambda (elt)
(list (car elt)
(package-version-join (cadr elt))))
(aref pkg-info 1))
;; Append our extra information
,@(apply #'append (mapcar (lambda (entry)
(let ((value (cdr entry)))
(when (or (symbolp value) (listp value))
;; We must quote lists and symbols,
;; because Emacs 24.3 and earlier evaluate
;; the package information, which would
;; break for unquoted symbols or lists
(setq value (list 'quote value)))
(list (car entry) value)))
(when (> (length pkg-info) 4)
(aref pkg-info 4)))))
(current-buffer))
(princ ";; Local Variables:\n;; no-byte-compile: t\n;; End:\n" (current-buffer))))
(defun package-build--read-from-file (file-name)
"Read and return the Lisp data stored in FILE-NAME, or nil if no such file exists."
(when (file-exists-p file-name)
(car (read-from-string (package-build--slurp-file file-name)))))
(defun package-build--create-tar (file dir &optional files)
"Create a tar FILE containing the contents of DIR, or just FILES if non-nil."
(when (eq system-type 'windows-nt)
(setq file (replace-regexp-in-string "^\\([a-z]\\):" "/\\1" file)))
(apply 'process-file
package-build-tar-executable nil
(get-buffer-create "*package-build-checkout*")
nil "-cvf"
file
"--exclude=.svn"
"--exclude=CVS"
"--exclude=.git*"
"--exclude=_darcs"
"--exclude=.fslckout"
"--exclude=_FOSSIL_"
"--exclude=.bzr"
"--exclude=.hg"
(or (mapcar (lambda (fn) (concat dir "/" fn)) files) (list dir))))
(defun package-build--find-package-commentary (file-path)
"Get commentary section from FILE-PATH."
(when (file-exists-p file-path)
(with-temp-buffer
(insert-file-contents file-path)
(lm-commentary))))
(defun package-build--write-pkg-readme (target-dir commentary file-name)
"In TARGET-DIR, write COMMENTARY to a -readme.txt file prefixed with FILE-NAME."
(when commentary
(with-temp-buffer
(insert commentary)
;; Adapted from `describe-package-1'.
(goto-char (point-min))
(save-excursion
(when (re-search-forward "^;;; Commentary:\n" nil t)
(replace-match ""))
(while (re-search-forward "^\\(;+ ?\\)" nil t)
(replace-match ""))
(goto-char (point-min))
(when (re-search-forward "\\`\\( *\n\\)+" nil t)
(replace-match "")))
(delete-trailing-whitespace)
(let ((coding-system-for-write buffer-file-coding-system))
(write-region nil nil
(package-build--readme-file-name target-dir file-name))))))
(defun package-build--readme-file-name (target-dir file-name)
"Name of the readme file in TARGET-DIR for the package FILE-NAME."
(expand-file-name (concat file-name "-readme.txt")
target-dir))
(defun package-build--update-or-insert-version (version)
"Ensure current buffer has a \"Package-Version: VERSION\" header."
(goto-char (point-min))
(if (let ((case-fold-search t))
(re-search-forward "^;+* *Package-Version *: *" nil t))
(progn
(move-beginning-of-line nil)
(search-forward "V" nil t)
(backward-char)
(insert "X-Original-")
(move-beginning-of-line nil))
;; Put the new header in a sensible place if we can
(re-search-forward "^;+* *\\(Version:\\|Keywords\\|URL\\)" nil t)
(forward-line))
(insert (format ";; Package-Version: %s" version))
(newline))
(defun package-build--ensure-ends-here-line (file-path)
"Add a 'FILE-PATH ends here' trailing line if missing."
(save-excursion
(goto-char (point-min))
(let* ((fname (file-name-nondirectory file-path))
(trailer (concat ";;; " fname " ends here")))
(unless (search-forward trailer nil t)
(goto-char (point-max))
(newline)
(insert trailer)
(newline)))))
(defun package-build--get-package-info (file-path)
"Get a vector of package info from the docstrings in FILE-PATH."
(when (file-exists-p file-path)
(ignore-errors
(with-temp-buffer
(insert-file-contents file-path)
;; next few lines are a hack for some packages that aren't
;; commented properly.
(package-build--update-or-insert-version "0")
(package-build--ensure-ends-here-line file-path)
(cl-flet ((package-strip-rcs-id (str) "0"))
(package-build--package-buffer-info-vec))))))
(defun package-build--get-pkg-file-info (file-path)
"Get a vector of package info from \"-pkg.el\" file FILE-PATH."
(when (file-exists-p file-path)
(let ((package-def (package-build--read-from-file file-path)))
(if (eq 'define-package (car package-def))
(let* ((pkgfile-info (cdr package-def))
(descr (nth 2 pkgfile-info))
(rest-plist (cl-subseq pkgfile-info (min 4 (length pkgfile-info))))
(extras (let (alist)
(while rest-plist
(unless (memq (car rest-plist) '(:kind :archive))
(let ((value (cadr rest-plist)))
(when value
(push (cons (car rest-plist)
(if (eq (car-safe value) 'quote)
(cadr value)
value))
alist))))
(setq rest-plist (cddr rest-plist)))
alist)))
(when (string-match "[\r\n]" descr)
(error "Illegal multi-line package description in %s" file-path))
(vector
(nth 0 pkgfile-info)
(mapcar
(lambda (elt)
(list (car elt) (version-to-list (cadr elt))))
(eval (nth 3 pkgfile-info)))
descr
(nth 1 pkgfile-info)
extras))
(error "No define-package found in %s" file-path)))))
(defun package-build--merge-package-info (pkg-info name version)
"Return a version of PKG-INFO updated with NAME, VERSION and info from CONFIG.
If PKG-INFO is nil, an empty one is created."
(let* ((merged (or (copy-sequence pkg-info)
(vector name nil "No description available." version))))
(aset merged 0 name)
(aset merged 3 version)
merged))
(defun package-build--archive-entry (pkg-info type)
"Return the archive-contents cons cell for PKG-INFO and TYPE."
(let* ((name (intern (aref pkg-info 0)))
(requires (aref pkg-info 1))
(desc (or (aref pkg-info 2) "No description available."))
(version (aref pkg-info 3))
(extras (when (> (length pkg-info) 4)
(aref pkg-info 4))))
(cons name
(vector (version-to-list version)
requires
desc
type
extras))))
(defun package-build--archive-file-name (archive-entry)
"Return the path of the file in which the package for ARCHIVE-ENTRY is stored."
(let* ((name (car archive-entry))
(pkg-info (cdr archive-entry))
(version (package-version-join (aref pkg-info 0)))
(flavour (aref pkg-info 3)))
(expand-file-name
(format "%s-%s.%s" name version (if (eq flavour 'single) "el" "tar"))
package-build-archive-dir)))
(defun package-build--entry-file-name (archive-entry)
"Return the path of the file in which the package for ARCHIVE-ENTRY is stored."
(let* ((name (car archive-entry))
(pkg-info (cdr archive-entry))
(version (package-version-join (aref pkg-info 0))))
(expand-file-name
(format "%s-%s.entry" name version)
package-build-archive-dir)))
(defun package-build--delete-file-if-exists (file)
"Delete FILE if it exists."
(when (file-exists-p file)
(delete-file file)))
(defun package-build--remove-archive-files (archive-entry)
"Remove ARCHIVE-ENTRY from archive-contents, and delete associated file.
Note that the working directory (if present) is not deleted by
this function, since the archive list may contain another version
of the same-named package which is to be kept."
(package-build--message "Removing archive: %s" archive-entry)
(mapcar 'package-build--delete-file-if-exists
(list (package-build--archive-file-name archive-entry)
(package-build--entry-file-name archive-entry))))
(defun package-build--read-recipe (file-name)
"Return the plist of recipe info for the package called FILE-NAME.
It performs some basic checks on the recipe to ensure that known
keys have values of the right types, and raises an error if that
is the not the case. If invalid combinations of keys are
supplied then errors will only be caught when an attempt is made
to build the recipe."
(let* ((pkg-info (package-build--read-from-file file-name))
(pkg-name (car pkg-info))
(rest (cdr pkg-info)))
(cl-assert pkg-name)
(cl-assert (symbolp pkg-name))
(cl-assert (string= (symbol-name pkg-name) (file-name-nondirectory file-name))
nil
"Recipe '%s' contains mismatched package name '%s'"
(file-name-nondirectory file-name)
(car pkg-info))
(cl-assert rest)
(let* ((symbol-keys '(:fetcher))
(string-keys '(:url :repo :module :commit :branch))
(list-keys '(:files :old-names))
(all-keys (append symbol-keys string-keys list-keys)))
(dolist (thing rest)
(when (keywordp thing)
(cl-assert (memq thing all-keys) nil "Unknown keyword %S" thing)))
(let ((fetcher (plist-get rest :fetcher)))
(cl-assert fetcher nil ":fetcher is missing")
(when (memq fetcher '(github gitlab))
(cl-assert (plist-get rest :repo) ":repo is missing")))
(dolist (key symbol-keys)
(let ((val (plist-get rest key)))
(when val
(cl-assert (symbolp val) nil "%s must be a list but is %S" key val))))
(dolist (key list-keys)
(let ((val (plist-get rest key)))
(when val
(cl-assert (listp val) nil "%s must be a list but is %S" key val ))))
(dolist (key string-keys)
(let ((val (plist-get rest key)))
(when val
(cl-assert (stringp val) nil "%s must be a string but is %S" key val )))))
pkg-info))
(defun package-build--read-recipes ()
"Return a list of data structures for all recipes in `package-build-recipes-dir'."
(cl-loop for file-name in (directory-files package-build-recipes-dir t "^[^.]")
collect (package-build--read-recipe file-name)))
(defun package-build--read-recipes-ignore-errors ()
"Return a list of data structures for all recipes in `package-build-recipes-dir'."
(cl-loop for file-name in (directory-files package-build-recipes-dir t "^[^.]")
for pkg-info = (condition-case err (package-build--read-recipe file-name)
(error (package-build--message "Error reading recipe %s: %s"
file-name
(error-message-string err))
nil))
when pkg-info
collect pkg-info))
(defun package-build-expand-file-specs (dir specs &optional subdir allow-empty)
"In DIR, expand SPECS, optionally under SUBDIR.
The result is a list of (SOURCE . DEST), where SOURCE is a source
file path and DEST is the relative path to which it should be copied.
If the resulting list is empty, an error will be reported. Pass t
for ALLOW-EMPTY to prevent this error."
(let ((default-directory dir)
(prefix (if subdir (format "%s/" subdir) ""))
(lst))
(dolist (entry specs lst)
(setq lst
(if (consp entry)
(if (eq :exclude (car entry))
(cl-nset-difference lst
(package-build-expand-file-specs dir (cdr entry) nil t)
:key 'car
:test 'equal)
(nconc lst
(package-build-expand-file-specs
dir
(cdr entry)
(concat prefix (car entry))
t)))
(nconc
lst (mapcar (lambda (f)
(let ((destname)))
(cons f
(concat prefix
(replace-regexp-in-string
"\\.in\\'"
""
(file-name-nondirectory f)))))
(file-expand-wildcards entry))))))
(when (and (null lst) (not allow-empty))
(error "No matching file(s) found in %s: %s" dir specs))
lst))
(defun package-build--config-file-list (config)
"Get the :files spec from CONFIG, or return `package-build-default-files-spec'."
(let ((file-list (plist-get config :files)))
(cond
((null file-list)
package-build-default-files-spec)
((eq :defaults (car file-list))
(append package-build-default-files-spec (cdr file-list)))
(t
file-list))))
(defun package-build--expand-source-file-list (dir config)
"Shorthand way to expand paths in DIR for source files listed in CONFIG."
(mapcar 'car (package-build-expand-file-specs dir (package-build--config-file-list config))))
(defun package-build--generate-info-files (files source-dir target-dir)
"Create .info files from any .texi files listed in FILES.
The source and destination file paths are expanded in SOURCE-DIR
and TARGET-DIR respectively.
Any of the original .texi(nfo) files found in TARGET-DIR are
deleted."
(dolist (spec files)
(let* ((source-file (car spec))
(source-path (expand-file-name source-file source-dir))
(dest-file (cdr spec))
(info-path (expand-file-name
(concat (file-name-sans-extension dest-file) ".info")
target-dir)))
(when (string-match ".texi\\(nfo\\)?$" source-file)
(when (not (file-exists-p info-path))
(with-current-buffer (get-buffer-create "*package-build-info*")
(ignore-errors
(package-build--run-process
(file-name-directory source-path)
"makeinfo"
source-path
"-o"
info-path)
(package-build--message "Created %s" info-path))))
(package-build--message "Removing %s" (expand-file-name dest-file target-dir))
(delete-file (expand-file-name dest-file target-dir))))))
(defun package-build--generate-dir-file (files target-dir)
"Create dir file from any .info files listed in FILES in TARGET-DIR."
(dolist (spec files)
(let* ((source-file (car spec))
(dest-file (cdr spec))
(info-path (expand-file-name
(concat (file-name-sans-extension dest-file) ".info")
target-dir)))
(when (and (or (string-match ".info$" source-file)
(string-match ".texi\\(nfo\\)?$" source-file))
(file-exists-p info-path))