-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.py
executable file
·152 lines (139 loc) · 4.41 KB
/
build.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
#!/usr/bin/env python3
#
# Copyright (C) 2021-2024 Sébastien Helleu <[email protected]>
#
# This program 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 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
"""Build WeeChat container image."""
from typing import List, Sequence, Tuple
import argparse
import urllib.request
import subprocess # nosec
DISTROS: Sequence[str] = (
"debian",
"alpine",
)
def get_parser() -> argparse.ArgumentParser:
"""Return the command line parser."""
parser = argparse.ArgumentParser(description="Build of WeeChat container")
parser.add_argument(
"-b",
"--builder",
default="docker",
help=(
"program used to build the container image, "
"like docker (default) or podman"
),
)
parser.add_argument(
"-d",
"--distro",
choices=DISTROS,
default=DISTROS[0],
help="base Linux distribution for the container",
)
parser.add_argument(
"-n",
"--dry-run",
action="store_true",
help=("dry run: display command but do not run it"),
)
parser.add_argument(
"-s",
"--slim",
action="store_true",
help=(
"slim version: build without plugins: " "script, scripts, spell"
),
)
parser.add_argument(
"version",
help=(
'version to build: numeric (like "3.0") or '
'alias: "latest" (or "stable"), "devel"'
),
)
return parser
def get_version_tags(
version: str, distro: str, slim: bool
) -> Tuple[str, List[str]]:
"""
Get WeeChat version and tags to apply on the container image.
:param version: x.y, x.y.z, "latest", "stable", "devel"
:param distro: "debian" or "alpine"
:paral slim: slim version
:return: tuple (version, tags) where version if the version of WeeChat
to build and tags is a list of tag arguments for command line,
for example: ['-t', 'weechat:3.0-alpine',
'-t', 'weechat/weechat:3.0-alpine',
'-t', 'weechat:3-alpine',
'-t', 'weechat/weechat:3-alpine']
"""
str_slim = "-slim" if slim else ""
if distro == "debian":
suffixes = [str_slim, f"-{distro}{str_slim}"]
else:
suffixes = [f"-{distro}{str_slim}"]
tags = []
if version == "devel":
tags.append(version)
else:
if version in ("latest", "stable"):
tags.append("latest")
url = "https://weechat.org/dev/info/stable/"
with urllib.request.urlopen(url) as response: # nosec
version = response.read().decode("utf-8").strip()
else:
tags.append(version)
numbers = version.split(".")
for i in range(len(numbers)):
tags.append(".".join(numbers[: i + 1]))
tags_args = []
for tag in reversed(tags):
for suffix in suffixes:
tags_args.extend(
[
"-t",
f"weechat:{tag}{suffix}",
"-t",
f"weechat/weechat:{tag}{suffix}",
]
)
return (version, tags_args)
def main() -> None:
"""Main function."""
args = get_parser().parse_args()
slim = ["--build-arg", "SLIM=1"] if args.slim else []
version, tags = get_version_tags(args.version, args.distro, args.slim)
command = [
f"{args.builder}",
"build",
"-f",
f"{args.distro}/Containerfile",
"--build-arg",
f"VERSION={version}",
*slim,
*tags,
".",
]
print(f'Running: {" ".join(command)}')
if not args.dry_run:
try:
subprocess.run(command, check=False) # nosec
except KeyboardInterrupt:
pass
except Exception as exc: # pylint: disable=broad-except
print(exc)
if __name__ == "__main__":
main()