create-sys-script: apply "ruff format" and "ruff check --fix"

This commit is contained in:
Yu Watanabe
2026-05-18 02:44:46 +09:00
parent 58aa8824f3
commit 8fd2385709
+30 -29
View File
@@ -1,28 +1,29 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-License-Identifier: LGPL-2.1-or-later
OUTFILE_HEADER = """#!/usr/bin/env python3 import filecmp
import os
import stat
import subprocess
import sys
import tempfile
OUTFILE_HEADER = '''#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-License-Identifier: LGPL-2.1-or-later
# #
# create-sys-script.py # create-sys-script.py
# #
# © 2017 Canonical Ltd. # © 2017 Canonical Ltd.
# Author: Dan Streetman <dan.streetman@canonical.com> # Author: Dan Streetman <dan.streetman@canonical.com>
""" '''
# Use this only to (re-)create the test/sys-script.py script, # Use this only to (re-)create the test/sys-script.py script,
# after adding or modifying anything in the test/sys/ directory # after adding or modifying anything in the test/sys/ directory
import os, sys
import stat
import tempfile
import filecmp
import subprocess
OUTFILE_MODE = 0o775 OUTFILE_MODE = 0o775
OUTFILE_FUNCS = r""" OUTFILE_FUNCS = r'''
import os, sys import os, sys
import shutil import shutil
@@ -36,9 +37,9 @@ def f(path, mode, contents):
with open(path, "wb") as f: with open(path, "wb") as f:
f.write(contents) f.write(contents)
os.chmod(path, mode) os.chmod(path, mode)
""" '''
OUTFILE_MAIN = """ OUTFILE_MAIN = '''
if len(sys.argv) < 2: if len(sys.argv) < 2:
exit("Usage: {} <target dir>".format(sys.argv[0])) exit("Usage: {} <target dir>".format(sys.argv[0]))
@@ -49,7 +50,7 @@ os.chdir(sys.argv[1])
if os.path.exists('sys'): if os.path.exists('sys'):
shutil.rmtree('sys') shutil.rmtree('sys')
""" '''
def handle_dir(outfile, path): def handle_dir(outfile, path):
@@ -67,17 +68,17 @@ def escape_single_quotes(b):
r = repr(b)[2:-1] r = repr(b)[2:-1]
# python escapes all ' only if there are ' and " in the string # python escapes all ' only if there are ' and " in the string
if '"' not in r: if '"' not in r:
r = r.replace("'", r"\'") r = r.replace("'", r'\'')
# return line with all ' escaped # return line with all ' escaped
return r return r
def handle_file(outfile, path): def handle_file(outfile, path):
m = os.lstat(path).st_mode & 0o777 m = os.lstat(path).st_mode & 0o777
with open(path, "rb") as f: with open(path, 'rb') as f:
b = f.read() b = f.read()
if b.count(b"\n") > 1: if b.count(b'\n') > 1:
r = "\n".join( escape_single_quotes(l) for l in b.split(b"\n") ) r = '\n'.join(escape_single_quotes(line) for line in b.split(b'\n'))
r = f"b'''{r}'''" r = f"b'''{r}'''"
else: else:
r = repr(b) r = repr(b)
@@ -85,7 +86,7 @@ def handle_file(outfile, path):
def process_sysdir(outfile): def process_sysdir(outfile):
for (dirpath, dirnames, filenames) in os.walk('sys'): for dirpath, dirnames, filenames in os.walk('sys'):
handle_dir(outfile, dirpath) handle_dir(outfile, dirpath)
for d in dirnames: for d in dirnames:
path = os.path.join(dirpath, d) path = os.path.join(dirpath, d)
@@ -105,17 +106,17 @@ def verify_dir(tmpd, path_a):
mode_a = os.lstat(path_a).st_mode mode_a = os.lstat(path_a).st_mode
mode_b = os.lstat(path_b).st_mode mode_b = os.lstat(path_b).st_mode
if not stat.S_ISDIR(mode_b): if not stat.S_ISDIR(mode_b):
raise Exception("Not directory") raise Exception('Not directory')
if (mode_a & 0o777) != (mode_b & 0o777): if (mode_a & 0o777) != (mode_b & 0o777):
raise Exception("Permissions mismatch") raise Exception('Permissions mismatch')
def verify_link(tmpd, path_a): def verify_link(tmpd, path_a):
path_b = os.path.join(tmpd, path_a) path_b = os.path.join(tmpd, path_a)
if not stat.S_ISLNK(os.lstat(path_b).st_mode): if not stat.S_ISLNK(os.lstat(path_b).st_mode):
raise Exception("Not symlink") raise Exception('Not symlink')
if os.readlink(path_a) != os.readlink(path_b): if os.readlink(path_a) != os.readlink(path_b):
raise Exception("Symlink dest mismatch") raise Exception('Symlink dest mismatch')
def verify_file(tmpd, path_a): def verify_file(tmpd, path_a):
@@ -123,16 +124,16 @@ def verify_file(tmpd, path_a):
mode_a = os.lstat(path_a).st_mode mode_a = os.lstat(path_a).st_mode
mode_b = os.lstat(path_b).st_mode mode_b = os.lstat(path_b).st_mode
if not stat.S_ISREG(mode_b): if not stat.S_ISREG(mode_b):
raise Exception("Not file") raise Exception('Not file')
if (mode_a & 0o777) != (mode_b & 0o777): if (mode_a & 0o777) != (mode_b & 0o777):
raise Exception("Permissions mismatch") raise Exception('Permissions mismatch')
if not filecmp.cmp(path_a, path_b, shallow=False): if not filecmp.cmp(path_a, path_b, shallow=False):
raise Exception("File contents mismatch") raise Exception('File contents mismatch')
def verify_script(tmpd): def verify_script(tmpd):
any = False any = False
for (dirpath, dirnames, filenames) in os.walk("sys"): for dirpath, dirnames, filenames in os.walk('sys'):
any = True any = True
try: try:
path = dirpath path = dirpath
@@ -154,7 +155,8 @@ def verify_script(tmpd):
if not any: if not any:
exit('Nothing found!') exit('Nothing found!')
if __name__ == "__main__":
if __name__ == '__main__':
if len(sys.argv) < 2: if len(sys.argv) < 2:
exit('Usage: create-sys-script.py /path/to/test/') exit('Usage: create-sys-script.py /path/to/test/')
@@ -163,10 +165,9 @@ if __name__ == "__main__":
os.chdir(sys.argv[1]) os.chdir(sys.argv[1])
with open(outfile, "w") as f: with open(outfile, 'w') as f:
os.chmod(outfile, OUTFILE_MODE) os.chmod(outfile, OUTFILE_MODE)
f.write(OUTFILE_HEADER.replace(os.path.basename(sys.argv[0]), f.write(OUTFILE_HEADER.replace(os.path.basename(sys.argv[0]), os.path.basename(outfile)))
os.path.basename(outfile)))
f.write(OUTFILE_FUNCS) f.write(OUTFILE_FUNCS)
f.write(OUTFILE_MAIN) f.write(OUTFILE_MAIN)
process_sysdir(f) process_sysdir(f)