ConvertUni.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ## @file
  2. # Check a patch for various format issues
  3. #
  4. # Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
  5. #
  6. # SPDX-License-Identifier: BSD-2-Clause-Patent
  7. #
  8. from __future__ import print_function
  9. VersionNumber = '0.1'
  10. __copyright__ = "Copyright (c) 2015, Intel Corporation All rights reserved."
  11. import argparse
  12. import codecs
  13. import os
  14. import sys
  15. class ConvertOneArg:
  16. """Converts utf-16 to utf-8 for one command line argument.
  17. This could be a single file, or a directory.
  18. """
  19. def __init__(self, utf8, source):
  20. self.utf8 = utf8
  21. self.source = source
  22. self.ok = True
  23. if not os.path.exists(source):
  24. self.ok = False
  25. elif os.path.isdir(source):
  26. for (root, dirs, files) in os.walk(source):
  27. files = filter(lambda a: a.endswith('.uni'), files)
  28. for filename in files:
  29. path = os.path.join(root, filename)
  30. self.ok &= self.convert_one_file(path)
  31. if not self.ok:
  32. break
  33. if not self.ok:
  34. break
  35. else:
  36. self.ok &= self.convert_one_file(source)
  37. def convert_one_file(self, source):
  38. if self.utf8:
  39. new_enc, old_enc = 'utf-8', 'utf-16'
  40. else:
  41. new_enc, old_enc = 'utf-16', 'utf-8'
  42. #
  43. # Read file
  44. #
  45. f = open(source, mode='rb')
  46. file_content = f.read()
  47. f.close()
  48. #
  49. # Detect UTF-16 Byte Order Mark at beginning of file.
  50. #
  51. bom = (file_content.startswith(codecs.BOM_UTF16_BE) or
  52. file_content.startswith(codecs.BOM_UTF16_LE))
  53. if bom != self.utf8:
  54. print("%s: already %s" % (source, new_enc))
  55. return True
  56. #
  57. # Decode old string data
  58. #
  59. str_content = file_content.decode(old_enc, 'ignore')
  60. #
  61. # Encode new string data
  62. #
  63. new_content = str_content.encode(new_enc, 'ignore')
  64. #
  65. # Write converted data back to file
  66. #
  67. f = open(source, mode='wb')
  68. f.write(new_content)
  69. f.close()
  70. print(source + ": converted, size", len(file_content), '=>', len(new_content))
  71. return True
  72. class ConvertUniApp:
  73. """Converts .uni files between utf-16 and utf-8."""
  74. def __init__(self):
  75. self.parse_options()
  76. sources = self.args.source
  77. self.ok = True
  78. for patch in sources:
  79. self.process_one_arg(patch)
  80. if self.ok:
  81. self.retval = 0
  82. else:
  83. self.retval = -1
  84. def process_one_arg(self, arg):
  85. self.ok &= ConvertOneArg(self.utf8, arg).ok
  86. def parse_options(self):
  87. parser = argparse.ArgumentParser(description=__copyright__)
  88. parser.add_argument('--version', action='version',
  89. version='%(prog)s ' + VersionNumber)
  90. parser.add_argument('source', nargs='+',
  91. help='[uni file | directory]')
  92. group = parser.add_mutually_exclusive_group()
  93. group.add_argument("--utf-8",
  94. action="store_true",
  95. help="Convert from utf-16 to utf-8 [default]")
  96. group.add_argument("--utf-16",
  97. action="store_true",
  98. help="Convert from utf-8 to utf-16")
  99. self.args = parser.parse_args()
  100. self.utf8 = not self.args.utf_16
  101. if __name__ == "__main__":
  102. sys.exit(ConvertUniApp().retval)