]>
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 """Converts utf-16 to utf-8 for one command line argument.
29 This could be a single file, or a directory.
32 def __init__(self
, utf8
, source
):
38 if not os
.path
.exists(source
):
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
)
52 self
.ok
&= self
.convert_one_file(source
)
54 def convert_one_file(self
, source
):
56 new_enc
, old_enc
= 'utf-8', 'utf-16'
58 new_enc
, old_enc
= 'utf-16', 'utf-8'
62 f
= open(source
, mode
='rb')
63 file_content
= f
.read()
67 # Detect UTF-16 Byte Order Mark at beginning of file.
69 bom
= (file_content
.startswith(codecs
.BOM_UTF16_BE
) or
70 file_content
.startswith(codecs
.BOM_UTF16_LE
))
72 print("%s: already %s" % (source
, new_enc
))
76 # Decode old string data
78 str_content
= file_content
.decode(old_enc
, 'ignore')
81 # Encode new string data
83 new_content
= str_content
.encode(new_enc
, 'ignore')
86 # Write converted data back to file
88 f
= open(source
, mode
='wb')
92 print(source
+ ": converted, size", len(file_content
), '=>', len(new_content
))
97 """Converts .uni files between utf-16 and utf-8."""
101 sources
= self
.args
.source
104 for patch
in sources
:
105 self
.process_one_arg(patch
)
112 def process_one_arg(self
, arg
):
113 self
.ok
&= ConvertOneArg(self
.utf8
, arg
).ok
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",
124 help="Convert from utf-16 to utf-8 [default]")
125 group
.add_argument("--utf-16",
127 help="Convert from utf-8 to utf-16")
128 self
.args
= parser
.parse_args()
129 self
.utf8
= not self
.args
.utf_16
131 if __name__
== "__main__":
132 sys
.exit(ConvertUniApp().retval
)