]>
git.ipfire.org Git - thirdparty/systemd.git/blob - test/create-sys-script.py
3 OUTFILE_HEADER
= """#!/usr/bin/env python3
7 # (C) 2017 Canonical Ltd.
8 # Author: Dan Streetman <dan.streetman@canonical.com>
10 # systemd is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU Lesser General Public License as published by
12 # the Free Software Foundation; either version 2.1 of the License, or
13 # (at your option) any later version.
15 # systemd is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Lesser General Public License for more details.
20 # You should have received a copy of the GNU Lesser General Public License
21 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 # Use this only to (re-)create the test/sys-script.py script,
26 # after adding or modifying anything in the test/sys/ directory
47 def f(path, mode, contents):
48 with open(path, "wb") as f:
55 exit("Usage: {} <target dir>".format(sys.argv[0]))
57 if not os.path.isdir(sys.argv[1]):
58 exit("Target dir {} not found".format(sys.argv[1]))
62 if os.path.exists('sys'):
67 def handle_dir(outfile
, path
):
68 m
= os
.lstat(path
).st_mode
& 0o777
69 outfile
.write(f
"d('{path}', {m:#o})\n")
72 def handle_link(outfile
, path
):
73 src
= os
.readlink(path
)
74 outfile
.write(f
"l('{path}', '{src}')\n")
77 def escape_single_quotes(b
):
78 # remove the b'' wrapping each line repr
80 # python escapes all ' only if there are ' and " in the string
82 r
= r
.replace("'", r
"\'")
83 # return line with all ' escaped
87 def handle_file(outfile
, path
):
88 m
= os
.lstat(path
).st_mode
& 0o777
89 with
open(path
, "rb") as f
:
91 if b
.count(b
"\n") > 1:
92 r
= "\n".join( escape_single_quotes(l
) for l
in b
.split(b
"\n") )
96 outfile
.write(f
"f('{path}', {m:#o}, {r})\n")
99 def process_sysdir(outfile
):
100 for (dirpath
, dirnames
, filenames
) in os
.walk('sys'):
101 handle_dir(outfile
, dirpath
)
103 path
= os
.path
.join(dirpath
, d
)
104 if stat
.S_ISLNK(os
.lstat(path
).st_mode
):
105 handle_link(outfile
, path
)
107 path
= os
.path
.join(dirpath
, f
)
108 mode
= os
.lstat(path
).st_mode
109 if stat
.S_ISLNK(mode
):
110 handle_link(outfile
, path
)
111 elif stat
.S_ISREG(mode
):
112 handle_file(outfile
, path
)
115 def verify_dir(tmpd
, path_a
):
116 path_b
= os
.path
.join(tmpd
, path_a
)
117 mode_a
= os
.lstat(path_a
).st_mode
118 mode_b
= os
.lstat(path_b
).st_mode
119 if not stat
.S_ISDIR(mode_b
):
120 raise Exception("Not directory")
121 if (mode_a
& 0o777) != (mode_b
& 0o777):
122 raise Exception("Permissions mismatch")
125 def verify_link(tmpd
, path_a
):
126 path_b
= os
.path
.join(tmpd
, path_a
)
127 if not stat
.S_ISLNK(os
.lstat(path_b
).st_mode
):
128 raise Exception("Not symlink")
129 if os
.readlink(path_a
) != os
.readlink(path_b
):
130 raise Exception("Symlink dest mismatch")
133 def verify_file(tmpd
, path_a
):
134 path_b
= os
.path
.join(tmpd
, path_a
)
135 mode_a
= os
.lstat(path_a
).st_mode
136 mode_b
= os
.lstat(path_b
).st_mode
137 if not stat
.S_ISREG(mode_b
):
138 raise Exception("Not file")
139 if (mode_a
& 0o777) != (mode_b
& 0o777):
140 raise Exception("Permissions mismatch")
141 if not filecmp
.cmp(path_a
, path_b
, shallow
=False):
142 raise Exception("File contents mismatch")
145 def verify_script(tmpd
):
147 for (dirpath
, dirnames
, filenames
) in os
.walk("sys"):
151 verify_dir(tmpd
, path
)
153 path
= os
.path
.join(dirpath
, d
)
154 if stat
.S_ISLNK(os
.lstat(path
).st_mode
):
155 verify_link(tmpd
, path
)
157 path
= os
.path
.join(dirpath
, f
)
158 mode
= os
.lstat(path
).st_mode
159 if stat
.S_ISLNK(mode
):
160 verify_link(tmpd
, path
)
161 elif stat
.S_ISREG(mode
):
162 verify_file(tmpd
, path
)
164 print(f
'FAIL on "{path}"', file=sys
.stderr
)
167 exit('Nothing found!')
169 if __name__
== "__main__":
170 if len(sys
.argv
) < 2:
171 exit('Usage: create-sys-script.py /path/to/test/')
173 outfile
= os
.path
.abspath(os
.path
.dirname(sys
.argv
[0]) + '/sys-script.py')
174 print(f
'Creating {outfile} using contents of {sys.argv[1]}/sys')
176 os
.chdir(sys
.argv
[1])
178 with
open(outfile
, "w") as f
:
179 os
.chmod(outfile
, OUTFILE_MODE
)
180 f
.write(OUTFILE_HEADER
.replace(os
.path
.basename(sys
.argv
[0]),
181 os
.path
.basename(outfile
)))
182 f
.write(OUTFILE_FUNCS
)
183 f
.write(OUTFILE_MAIN
)
186 with tempfile
.TemporaryDirectory() as tmpd
:
187 print(f
'Recreating sys/ using {outfile} at {tmpd}')
188 subprocess
.check_call([outfile
, tmpd
])
191 print(f
'Verification successful, {outfile} is correct')