diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000000..2ac3403b169b4 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,96 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +default_stages: [pre-commit] + +default_language_version: + python: python3 + +files: | + (?x)^( + .github/| + chrobalt/| + starboard/| + .pre-commit-config.yaml| + docker-compose.yaml + ) + +repos: +- repo: https://cobalt.googlesource.com/pre-commit-hooks + rev: v5.0.0 + hooks: + - id: check-case-conflict + - id: check-xml + - id: check-yaml + - id: end-of-file-fixer + - id: mixed-line-ending + - id: trailing-whitespace + +- repo: https://cobalt.googlesource.com/codespell + rev: v2.3.0 + hooks: + - id: codespell + name: Spell Check + args: [-x, chrobalt/precommit/.codespellignorelines, + # The --ignore-words-list argument has a bug where it needs to + # be lowercase, see + # https://github.com/codespell-project/codespell/issues/1390 + --ignore-words-list, "atleast,varius", + ] + exclude: | + (?x)^( + starboard/content/ssl/certs/| + starboard/loader_app/app_key_test.cc| + starboard/shared/starboard/player/testdata + ) + +- repo: local + hooks: + - id: clang-format + name: clang-format + entry: clang-format + language: python + types: [c++] + args: [-i, -style=file] + additional_dependencies: ['clang-format'] + - id: cpplint + name: cpplint + entry: cpplint + language: python + types: [c++] + args: [--verbose=4, --quiet] + additional_dependencies: ['cpplint'] + - id: yapf + name: yapf + entry: yapf + language: python + types: [python] + args: [-i, -vv] + additional_dependencies: ['yapf'] + - id: pylint + name: pylint + entry: pylint + language: python + types: [python] + args: [-d W0201] + additional_dependencies: ['pylint'] + - id: python3-compatibility-check + name: Python 3 Compatibility Check + entry: python3 ./chrobalt/precommit/python3_check.py + language: python + types: [python] + - id: google-java-format + name: google-java-format + entry: python3 ./chrobalt/precommit/google_java_format_wrapper.py + language: python + types: [java] + args: [-i] + - id: gcheckstyle + name: gcheckstyle + entry: python3 ./chrobalt/precommit/gcheckstyle_wrapper.py + language: python + types: [java] + - id: gn-format + name: gn-format + entry: gn format + language: system + files: '.*\.gni?$' diff --git a/chrobalt/precommit/.codespellignorelines b/chrobalt/precommit/.codespellignorelines new file mode 100644 index 0000000000000..5046ccd8b7877 --- /dev/null +++ b/chrobalt/precommit/.codespellignorelines @@ -0,0 +1,2 @@ + vp9AllowList.get("Technicolor").add("STING"); +// Onces represent initializations that should only ever happen once per diff --git a/chrobalt/precommit/gcheckstyle_wrapper.py b/chrobalt/precommit/gcheckstyle_wrapper.py new file mode 100644 index 0000000000000..01aeee3243f31 --- /dev/null +++ b/chrobalt/precommit/gcheckstyle_wrapper.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +# +# Copyright 2024 The Cobalt Authors. All Rights Reserved. +# +# 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. +"""Wrapper to run internal gcheckstyle tool.""" + +import subprocess +import sys + +if __name__ == '__main__': + gcheckstyle_args = sys.argv[1:] + + try: + sys.exit( + subprocess.call([ + '/home/build/nonconf/google3/tools/java/checkstyle/gcheckstyle.sh' + ] + gcheckstyle_args)) + except FileNotFoundError: + print('gcheckstyle not found, skipping.') + sys.exit(0) + except OSError as e: + print('You may need to run gcert.') + raise e diff --git a/chrobalt/precommit/google_java_format_wrapper.py b/chrobalt/precommit/google_java_format_wrapper.py new file mode 100644 index 0000000000000..0f3510fd0e596 --- /dev/null +++ b/chrobalt/precommit/google_java_format_wrapper.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +# +# Copyright 2024 The Cobalt Authors. All Rights Reserved. +# +# 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. +"""Wrapper to run google-java-format tool.""" + +import platform +import subprocess +import sys + +if __name__ == '__main__': + if platform.system() != 'Linux': + sys.exit(0) + + google_java_format_args = sys.argv[1:] + try: + sys.exit( + subprocess.call(['google-java-format'] + google_java_format_args)) + except FileNotFoundError: + print('google-java-format not found, skipping.') + sys.exit(0) diff --git a/chrobalt/precommit/python3_check.py b/chrobalt/precommit/python3_check.py new file mode 100644 index 0000000000000..523562f4c208c --- /dev/null +++ b/chrobalt/precommit/python3_check.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +# +# Copyright 2024 The Cobalt Authors. All Rights Reserved. +# +# 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. +"""Warns if Python code isn't compatible with Python 3.""" + +import os +import py_compile +import sys +import tempfile +from typing import List + + +def _check_file_for_python3_compatibility(filename: str) -> bool: + if not os.path.exists(filename): + print(f'{filename} is not a valid path, skipping.') + return False + + temp_directory = tempfile.TemporaryDirectory() # pylint: disable=consider-using-with + temp_file = os.path.join(temp_directory.name, 'cfile') + had_errors = False + + try: + py_compile.compile(filename, cfile=temp_file, doraise=True) + except py_compile.PyCompileError as e: + print(e) + print(f'{filename} is not valid in Python 3, consider updating it.') + had_errors = True + + temp_directory.cleanup() + return had_errors + + +def check_files_for_python3_compatibility(files: List[str]) -> bool: + """Checks files for Python 3 compatibility.""" + rets = [_check_file_for_python3_compatibility(file) for file in files] + return any(rets) + + +if __name__ == '__main__': + sys.exit(check_files_for_python3_compatibility(sys.argv[1:]))