check_js_msg_encoding.py 1.8 KB

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