VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/BaseTools/Scripts/ConvertUni.py@ 77662

Last change on this file since 77662 was 77662, checked in by vboxsync, 6 years ago

EFI: First step in UDK2018 merge. Does not build yet.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 4.0 KB
Line 
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
16from __future__ import print_function
17
18VersionNumber = '0.1'
19__copyright__ = "Copyright (c) 2015, Intel Corporation All rights reserved."
20
21import argparse
22import codecs
23import os
24import sys
25
26try:
27 from io import StringIO
28except ImportError:
29 from StringIO import StringIO
30
31class ConvertOneArg:
32 """Converts utf-16 to utf-8 for one command line argument.
33
34 This could be a single file, or a directory.
35 """
36
37 def __init__(self, utf8, source):
38 self.utf8 = utf8
39 self.source = source
40
41 self.ok = True
42
43 if not os.path.exists(source):
44 self.ok = False
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)
51 if not self.ok:
52 break
53
54 if not self.ok:
55 break
56 else:
57 self.ok &= self.convert_one_file(source)
58
59 def convert_one_file(self, source):
60 if self.utf8:
61 new_enc, old_enc = 'utf-8', 'utf-16'
62 else:
63 new_enc, old_enc = 'utf-16', 'utf-8'
64 #
65 # Read file
66 #
67 f = open(source, mode='rb')
68 file_content = f.read()
69 f.close()
70
71 #
72 # Detect UTF-16 Byte Order Mark at beginning of file.
73 #
74 bom = (file_content.startswith(codecs.BOM_UTF16_BE) or
75 file_content.startswith(codecs.BOM_UTF16_LE))
76 if bom != self.utf8:
77 print("%s: already %s" % (source, new_enc))
78 return True
79
80 #
81 # Decode old string data
82 #
83 str_content = file_content.decode(old_enc, 'ignore')
84
85 #
86 # Encode new string data
87 #
88 new_content = str_content.encode(new_enc, 'ignore')
89
90 #
91 # Write converted data back to file
92 #
93 f = open(source, mode='wb')
94 f.write(new_content)
95 f.close()
96
97 print(source + ": converted, size", len(file_content), '=>', len(new_content))
98 return True
99
100
101class ConvertUniApp:
102 """Converts .uni files between utf-16 and utf-8."""
103
104 def __init__(self):
105 self.parse_options()
106 sources = self.args.source
107
108 self.ok = True
109 for patch in sources:
110 self.process_one_arg(patch)
111
112 if self.ok:
113 self.retval = 0
114 else:
115 self.retval = -1
116
117 def process_one_arg(self, arg):
118 self.ok &= ConvertOneArg(self.utf8, arg).ok
119
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",
128 action="store_true",
129 help="Convert from utf-16 to utf-8 [default]")
130 group.add_argument("--utf-16",
131 action="store_true",
132 help="Convert from utf-8 to utf-16")
133 self.args = parser.parse_args()
134 self.utf8 = not self.args.utf_16
135
136if __name__ == "__main__":
137 sys.exit(ConvertUniApp().retval)
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette