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
12from __future__ import absolute_import, print_function, unicode_literals
13
14import os
15import sys
16
17from mozversioncontrol import get_repository_from_env
18
19
20scriptname = os.path.basename(__file__)
21expected_encoding = "ascii"
22
23# The following files don't define JSErrorFormatString.
24ignore_files = [
25    "dom/base/domerr.msg",
26    "js/xpconnect/src/xpc.msg",
27]
28
29
30def log_pass(filename, text):
31    print("TEST-PASS | {} | {} | {}".format(scriptname, filename, text))
32
33
34def log_fail(filename, text):
35    print("TEST-UNEXPECTED-FAIL | {} | {} | {}".format(scriptname, filename, text))
36
37
38def check_single_file(filename):
39    with open(filename, "rb") as f:
40        data = f.read()
41        try:
42            data.decode(expected_encoding)
43        except Exception:
44            log_fail(filename, "not in {} encoding".format(expected_encoding))
45
46    log_pass(filename, "ok")
47    return True
48
49
50def check_files():
51    result = True
52
53    with get_repository_from_env() as repo:
54        root = repo.path
55
56        for filename, _ in repo.get_tracked_files_finder().find("**/*.msg"):
57            if filename not in ignore_files:
58                if not check_single_file(os.path.join(root, filename)):
59                    result = False
60
61    return result
62
63
64def main():
65    if not check_files():
66        sys.exit(1)
67
68    sys.exit(0)
69
70
71if __name__ == "__main__":
72    main()
73