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