Bobby R. Bruce has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/39795 )

Change subject: misc,python: Fix Pre-commit hooks to ignore non-utf-8 files
......................................................................

misc,python: Fix Pre-commit hooks to ignore non-utf-8 files

Previously if binary blobs were modified the pre-commit hook attempted
to run style-checks on the binary, causing an error when attempting to
decode to utf-8. This commit runs a check on each file to ensure it's
utf-8 encoded before running pre-commit procedures. If not utf-8, the
pre-commit checks are not performed on that file.

Change-Id: Id1263cac0d6c190ad1a3d67720b3f373e0e42234
Issue-on: https://gem5.atlassian.net/browse/GEM5-903
---
M util/style/style.py
1 file changed, 27 insertions(+), 1 deletion(-)



diff --git a/util/style/style.py b/util/style/style.py
index e765a92..2fb3def 100644
--- a/util/style/style.py
+++ b/util/style/style.py
@@ -40,6 +40,7 @@
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 from abc import ABCMeta, abstractmethod
+import codecs
 import difflib
 import re
 import sys
@@ -86,6 +87,29 @@
         return rex.match(fname)
     return match_re

+def _not_utf8(fname: str) -> bool:
+    """Returns True if a file is not utf-8.
+
+    Parameters
+    ----------
+    fname: str
+        The file to check
+
+    Returns
+    -------
+    bool
+        False if the file is utf-8 formatted, otherwise True.
+    """
+
+    try:
+        f = codecs.open(fname, encoding='utf-8', errors='strict')
+        for line in f:
+            pass
+    except UnicodeDecodeError:
+        return True
+
+    return False
+
 # This list contains a list of functions that are called to determine
 # if a file should be excluded from the style matching rules or
 # not. The functions are called with the file name relative to the
@@ -101,7 +125,9 @@
     # project that does not follow the gem5 coding convention
     _re_ignore("tests/test-progs/asmtest/src/riscv/"),
     # Ignore RISC-V assembly dump files
-    _re_ignore("tests/test-progs/asmtest/dump/riscv/")
+    _re_ignore("tests/test-progs/asmtest/dump/riscv/"),
+    # Ignore files that are not utf-8 formatted.
+    _not_utf8
 ]

 def check_ignores(fname):

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/39795
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: Id1263cac0d6c190ad1a3d67720b3f373e0e42234
Gerrit-Change-Number: 39795
Gerrit-PatchSet: 1
Gerrit-Owner: Bobby R. Bruce <bbr...@ucdavis.edu>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to