]>
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 # SPDX-License-Identifier: BSD-2-Clause-Patent
9 from __future__
import print_function
12 __copyright__
= "Copyright (c) 2015, Intel Corporation All rights reserved."
20 """Converts utf-16 to utf-8 for one command line argument.
22 This could be a single file, or a directory.
25 def __init__(self
, utf8
, source
):
31 if not os
.path
.exists(source
):
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
)
45 self
.ok
&= self
.convert_one_file(source
)
47 def convert_one_file(self
, source
):
49 new_enc
, old_enc
= 'utf-8', 'utf-16'
51 new_enc
, old_enc
= 'utf-16', 'utf-8'
55 f
= open(source
, mode
='rb')
56 file_content
= f
.read()
60 # Detect UTF-16 Byte Order Mark at beginning of file.
62 bom
= (file_content
.startswith(codecs
.BOM_UTF16_BE
) or
63 file_content
.startswith(codecs
.BOM_UTF16_LE
))
65 print("%s: already %s" % (source
, new_enc
))
69 # Decode old string data
71 str_content
= file_content
.decode(old_enc
, 'ignore')
74 # Encode new string data
76 new_content
= str_content
.encode(new_enc
, 'ignore')
79 # Write converted data back to file
81 f
= open(source
, mode
='wb')
85 print(source
+ ": converted, size", len(file_content
), '=>', len(new_content
))
90 """Converts .uni files between utf-16 and utf-8."""
94 sources
= self
.args
.source
98 self
.process_one_arg(patch
)
105 def process_one_arg(self
, arg
):
106 self
.ok
&= ConvertOneArg(self
.utf8
, arg
).ok
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",
117 help="Convert from utf-16 to utf-8 [default]")
118 group
.add_argument("--utf-16",
120 help="Convert from utf-8 to utf-16")
121 self
.args
= parser
.parse_args()
122 self
.utf8
= not self
.args
.utf_16
124 if __name__
== "__main__":
125 sys
.exit(ConvertUniApp().retval
)