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