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