| 1 |
# vim: set ts=8 sts=4 et sw=4 tw=99: |
| 2 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 |
|
| 6 |
# ---------------------------------------------------------------------------- |
| 7 |
# This script checks encoding of the files that define JSErrorFormatStrings. |
| 8 |
# |
| 9 |
# JSErrorFormatString.format member should be in ASCII encoding. |
| 10 |
# ---------------------------------------------------------------------------- |
| 11 |
|
| 12 |
import os |
| 13 |
import sys |
| 14 |
|
| 15 |
from mozversioncontrol import get_repository_from_env |
| 16 |
|
| 17 |
scriptname = os.path.basename(__file__) |
| 18 |
expected_encoding = "ascii" |
| 19 |
|
| 20 |
# The following files don't define JSErrorFormatString. |
| 21 |
ignore_files = [ |
| 22 |
"dom/base/domerr.msg", |
| 23 |
"js/xpconnect/src/xpc.msg", |
| 24 |
] |
| 25 |
|
| 26 |
|
| 27 |
def log_pass(filename, text): |
| 28 |
print(f"TEST-PASS | {scriptname} | {filename} | {text}") |
| 29 |
|
| 30 |
|
| 31 |
def log_fail(filename, text): |
| 32 |
print(f"TEST-UNEXPECTED-FAIL | {scriptname} | {filename} | {text}") |
| 33 |
|
| 34 |
|
| 35 |
def check_single_file(filename): |
| 36 |
with open(filename, "rb") as f: |
| 37 |
data = f.read() |
| 38 |
try: |
| 39 |
data.decode(expected_encoding) |
| 40 |
except Exception: |
| 41 |
log_fail(filename, f"not in {expected_encoding} encoding") |
| 42 |
|
| 43 |
log_pass(filename, "ok") |
| 44 |
return True |
| 45 |
|
| 46 |
|
| 47 |
def check_files(): |
| 48 |
result = True |
| 49 |
|
| 50 |
with get_repository_from_env() as repo: |
| 51 |
root = repo.path |
| 52 |
|
| 53 |
for filename, _ in repo.get_tracked_files_finder().find("**/*.msg"): |
| 54 |
if filename not in ignore_files: |
| 55 |
if not check_single_file(os.path.join(root, filename)): |
| 56 |
result = False |
| 57 |
|
| 58 |
return result |
| 59 |
|
| 60 |
|
| 61 |
def main(): |
| 62 |
if not check_files(): |
| 63 |
sys.exit(1) |
| 64 |
|
| 65 |
sys.exit(0) |
| 66 |
|
| 67 |
|
| 68 |
if __name__ == "__main__": |
| 69 |
main() |