From 16169dfe093bfef5b9916beba6acd7f02d335701 Mon Sep 17 00:00:00 2001 From: AlexeyBarabash Date: Fri, 16 Jun 2023 12:05:56 +0300 Subject: [PATCH] Make upstream's remove_unused_imports.py to be usable at Brave repo (#18903) This PR introduces remove_unused_imports.sh script which wraps the same upstream's tool and makes it usable at Brave repo. Fixes brave/brave-browser#31045 --- PRESUBMIT.py | 6 +--- .../checkstyle/remove_unused_imports.py | 32 +++++++++++++++++++ .../checkstyle/remove_unused_imports.sh | 15 +++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 tools/android/checkstyle/remove_unused_imports.py create mode 100755 tools/android/checkstyle/remove_unused_imports.sh diff --git a/PRESUBMIT.py b/PRESUBMIT.py index 1de8ca7bc6a..aacd64e1468 100644 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -391,15 +391,11 @@ def CheckJavaStyle(_original_check, input_api, output_api): if warnings: ret.append(output_api.PresubmitPromptWarning('\n'.join(warnings))) if errors: - remove_unused_imports_path = input_api.os_path.join( - input_api.PresubmitLocalPath(), 'tools', 'android', 'checkstyle', - 'remove_unused_imports.py') msg = '\n'.join(errors) if 'Unused import:' in msg or 'Duplicate import' in msg: msg += """ -To remove unused imports: """ + input_api.os_path.relpath( - remove_unused_imports_path, local_path) +To remove unused imports: ./tools/android/checkstyle/remove_unused_imports.sh""" ret.append(output_api.PresubmitError(msg)) return ret diff --git a/tools/android/checkstyle/remove_unused_imports.py b/tools/android/checkstyle/remove_unused_imports.py new file mode 100644 index 00000000000..d982e233535 --- /dev/null +++ b/tools/android/checkstyle/remove_unused_imports.py @@ -0,0 +1,32 @@ +# Copyright (c) 2023 The Brave Authors. All rights reserved. +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at https://mozilla.org/MPL/2.0/. + +import os + + +def main(): + src_dir = os.path.abspath( + os.path.join(__file__, os.pardir, os.pardir, os.pardir, os.pardir, + os.pardir)) + chromium_checkstyle_dir = os.path.join(src_dir, "tools", "android", + "checkstyle") + chromium_script = os.path.join(chromium_checkstyle_dir, + "remove_unused_imports.py") + chromium_style_path = os.path.join(chromium_checkstyle_dir, + "unused-imports.xml") + + with open(chromium_script, "r") as f: + contents = f.read() + contents = contents.replace("main()", "chromium_main()") + contents = contents.replace("if __name__ == '__main__':", + "if __name__ != '__main__':") + exec(contents, globals(), globals()) # pylint: disable=exec-used + + globals()['_STYLE_FILE'] = chromium_style_path + globals()['chromium_main']() + + +if __name__ == '__main__': + main() diff --git a/tools/android/checkstyle/remove_unused_imports.sh b/tools/android/checkstyle/remove_unused_imports.sh new file mode 100755 index 00000000000..5c9d917765a --- /dev/null +++ b/tools/android/checkstyle/remove_unused_imports.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +# Copyright (c) 2023 The Brave Authors. All rights reserved. +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at https://mozilla.org/MPL/2.0/. + +SCRIPT_DIR="$( dirname -- "${BASH_SOURCE[0]}"; )" +SCRIPT_DIR="$( realpath -e -- "$SCRIPT_DIR"; )" + +CHROMIUM_CHECKSTYLE_DIR="$SCRIPT_DIR/../../../../tools/android/checkstyle" +CHROMIUM_CHECKSTYLE_DIR="$( realpath -e -- "$CHROMIUM_CHECKSTYLE_DIR"; )" +export PYTHONPATH="$CHROMIUM_CHECKSTYLE_DIR" + +python3 "$SCRIPT_DIR/remove_unused_imports.py"