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