]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/create-sys-script.py
network: make all failures in route configuration fatal
[thirdparty/systemd.git] / test / create-sys-script.py
CommitLineData
3e67e5c9 1#!/usr/bin/env python3
35df7443 2# SPDX-License-Identifier: LGPL-2.1+
d001ac2c 3
3e67e5c9 4OUTFILE_HEADER = """#!/usr/bin/env python3
35df7443 5# SPDX-License-Identifier: LGPL-2.1+
d001ac2c
DS
6#
7# create-sys-script.py
8#
810adae9 9# © 2017 Canonical Ltd.
d001ac2c 10# Author: Dan Streetman <dan.streetman@canonical.com>
d001ac2c
DS
11"""
12
13# Use this only to (re-)create the test/sys-script.py script,
14# after adding or modifying anything in the test/sys/ directory
15
16
227ef9bc
ZJS
17import os, sys
18import stat
19import tempfile
20import filecmp
21import subprocess
d001ac2c 22
d001ac2c
DS
23OUTFILE_MODE = 0o775
24
25OUTFILE_FUNCS = r"""
26import os, sys
0bca7954 27import shutil
d001ac2c
DS
28
29def d(path, mode):
30 os.mkdir(path, mode)
31
32def l(path, src):
33 os.symlink(src, path)
34
35def f(path, mode, contents):
36 with open(path, "wb") as f:
37 f.write(contents)
38 os.chmod(path, mode)
d001ac2c
DS
39"""
40
41OUTFILE_MAIN = """
42if len(sys.argv) < 2:
43 exit("Usage: {} <target dir>".format(sys.argv[0]))
44
45if not os.path.isdir(sys.argv[1]):
46 exit("Target dir {} not found".format(sys.argv[1]))
47
48os.chdir(sys.argv[1])
49
0bca7954
ZJS
50if os.path.exists('sys'):
51 shutil.rmtree('sys')
d001ac2c
DS
52"""
53
54
55def handle_dir(outfile, path):
56 m = os.lstat(path).st_mode & 0o777
227ef9bc 57 outfile.write(f"d('{path}', {m:#o})\n")
d001ac2c
DS
58
59
60def handle_link(outfile, path):
61 src = os.readlink(path)
227ef9bc 62 outfile.write(f"l('{path}', '{src}')\n")
d001ac2c
DS
63
64
65def escape_single_quotes(b):
66 # remove the b'' wrapping each line repr
67 r = repr(b)[2:-1]
68 # python escapes all ' only if there are ' and " in the string
69 if '"' not in r:
70 r = r.replace("'", r"\'")
71 # return line with all ' escaped
72 return r
73
74
75def handle_file(outfile, path):
76 m = os.lstat(path).st_mode & 0o777
77 with open(path, "rb") as f:
78 b = f.read()
79 if b.count(b"\n") > 1:
227ef9bc
ZJS
80 r = "\n".join( escape_single_quotes(l) for l in b.split(b"\n") )
81 r = f"b'''{r}'''"
d001ac2c
DS
82 else:
83 r = repr(b)
227ef9bc 84 outfile.write(f"f('{path}', {m:#o}, {r})\n")
d001ac2c
DS
85
86
87def process_sysdir(outfile):
227ef9bc 88 for (dirpath, dirnames, filenames) in os.walk('sys'):
d001ac2c
DS
89 handle_dir(outfile, dirpath)
90 for d in dirnames:
91 path = os.path.join(dirpath, d)
92 if stat.S_ISLNK(os.lstat(path).st_mode):
93 handle_link(outfile, path)
94 for f in filenames:
95 path = os.path.join(dirpath, f)
96 mode = os.lstat(path).st_mode
97 if stat.S_ISLNK(mode):
98 handle_link(outfile, path)
99 elif stat.S_ISREG(mode):
100 handle_file(outfile, path)
101
102
103def verify_dir(tmpd, path_a):
104 path_b = os.path.join(tmpd, path_a)
105 mode_a = os.lstat(path_a).st_mode
106 mode_b = os.lstat(path_b).st_mode
107 if not stat.S_ISDIR(mode_b):
108 raise Exception("Not directory")
109 if (mode_a & 0o777) != (mode_b & 0o777):
110 raise Exception("Permissions mismatch")
111
112
113def verify_link(tmpd, path_a):
114 path_b = os.path.join(tmpd, path_a)
115 if not stat.S_ISLNK(os.lstat(path_b).st_mode):
116 raise Exception("Not symlink")
117 if os.readlink(path_a) != os.readlink(path_b):
118 raise Exception("Symlink dest mismatch")
119
120
121def verify_file(tmpd, path_a):
122 path_b = os.path.join(tmpd, path_a)
123 mode_a = os.lstat(path_a).st_mode
124 mode_b = os.lstat(path_b).st_mode
125 if not stat.S_ISREG(mode_b):
126 raise Exception("Not file")
127 if (mode_a & 0o777) != (mode_b & 0o777):
128 raise Exception("Permissions mismatch")
129 if not filecmp.cmp(path_a, path_b, shallow=False):
130 raise Exception("File contents mismatch")
131
132
133def verify_script(tmpd):
227ef9bc 134 any = False
d001ac2c 135 for (dirpath, dirnames, filenames) in os.walk("sys"):
227ef9bc 136 any = True
d001ac2c
DS
137 try:
138 path = dirpath
139 verify_dir(tmpd, path)
140 for d in dirnames:
141 path = os.path.join(dirpath, d)
142 if stat.S_ISLNK(os.lstat(path).st_mode):
143 verify_link(tmpd, path)
144 for f in filenames:
145 path = os.path.join(dirpath, f)
146 mode = os.lstat(path).st_mode
147 if stat.S_ISLNK(mode):
148 verify_link(tmpd, path)
149 elif stat.S_ISREG(mode):
150 verify_file(tmpd, path)
151 except Exception:
227ef9bc 152 print(f'FAIL on "{path}"', file=sys.stderr)
d001ac2c 153 raise
227ef9bc
ZJS
154 if not any:
155 exit('Nothing found!')
d001ac2c
DS
156
157if __name__ == "__main__":
227ef9bc
ZJS
158 if len(sys.argv) < 2:
159 exit('Usage: create-sys-script.py /path/to/test/')
d001ac2c 160
227ef9bc
ZJS
161 outfile = os.path.abspath(os.path.dirname(sys.argv[0]) + '/sys-script.py')
162 print(f'Creating {outfile} using contents of {sys.argv[1]}/sys')
d001ac2c 163
227ef9bc 164 os.chdir(sys.argv[1])
d001ac2c 165
227ef9bc
ZJS
166 with open(outfile, "w") as f:
167 os.chmod(outfile, OUTFILE_MODE)
168 f.write(OUTFILE_HEADER.replace(os.path.basename(sys.argv[0]),
169 os.path.basename(outfile)))
d001ac2c
DS
170 f.write(OUTFILE_FUNCS)
171 f.write(OUTFILE_MAIN)
172 process_sysdir(f)
173
174 with tempfile.TemporaryDirectory() as tmpd:
227ef9bc
ZJS
175 print(f'Recreating sys/ using {outfile} at {tmpd}')
176 subprocess.check_call([outfile, tmpd])
d001ac2c
DS
177 verify_script(tmpd)
178
227ef9bc 179 print(f'Verification successful, {outfile} is correct')