-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·150 lines (122 loc) · 5.33 KB
/
build.py
File metadata and controls
executable file
·150 lines (122 loc) · 5.33 KB
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
#!/usr/bin/env python3
import os
import sys
import glob
import shutil
import subprocess
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--dir', default='/tmp')
parser.add_argument('--package-dir', default='/tmp/packages')
parser.add_argument('--project', action='append',
help='Only build project. Can be specified multiple times')
parser.add_argument('--os', action='append',
help='Only build for OS. Can be specified multiple times')
parser.add_argument('--dry', action='store_true',
help='Only print out what would be built')
OPTS = parser.parse_args()
SCRIPT_PATH = os.path.abspath(sys.argv[0])
SCRIPT_DIR = os.path.dirname(SCRIPT_PATH)
os.chdir(SCRIPT_DIR)
if os.geteuid() != 0:
raise Exception("Please run this script as root")
def run(*args):
result = subprocess.run(
args,
stderr=sys.stderr,
stdout=sys.stdout,
check=False)
if result.returncode != 0:
raise Exception('Command `%s` failed' % ' '.join(args))
class ProjectBuild:
def __init__(self, os, image, project, package_glob):
self.os = os
self.image = image
self.project = project
self.package_glob = package_glob
self.directory = OPTS.dir
self.chroot_dir = '%s/%s-%s-chroot' % (self.directory, self.os, self.project)
self.podman_root = '%s/podman-root' % self.directory
self.podman_runroot = '%s/podman-runroot' % self.directory
def create_chroot(self):
if os.path.exists(self.chroot_dir):
print('Skip installing chroot %s ...' % self.chroot_dir)
return
print('Installing chroot %s ...' % self.chroot_dir)
run('./create_chroot.sh', self.podman_root, self.podman_runroot, self.image, self.chroot_dir)
def mount_chroot(self):
shutil.copy('/etc/resolv.conf', '%s/etc/resolv.conf' % self.chroot_dir)
run('mount', '-t', 'proc', 'proc', '%s/proc' % self.chroot_dir)
run('mount', '--rbind', '/sys', '%s/sys' % self.chroot_dir)
run('mount', '--make-rslave', '%s/sys' % self.chroot_dir)
run('mount', '--rbind', '/dev', '%s/dev' % self.chroot_dir)
run('mount', '--make-rslave', '%s/dev' % self.chroot_dir)
run('mount', '--rbind', '/run', '%s/run' % self.chroot_dir)
run('mount', '--make-rslave', '%s/run' % self.chroot_dir)
def umount_chroot(self):
try: run('umount', '-R', '%s/dev' % self.chroot_dir)
except: pass
try: run('umount', '-R', '%s/sys' % self.chroot_dir)
except: pass
try: run('umount', '-R', '%s/proc' % self.chroot_dir)
except: pass
try: run('umount', '-R', '%s/run' % self.chroot_dir)
except: pass
def package(self):
print('Building %s for %s ...' % (self.project, self.os))
script = './packaging/%s/%s.sh' % (self.project, self.os)
dest = '%s/root' % self.chroot_dir
print("Copying %s to %s" % (script, dest))
shutil.copy(script, dest)
run('chroot', self.chroot_dir, './root/%s.sh' % self.os)
def move_package(self):
pattern = '%s/root/%s/%s' % (self.chroot_dir, self.project, self.package_glob)
files = glob.glob(pattern)
if not files:
raise Exception('No package found')
if len(files) > 1:
raise Exception('Too much packages found: %s' % files)
package = files[0]
package_basename = os.path.basename(package)
print("Found package: ", package)
dest_package = '%s/%s-%s' % (OPTS.package_dir, self.os, package_basename)
print("Copying %s to %s" % (package, dest_package))
shutil.copy(package, dest_package)
os.makedirs(OPTS.package_dir, exist_ok=True)
DEBIAN_IMAGE = "docker.io/library/debian:trixie"
FEDORA_IMAGE = "registry.fedoraproject.org/fedora:43"
OPENSUSE_IMAGE = "registry.opensuse.org/opensuse/tumbleweed:latest"
ARCHLINUX_IMAGE = "docker.io/library/archlinux:latest"
builds = [
# NBFC-Linux
ProjectBuild("debian", DEBIAN_IMAGE, "nbfc-linux", "*.deb"),
ProjectBuild("fedora", FEDORA_IMAGE, "nbfc-linux", "*.rpm"),
ProjectBuild("opensuse", OPENSUSE_IMAGE, "nbfc-linux", "*.rpm"),
ProjectBuild("arch-linux", ARCHLINUX_IMAGE, "nbfc-linux", "*.pkg.tar.zst"),
# NBFC-Gtk
ProjectBuild("debian", DEBIAN_IMAGE, "nbfc-gtk", "*.deb"),
ProjectBuild("fedora", FEDORA_IMAGE, "nbfc-gtk", "*.rpm"),
ProjectBuild("opensuse", OPENSUSE_IMAGE, "nbfc-gtk", "*.rpm"),
ProjectBuild("arch-linux", ARCHLINUX_IMAGE, "nbfc-gtk", "*.pkg.tar.zst"),
# NBFC-Qt
ProjectBuild("debian", DEBIAN_IMAGE, "nbfc-qt", "*.deb"),
ProjectBuild("fedora", FEDORA_IMAGE, "nbfc-qt", "*.rpm"),
ProjectBuild("opensuse", OPENSUSE_IMAGE, "nbfc-qt", "*.rpm"),
ProjectBuild("arch-linux", ARCHLINUX_IMAGE, "nbfc-qt", "*.pkg.tar.zst")
]
selected_builds = builds
if OPTS.project:
selected_builds = filter(lambda rule: rule.project in OPTS.project, selected_builds)
if OPTS.os:
selected_builds = filter(lambda rule: rule.os in OPTS.os, selected_builds)
for build in selected_builds:
if OPTS.dry:
print('Building:', build.os, build.project)
continue
build.create_chroot()
try:
build.mount_chroot()
build.package()
build.move_package()
finally:
build.umount_chroot()