]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Scripts/ConvertUni.py
2 # Check a patch for various format issues
4 # Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
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
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
12 # BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
16 from __future__
import print_function
19 __copyright__
= "Copyright (c) 2015, Intel Corporation All rights reserved."
27 from io
import StringIO
29 from StringIO
import StringIO
32 """Converts utf-16 to utf-8 for one command line argument.
34 This could be a single file, or a directory.
37 def __init__(self
, utf8
, source
):
43 if not os
.path
.exists(source
):
45 elif os
.path
.isdir(source
):
46 for (root
, dirs
, files
) in os
.walk(source
):
47 files
= filter(lambda a
: a
.endswith('.uni'), files
)
48 for filename
in files
:
49 path
= os
.path
.join(root
, filename
)
50 self
.ok
&= self
.convert_one_file(path
)
57 self
.ok
&= self
.convert_one_file(source
)
59 def convert_one_file(self
, source
):
61 new_enc
, old_enc
= 'utf-8', 'utf-16'
63 new_enc
, old_enc
= 'utf-16', 'utf-8'
67 f
= open(source
, mode
='rb')
68 file_content
= f
.read()
72 # Detect UTF-16 Byte Order Mark at beginning of file.
74 bom
= (file_content
.startswith(codecs
.BOM_UTF16_BE
) or
75 file_content
.startswith(codecs
.BOM_UTF16_LE
))
77 print("%s: already %s" % (source
, new_enc
))
81 # Decode old string data
83 str_content
= file_content
.decode(old_enc
, 'ignore')
86 # Encode new string data
88 new_content
= str_content
.encode(new_enc
, 'ignore')
91 # Write converted data back to file
93 f
= open(source
, mode
='wb')
97 print(source
+ ": converted, size", len(file_content
), '=>', len(new_content
))
102 """Converts .uni files between utf-16 and utf-8."""
106 sources
= self
.args
.source
109 for patch
in sources
:
110 self
.process_one_arg(patch
)
117 def process_one_arg(self
, arg
):
118 self
.ok
&= ConvertOneArg(self
.utf8
, arg
).ok
120 def parse_options(self
):
121 parser
= argparse
.ArgumentParser(description
=__copyright__
)
122 parser
.add_argument('--version', action
='version',
123 version
='%(prog)s ' + VersionNumber
)
124 parser
.add_argument('source', nargs
='+',
125 help='[uni file | directory]')
126 group
= parser
.add_mutually_exclusive_group()
127 group
.add_argument("--utf-8",
129 help="Convert from utf-16 to utf-8 [default]")
130 group
.add_argument("--utf-16",
132 help="Convert from utf-8 to utf-16")
133 self
.args
= parser
.parse_args()
134 self
.utf8
= not self
.args
.utf_16
136 if __name__
== "__main__":
137 sys
.exit(ConvertUniApp().retval
)