-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcreate_tarballs.py
executable file
·101 lines (86 loc) · 2.76 KB
/
create_tarballs.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
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright © 2024-2025 The TokTok team
import argparse
import os
import subprocess # nosec
import tempfile
from dataclasses import dataclass
import requests
from lib import git
from lib import github
@dataclass
class Config:
upload: bool
tag: str
def parse_args() -> Config:
parser = argparse.ArgumentParser(description="""
Create and optionally upload source tarballs for the project.
""")
parser.add_argument(
"--upload",
action=argparse.BooleanOptionalAction,
help="Upload tarballs to GitHub",
default=False,
)
parser.add_argument(
"--tag",
help="Tag to create tarballs for",
default=git.current_tag(),
)
return Config(**vars(parser.parse_args()))
def create_tarballs(tag: str, tmpdir: str) -> None:
"""Create source tarballs with both .gz and .xz for the given tag."""
for prog in ("gzip", "xz"):
tarname = f"{os.path.join(tmpdir, tag)}.tar"
print(f"Creating {prog} tarball for {tag}")
subprocess.run( # nosec
[
"git",
"archive",
"--format=tar",
f"--prefix=qTox-{tag}/",
tag,
f"--output={tarname}",
],
check=True,
)
subprocess.run([prog, "-f", tarname], check=True) # nosec
def sign_tarballs(tag: str, tmpdir: str) -> None:
"""Sign the tarballs with gpg."""
for ext in ("gz", "xz"):
print(f"Signing {ext} tarball")
subprocess.run( # nosec
[
"gpg",
"--armor",
"--detach-sign",
f"{tmpdir}/{tag}.tar.{ext}",
],
check=True,
)
def upload_tarballs(tag: str, tmpdir: str) -> None:
"""Upload the tarballs and signatures to GitHub."""
content_type = {
".asc": "application/pgp-signature",
"gz": "application/gzip",
"xz": "application/x-xz",
}
for ext in ("gz", "xz"):
for suffix in ("", ".asc"):
filename = f"{tag}.tar.{ext}{suffix}"
print(f"Uploading {filename} to GitHub release {tag}")
with open(os.path.join(tmpdir, filename), "rb") as f:
github.upload_asset(tag, filename, content_type[suffix or ext],
f)
def main(config: Config) -> None:
if config.upload:
with tempfile.TemporaryDirectory() as tmpdir:
create_tarballs(config.tag, tmpdir)
sign_tarballs(config.tag, tmpdir)
upload_tarballs(config.tag, tmpdir)
else:
create_tarballs(config.tag, ".")
sign_tarballs(config.tag, ".")
if __name__ == "__main__":
main(parse_args())