-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnu_elpa_mirror.py
executable file
Β·612 lines (569 loc) Β· 20.1 KB
/
gnu_elpa_mirror.py
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
#!/usr/bin/env python3
import argparse
from dataclasses import dataclass
from datetime import datetime
import json
import os
from pathlib import Path
import re
import shutil
import subprocess
import sys
import tempfile
from typing import Any
import dotenv
import github
import requests
os.chdir(os.path.dirname(__file__))
dotenv.load_dotenv()
def remove_prefix(prefix, string):
if string.startswith(prefix):
return string[len(prefix) :]
else:
return string
def log(message):
print(message, file=sys.stderr)
def die(message):
log("gnu_elpa_mirror: " + message)
sys.exit(1)
try:
ACCESS_TOKEN = os.environ["ACCESS_TOKEN"]
except KeyError:
die("please export ACCESS_TOKEN to a valid GitHub API token")
WEBHOOK_URL = os.environ.get("WEBHOOK_URL")
def clone_git_repo(
git_url,
repo_dir,
*,
private_url,
bare=False,
exclude_patterns=[],
additional_refspecs=[],
recursive=False,
):
# Basically reimplement --mirror ourselves because it is the most
# elegant way to solve https://stackoverflow.com/a/54413257/3538165.
# We previously used --bare instead, but that doesn't do anything
# like what we actually want, and I clearly wasn't thinking very
# hard when I wrote the original code.
if not repo_dir.is_dir():
cmd = ["git", "init"]
if bare:
cmd += ["--bare"]
cmd += [repo_dir]
subprocess.run(cmd, check=True)
try:
subprocess.run(
[
"git",
"fetch",
"--prune",
"--force",
"--update-head-ok",
git_url,
"+refs/heads/*:refs/heads/*",
"+refs/tags/*:refs/tags/*",
"+refs/change/*:refs/change/*",
*additional_refspecs,
],
cwd=repo_dir,
check=True,
)
except subprocess.CalledProcessError:
if private_url:
die("cloning repository failed (details omitted for security)")
raise
try:
result = subprocess.run(
["git", "ls-remote", "--symref", git_url, "HEAD"],
cwd=repo_dir,
stdout=subprocess.PIPE,
check=True,
)
output = result.stdout.decode().splitlines()
if not output:
# Probably a new/empty repository
return
match = re.fullmatch(
r"ref: (refs/heads/.+?)\s+HEAD",
output[0],
)
if not match:
die("failed to parse ls-remote output: " + "\n".join(output))
remote_head = match.group(1) # type: ignore
except subprocess.CalledProcessError:
if private_url:
die("determining remote HEAD failed (details omitted for security)")
raise
if bare:
subprocess.run(
["git", "symbolic-ref", "HEAD", remote_head], cwd=repo_dir, check=True
)
else:
local_head = remote_head.removeprefix("refs/heads/")
subprocess.run(
["git", "checkout", "-B", local_head, remote_head, "--force"],
cwd=repo_dir,
check=True,
)
subprocess.run(
[
"git",
"clean",
"-ffdx",
*("--exclude=" + pat for pat in exclude_patterns),
],
cwd=repo_dir,
check=True,
)
if recursive:
subprocess.run(
[
"git",
"submodule",
"update",
"--init",
"--recursive",
"--checkout",
"--force",
],
cwd=repo_dir,
check=True,
)
def push_git_repo(git_url, repo_dir, repo_obj):
try:
subprocess.run(
[
"git",
"push",
"--prune",
"--force",
git_url,
"+refs/heads/*:refs/heads/*",
"+refs/tags/*:refs/tags/*",
"+refs/change/*:refs/change/*",
],
cwd=repo_dir,
check=True,
)
except subprocess.CalledProcessError:
die("cloning repository failed (details omitted for security)")
branch = (
subprocess.run(
["git", "symbolic-ref", "HEAD"],
stdout=subprocess.PIPE,
check=True,
cwd=repo_dir,
)
.stdout.decode()
.strip()
.removeprefix("refs/heads/")
)
repo_obj.edit(default_branch=branch)
def delete_contents(path):
for entry in sorted(path.iterdir()):
if entry.name == ".git":
continue
if entry.is_dir() and not entry.is_symlink():
shutil.rmtree(entry)
else:
try:
entry.unlink()
except FileNotFoundError:
pass
def stage_and_commit(repo_dir, message):
# Note the use of --force because some packages like AUCTeX need
# files to be checked into version control that are nevertheless
# in their .gitignore. See [1].
#
# [1]: https://github.com/raxod502/straight.el/issues/299
subprocess.run(["git", "add", "--all", "--force"], cwd=repo_dir, check=True)
anything_staged = (
subprocess.run(["git", "diff", "--cached", "--quiet"], cwd=repo_dir).returncode
!= 0
)
if anything_staged:
subprocess.run(
[
"git",
"-c",
"user.name=GNU ELPA Mirror Bot",
"-c",
"user.email=contact+gnu-elpa-mirror@radian.codes",
"commit",
"-m",
message,
],
cwd=repo_dir,
check=True,
)
else:
log("(no changes)")
THIS_DIR = Path(".").resolve()
REPOS_SUBDIR = THIS_DIR / "repos"
GNU_ELPA_SUBDIR = THIS_DIR / "gnu-elpa"
@dataclass
class ELPAPackage:
name: str
version: str
@property
def tarball_name(self) -> str:
return f"{self.name}-{self.version}.tar"
@property
def tarball_url(self) -> str:
return f"https://elpa.gnu.org/devel/{self.tarball_name}"
def make_commit_message(
message: str, timestamp: datetime, pkg: ELPAPackage | None = None
):
message += f"\n\nTimestamp: {timestamp.strftime('%Y-%m-%d %H:%M:%S')}"
if pkg:
message += f"\nSourced from {pkg.name} version {pkg.version} on GNU ELPA Devel"
message += f"\n(see https://elpa.gnu.org/devel/{pkg.name}.html)"
return message
def read_elpa_index(path: Path) -> Any:
# WARNING: path basename can't have special chars
path = path.resolve()
return json.loads(
subprocess.run(
[
"emacs",
"-Q",
"--batch",
"-l",
"package",
"-l",
"json",
"--eval",
f"""\
(with-temp-buffer
(insert-file-contents "{path.name}")
(princ
(json-encode
(mapcar
(lambda (spec)
(cons
(car spec)
(package-version-join (aref (cdr spec) 0))))
(seq-filter #'listp (read (current-buffer)))))))
""",
],
stdout=subprocess.PIPE,
cwd=path.parent,
).stdout.decode()
)
def get_elpa_contents(archive_url: str) -> list[ELPAPackage]:
if not archive_url.endswith("/"):
archive_url += "/"
resp = requests.get(archive_url + "archive-contents")
resp.raise_for_status()
with tempfile.NamedTemporaryFile("w") as f:
f.write(resp.text)
f.flush()
data = read_elpa_index(Path(f.name))
return [
ELPAPackage(name, version)
for name, version in data.items()
# Denylist some special names to make sure there is nothing
# unrelated that gets accidentally overwritten by somebody
# publishing a naughty package on GNU ELPA.
if name
not in {"gnu-elpa-mirror", "epkgs", "emacsmirror-mirror", "org-mode", "elpa"}
]
def mirror_gnu_elpa(args, api, existing_repos):
package_filter = (
lambda name: name == args.mirror_only_one or not args.mirror_only_one
)
log("--> check timestamp and commit hashes")
timestamp = datetime.now()
commit_data = {
"timestamp": timestamp,
}
log("--> check GNU ELPA archive index")
elpa_packages = get_elpa_contents("https://elpa.gnu.org/devel/")
log("--> download GNU ELPA tarballs")
GNU_ELPA_SUBDIR.mkdir(exist_ok=True)
existing_tarballs = set(os.listdir(GNU_ELPA_SUBDIR))
for pkg in elpa_packages:
if not package_filter(pkg.name):
continue
if pkg.tarball_name in existing_tarballs:
continue
log(f"----> download {pkg.tarball_url}")
resp = requests.get(pkg.tarball_url, stream=True)
resp.raise_for_status()
with open(GNU_ELPA_SUBDIR / pkg.tarball_name, "wb") as f:
for chunk in resp.iter_content(10 * 1024):
f.write(chunk)
log("--> clone/update mirror repositories")
org = api.get_organization("emacs-straight")
REPOS_SUBDIR.mkdir(exist_ok=True)
for pkg in elpa_packages:
if not package_filter(pkg.name):
continue
github_package = pkg.name.replace("+", "-plus")
git_url = "https://raxod502:{}@github.com/emacs-straight/{}.git".format(
ACCESS_TOKEN, github_package
)
repo_dir = REPOS_SUBDIR / pkg.name
if github_package not in existing_repos:
log("----> create mirror repository {}".format(pkg.name))
org.create_repo(
github_package,
description=("Mirror of the {} package from GNU ELPA".format(pkg.name)),
homepage=("https://elpa.gnu.org/packages/{}.html".format(pkg.name)),
has_issues=False,
has_wiki=False,
has_projects=False,
auto_init=False,
)
if args.skip_mirror_pulls and repo_dir.is_dir():
continue
log("----> clone/update mirror repository {}".format(pkg.name))
clone_git_repo(git_url, repo_dir, private_url=True)
log("--> update mirrored packages")
for pkg in elpa_packages:
if not package_filter(pkg.name):
continue
log("----> update package {}".format(pkg.name))
repo_dir = REPOS_SUBDIR / pkg.name
delete_contents(repo_dir)
subprocess.run(
[
"tar",
"-C",
str(repo_dir),
"-xf",
str(GNU_ELPA_SUBDIR / pkg.tarball_name),
"--strip-components=1",
],
check=True,
)
# Remove files that may make GitHub interpret this repo
# specially, as it should just be a static fork with the
# packaging files.
try:
shutil.rmtree(repo_dir / ".github")
except FileNotFoundError:
pass
# Add a file to tell people not to file pull requests.
pr_template = repo_dir / ".github" / "PULL_REQUEST_TEMPLATE.md"
pr_template.parent.mkdir()
with open(pr_template, "w") as f:
f.write(
":warning: This repo is a read-only mirror. Please submit changes upstream instead :warning:\n"
)
stage_and_commit(
repo_dir, make_commit_message("Update " + pkg.name, timestamp, pkg)
)
if not args.skip_mirror_pushes:
log("--> push changes to mirrored packages")
for pkg in elpa_packages:
if not package_filter(pkg.name):
continue
log("----> push changes to package {}".format(pkg.name))
repo_dir = REPOS_SUBDIR / pkg.name
github_package = pkg.name.replace("+", "-plus")
git_url = "https://raxod502:{}@github.com/emacs-straight/{}.git".format(
ACCESS_TOKEN, github_package
)
repo_obj = org.get_repo(github_package)
push_git_repo(git_url, repo_dir, repo_obj)
log("----> update repo description for package {}".format(pkg.name))
repo_obj.edit(
description="Mirror of the {} package from GNU ELPA, current as of {}".format(
pkg.name,
timestamp.strftime("%Y-%m-%d"),
)
)
if not args.skip_mirror_index:
git_url = "https://raxod502:{}@github.com/emacs-straight/{}.git".format(
ACCESS_TOKEN, "gnu-elpa-mirror"
)
repo_dir = REPOS_SUBDIR / "gnu-elpa-mirror"
if "gnu-elpa-mirror" not in existing_repos:
log("--> create mirror list repository")
org.create_repo(
"gnu-elpa-mirror",
description="List packages mirrored from GNU ELPA",
homepage="https://elpa.gnu.org/packages/",
has_issues=False,
has_wiki=False,
has_projects=False,
auto_init=False,
)
log("--> clone/update mirror list repository")
clone_git_repo(git_url, repo_dir, private_url=True)
log("--> update mirror list repository")
delete_contents(repo_dir)
for pkg in elpa_packages:
with open(repo_dir / pkg.name, "w"):
pass
stage_and_commit(repo_dir, make_commit_message("Update mirror list", timestamp))
log("--> push changes to mirror list repository")
repo = org.get_repo("gnu-elpa-mirror")
push_git_repo(git_url, repo_dir, repo_obj=repo)
log("--> update repo description for mirror list repository")
repo.edit(description="List packages mirrored from GNU ELPA")
def mirror_emacsmirror(_, api, existing_repos):
org = api.get_organization("emacs-straight")
epkgs_dir = REPOS_SUBDIR / "epkgs"
epkgs_git_url = "https://github.com/emacsmirror/epkgs.git"
epkgs_mirror_dir = REPOS_SUBDIR / "emacsmirror-mirror"
epkgs_mirror_git_url = (
"https://raxod502:{}@github.com/emacs-straight/emacsmirror-mirror.git".format(
ACCESS_TOKEN
)
)
log("--> clone/update Emacsmirror")
clone_git_repo(epkgs_git_url, epkgs_dir, private_url=False)
if "emacsmirror-mirror" not in existing_repos:
log("--> create Emacsmirror mirror repository")
org.create_repo(
"emacsmirror-mirror",
description="Light-weight mirror of the Emacsmirror index",
homepage="https://github.com/emacsmirror/epkgs",
has_issues=False,
has_wiki=False,
has_projects=False,
auto_init=False,
)
log("--> clone/update Emacsmirror mirror repository")
clone_git_repo(
epkgs_mirror_git_url,
epkgs_mirror_dir,
private_url=True,
)
log("--> update Emacsmirror mirror")
delete_contents(epkgs_mirror_dir)
attic_file = epkgs_mirror_dir / "attic"
mirror_file = epkgs_mirror_dir / "mirror"
num_attic = 0
num_mirror = 0
with open(attic_file, "w") as attic, open(mirror_file, "w") as mirror:
with open(epkgs_dir / ".gitmodules") as gitmodules:
for line in gitmodules:
m = re.fullmatch(
"|".join(
regex + r"\n"
for regex in (
r'\[submodule "[^"]+"\]',
r"\tpath = .+",
r"\turl = https://git.savannah.gnu.org/git/emacs/elpa(?:\.git)?",
r"\turl = https://git.savannah.gnu.org/git/emacs/nongnu(?:\.git)?",
r"\turl = https://code.orgmode.org/bzg/org-mode(?:\.git)?",
r"\turl = git@github.com:(?P<org1>[^/]+)/(?P<repo1>.+?)(?:\.git)?",
r"\turl = https://github.com/(?P<org2>[^/]+)/(?P<repo2>.+?)(?:\.git)?",
r"\tbranch = .+",
)
),
line,
)
assert m, line
orgname = m.group("org1") or m.group("org2")
name = m.group("repo1") or m.group("repo2")
if name == "sql-ident":
# Jonas made a typo and included a spurious
# submodule called sql-ident in addition to the
# real sql-indent one. Filter it out.
continue
if orgname == "melpa" and name == "melpa":
continue
elif orgname == "emacsmirror" and name == "emacswiki.org":
continue
elif orgname == "emacsattic":
attic.write(name + "\n")
num_attic += 1
elif orgname == "emacsmirror":
mirror.write(name + "\n")
num_mirror += 1
elif orgname is None:
continue
else:
assert False, line
assert num_attic >= 500 and num_mirror >= 1000
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
epkgs_commit = (
subprocess.run(
["git", "rev-parse", "HEAD"],
cwd=epkgs_dir,
stdout=subprocess.PIPE,
check=True,
)
.stdout.decode()
.strip()
)
stage_and_commit(
epkgs_mirror_dir,
"Update Emacsmirror mirror\n\nTimestamp: {}\nEmacsmirror commit: {}".format(
timestamp, epkgs_commit
),
)
log("--> push changes to Emacsmirror mirror repository")
repo = org.get_repo("emacsmirror-mirror")
push_git_repo(epkgs_mirror_git_url, epkgs_mirror_dir, repo_obj=repo)
log("--> update repo description for Emacsmirror mirror repository")
repo.edit(description="Light-weight mirror of the Emacsmirror index")
def mirror_orgmode(_, api, existing_repos):
brief_timestamp = datetime.now().strftime("%Y-%m-%d")
org = api.get_organization("emacs-straight")
orgmode_dir = REPOS_SUBDIR / "org-mode"
orgmode_git_url = "https://git.savannah.gnu.org/git/emacs/org-mode.git"
orgmode_mirror_git_url = (
"https://raxod502:{}@github.com/emacs-straight/org-mode.git".format(
ACCESS_TOKEN
)
)
log("--> clone/update Org")
clone_git_repo(
orgmode_git_url,
orgmode_dir,
private_url=False,
bare=True,
)
if "org-mode" not in existing_repos:
log("--> create org-mode repository")
org.create_repo(
"org-mode",
description="Mirror of org-mode from Savannah",
homepage="https://git.savannah.gnu.org/git/emacs/org-mode.git",
has_issues=False,
has_wiki=False,
has_projects=False,
auto_init=False,
)
repo = org.get_repo("org-mode")
log("--> push org-mode repository")
push_git_repo(orgmode_mirror_git_url, orgmode_dir, repo_obj=repo)
log("--> update repo description for Org")
repo.edit(
description="Mirror of org-mode from Savannah, current as of {}".format(
brief_timestamp,
)
)
def mirror():
parser = argparse.ArgumentParser()
parser.add_argument("--skip-gnu-elpa", action="store_true")
parser.add_argument("--skip-emacsmirror", action="store_true")
parser.add_argument("--skip-mirror-index", action="store_true")
parser.add_argument("--skip-mirror-pulls", action="store_true")
parser.add_argument("--skip-mirror-pushes", action="store_true")
parser.add_argument("--skip-orgmode", action="store_true")
parser.add_argument("--mirror-only-one", type=str)
args = parser.parse_args()
api = github.Github(ACCESS_TOKEN)
log("--> get list of mirror repositories")
existing_repos = []
for repo in api.get_user("emacs-straight").get_repos():
existing_repos.append(repo.name)
if not args.skip_gnu_elpa:
mirror_gnu_elpa(args, api, existing_repos)
if not args.skip_emacsmirror:
mirror_emacsmirror(args, api, existing_repos)
if not args.skip_orgmode:
mirror_orgmode(args, api, existing_repos)
if WEBHOOK_URL:
log("--> update webhook")
resp = requests.get(WEBHOOK_URL)
log(resp)
if __name__ == "__main__":
mirror()