-
Notifications
You must be signed in to change notification settings - Fork 1
/
archive-contents.el
617 lines (585 loc) · 19 KB
/
archive-contents.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
;; (version< "1alpha" "1beta")
;; (version= "2.4-alpha" "2.4-git")
;; (version< "2.4-snapshot3" "2.4-snapshot2-snapshot2019")
;; (version-to-list "6.0.2-snapshot10")
;; (last (version-to-list "32-alpha0-snapshot20210321.852"))
;; (package-version-join (version-to-list "6.0b"))
;; (cl-every (cl-function (lambda (what &aux (x (car what)) (y (cdr what)))
;; (version-list-< (version-to-list x) (version-to-list y))))
;; '(("0.17.0pre" . "0.17.0pre1-snapshot20210330.1531")
;; ("0.17.0pre3" . "0.17.0pre3.1-snapshot20210330.1531")
;; ;; ("0.17.0pre" . "0.17.0pre.1-snapshot20210330.1531")
;; ("0.17.0snapshot" . "0.17.0snapshot20210330.1531")
;; ("0.17.0snapshot2" . "0.17.0snapshot2.1-snapshot20210330.1531")
;; ))
(require 'subr-x)
(require 'package)
(require 'url-http)
(require 'lisp-mnt)
(require 'tar-mode)
(defvar shmelpa--outliers nil)
(defconst shmelpa--problematic-longlink '(ac-html ox-ioslide ptemplate-templates stan-snippets yasnippet-snippets))
(defconst shmelpa--outliers-check-pkg
'(weechat
tracking
expand-region
vdiff
caml
slime
all-the-icons
bind-map
ov
evil-cleverparens
treepy
base16-theme
snapshot-timemachine
uuidgen
thrift
haml-mode
jump
yasnippet
transient
purescript-mode
codesearch
paredit
pager
ox-gfm
forge
s
bibtex-completion
frecency
alert
org-present
matlab-mode
ox-slimhtml
graphql-mode
ess
jest
virtualenvwrapper
flim
mozc
eval-in-repl
calfw
mandoku
github-clone
friendly-shell-command
hl-todo
look-mode
stem
graphql
highlight
f
inflections
ddskk
keypress-multi-event
cfrs
gitignore-mode
bm
avy-menu
haskell-emacs
exec-path-from-shell
names
gitlab-ci-mode
magit
with-editor
ghub
add-node-modules-path
less-css-mode
flymake-easy
request-deferred
merlin
keg
flx
faust-mode
projectile-rails
smartparens
polymode
ctable
helm
closql
dash
inheritenv
iedit
emms
package-lint
initsplit
ace-jump-mode
osx-location
web-server
websocket
auto-complete
ede-php-autoload
e2wm
inlineR
debian-el
json-mode
ledger-mode
js2-refactor
w3m
pg
page-break-lines
popup
notmuch
ycmd
know-your-http-well
lean-mode
erlang
ivy
math-symbol-lists
axiom-environment
company-c-headers
evil
shell-split-string
emacsql-sqlite
clang-format
package-build
tree-mode
dash-functional
choice-program
langdoc
epkg
clmemo
key-chord
bbdb
outline-minor-faces
magit-popup
deferred
packed
reformatter
markdown-mode
projectile
flycheck
request
ccc
xcscope
skewer-mode
helm-core
geiser
pos-tip
parseedn))
(defconst shmelpa-inception "0.1.0")
(defconst shmelpa-recipes-dir
(expand-file-name "recipes" "~/melpa"))
(defconst shmelpa-melpa-dir "/opt/bitnami/nginx/html/packages/melpa")
(defconst shmelpa-targets-dir
(if (string= user-login-name "bitnami")
"/opt/bitnami/nginx/tmp/targets"
default-directory))
(defconst shmelpa-packages-dir
(if (string= user-login-name "bitnami")
"/opt/bitnami/nginx/html/packages"
default-directory))
(defconst shmelpa-ingested-file
(concat (file-name-as-directory
(if (string= user-login-name "bitnami")
shmelpa-melpa-dir
default-directory))
"archive-contents-ingested"))
(defconst shmelpa-final-file
(concat (file-name-as-directory
(if (string= user-login-name "bitnami")
shmelpa-melpa-dir
default-directory))
"archive-contents-final"))
(defconst shmelpa-staging-file
(concat (file-name-as-directory
(if (string= user-login-name "bitnami")
shmelpa-melpa-dir
default-directory))
"archive-contents-staging"))
(cl-defun shmelpa--pkg-el-version (name* commit &aux version (name (symbol-name name*)))
(when-let* ((recipe
(cdr (shmelpa--file-to-sexpr (expand-file-name name shmelpa-recipes-dir))))
(fetcher (plist-get recipe :fetcher))
(repo (plist-get recipe :repo))
(fragile (pcase fetcher
('github
(mapconcat #'identity
(list "https://raw.githubusercontent.com"
repo
commit)
"/"))
('gitlab
(mapconcat #'identity
(list "https://gitlab.com"
repo
"-"
"raw"
commit)
"/"))))
(search-dirs
(append '("" "lisp") ;; melpa recipe :defaults keyword
(cl-remove-if-not
#'identity
(mapcar #'file-name-directory
(cl-remove-if-not
#'stringp
(plist-get recipe :files))))))
(search-urls
(mapcar (lambda (dir) (format "%s/%s%s-pkg.el" fragile (file-name-as-directory dir) name))
search-dirs)))
(dolist (url search-urls)
(if-let ((buffer (url-retrieve-synchronously url t nil 5)))
(unwind-protect
(with-current-buffer buffer
(when (and (ignore-errors (url-http-parse-headers)) (= url-http-response-status 200))
(goto-char (marker-position url-http-end-of-headers))
(when-let* ((desc (ignore-errors
(package-process-define-package
(read (current-buffer)))))
(candidate (package-desc-version desc)))
(when (or (null version)
(version-list-< version candidate))
(setq version candidate)))))
(let (kill-buffer-query-functions)
(ignore-errors (kill-buffer buffer))))
(message "url-retrieve (%s): no response" url))))
(when version (package-version-join version)))
(defun shmelpa-ingest ()
(interactive)
(if-let* ((url "https://melpa.org/packages/archive-contents")
(buffer (url-retrieve-synchronously url t nil 5)))
(unwind-protect
(with-current-buffer buffer
(goto-char (marker-position url-http-end-of-headers))
(let ((contents (shmelpa--winnow (read (current-buffer)))))
(write-region (pp-to-string contents) nil
shmelpa-ingested-file
nil 'silent)))
(let (kill-buffer-query-functions)
(ignore-errors (kill-buffer buffer))))
(error "url-retrieve (%s): no response" url)))
(defun shmelpa--undesired-p (version)
(let* ((vlist (if (listp version) version (version-to-list version))))
(and (= (length vlist) 2)
(> (cl-first vlist) 20101231)
(>= (cl-second vlist) 0)
(<= (cl-second vlist) 2359))))
(defun shmelpa--guess-version (version)
(condition-case err
(progn
(version-to-list version)
version)
(error (when (cl-search "Invalid version" (error-message-string err))
(when (string-match "^\\([0-9.]+\\)" version)
(match-string 1 version))))))
(defun shmelpa--genuine-version ()
(cl-flet ((strip-rcs-id
(str)
(when str
(if (string-match "\\`[ \t]*[$]Revision:[ \t]+" str)
(substring str (match-end 0))
str))))
(let ((version (shmelpa--guess-version (strip-rcs-id (lm-header "version"))))
(pkg-version (shmelpa--guess-version (strip-rcs-id (lm-header "package-version")))))
(cond ((and pkg-version (not (shmelpa--undesired-p pkg-version)))
pkg-version)
(t version)))))
(defun shmelpa--buffer-info ()
(let ((foo (lambda (args)
(when (and (stringp (car args))
(string= (car args) "package-version"))
(setcar args "version"))
args)))
;; later: `foo' cannot help since `package-desc-version'
;; returns a version-list. Resort to `shmelpa--genuine-version'.
(unwind-protect
(progn
(add-function :filter-args (symbol-function 'lm-header) foo)
(package-buffer-info))
(remove-function (symbol-function 'lm-header) foo))))
(defun shmelpa--untar-buffer ()
"Counterpart `package-untar-buffer'."
(tar-mode)
(let ((data-buf (if (tar-data-swapped-p)
tar-data-buffer
(current-buffer))))
(with-current-buffer data-buf
(cl-assert (not enable-multibyte-characters)))
(cl-loop with version
for descriptor in tar-parse-info
do (let* ((name (tar-header-name descriptor))
(dir-p (eq (tar-header-link-type descriptor) 5))
(start (tar-header-data-start descriptor))
(end (+ start (tar-header-size descriptor))))
(when (and (not dir-p)
(string= "el" (file-name-extension name)))
(with-current-buffer data-buf
(let ((body (buffer-substring-no-properties start end)))
(with-temp-buffer
(save-excursion (insert body))
(when-let ((candidate (shmelpa--genuine-version)))
(when (or (null version)
(string> candidate version))
(setq version candidate))))))))
finally return version)))
(defun shmelpa--winnow (contents)
(cl-flet ((winnow-p
(name-desc)
(cl-destructuring-bind (name . desc)
name-desc
(let ((pkg-version (package--ac-desc-version desc)))
(or (version-list-< pkg-version
(version-to-list "20130101"))
(memq name shmelpa--problematic-longlink))))))
(cons (car contents) (cl-remove-if #'winnow-p (cdr contents)))))
(defun shmelpa--file-to-sexpr (infile)
(read (with-temp-buffer
(let ((coding-system-for-read 'utf-8))
(insert-file-contents infile)
(buffer-string)))))
(defun shmelpa--doctor-tar (name src dest undesired desired)
(save-excursion
(let (large-file-warning-threshold)
(find-file src))
(write-file dest)
(unwind-protect
(cl-loop
do (when-let* ((descriptor (tar-current-descriptor t))
(oname (tar-header-name descriptor))
(dirname (file-name-directory oname))
(therest (cl-subseq oname (length dirname)))
(nname
(concat (replace-regexp-in-string
(regexp-quote undesired)
desired dirname nil 'literal)
therest)))
(condition-case err
(tar-rename-entry nname)
(error (message "tar-rename-entry[%s]: %s" name (error-message-string err))))
(when (string= (file-name-nondirectory oname)
(package--description-file (file-name-directory oname)))
(save-excursion
(tar-extract)
(when tar-subfile-mode
(unwind-protect
(progn
(save-excursion
(while (re-search-forward
(regexp-quote
(concat "\"" (cl-subseq undesired (1+ (length name))) "\""))
nil t)
(replace-match
(concat "\"" (cl-subseq desired (1+ (length name))) "\""))))
(save-buffer))
(let (kill-buffer-query-functions)
(ignore-errors (kill-buffer))))))))
until (not (zerop (forward-line)))
finally do (save-buffer))
(let (kill-buffer-query-functions)
(ignore-errors (kill-buffer))))))
(defun shmelpa-doctor-one-deliverable (name file kind undesired desired)
(let* ((src (expand-file-name file shmelpa-targets-dir))
(srcr (expand-file-name (concat name "-readme.txt") shmelpa-targets-dir))
(destr (expand-file-name (concat name "-readme.txt") shmelpa-packages-dir))
(nfile (replace-regexp-in-string
(regexp-quote undesired)
desired file nil 'literal))
(dest (expand-file-name nfile shmelpa-packages-dir))
(to-delete (cl-remove-if
(lambda (f) (member f (list src dest srcr destr)))
(directory-files (file-name-directory dest) t
(concat "^" (regexp-quote name) "-[^-]+$")))))
(mapc #'delete-file to-delete)
(when (and (file-exists-p srcr) (not (string= srcr destr)))
(copy-file srcr destr t))
(when (file-exists-p src)
(pcase kind
('tar (shmelpa--doctor-tar name src dest undesired desired))
('single (let ((coding-system-for-write 'raw-text))
(with-temp-file dest
(let ((coding-system-for-read 'utf-8))
(save-excursion
(insert-file-contents src))
(save-excursion
(while (re-search-forward
(regexp-quote (cl-subseq undesired (1+ (length name))))
nil t)
(replace-match (cl-subseq desired (1+ (length name))))))))))))))
(defun shmelpa-check-one-deliverable (name file kind undesired desired)
(let* ((src (expand-file-name file shmelpa-targets-dir))
(srcr (expand-file-name (concat name "-readme.txt") shmelpa-targets-dir))
(destr (expand-file-name (concat name "-readme.txt") shmelpa-packages-dir))
(nfile (replace-regexp-in-string
(regexp-quote undesired)
desired file nil 'literal))
(dest (expand-file-name nfile shmelpa-packages-dir)))
(cl-every #'file-exists-p (list src dest srcr destr))))
(cl-defun shmelpa-check-deliverables ()
(cl-loop with bad
with contents = (shmelpa--file-to-sexpr shmelpa-final-file)
for (name . desc) in (cdr contents)
for desired = (package--ac-desc-version desc)
for undesired = (nthcdr (- (length desired) 2) desired)
unless (shmelpa--undesired-p desired)
do (let* ((kind (package--ac-desc-kind desc))
(desired-desc
(package-desc-create
:name name
:version desired
:kind kind))
(undesired-desc
(package-desc-create
:name name
:version undesired
:kind kind)))
(unless (shmelpa-check-one-deliverable
(symbol-name name)
(concat (package-desc-full-name undesired-desc)
(package-desc-suffix undesired-desc))
kind
(package-desc-full-name undesired-desc)
(package-desc-full-name desired-desc))
(push name bad)))
finally return bad))
(defun shmelpa-all-contents ()
(mapcar #'car (cdr (shmelpa--file-to-sexpr shmelpa-final-file))))
(cl-defun shmelpa-doctor-contents (infile outfile midfile &key specific (at-most most-positive-fixnum))
(let ((package-archives '(("shmelpa" . "https://shmelpa.commandlinesystems.com/packages/")))
(ocontents (and (file-exists-p midfile)
(cdr (shmelpa--file-to-sexpr midfile))))
(contents (shmelpa--file-to-sexpr infile)))
(cl-loop with nchanged = 0
for (name . desc) in (cdr contents)
for version = (let ((full (package--ac-desc-version desc)))
(nthcdr (- (length full) 2) full))
for odesc = (alist-get name ocontents)
for full-oversion = (when odesc (package--ac-desc-version odesc))
for oversion = (when full-oversion (nthcdr (- (length full-oversion) 2) full-oversion))
if full-oversion
do (setf (package--ac-desc-version desc) full-oversion)
end
if (and (< nchanged at-most)
(if specific
(memq name specific)
(or (null oversion)
(not (version-list-= version oversion))
(shmelpa--undesired-p full-oversion))))
do (let* ((pkg-desc
(package-desc-create
:name name
:version version
:reqs (package--ac-desc-reqs desc)
:summary (package--ac-desc-summary desc)
:kind (package--ac-desc-kind desc)
:archive (cl-first (car package-archives))
:extras (and (> (length desc) 4)
;; Older archive-contents files have only 4
;; elements here.
(package--ac-desc-extras desc))))
(location (file-name-directory (directory-file-name (package-archive-base pkg-desc))))
(commit (alist-get :commit (package-desc-extras pkg-desc)))
(undesired (package-desc-full-name pkg-desc))
(file (concat undesired (package-desc-suffix pkg-desc)))
(url (directory-file-name (concat location file)))
(save-silently t))
(message "%s" url)
(if-let ((buffer (url-retrieve-synchronously url t nil 5)))
(unwind-protect
(condition-case-unless-debug err
(package--with-response-buffer location :file file
(let* ((version*
(or (pcase (package-desc-kind pkg-desc)
('tar
(let* ((easy (or (shmelpa--untar-buffer) ""))
(pkg-version
(or (when (or (zerop (length easy))
(memq name shmelpa--outliers-check-pkg))
(shmelpa--pkg-el-version name commit))
"")))
(if (string< easy pkg-version)
pkg-version
(unless (zerop (length easy))
easy))))
('single
(shmelpa--genuine-version)))
shmelpa-inception))
(vlist (version-to-list version*))
(version ;; 6.0z => 6.0.26
(if (and (> (car (last vlist)) 0)
(not (string-match-p
"[0-9]"
(cl-subseq version* -1))))
(package-version-join vlist)
version*))
(shmelpa-version*
(format "%s%s%s"
version
(if (= (car (last vlist)) -4)
""
(if (< (car (last vlist)) 0)
"1-snapshot"
(concat version-separator "1-snapshot")))
(mapconcat (apply-partially #'format "%s")
(package-desc-version pkg-desc) ".")))
(shmelpa-version
(package-version-join
(version-to-list shmelpa-version*))))
(shmelpa-doctor-one-deliverable
(symbol-name name)
file
(package-desc-kind pkg-desc)
undesired
(package-desc-full-name
(package-desc-create
:name name
:version (version-to-list shmelpa-version))))
(setf (package--ac-desc-version desc)
(version-to-list shmelpa-version))
(cl-incf nchanged)))
(error (message "next time then (%s): %s"
name (error-message-string err))))
(let (kill-buffer-query-functions)
(ignore-errors (kill-buffer buffer))))
(message "url-retrieve (%s): no response" url)))
end)
(write-region (pp-to-string contents) nil outfile nil 'silent)))
(cl-defun shmelpa-doit (&optional ingest-p at-most)
(when ingest-p
(shmelpa-ingest))
(shmelpa-doctor-contents
shmelpa-ingested-file
shmelpa-staging-file
shmelpa-final-file :at-most (or at-most 30)))
(defun shmelpa--gnutls-advice (f &rest args)
(let ((gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3"))
(apply f args)))
(defun shmelpa-test-install ()
(let* (package-alist
package-archive-contents
(user-emacs-directory "/var/tmp")
(package-user-dir (locate-user-emacs-file "elpa"))
(load-path load-path)
(package-archives '(("gnu" . "https://elpa.gnu.org/packages/")
("shmelpa" . "https://shmelpa.commandlinesystems.com/packages/")))
(_ (package-refresh-contents))
(lst
(cl-remove-if-not
(lambda (desc) (string= (package-desc-archive desc) "shmelpa"))
(mapcar #'cl-second package-archive-contents))))
(cl-letf (((symbol-function 'package-activate-1) #'ignore))
(dolist (shmelp (list (car lst)))
(let ((key (intern (package-desc-full-name shmelp))))
(condition-case err
(package-install shmelp)
(error (setf (alist-get key shmelpa--outliers)
(error-message-string err))))
(unless (alist-get key shmelpa--outliers)
(let ((package-table
(mapcar
(lambda (p) (cons (package-desc-full-name p) p))
(delq nil
(mapcar (lambda (p) (unless (package-built-in-p p) p))
(apply #'append (mapcar #'cdr (package--alist))))))))
(dolist (desc (mapcar #'cdr package-table))
(package-delete desc t nil)))))))))
(when (< emacs-major-version 27)
(condition-case err
(progn (advice-remove 'url-retrieve-synchronously #'shmelpa--gnutls-advice)
(advice-add 'url-retrieve-synchronously :around
#'shmelpa--gnutls-advice))
(error
(advice-remove 'url-retrieve-synchronously #'shmelpa--gnutls-advice)
(display-warning 'error
(format "advice aborted: %s"
(error-message-string err))))))