Skip to content

Commit

Permalink
Setup pre-commit hooks. (#4250)
Browse files Browse the repository at this point in the history
Adds pre-commit hooks to just Cobalt added directories and files.

b/372338856
  • Loading branch information
briantting authored Oct 12, 2024
1 parent 8b563ac commit 102a04d
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 0 deletions.
96 changes: 96 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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?$'
2 changes: 2 additions & 0 deletions chrobalt/precommit/.codespellignorelines
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vp9AllowList.get("Technicolor").add("STING");
// Onces represent initializations that should only ever happen once per
34 changes: 34 additions & 0 deletions chrobalt/precommit/gcheckstyle_wrapper.py
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions chrobalt/precommit/google_java_format_wrapper.py
Original file line number Diff line number Diff line change
@@ -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)
52 changes: 52 additions & 0 deletions chrobalt/precommit/python3_check.py
Original file line number Diff line number Diff line change
@@ -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:]))

0 comments on commit 102a04d

Please sign in to comment.