blob: a4f1f302f60cd40a0a45131fe81b294dc5984168 [file] [log] [blame]
Jörg Thalheim3e67e5c2017-05-01 02:26:56 +02001#!/usr/bin/env python3
Dan Streetmand001ac22017-02-10 15:27:18 -05002
Jörg Thalheim3e67e5c2017-05-01 02:26:56 +02003OUTFILE_HEADER = """#!/usr/bin/env python3
Dan Streetmand001ac22017-02-10 15:27:18 -05004#
5# create-sys-script.py
6#
7# (C) 2017 Canonical Ltd.
8# Author: Dan Streetman <dan.streetman@canonical.com>
9#
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.
14#
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.
19#
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/>.
22#
23"""
24
25# Use this only to (re-)create the test/sys-script.py script,
26# after adding or modifying anything in the test/sys/ directory
27
28
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +020029import os, sys
30import stat
31import tempfile
32import filecmp
33import subprocess
Dan Streetmand001ac22017-02-10 15:27:18 -050034
Dan Streetmand001ac22017-02-10 15:27:18 -050035OUTFILE_MODE = 0o775
36
37OUTFILE_FUNCS = r"""
38import os, sys
Zbigniew Jędrzejewski-Szmek0bca7952017-09-29 12:28:25 +020039import shutil
Dan Streetmand001ac22017-02-10 15:27:18 -050040
41def d(path, mode):
42 os.mkdir(path, mode)
43
44def l(path, src):
45 os.symlink(src, path)
46
47def f(path, mode, contents):
48 with open(path, "wb") as f:
49 f.write(contents)
50 os.chmod(path, mode)
Dan Streetmand001ac22017-02-10 15:27:18 -050051"""
52
53OUTFILE_MAIN = """
54if len(sys.argv) < 2:
55 exit("Usage: {} <target dir>".format(sys.argv[0]))
56
57if not os.path.isdir(sys.argv[1]):
58 exit("Target dir {} not found".format(sys.argv[1]))
59
60os.chdir(sys.argv[1])
61
Zbigniew Jędrzejewski-Szmek0bca7952017-09-29 12:28:25 +020062if os.path.exists('sys'):
63 shutil.rmtree('sys')
Dan Streetmand001ac22017-02-10 15:27:18 -050064"""
65
66
67def handle_dir(outfile, path):
68 m = os.lstat(path).st_mode & 0o777
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +020069 outfile.write(f"d('{path}', {m:#o})\n")
Dan Streetmand001ac22017-02-10 15:27:18 -050070
71
72def handle_link(outfile, path):
73 src = os.readlink(path)
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +020074 outfile.write(f"l('{path}', '{src}')\n")
Dan Streetmand001ac22017-02-10 15:27:18 -050075
76
77def escape_single_quotes(b):
78 # remove the b'' wrapping each line repr
79 r = repr(b)[2:-1]
80 # python escapes all ' only if there are ' and " in the string
81 if '"' not in r:
82 r = r.replace("'", r"\'")
83 # return line with all ' escaped
84 return r
85
86
87def handle_file(outfile, path):
88 m = os.lstat(path).st_mode & 0o777
89 with open(path, "rb") as f:
90 b = f.read()
91 if b.count(b"\n") > 1:
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +020092 r = "\n".join( escape_single_quotes(l) for l in b.split(b"\n") )
93 r = f"b'''{r}'''"
Dan Streetmand001ac22017-02-10 15:27:18 -050094 else:
95 r = repr(b)
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +020096 outfile.write(f"f('{path}', {m:#o}, {r})\n")
Dan Streetmand001ac22017-02-10 15:27:18 -050097
98
99def process_sysdir(outfile):
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +0200100 for (dirpath, dirnames, filenames) in os.walk('sys'):
Dan Streetmand001ac22017-02-10 15:27:18 -0500101 handle_dir(outfile, dirpath)
102 for d in dirnames:
103 path = os.path.join(dirpath, d)
104 if stat.S_ISLNK(os.lstat(path).st_mode):
105 handle_link(outfile, path)
106 for f in filenames:
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)
113
114
115def 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")
123
124
125def 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")
131
132
133def 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")
143
144
145def verify_script(tmpd):
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +0200146 any = False
Dan Streetmand001ac22017-02-10 15:27:18 -0500147 for (dirpath, dirnames, filenames) in os.walk("sys"):
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +0200148 any = True
Dan Streetmand001ac22017-02-10 15:27:18 -0500149 try:
150 path = dirpath
151 verify_dir(tmpd, path)
152 for d in dirnames:
153 path = os.path.join(dirpath, d)
154 if stat.S_ISLNK(os.lstat(path).st_mode):
155 verify_link(tmpd, path)
156 for f in filenames:
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)
163 except Exception:
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +0200164 print(f'FAIL on "{path}"', file=sys.stderr)
Dan Streetmand001ac22017-02-10 15:27:18 -0500165 raise
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +0200166 if not any:
167 exit('Nothing found!')
Dan Streetmand001ac22017-02-10 15:27:18 -0500168
169if __name__ == "__main__":
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +0200170 if len(sys.argv) < 2:
171 exit('Usage: create-sys-script.py /path/to/test/')
Dan Streetmand001ac22017-02-10 15:27:18 -0500172
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +0200173 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')
Dan Streetmand001ac22017-02-10 15:27:18 -0500175
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +0200176 os.chdir(sys.argv[1])
Dan Streetmand001ac22017-02-10 15:27:18 -0500177
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +0200178 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)))
Dan Streetmand001ac22017-02-10 15:27:18 -0500182 f.write(OUTFILE_FUNCS)
183 f.write(OUTFILE_MAIN)
184 process_sysdir(f)
185
186 with tempfile.TemporaryDirectory() as tmpd:
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +0200187 print(f'Recreating sys/ using {outfile} at {tmpd}')
188 subprocess.check_call([outfile, tmpd])
Dan Streetmand001ac22017-02-10 15:27:18 -0500189 verify_script(tmpd)
190
Zbigniew Jędrzejewski-Szmek227ef9b2017-09-29 12:27:21 +0200191 print(f'Verification successful, {outfile} is correct')