forked from python/release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_release.py
executable file
·1225 lines (984 loc) · 41.9 KB
/
run_release.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
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
#!/usr/bin/env python3
"""An automatic engine for Python releases
Original code by Pablo Galindo
"""
from __future__ import annotations
import argparse
import asyncio
import builtins
import contextlib
import functools
import getpass
import json
import os
import pathlib
import re
import shelve
import shutil
import subprocess
import sys
import time
import urllib.request
from dataclasses import dataclass
from shelve import DbfilenameShelf
from typing import Any, Callable, Generator, Iterator
import aiohttp
import gnupg
import paramiko
import sigstore.oidc
from alive_progress import alive_bar
import release as release_mod
import sbom
from buildbotapi import BuildBotAPI, Builder
API_KEY_REGEXP = re.compile(r"(?P<major>\w+):(?P<minor>\w+)")
RELEASE_REGEXP = re.compile(
r"(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\.?(?P<extra>.*)?"
)
DOWNLOADS_SERVER = "downloads.nyc1.psf.io"
DOCS_SERVER = "docs.nyc1.psf.io"
WHATS_NEW_TEMPLATE = """
****************************
What's New In Python {version}
****************************
:Editor: TBD
.. Rules for maintenance:
* Anyone can add text to this document. Do not spend very much time
on the wording of your changes, because your text will probably
get rewritten to some degree.
* The maintainer will go through Misc/NEWS periodically and add
changes; it's therefore more important to add your changes to
Misc/NEWS than to this file.
* This is not a complete list of every single change; completeness
is the purpose of Misc/NEWS. Some changes I consider too small
or esoteric to include. If such a change is added to the text,
I'll just remove it. (This is another reason you shouldn't spend
too much time on writing your addition.)
* If you want to draw your new text to the attention of the
maintainer, add 'XXX' to the beginning of the paragraph or
section.
* It's OK to just add a fragmentary note about a change. For
example: "XXX Describe the transmogrify() function added to the
socket module." The maintainer will research the change and
write the necessary text.
* You can comment out your additions if you like, but it's not
necessary (especially when a final release is some months away).
* Credit the author of a patch or bugfix. Just the name is
sufficient; the e-mail address isn't necessary.
* It's helpful to add the issue number as a comment:
XXX Describe the transmogrify() function added to the socket
module.
(Contributed by P.Y. Developer in :gh:`12345`.)
This saves the maintainer the effort of going through the VCS log
when researching a change.
This article explains the new features in Python {version}, compared to {prev_version}.
For full details, see the :ref:`changelog <changelog>`.
.. note::
Prerelease users should be aware that this document is currently in draft
form. It will be updated substantially as Python {version} moves towards release,
so it's worth checking back even after reading earlier versions.
Summary -- Release highlights
=============================
.. This section singles out the most important changes in Python {version}.
Brevity is key.
.. PEP-sized items next.
New Features
============
Other Language Changes
======================
New Modules
===========
* None yet.
Improved Modules
================
Optimizations
=============
Deprecated
==========
Removed
=======
Porting to Python {version}
======================
This section lists previously described changes and other bugfixes
that may require changes to your code.
Build Changes
=============
C API Changes
=============
New Features
------------
Porting to Python {version}
----------------------
Deprecated
----------
Removed
-------
"""
@dataclass
class Task:
function: Callable[[DbfilenameShelf], None]
description: str
def __call__(self, db: DbfilenameShelf) -> Any:
return getattr(self, "function")(db)
class ReleaseException(Exception):
"""An error happened in the release process"""
class ReleaseDriver:
def __init__(
self,
tasks: list[Task],
*,
release_tag: release_mod.Tag,
git_repo: str,
api_key: str,
ssh_user: str,
first_state: Task | None = None,
) -> None:
self.tasks = tasks
dbfile = pathlib.Path.home() / ".python_release"
self.db = shelve.open(str(dbfile), "c")
if not self.db.get("finished"):
self.db["finished"] = False
else:
self.db.close()
self.db = shelve.open(str(dbfile), "n")
self.current_task: Task | None = first_state
self.completed_tasks = self.db.get("completed_tasks", [])
self.remaining_tasks = iter(tasks[len(self.completed_tasks) :])
if self.db.get("gpg_key"):
os.environ["GPG_KEY_FOR_RELEASE"] = self.db["gpg_key"]
if not self.db.get("git_repo"):
self.db["git_repo"] = pathlib.Path(git_repo)
if not self.db.get("auth_info"):
self.db["auth_info"] = api_key
if not self.db.get("ssh_user"):
self.db["ssh_user"] = ssh_user
if not self.db.get("release"):
self.db["release"] = release_tag
print("Release data: ")
print(f"- Branch: {release_tag.branch}")
print(f"- Release tag: {self.db['release']}")
print(f"- Normalized release tag: {release_tag.normalized()}")
print(f"- Git repo: {self.db['git_repo']}")
print(f"- SSH username: {self.db['ssh_user']}")
print(f"- python.org API key : {self.db['auth_info']}")
print()
def checkpoint(self) -> None:
self.db["completed_tasks"] = self.completed_tasks
def run(self) -> None:
for task in self.completed_tasks:
print(f"✅ {task.description}")
self.current_task = next(self.remaining_tasks, None)
while self.current_task is not None:
self.checkpoint()
try:
self.current_task(self.db)
except Exception as e:
print(f"\r💥 {self.current_task.description}")
raise e from None
print(f"\r✅ {self.current_task.description}")
self.completed_tasks.append(self.current_task)
self.current_task = next(self.remaining_tasks, None)
self.db["finished"] = True
print()
print(f"Congratulations, Python {self.db['release']} is released 🎉🎉🎉")
def ask_question(question: str) -> bool:
answer = ""
print(question)
while answer not in ("yes", "no"):
answer = input("Enter yes or no: ")
if answer == "yes":
return True
elif answer == "no":
return False
else:
print("Please enter yes or no.")
return True
@contextlib.contextmanager
def cd(path: str) -> Iterator[None]:
current_path = os.getcwd()
os.chdir(path)
yield
os.chdir(current_path)
@contextlib.contextmanager
def supress_print() -> Generator[None, None, None]:
print_func = builtins.print
builtins.print = lambda *args, **kwargs: None
yield
builtins.print = print_func
def check_tool(db: DbfilenameShelf, tool: str) -> None:
if shutil.which(tool) is None:
raise ReleaseException(f"{tool} is not available")
check_git = functools.partial(check_tool, tool="git")
check_make = functools.partial(check_tool, tool="make")
check_blurb = functools.partial(check_tool, tool="blurb")
check_autoconf = functools.partial(check_tool, tool="autoconf")
check_docker = functools.partial(check_tool, tool="docker")
def check_gpg_keys(db: DbfilenameShelf) -> None:
pg = gnupg.GPG()
keys = pg.list_keys(secret=True)
if not keys:
raise ReleaseException("There are no valid GPG keys for release")
for index, key in enumerate(keys):
print(f"{index} - {key['keyid']}: {key['uids']}")
selected_key_index = -1
while not (0 <= selected_key_index < len(keys)):
with contextlib.suppress(ValueError):
selected_key_index = int(
input("Select one GPG key for release (by index):")
)
selected_key = keys[selected_key_index]["keyid"]
os.environ["GPG_KEY_FOR_db['release']"] = selected_key
if selected_key not in {key["keyid"] for key in keys}:
raise ReleaseException("Invalid GPG key selected")
db["gpg_key"] = selected_key
os.environ["GPG_KEY_FOR_RELEASE"] = db["gpg_key"]
def check_ssh_connection(db: DbfilenameShelf) -> None:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect(DOWNLOADS_SERVER, port=22, username=db["ssh_user"])
client.exec_command("pwd")
client.connect(DOCS_SERVER, port=22, username=db["ssh_user"])
client.exec_command("pwd")
def check_buildbots(db: DbfilenameShelf) -> None:
async def _check() -> set[Builder]:
async def _get_builder_status(
buildbot_api: BuildBotAPI, the_builder: Builder
) -> tuple[Builder, bool]:
return the_builder, await buildbot_api.is_builder_failing_currently(
the_builder
)
async with aiohttp.ClientSession() as session:
api = BuildBotAPI(session)
await api.authenticate(token="")
release_branch = db["release"].branch
stable_builders = await api.stable_builders(branch=release_branch)
if not stable_builders:
release_branch = "3.x"
stable_builders = await api.stable_builders(branch="3.x")
if not stable_builders:
raise ReleaseException(
f"Failed to get the stable buildbots for the {release_branch} tag"
)
builders = await asyncio.gather(
*[
_get_builder_status(api, the_builder)
for the_builder in stable_builders.values()
]
)
return {the_builder for (the_builder, is_failing) in builders if is_failing}
failing_builders = asyncio.run(_check())
if not failing_builders:
return
print()
print("The following buildbots are failing:")
for builder in failing_builders:
print(f"- {builder.name}")
print()
print("Check https://buildbot.python.org/all/#/release_status for more information")
print()
if not ask_question("Do you want to continue even if these builders are failing?"):
raise ReleaseException("Buildbots are failing!")
def run_blurb_release(db: DbfilenameShelf) -> None:
subprocess.check_call(["blurb", "release", str(db["release"])], cwd=db["git_repo"])
subprocess.check_call(
["git", "commit", "-m", f"Python {db['release']}"],
cwd=db["git_repo"],
)
def check_cpython_repo_is_clean(db: DbfilenameShelf) -> None:
if subprocess.check_output(["git", "status", "--porcelain"], cwd=db["git_repo"]):
raise ReleaseException("Git repository is not clean")
def preapre_temporary_branch(db: DbfilenameShelf) -> None:
subprocess.check_call(
["git", "checkout", "-b", f"branch-{db['release']}"], cwd=db["git_repo"]
)
def remove_temporary_branch(db: DbfilenameShelf) -> None:
subprocess.check_call(
["git", "branch", "-D", f"branch-{db['release']}"], cwd=db["git_repo"]
)
def prepare_pydoc_topics(db: DbfilenameShelf) -> None:
subprocess.check_call(["make", "pydoc-topics"], cwd=db["git_repo"] / "Doc")
shutil.copy2(
db["git_repo"] / "Doc" / "build" / "pydoc-topics" / "topics.py",
db["git_repo"] / "Lib" / "pydoc_data" / "topics.py",
)
subprocess.check_call(
["git", "commit", "-a", "--amend", "--no-edit"], cwd=db["git_repo"]
)
def run_autoconf(db: DbfilenameShelf) -> None:
# Python 3.12 and newer have a script that runs autoconf.
regen_configure_sh = db["git_repo"] / "Tools/build/regen-configure.sh"
if regen_configure_sh.exists():
subprocess.check_call(
[regen_configure_sh],
cwd=db["git_repo"],
)
# Python 3.11 and prior rely on autoconf built within a container
# in order to maintain stability of autoconf generation.
else:
# Corresponds to the tag '269' and 'cp311'
cpython_autoconf_sha256 = (
"f370fee95eefa3d57b00488bce4911635411fa83e2d293ced8cf8a3674ead939"
)
subprocess.check_call(
[
"docker",
"run",
"--rm",
"--pull=always",
f"-v{db['git_repo']}:/src",
f"quay.io/tiran/cpython_autoconf@sha256:{cpython_autoconf_sha256}",
],
cwd=db["git_repo"],
)
subprocess.check_call(["docker", "rmi", "quay.io/tiran/cpython_autoconf", "-f"])
subprocess.check_call(
["git", "commit", "-a", "--amend", "--no-edit"], cwd=db["git_repo"]
)
def check_pyspecific(db):
with open(
db["git_repo"] / "Doc" / "tools" / "extensions" / "pyspecific.py"
) as pyspecific:
for line in pyspecific:
if "SOURCE_URI =" in line:
break
expected_branch = db["release"].branch
expected = (
f"SOURCE_URI = 'https://github.com/python/cpython/tree/{expected_branch}/%s'"
)
if expected != line.strip():
raise ReleaseException("SOURCE_URI is incorrect")
def bump_version(db: DbfilenameShelf) -> None:
with cd(db["git_repo"]):
release_mod.bump(db["release"])
subprocess.check_call(
["git", "commit", "-a", "--amend", "--no-edit"], cwd=db["git_repo"]
)
def create_tag(db: DbfilenameShelf) -> None:
with cd(db["git_repo"]):
if not release_mod.make_tag(db["release"]):
raise ReleaseException("Error when creating tag")
subprocess.check_call(
["git", "commit", "-a", "--amend", "--no-edit"], cwd=db["git_repo"]
)
def wait_for_source_and_docs_artifacts(db: DbfilenameShelf) -> None:
# Determine if we need to wait for docs or only source artifacts.
release_tag = db["release"]
should_wait_for_docs = release_tag.is_final or release_tag.is_release_candidate
# Create the directory so it's easier to place the artifacts there.
release_path = pathlib.Path(db["git_repo"] / str(release_tag))
release_path.mkdir(parents=True, exist_ok=True)
# Build the list of filepaths we're expecting.
wait_for_paths = [
release_path / "src" / f"Python-{release_tag}.tgz",
release_path / "src" / f"Python-{release_tag}.tar.xz",
]
if should_wait_for_docs:
docs_path = release_path / "docs"
wait_for_paths.extend(
[
docs_path / f"python-{release_tag}-docs.epub",
docs_path / f"python-{release_tag}-docs-html.tar.bz2",
docs_path / f"python-{release_tag}-docs-html.zip",
docs_path / f"python-{release_tag}-docs-pdf-a4.tar.bz2",
docs_path / f"python-{release_tag}-docs-pdf-a4.zip",
docs_path / f"python-{release_tag}-docs-pdf-letter.tar.bz2",
docs_path / f"python-{release_tag}-docs-pdf-letter.zip",
docs_path / f"python-{release_tag}-docs-texinfo.tar.bz2",
docs_path / f"python-{release_tag}-docs-texinfo.zip",
docs_path / f"python-{release_tag}-docs-text.tar.bz2",
docs_path / f"python-{release_tag}-docs-text.zip",
]
)
print(
f"Waiting for source{' and docs' if should_wait_for_docs else ''} artifacts to be built"
)
print(f"Artifacts should be placed at '{release_path}':")
for path in wait_for_paths:
print(f"- '{os.path.relpath(path, release_path)}'")
while not all(path.exists() for path in wait_for_paths):
time.sleep(1)
def sign_source_artifacts(db: DbfilenameShelf) -> None:
print("Signing tarballs with GPG")
uid = os.environ.get("GPG_KEY_FOR_RELEASE")
if not uid:
print("List of available private keys:")
subprocess.check_call('gpg -K | grep -A 1 "^sec"', shell=True)
uid = input("Please enter key ID to use for signing: ")
tarballs_path = pathlib.Path(db["git_repo"] / str(db["release"]) / "src")
tgz = str(tarballs_path / (f"Python-{db['release']}.tgz"))
xz = str(tarballs_path / (f"Python-{db['release']}.tar.xz"))
subprocess.check_call(["gpg", "-bas", "-u", uid, tgz])
subprocess.check_call(["gpg", "-bas", "-u", uid, xz])
print("Signing tarballs with Sigstore")
subprocess.check_call(
[
"python3",
"-m",
"sigstore",
"sign",
"--oidc-disable-ambient-providers",
tgz,
xz,
]
)
def build_sbom_artifacts(db):
# Skip building an SBOM if there isn't a 'Misc/sbom.spdx.json' file.
if not (db["git_repo"] / "Misc/sbom.spdx.json").exists():
print("Skipping building an SBOM, missing 'Misc/sbom.spdx.json'")
return
release_version = db["release"]
# For each source tarball build an SBOM.
for ext in (".tgz", ".tar.xz"):
tarball_name = f"Python-{release_version}{ext}"
tarball_path = str(db["git_repo"] / str(db["release"]) / "src" / tarball_name)
print(f"Building an SBOM for artifact '{tarball_name}'")
sbom_data = sbom.create_sbom_for_source_tarball(tarball_path)
with open(tarball_path + ".spdx.json", mode="w") as f:
f.write(json.dumps(sbom_data, indent=2, sort_keys=True))
class MySFTPClient(paramiko.SFTPClient):
def put_dir(self, source: str, target: str, progress: Any = None) -> None:
for item in os.listdir(source):
if os.path.isfile(os.path.join(source, item)):
progress.text(item)
self.put(os.path.join(source, item), f"{target}/{item}")
progress()
else:
self.mkdir(f"{target}/{item}", ignore_existing=True)
self.put_dir(
os.path.join(source, item),
f"{target}/{item}",
progress=progress,
)
def mkdir(self, path: str, mode: int = 511, ignore_existing: bool = False) -> None:
try:
super().mkdir(path, mode)
except OSError:
if ignore_existing:
pass
else:
raise
def upload_files_to_server(db: DbfilenameShelf) -> None:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect(DOWNLOADS_SERVER, port=22, username=db["ssh_user"])
destination = pathlib.Path(f"/home/psf-users/{db['ssh_user']}/{db['release']}")
ftp_client = MySFTPClient.from_transport(client.get_transport())
client.exec_command(f"rm -rf {destination}")
with contextlib.suppress(OSError):
ftp_client.mkdir(str(destination))
artifacts_path = pathlib.Path(db["git_repo"] / str(db["release"]))
shutil.rmtree(artifacts_path / f"Python-{db['release']}", ignore_errors=True)
def upload_subdir(subdir: str) -> None:
with contextlib.suppress(OSError):
ftp_client.mkdir(str(destination / subdir))
with alive_bar(len(tuple((artifacts_path / subdir).glob("**/*")))) as progress:
ftp_client.put_dir(
artifacts_path / subdir,
str(destination / subdir),
progress=progress,
)
upload_subdir("src")
if (artifacts_path / "docs").exists():
upload_subdir("docs")
ftp_client.close()
def place_files_in_download_folder(db: DbfilenameShelf) -> None:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect(DOWNLOADS_SERVER, port=22, username=db["ssh_user"])
# Sources
source = f"/home/psf-users/{db['ssh_user']}/{db['release']}"
destination = f"/srv/www.python.org/ftp/python/{db['release'].normalized()}"
def execute_command(command: str) -> None:
channel = client.get_transport().open_session()
channel.exec_command(command)
if channel.recv_exit_status() != 0:
raise ReleaseException(channel.recv_stderr(1000))
execute_command(f"mkdir -p {destination}")
execute_command(f"cp {source}/src/* {destination}")
execute_command(f"chgrp downloads {destination}")
execute_command(f"chmod 775 {destination}")
execute_command(f"find {destination} -type f -exec chmod 664 {{}} \\;")
# Docs
release_tag: release_mod.Tag = db["release"]
if release_tag.is_final or release_tag.is_release_candidate:
source = f"/home/psf-users/{db['ssh_user']}/{db['release']}"
destination = f"/srv/www.python.org/ftp/python/doc/{release_tag}"
def execute_command(command: str) -> None:
channel = client.get_transport().open_session()
channel.exec_command(command)
if channel.recv_exit_status() != 0:
raise ReleaseException(channel.recv_stderr(1000))
execute_command(f"mkdir -p {destination}")
execute_command(f"cp {source}/docs/* {destination}")
execute_command(f"chgrp downloads {destination}")
execute_command(f"chmod 775 {destination}")
execute_command(f"find {destination} -type f -exec chmod 664 {{}} \\;")
def upload_docs_to_the_docs_server(db: DbfilenameShelf) -> None:
release_tag: release_mod.Tag = db["release"]
if not (release_tag.is_final or release_tag.is_release_candidate):
return
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect(DOCS_SERVER, port=22, username=db["ssh_user"])
destination = pathlib.Path(f"/home/psf-users/{db['ssh_user']}/{db['release']}")
ftp_client = MySFTPClient.from_transport(client.get_transport())
client.exec_command(f"rm -rf {destination}")
with contextlib.suppress(OSError):
ftp_client.mkdir(str(destination))
artifacts_path = pathlib.Path(db["git_repo"] / str(db["release"]))
shutil.rmtree(artifacts_path / f"Python-{db['release']}", ignore_errors=True)
def upload_subdir(subdir: str) -> None:
with contextlib.suppress(OSError):
ftp_client.mkdir(str(destination / subdir))
with alive_bar(len(tuple((artifacts_path / subdir).glob("**/*")))) as progress:
ftp_client.put_dir(
artifacts_path / subdir,
str(destination / subdir),
progress=progress,
)
upload_subdir("docs")
ftp_client.close()
def unpack_docs_in_the_docs_server(db: DbfilenameShelf) -> None:
release_tag: release_mod.Tag = db["release"]
if not (release_tag.is_final or release_tag.is_release_candidate):
return
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect(DOCS_SERVER, port=22, username=db["ssh_user"])
# Sources
source = f"/home/psf-users/{db['ssh_user']}/{db['release']}"
destination = f"/srv/docs.python.org/release/{release_tag}"
def execute_command(command: str) -> None:
channel = client.get_transport().open_session()
channel.exec_command(command)
if channel.recv_exit_status() != 0:
raise ReleaseException(channel.recv_stderr(1000))
docs_filename = f"python-{release_tag}-docs-html"
execute_command(f"mkdir -p {destination}")
execute_command(f"unzip {source}/docs/{docs_filename}.zip -d {destination}")
execute_command(f"mv /{destination}/{docs_filename}/* {destination}")
execute_command(f"rm -rf /{destination}/{docs_filename}")
execute_command(f"chgrp -R docs {destination}")
execute_command(f"chmod -R 775 {destination}")
execute_command(f"find {destination} -type f -exec chmod 664 {{}} \\;")
def start_build_of_source_and_docs(db: DbfilenameShelf) -> None:
# Get the git commit SHA for the tag
commit_sha = (
subprocess.check_output(
["git", "rev-list", "-n", "1", db["release"].gitname], cwd=db["git_repo"]
)
.decode()
.strip()
)
# Get the owner of the GitHub repo (first path segment in a 'github.com' remote URL)
# This works for both 'https' and 'ssh' style remote URLs.
origin_remote_url = (
subprocess.check_output(
["git", "ls-remote", "--get-url", "origin"], cwd=db["git_repo"]
)
.decode()
.strip()
)
if https_match := re.match(r"github\.com/([^/]+)/", origin_remote_url):
origin_remote_github_owner = https_match.group(1)
elif ssh_match := re.match(r"^git@github\.com:([^/]+)/", origin_remote_url):
origin_remote_github_owner = ssh_match.group(1)
else:
raise ReleaseException(
f"Could not parse GitHub owner from 'origin' remote URL: {origin_remote_url}"
)
# We ask for human verification at this point since this commit SHA is 'locked in'
print()
print(
f"Go to https://github.com/{origin_remote_github_owner}/cpython/commit/{commit_sha}"
)
print(
"- Ensure that there is no warning that the commit does not belong to this repository."
)
print("- Ensure that the commit diff does not contain any unexpected changes.")
print(
"- For the next step, ensure the commit SHA matches the one you verified on GitHub in this step."
)
print()
if not ask_question(
"Have you verified the release commit hasn't been tampered with on GitHub?"
):
raise ReleaseException("Commit must be visually reviewed before starting build")
# After visually confirming the release manager can start the build process
# with the known good commit SHA.
print()
print(
"Go to https://github.com/python/release-tools/actions/workflows/source-and-docs-release.yml"
)
print("Select 'Run workflow' and enter the following values:")
print(f"- Git remote to checkout: {origin_remote_github_owner}")
print(f"- Git commit to target for the release: {commit_sha}")
print(f"- CPython release number: {db['release']}")
print()
if not ask_question("Have you started the source and docs build?"):
raise ReleaseException("Source and docs build must be started")
def send_email_to_platform_release_managers(db: DbfilenameShelf) -> None:
if not ask_question(
"Have you notified the platform release managers about the availability of the commit SHA and tag?"
):
raise ReleaseException("Platform release managers muy be notified")
def create_release_object_in_db(db: DbfilenameShelf) -> None:
print(
"Go to https://www.python.org/admin/downloads/release/add/ and create a new release"
)
if not ask_question(f"Have you already created a new release for {db['release']}"):
raise ReleaseException("The django release object has not been created")
def wait_until_all_files_are_in_folder(db: DbfilenameShelf) -> None:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect(DOWNLOADS_SERVER, port=22, username=db["ssh_user"])
ftp_client = client.open_sftp()
destination = f"/srv/www.python.org/ftp/python/{db['release'].normalized()}"
are_all_files_there = False
release = str(db["release"])
print()
while not are_all_files_there:
try:
all_files = set(ftp_client.listdir(destination))
except FileNotFoundError:
raise FileNotFoundError(
f"The release folder in {destination} has not been created"
) from None
are_windows_files_there = f"python-{release}.exe" in all_files
are_macos_files_there = f"python-{release}-macos11.pkg" in all_files
are_linux_files_there = f"Python-{release}.tgz" in all_files
are_all_files_there = (
are_linux_files_there and are_windows_files_there and are_macos_files_there
)
if not are_all_files_there:
linux_tick = "✅" if are_linux_files_there else "❎"
windows_tick = "✅" if are_windows_files_there else "❎"
macos_tick = "✅" if are_macos_files_there else "❎"
print(
f"\rWaiting for files: Linux {linux_tick} Windows {windows_tick} Mac {macos_tick}",
flush=True,
end="",
)
time.sleep(1)
print()
def run_add_to_python_dot_org(db: DbfilenameShelf) -> None:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect(DOWNLOADS_SERVER, port=22, username=db["ssh_user"])
# Ensure the file is there
source = pathlib.Path(__file__).parent / "add-to-pydotorg.py"
destination = pathlib.Path(f"/home/psf-users/{db['ssh_user']}/add-to-pydotorg.py")
ftp_client = MySFTPClient.from_transport(client.get_transport())
ftp_client.put(str(source), str(destination))
ftp_client.close()
auth_info = db["auth_info"]
assert auth_info is not None
# Do the interactive flow to get an identity for Sigstore
issuer = sigstore.oidc.Issuer(sigstore.oidc.DEFAULT_OAUTH_ISSUER_URL)
identity_token = issuer.identity_token()
stdin, stdout, stderr = client.exec_command(
f"AUTH_INFO={auth_info} SIGSTORE_IDENTITY_TOKEN={identity_token} python3 add-to-pydotorg.py {db['release']}"
)
stderr_text = stderr.read().decode()
if stderr_text:
raise paramiko.SSHException(f"Failed to execute the command: {stderr_text}")
stdout_text = stdout.read().decode()
print("-- Command output --")
print(stdout_text)
print("-- End of command output --")
def purge_the_cdn(db: DbfilenameShelf) -> None:
headers = {
"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6"
}
normalized_release = db["release"].normalized()
urls = [
f"https://www.python.org/downloads/release/python-{str(db['release']).replace('.', '')}/",
f"https://docs.python.org/release/{db['release']}/",
f"https://www.python.org/ftp/python/{normalized_release}/",
f"https://www.python.org/ftp/python/{normalized_release}/Python-{db['release']}.tgz",
f"https://www.python.org/ftp/python/{normalized_release}/Python-{db['release']}.tgz.asc",
f"https://www.python.org/ftp/python/{normalized_release}/Python-{db['release']}.tar.xz",
f"https://www.python.org/ftp/python/{normalized_release}/Python-{db['release']}.tar.xz.asc",
f"https://docs.python.org/release/{normalized_release}/",
"https://www.python.org/downloads/",
"https://www.python.org/downloads/windows/",
"https://www.python.org/downloads/macos/",
]
for url in urls:
req = urllib.request.Request(url=url, headers=headers, method="PURGE")
# try:
response = urllib.request.urlopen(req)
if response.code != 200:
raise RuntimeError("Failed to purge the python.org/downloads CDN")
def modify_the_release_to_the_prerelease_pages(db: DbfilenameShelf) -> None:
pre_release_tags = {"rc", "b", "a"}
if any(tag in str(db["release"]) for tag in pre_release_tags):
if not ask_question(
"Have you already added the release to https://www.python.org/download/pre-releases/"
):
raise ReleaseException(
"The release has not been added to the pre-release page"
)
else:
if not ask_question(
"Have you already removed the release from https://www.python.org/download/pre-releases/"
):
raise ReleaseException(
"The release has not been removed from the pre-release page"
)
def post_release_merge(db: DbfilenameShelf) -> None:
subprocess.check_call(
["git", "fetch", "--all"],
cwd=db["git_repo"],
)
release_tag: release_mod.Tag = db["release"]
if release_tag.is_feature_freeze_release:
subprocess.check_call(
["git", "checkout", "main"],
cwd=db["git_repo"],
)
else:
subprocess.check_call(
["git", "checkout", release_tag.branch],
cwd=db["git_repo"],
)
subprocess.check_call(
["git", "merge", "--no-squash", f"v{db['release']}"],
cwd=db["git_repo"],
)
def post_release_tagging(db: DbfilenameShelf) -> None:
release_tag: release_mod.Tag = db["release"]
subprocess.check_call(
["git", "fetch", "--all"],
cwd=db["git_repo"],
)
subprocess.check_call(
["git", "checkout", release_tag.branch],
cwd=db["git_repo"],
)
with cd(db["git_repo"]):
release_mod.done(db["release"])
subprocess.check_call(
["git", "commit", "-a", "-m", f"Post {db['release']}"],
cwd=db["git_repo"],
)
def maybe_prepare_new_main_branch(db: DbfilenameShelf) -> None:
release_tag: release_mod.Tag = db["release"]
if not release_tag.is_feature_freeze_release:
return
subprocess.check_call(
["git", "checkout", "main"],
cwd=db["git_repo"],
)
new_release = release_tag.next_minor_release()
with cd(db["git_repo"]):
release_mod.bump(new_release)
prev_branch = f"{release_tag.major}.{release_tag.minor}"
new_branch = f"{release_tag.major}.{int(release_tag.minor)+1}"
whatsnew_file = f"Doc/whatsnew/{new_branch}.rst"
with cd(db["git_repo"]), open(whatsnew_file, "w") as f:
f.write(WHATS_NEW_TEMPLATE.format(version=new_branch, prev_version=prev_branch))
subprocess.check_call(
["git", "add", whatsnew_file],
cwd=db["git_repo"],