diff options
author | Dan Albert <danalbert@google.com> | 2023-05-05 15:11:00 -0700 |
---|---|---|
committer | Dan Albert <danalbert@google.com> | 2023-05-08 15:14:16 -0700 |
commit | e5273558b1c999ce743d469b97201b24f4a318b3 (patch) | |
tree | 870a650d5c57ea5950f0a52a1b73bf37eac4076d /ndk | |
parent | 85e4bc8f58e182f5734bc9dfb7e8ebb72e5f6d5e (diff) | |
download | ndk-e5273558b1c999ce743d469b97201b24f4a318b3.tar.gz |
Enable unspecified encoding lint.
We probably don't have any bugs here since we're unlikely to write
anything not in the ASCII range, and most platforms use UTF-8 as the
default anyway. The changes in ndk-build might be symptomatic for some
inputs on Windows though.
Bug: None
Test: poetry run ./checkbuild.py pythonlint
Change-Id: I1069d165db8eae1ea6fb31ce3216ca58a72e8e9e
Diffstat (limited to 'ndk')
-rwxr-xr-x | ndk/checkbuild.py | 127 | ||||
-rw-r--r-- | ndk/file.py | 29 | ||||
-rw-r--r-- | ndk/test/builder.py | 2 | ||||
-rw-r--r-- | ndk/test/buildtest/case.py | 2 | ||||
-rw-r--r-- | ndk/test/devices.py | 2 | ||||
-rw-r--r-- | ndk/test/spec.py | 2 |
6 files changed, 58 insertions, 106 deletions
diff --git a/ndk/checkbuild.py b/ndk/checkbuild.py index 5db545717..55ceb90e1 100755 --- a/ndk/checkbuild.py +++ b/ndk/checkbuild.py @@ -58,7 +58,6 @@ import ndk.builds import ndk.cmake import ndk.config import ndk.deps -import ndk.file import ndk.notify import ndk.paths import ndk.test.builder @@ -343,7 +342,7 @@ def build_ndk_tests(out_dir: Path, dist_dir: Path, args: argparse.Namespace) -> # Write out the result to logs/build_error.log so we can find the # failure easily on the build server. log_path = dist_dir / "logs" / "build_error.log" - with open(log_path, "a") as error_log: + with log_path.open("a", encoding="utf-8") as error_log: error_log_printer = ndk.test.printers.FilePrinter(error_log) error_log_printer.print_summary(report) @@ -892,7 +891,7 @@ def install_exe(out_dir: Path, install_dir: Path, name: str, host: Host) -> None def make_linker_script(path: Path, libs: List[str]) -> None: - ndk.file.write_file(path, "INPUT({})\n".format(" ".join(libs))) + path.write_text(f"INPUT({' '.join(libs)})\n") @register @@ -1103,17 +1102,15 @@ class Sysroot(ndk.builds.Module): for remove_path in remove_paths: os.remove(install_path / remove_path) - ndk_version_h_path = install_path / "usr/include/android/ndk-version.h" - with open(ndk_version_h_path, "w") as ndk_version_h: - major = ndk.config.major - minor = ndk.config.hotfix - beta = ndk.config.beta - canary = "1" if ndk.config.canary else "0" - assert self.context is not None - - ndk_version_h.write( - textwrap.dedent( - f"""\ + major = ndk.config.major + minor = ndk.config.hotfix + beta = ndk.config.beta + canary = "1" if ndk.config.canary else "0" + assert self.context is not None + + (install_path / "usr/include/android/ndk-version.h").write_text( + textwrap.dedent( + f"""\ #pragma once /** @@ -1154,8 +1151,8 @@ class Sysroot(ndk.builds.Module): */ #define __NDK_CANARY__ {canary} """ - ) ) + ) # Install the CRT objects that we just built. assert self.crt_builder is not None @@ -1170,23 +1167,20 @@ class Sysroot(ndk.builds.Module): def write_clang_shell_script( wrapper_path: Path, clang_name: str, flags: List[str] ) -> None: - with open(wrapper_path, "w") as wrapper: - wrapper.write( - textwrap.dedent( - """\ + wrapper_path.write_text( + textwrap.dedent( + f"""\ #!/usr/bin/env bash bin_dir=`dirname "$0"` if [ "$1" != "-cc1" ]; then - "$bin_dir/{clang}" {flags} "$@" + "$bin_dir/{clang_name}" {' '.join(flags)} "$@" else # Target is already an argument. - "$bin_dir/{clang}" "$@" + "$bin_dir/{clang_name}" "$@" fi - """.format( - clang=clang_name, flags=" ".join(flags) - ) - ) + """ ) + ) mode = os.stat(wrapper_path).st_mode os.chmod(wrapper_path, mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) @@ -1195,22 +1189,21 @@ def write_clang_shell_script( def write_clang_batch_script( wrapper_path: Path, clang_name: str, flags: List[str] ) -> None: - with open(wrapper_path, "w") as wrapper: - wrapper.write( - textwrap.dedent( - """\ + wrapper_path.write_text( + textwrap.dedent( + f"""\ @echo off setlocal call :find_bin if "%1" == "-cc1" goto :L - set "_BIN_DIR=" && "%_BIN_DIR%{clang}" {flags} %* + set "_BIN_DIR=" && "%_BIN_DIR%{clang_name}" {' '.join(flags)} %* if ERRORLEVEL 1 exit /b 1 goto :done :L rem Target is already an argument. - set "_BIN_DIR=" && "%_BIN_DIR%{clang}" %* + set "_BIN_DIR=" && "%_BIN_DIR%{clang_name}" %* if ERRORLEVEL 1 exit /b 1 goto :done @@ -1221,11 +1214,9 @@ def write_clang_batch_script( exit /b :done - """.format( - clang=clang_name, flags=" ".join(flags) - ) - ) + """ ) + ) def write_clang_wrapper( @@ -1452,16 +1443,8 @@ class Toolchain(ndk.builds.Module): triple = ndk.abis.abi_to_triple(abi) dst_dir = install_dir / "sysroot/usr/lib" / triple / str(api) - static_script = ["-lc++_static", "-lc++abi"] - shared_script = ["-lc++_shared"] - - libcxx_so_path = dst_dir / "libc++.so" - with open(libcxx_so_path, "w") as script: - script.write("INPUT({})".format(" ".join(shared_script))) - - libcxx_a_path = dst_dir / "libc++.a" - with open(libcxx_a_path, "w") as script: - script.write("INPUT({})".format(" ".join(static_script))) + (dst_dir / "libc++.so").write_text("INPUT(-lc++_shared)") + (dst_dir / "libc++.a").write_text("INPUT(-lc++_static -lc++abi)") @register @@ -1666,17 +1649,12 @@ class NdkBuild(ndk.builds.PackageModule): ) -> None: install_path = self.get_install_path() json_path = self.get_dep("meta").get_install_path() / (name + ".json") - meta = json.loads(ndk.file.read_file(json_path)) + with json_path.open(encoding="utf-8") as json_file: + meta = json.load(json_file) meta_vars = func(meta) - ndk.file.write_file( - install_path / "core/{}.mk".format(name), - var_dict_to_make(meta_vars), - ) - ndk.file.write_file( - install_path / "cmake/{}.cmake".format(name), - var_dict_to_cmake(meta_vars), - ) + (install_path / f"core/{name}.mk").write_text(var_dict_to_make(meta_vars)) + (install_path / f"cmake/{name}.cmake").write_text(var_dict_to_cmake(meta_vars)) @register @@ -1948,7 +1926,7 @@ class Meta(ndk.builds.PackageModule): system_libs = collections.OrderedDict(sorted(system_libs.items())) json_path = self.get_install_path() / "system_libs.json" - with open(json_path, "w") as json_file: + with json_path.open("w", encoding="utf-8") as json_file: json.dump(system_libs, json_file, indent=2, separators=(",", ": ")) @@ -1971,14 +1949,18 @@ class SourceProperties(ndk.builds.Module): def install(self) -> None: path = self.get_install_path() - with open(path, "w") as source_properties: - assert self.context is not None - version = get_version_string(self.context.build_number) - if ndk.config.beta > 0: - version += "-beta{}".format(ndk.config.beta) - source_properties.writelines( - ["Pkg.Desc = Android NDK\n", "Pkg.Revision = {}\n".format(version)] + assert self.context is not None + version = get_version_string(self.context.build_number) + if ndk.config.beta > 0: + version += "-beta{}".format(ndk.config.beta) + path.write_text( + textwrap.dedent( + f"""\ + Pkg.Desc = Android NDK + Pkg.Revision = {version} + """ ) + ) def create_notice_file(path: Path, for_group: ndk.builds.NoticeGroup) -> None: @@ -2270,17 +2252,16 @@ def parse_args() -> Tuple[argparse.Namespace, List[str]]: def log_build_failure(log_path: Path, dist_dir: Path) -> None: - with open(log_path, "r") as log_file: - contents = log_file.read() - print(contents) - - # The build server has a build_error.log file that is supposed to be - # the short log of the failure that stopped the build. Append our - # failing log to that. - build_error_log = dist_dir / "logs/build_error.log" - with open(build_error_log, "a") as error_log: - error_log.write("\n") - error_log.write(contents) + contents = log_path.read_text() + print(contents) + + # The build server has a build_error.log file that is supposed to be + # the short log of the failure that stopped the build. Append our + # failing log to that. + build_error_log = dist_dir / "logs/build_error.log" + with build_error_log.open("a", encoding="utf-8") as error_log: + error_log.write("\n") + error_log.write(contents) def launch_buildable( diff --git a/ndk/file.py b/ndk/file.py deleted file mode 100644 index 57a1ab775..000000000 --- a/ndk/file.py +++ /dev/null @@ -1,29 +0,0 @@ -# -# Copyright (C) 2018 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -"""Contains file I/O APIs.""" -from pathlib import Path - - -def read_file(path: Path) -> str: - """Reads the contents of a file into a string, closing the file.""" - with open(path) as the_file: - return the_file.read() - - -def write_file(path: Path, contents: str) -> None: - """Writes the given string to the path specified, closing the file.""" - with open(path, "w") as the_file: - the_file.write(contents) diff --git a/ndk/test/builder.py b/ndk/test/builder.py index f4b12714a..89f848c57 100644 --- a/ndk/test/builder.py +++ b/ndk/test/builder.py @@ -327,7 +327,7 @@ class TestBuilder: testlist.append(testobj) tests_json[str(config)] = testlist json_config_path = self.test_options.out_dir / "dist" / "tests.json" - with open(json_config_path, "w") as outfile: + with json_config_path.open("w", encoding="utf-8") as outfile: json.dump(tests_json, outfile, indent=2) shutil.copy2(json_config_path, self.test_options.package_path.parent) shutil.copy2( diff --git a/ndk/test/buildtest/case.py b/ndk/test/buildtest/case.py index 561085605..8313b2dda 100644 --- a/ndk/test/buildtest/case.py +++ b/ndk/test/buildtest/case.py @@ -298,7 +298,7 @@ def _platform_from_application_mk(test_dir: Path) -> Optional[int]: if not application_mk.exists(): return None - with open(application_mk) as application_mk_file: + with application_mk.open(encoding="utf-8") as application_mk_file: for line in application_mk_file: if line.startswith("APP_PLATFORM"): _, platform_str = line.split(":=") diff --git a/ndk/test/devices.py b/ndk/test/devices.py index bf2079010..99abe1537 100644 --- a/ndk/test/devices.py +++ b/ndk/test/devices.py @@ -425,7 +425,7 @@ def exclude_device(device: Device) -> bool: exclusion_list_env = os.getenv("NDK_DEVICE_EXCLUSION_LIST") if exclusion_list_env is None: return False - exclusion_list = Path(exclusion_list_env).read_text().splitlines() + exclusion_list = Path(exclusion_list_env).read_text(encoding="utf-8").splitlines() return device.serial in exclusion_list diff --git a/ndk/test/spec.py b/ndk/test/spec.py index 8dc53aad2..5c13c8654 100644 --- a/ndk/test/spec.py +++ b/ndk/test/spec.py @@ -83,7 +83,7 @@ class TestSpec: @classmethod def load(cls, path: Path, abis: Optional[Iterable[Abi]] = None) -> TestSpec: - with open(path) as config_file: + with path.open(encoding="utf-8") as config_file: test_config: dict[str, Any] = json.load(config_file) if abis is None: abis = test_config.get("abis", ndk.abis.ALL_ABIS) |