-
Notifications
You must be signed in to change notification settings - Fork 15
/
make-new-image
executable file
·87 lines (71 loc) · 2.27 KB
/
make-new-image
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
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import os.path
import re
import shlex
import subprocess
import sys
DOCKERFILE_MK_BUILD_DEPS = (
'FROM ubuntu:{codename}\n'
'RUN : \\\n'
' && apt-get update -qq \\\n'
' && DEBIAN_FRONTEND=noninteractive apt-get install \\\n'
' -qq -y --no-install-recommends \\\n'
' devscripts equivs \\\n'
' && apt-get clean \\\n'
' && rm -rf /var/lib/apt/lists/*'
)
DOCKERFILE_TEMPLATE = (
'# Created with `{cmd}` DO NOT EDIT\n'
'FROM ubuntu:{codename}\n'
'RUN : \\\n'
' && apt-get update -qq \\\n'
' && DEBIAN_FRONTEND=noninteractive apt-get install \\\n'
' -qq -y --no-install-recommends \\\n'
' {packages} \\\n'
' && apt-get clean \\\n'
' && rm -rf /var/lib/apt/lists/*'
)
DEPENDS = ' Depends:'
TO_APT_RE = re.compile(r'(,| \([^)]+\))')
ALWAYS_DEPS = {
'devscripts', 'equivs', 'git-buildpackage', 'gnupg', 'pristine-tar',
}
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument('--codename', default='focal')
parser.add_argument('control_filename')
args = parser.parse_args()
img = f'make-new-image-{args.codename}'
subprocess.run(
('docker', 'build', '-qt', img, '-f', '-', '.'),
input=DOCKERFILE_MK_BUILD_DEPS.format(codename=args.codename).encode(),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
)
lines = subprocess.run(
(
'docker', 'run', '--rm',
'-v', f'{os.path.abspath(args.control_filename)}:/control:ro',
img, 'bash', '-ec',
'mk-build-deps /control && '
'dpkg -I *.deb | grep "^ Depends:"',
),
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
).stdout.decode().splitlines()
depends, = (line for line in lines if line.startswith(DEPENDS))
depends = depends[len(DEPENDS):]
deps = set(TO_APT_RE.sub('', depends).split()) | ALWAYS_DEPS
print(
DOCKERFILE_TEMPLATE.format(
cmd=shlex.join(sys.argv),
codename=args.codename,
packages=' \\\n '.join(sorted(deps)),
),
)
return 0
if __name__ == '__main__':
raise SystemExit(main())