Skip to content

Commit 3bb2b49

Browse files
authored
Apply ruff checks to files under system/lib. NFC (emscripten-core#27384)
Remove `system/lib` exclusions from `pyproject.toml` and fix all lint warnings in `system/lib/*.py`. Instead of adding `encoding='utf-8'` to individual `open()` calls, use `read_file()` and `write_file()` from `tools/utils.py`.
1 parent 2e02343 commit 3bb2b49

10 files changed

Lines changed: 71 additions & 45 deletions

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ exclude = [
1111
"./node_modules/",
1212
"./site/source/_themes/",
1313
"./site/source/conf.py",
14-
"./system/lib/",
1514
"./test/third_party/",
1615
"./third_party/",
1716
"./tools/filelock.py",

system/lib/push_llvm_changes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
# and update_libcxxabi.py which copy changes from the upstream llvm
1010
# into emscripten.
1111

12-
import glob
1312
import os
14-
import sys
1513
import shutil
14+
import sys
1615

1716
script_dir = os.path.abspath(os.path.dirname(__file__))
1817
emscripten_root = os.path.dirname(os.path.dirname(script_dir))

system/lib/push_musl_changes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
# This is the logical inverse of update_musl.py which copies changes
99
# from the upstream musl tree into emscripten.
1010

11-
import glob
1211
import os
13-
import sys
1412
import shutil
13+
import sys
1514

1615
script_dir = os.path.abspath(os.path.dirname(__file__))
1716
local_dir = os.path.join(script_dir, 'libc', 'musl')

system/lib/update_common.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import argparse
12
import os
2-
import sys
3-
import shutil
43
import re
5-
import argparse
4+
import shutil
5+
import sys
66

77
script_dir = os.path.abspath(os.path.dirname(__file__))
88
emscripten_root = os.path.dirname(os.path.dirname(script_dir))
9+
sys.path.append(emscripten_root)
10+
11+
from tools.utils import read_file, write_file
12+
913
default_llvm_dir = os.path.join(os.path.dirname(emscripten_root), 'llvm-project')
1014

1115

@@ -46,7 +50,7 @@ def copy_tree(upstream_dir, local_dir, excludes=()):
4650
elif f not in excludes:
4751
shutil.copy2(full, os.path.join(local_dir, f))
4852
if excludes:
49-
for root, dirs, files in os.walk(local_dir):
53+
for root, _dirs, files in os.walk(local_dir):
5054
for f in files:
5155
if f in excludes:
5256
full = os.path.join(root, f)
@@ -57,8 +61,7 @@ def get_llvm_version(upstream_dir):
5761
cmake_file = os.path.join(upstream_dir, 'cmake', 'Modules', 'LLVMVersion.cmake')
5862
if not os.path.exists(cmake_file):
5963
return None, None
60-
with open(cmake_file, 'r') as f:
61-
content = f.read()
64+
content = read_file(cmake_file)
6265
major = re.search(r'set\(LLVM_VERSION_MAJOR\s+(\d+)\)', content)
6366
minor = re.search(r'set\(LLVM_VERSION_MINOR\s+(\d+)\)', content)
6467
patch = re.search(r'set\(LLVM_VERSION_PATCH\s+(\d+)\)', content)
@@ -72,8 +75,7 @@ def update_readme(local_dir, llvm_dir):
7275
if not os.path.exists(readme_path):
7376
return
7477

75-
with open(readme_path, 'r') as f:
76-
content = f.read()
78+
content = read_file(readme_path)
7779

7880
major, full_version = get_llvm_version(llvm_dir)
7981
if major and full_version:
@@ -82,5 +84,4 @@ def update_readme(local_dir, llvm_dir):
8284
# Find 'emscripten-libs-NN' strings and update them with the major version
8385
content = re.sub(r'emscripten-libs-\d+', f'emscripten-libs-{major}', content)
8486

85-
with open(readme_path, 'w') as f:
86-
f.write(content)
87+
write_file(readme_path, content)

system/lib/update_compiler_rt.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
# University of Illinois/NCSA Open Source License. Both these licenses can be
55
# found in the LICENSE file.
66

7-
import glob
87
import os
9-
import sys
108
import shutil
119

12-
from update_common import *
10+
from update_common import (
11+
clean_dir,
12+
default_llvm_dir,
13+
parse_args,
14+
script_dir,
15+
update_readme,
16+
)
1317

1418
local_src = os.path.join(script_dir, 'compiler-rt')
1519

@@ -44,9 +48,9 @@ def main():
4448
dest = os.path.join(local_src, *dirname)
4549
clean_dir(dest, preserve_files)
4650
for name in os.listdir(srcdir):
47-
if name in ('.clang-format', 'CMakeLists.txt', 'README.txt', 'weak_symbols.txt'):
51+
if name in {'.clang-format', 'CMakeLists.txt', 'README.txt', 'weak_symbols.txt'}:
4852
continue
49-
if name.endswith('.syms.extra') or name.endswith('.S'):
53+
if name.endswith(('.syms.extra', '.S')):
5054
continue
5155
if os.path.isfile(os.path.join(srcdir, name)):
5256
shutil.copy2(os.path.join(srcdir, name), dest)

system/lib/update_libcxx.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@
66

77
import os
88
import re
9-
import sys
109
import shutil
1110
import subprocess
11+
import sys
1212

13-
from update_common import *
13+
from update_common import (
14+
clean_dir,
15+
copy_tree,
16+
default_llvm_dir,
17+
emscripten_root,
18+
parse_args,
19+
script_dir,
20+
update_readme,
21+
write_file,
22+
)
1423

1524
local_root = os.path.join(script_dir, 'libcxx')
1625
local_src = os.path.join(local_root, 'src')
@@ -41,12 +50,11 @@ def generate_modules(cmake_version: str):
4150

4251
clean_dir(build_dir, preserve_files)
4352
os.makedirs(build_dir, exist_ok=True)
44-
with open(f'{build_dir}/CMakeLists.txt', 'x', encoding='utf-8') as CMakeLists:
45-
CMakeLists.write(f'''\
46-
cmake_minimum_required(VERSION {cmake_version})
47-
project(libcxx-modules)
48-
add_subdirectory("{local_modules}" libcxx)
49-
''')
53+
write_file(f'{build_dir}/CMakeLists.txt', f'''\
54+
cmake_minimum_required(VERSION {cmake_version})
55+
project(libcxx-modules)
56+
add_subdirectory("{local_modules}" libcxx)
57+
''')
5058

5159
vars = [
5260
('LIBCXX_INSTALL_LIBRARY_DIR', lib),
@@ -62,18 +70,18 @@ def generate_modules(cmake_version: str):
6270
subprocess.run(['cmake', '--build', binary_dir], stdout=subprocess.DEVNULL)
6371
shutil.copytree(dist, dst, dirs_exist_ok=True)
6472

73+
6574
def main():
6675
llvm_dir = parse_args(default_llvm_dir, 'llvm_dir')
6776
# Exit early if cmake is not found
6877
try:
69-
output= subprocess.check_output(['cmake', '--version'], text=True)
78+
output = subprocess.check_output(['cmake', '--version'], text=True)
7079
except OSError:
7180
print('CMake not found', file=sys.stderr)
7281
sys.exit(1)
7382

7483
cmake_version = re.search(r'^cmake version (\d+(?:\.\d+)*)', output).group(1)
7584

76-
llvm_dir = get_llvm_dir()
7785
libcxx_dir = os.path.join(llvm_dir, 'libcxx')
7886
upstream_src = os.path.join(libcxx_dir, 'src')
7987
upstream_inc = os.path.join(libcxx_dir, 'include')
@@ -115,5 +123,6 @@ def main():
115123
shutil.copy2(os.path.join(libc_upstream_dir, 'LICENSE.TXT'), libc_local_dir)
116124
update_readme(local_root, llvm_dir)
117125

126+
118127
if __name__ == '__main__':
119128
main()

system/lib/update_libcxxabi.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
# found in the LICENSE file.
66

77
import os
8-
import sys
98
import shutil
109

11-
from update_common import *
10+
from update_common import (
11+
clean_dir,
12+
copy_tree,
13+
default_llvm_dir,
14+
parse_args,
15+
script_dir,
16+
update_readme,
17+
)
1218

1319
local_root = os.path.join(script_dir, 'libcxxabi')
1420
local_src = os.path.join(local_root, 'src')

system/lib/update_libunwind.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
# found in the LICENSE file.
66

77
import os
8-
import sys
98
import shutil
109

11-
from update_common import *
10+
from update_common import (
11+
clean_dir,
12+
copy_tree,
13+
default_llvm_dir,
14+
parse_args,
15+
script_dir,
16+
update_readme,
17+
)
1218

1319
local_root = os.path.join(script_dir, 'libunwind')
1420
local_src = os.path.join(local_root, 'src')

system/lib/update_llvm_libc.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
# University of Illinois/NCSA Open Source License. Both these licenses can be
55
# found in the LICENSE file.
66

7-
import os
8-
import sys
9-
import shutil
107
import glob
8+
import os
119

12-
from update_common import *
10+
from update_common import (
11+
clean_dir,
12+
copy_tree,
13+
default_llvm_dir,
14+
parse_args,
15+
script_dir,
16+
update_readme,
17+
)
1318

1419
preserve_files = ('readme.txt', '__assertion_handler', '__config_site')
1520
# ryu_long_double_constants.h from libc is unused (and very large)
@@ -68,5 +73,6 @@ def main():
6873

6974
update_readme(libc_local_dir, llvm_dir)
7075

76+
7177
if __name__ == '__main__':
7278
main()

system/lib/update_musl.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
"""
1919

2020
import os
21-
import sys
2221
import shutil
23-
2422
from pathlib import Path
2523

26-
from update_common import *
24+
from update_common import emscripten_root, parse_args, read_file, script_dir, write_file
2725

2826
local_src = os.path.join(script_dir, 'libc', 'musl')
2927
default_musl_dir = os.path.join(os.path.dirname(emscripten_root), 'musl')
@@ -36,7 +34,7 @@
3634
'aarch64', 'arm', 'i386', 'loongarch64', 'm68k',
3735
'microblaze', 'mips', 'mips64', 'mipsn32', 'or1k',
3836
'powerpc', 'powerpc64', 'riscv32', 'riscv64', 's390x',
39-
'sh', 'x32', 'x86_64'
37+
'sh', 'x32', 'x86_64',
4038
)
4139
exclude_files = (
4240
'aio.h',
@@ -102,9 +100,8 @@ def main():
102100
shutil.copytree(musl_dir, local_src, ignore=make_ignore(musl_dir))
103101

104102
# Create version.h
105-
version = open(os.path.join(local_src, 'VERSION')).read().strip()
106-
with open(os.path.join(local_src, 'src', 'internal', 'version.h'), 'w') as f:
107-
f.write('#define VERSION "%s"\n' % version)
103+
version = read_file(os.path.join(local_src, 'VERSION')).strip()
104+
write_file(os.path.join(local_src, 'src', 'internal', 'version.h'), f'#define VERSION "{version}"\n')
108105

109106

110107
if __name__ == '__main__':

0 commit comments

Comments
 (0)