]>
git.ipfire.org Git - thirdparty/systemd.git/blob - test/create-sys-script.py
2 # SPDX-License-Identifier: LGPL-2.1+
4 OUTFILE_HEADER
= """#!/usr/bin/env python3
5 # SPDX-License-Identifier: LGPL-2.1+
9 # (C) 2017 Canonical Ltd.
10 # Author: Dan Streetman <dan.streetman@canonical.com>
12 # systemd is free software; you can redistribute it and/or modify it
13 # under the terms of the GNU Lesser General Public License as published by
14 # the Free Software Foundation; either version 2.1 of the License, or
15 # (at your option) any later version.
17 # systemd is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # Lesser General Public License for more details.
22 # You should have received a copy of the GNU Lesser General Public License
23 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 # Use this only to (re-)create the test/sys-script.py script,
28 # after adding or modifying anything in the test/sys/ directory
49 def f(path, mode, contents):
50 with open(path, "wb") as f:
57 exit("Usage: {} <target dir>".format(sys.argv[0]))
59 if not os.path.isdir(sys.argv[1]):
60 exit("Target dir {} not found".format(sys.argv[1]))
64 if os.path.exists('sys'):
69 def handle_dir(outfile
, path
):
70 m
= os
.lstat(path
).st_mode
& 0o777
71 outfile
.write(f
"d('{path}', {m:#o})\n")
74 def handle_link(outfile
, path
):
75 src
= os
.readlink(path
)
76 outfile
.write(f
"l('{path}', '{src}')\n")
79 def escape_single_quotes(b
):
80 # remove the b'' wrapping each line repr
82 # python escapes all ' only if there are ' and " in the string
84 r
= r
.replace("'", r
"\'")
85 # return line with all ' escaped
89 def handle_file(outfile
, path
):
90 m
= os
.lstat(path
).st_mode
& 0o777
91 with
open(path
, "rb") as f
:
93 if b
.count(b
"\n") > 1:
94 r
= "\n".join( escape_single_quotes(l
) for l
in b
.split(b
"\n") )
98 outfile
.write(f
"f('{path}', {m:#o}, {r})\n")
101 def process_sysdir(outfile
):
102 for (dirpath
, dirnames
, filenames
) in os
.walk('sys'):
103 handle_dir(outfile
, dirpath
)
105 path
= os
.path
.join(dirpath
, d
)
106 if stat
.S_ISLNK(os
.lstat(path
).st_mode
):
107 handle_link(outfile
, path
)
109 path
= os
.path
.join(dirpath
, f
)
110 mode
= os
.lstat(path
).st_mode
111 if stat
.S_ISLNK(mode
):
112 handle_link(outfile
, path
)
113 elif stat
.S_ISREG(mode
):
114 handle_file(outfile
, path
)
117 def verify_dir(tmpd
, path_a
):
118 path_b
= os
.path
.join(tmpd
, path_a
)
119 mode_a
= os
.lstat(path_a
).st_mode
120 mode_b
= os
.lstat(path_b
).st_mode
121 if not stat
.S_ISDIR(mode_b
):
122 raise Exception("Not directory")
123 if (mode_a
& 0o777) != (mode_b
& 0o777):
124 raise Exception("Permissions mismatch")
127 def verify_link(tmpd
, path_a
):
128 path_b
= os
.path
.join(tmpd
, path_a
)
129 if not stat
.S_ISLNK(os
.lstat(path_b
).st_mode
):
130 raise Exception("Not symlink")
131 if os
.readlink(path_a
) != os
.readlink(path_b
):
132 raise Exception("Symlink dest mismatch")
135 def verify_file(tmpd
, path_a
):
136 path_b
= os
.path
.join(tmpd
, path_a
)
137 mode_a
= os
.lstat(path_a
).st_mode
138 mode_b
= os
.lstat(path_b
).st_mode
139 if not stat
.S_ISREG(mode_b
):
140 raise Exception("Not file")
141 if (mode_a
& 0o777) != (mode_b
& 0o777):
142 raise Exception("Permissions mismatch")
143 if not filecmp
.cmp(path_a
, path_b
, shallow
=False):
144 raise Exception("File contents mismatch")
147 def verify_script(tmpd
):
149 for (dirpath
, dirnames
, filenames
) in os
.walk("sys"):
153 verify_dir(tmpd
, path
)
155 path
= os
.path
.join(dirpath
, d
)
156 if stat
.S_ISLNK(os
.lstat(path
).st_mode
):
157 verify_link(tmpd
, path
)
159 path
= os
.path
.join(dirpath
, f
)
160 mode
= os
.lstat(path
).st_mode
161 if stat
.S_ISLNK(mode
):
162 verify_link(tmpd
, path
)
163 elif stat
.S_ISREG(mode
):
164 verify_file(tmpd
, path
)
166 print(f
'FAIL on "{path}"', file=sys
.stderr
)
169 exit('Nothing found!')
171 if __name__
== "__main__":
172 if len(sys
.argv
) < 2:
173 exit('Usage: create-sys-script.py /path/to/test/')
175 outfile
= os
.path
.abspath(os
.path
.dirname(sys
.argv
[0]) + '/sys-script.py')
176 print(f
'Creating {outfile} using contents of {sys.argv[1]}/sys')
178 os
.chdir(sys
.argv
[1])
180 with
open(outfile
, "w") as f
:
181 os
.chmod(outfile
, OUTFILE_MODE
)
182 f
.write(OUTFILE_HEADER
.replace(os
.path
.basename(sys
.argv
[0]),
183 os
.path
.basename(outfile
)))
184 f
.write(OUTFILE_FUNCS
)
185 f
.write(OUTFILE_MAIN
)
188 with tempfile
.TemporaryDirectory() as tmpd
:
189 print(f
'Recreating sys/ using {outfile} at {tmpd}')
190 subprocess
.check_call([outfile
, tmpd
])
193 print(f
'Verification successful, {outfile} is correct')