From 95195b557466708c06f9b631c3909a557dcd5b57 Mon Sep 17 00:00:00 2001 From: Pieter De Gendt Date: Tue, 15 Oct 2024 11:12:19 +0200 Subject: [PATCH] scripts: ci: check_compliance: Add text encoding check Add a check for text files to make sure these are encoded in ascii or utf-8. Signed-off-by: Pieter De Gendt --- .gitignore | 1 + scripts/ci/check_compliance.py | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/.gitignore b/.gitignore index f9fa32b7a6b..c33e2d93747 100644 --- a/.gitignore +++ b/.gitignore @@ -103,4 +103,5 @@ ModulesMaintainers.txt Nits.txt Pylint.txt SphinxLint.txt +TextEncoding.txt YAMLLint.txt diff --git a/scripts/ci/check_compliance.py b/scripts/ci/check_compliance.py index 038370ae3c4..f586b53d147 100755 --- a/scripts/ci/check_compliance.py +++ b/scripts/ci/check_compliance.py @@ -1606,6 +1606,33 @@ class KeepSorted(ComplianceTest): with open(file, "r") as fp: self.check_file(file, fp) + +class TextEncoding(ComplianceTest): + """ + Check that any text file is encoded in ascii or utf-8. + """ + name = "TextEncoding" + doc = "Check the encoding of text files." + path_hint = "" + + ALLOWED_CHARSETS = ["us-ascii", "utf-8"] + + def run(self): + m = magic.Magic(mime=True, mime_encoding=True) + + for file in get_files(filter="d"): + full_path = os.path.join(GIT_TOP, file) + mime_type = m.from_file(full_path) + + if not mime_type.startswith("text/"): + continue + + # format is "text/; charset=" + if mime_type.rsplit('=')[-1] not in self.ALLOWED_CHARSETS: + desc = f"Text file with unsupported encoding: {file} has mime type {mime_type}" + self.fmtd_failure("error", "TextEncoding", file, desc=desc) + + def init_logs(cli_arg): # Initializes logging