OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import argparse | 5 import argparse |
6 import os | 6 import os |
7 import shutil | 7 import shutil |
8 import sys | 8 import sys |
9 | 9 |
10 | 10 |
11 def detect_encoding(data, default_encoding='UTF-8'): | 11 def DetectEncoding(data, default_encoding='UTF-8'): |
12 """Detects the encoding used by |data| from the Byte-Order-Mark if present. | 12 """Detects the encoding used by |data| from the Byte-Order-Mark if present. |
13 | 13 |
14 Args: | 14 Args: |
15 data: string whose encoding needs to be detected | 15 data: string whose encoding needs to be detected |
16 default_encoding: encoding returned if no BOM is found. | 16 default_encoding: encoding returned if no BOM is found. |
17 | 17 |
18 Returns: | 18 Returns: |
19 The encoding determined from the BOM if present or |default_encoding| if | 19 The encoding determined from the BOM if present or |default_encoding| if |
20 no BOM was found. | 20 no BOM was found. |
21 """ | 21 """ |
22 if data.startswith('\xFE\xFF'): | 22 if data.startswith('\xFE\xFF'): |
23 return 'UTF-16BE' | 23 return 'UTF-16BE' |
24 | 24 |
25 if data.startswith('\xFF\xFE'): | 25 if data.startswith('\xFF\xFE'): |
26 return 'UTF-16LE' | 26 return 'UTF-16LE' |
27 | 27 |
28 if data.startswith('\xEF\xBB\xBF'): | 28 if data.startswith('\xEF\xBB\xBF'): |
29 return 'UTF-8' | 29 return 'UTF-8' |
30 | 30 |
31 return default_encoding | 31 return default_encoding |
32 | 32 |
33 | 33 |
34 def copy_strings_file(source, dest): | 34 def CopyStringsFile(source, dest, strings_format): |
35 """Copies a .strings file from |source| to |dest| and convert it to UTF-16. | 35 """Copies a .strings file from |source| to |dest| and convert it to UTF-16. |
36 | 36 |
37 Args: | 37 Args: |
38 source: string, path to the source file | 38 source: string, path to the source file |
39 dest: string, path to the destination file | 39 dest: string, path to the destination file |
40 """ | 40 """ |
41 with open(source, 'rb') as source_file: | 41 with open(source, 'rb') as source_file: |
42 data = source_file.read() | 42 data = source_file.read() |
43 | 43 |
44 # Xcode's CpyCopyStringsFile / builtin-copyStrings seems to call | 44 # Xcode's CpyCopyStringsFile / builtin-copyStrings seems to call |
45 # CFPropertyListCreateFromXMLData() behind the scenes; at least it prints | 45 # CFPropertyListCreateFromXMLData() behind the scenes; at least it prints |
46 # CFPropertyListCreateFromXMLData(): Old-style plist parser: missing | 46 # CFPropertyListCreateFromXMLData(): Old-style plist parser: missing |
47 # semicolon in dictionary. | 47 # semicolon in dictionary. |
48 # on invalid files. Do the same kind of validation. | 48 # on invalid files. Do the same kind of validation. |
49 from CoreFoundation import CFDataCreate, CFPropertyListCreateFromXMLData | 49 import CoreFoundation as CF |
50 cfdata = CFDataCreate(None, data, len(data)) | 50 cfdata = CF.CFDataCreate(None, data, len(data)) |
51 _, error = CFPropertyListCreateFromXMLData(None, cfdata, 0, None) | 51 plist, error = CF.CFPropertyListCreateFromXMLData(None, cfdata, 0, None) |
52 if error: | 52 if error: |
53 raise ValueError(error) | 53 raise ValueError(error) |
54 | 54 |
55 encoding = detect_encoding(data) | 55 if strings_format == 'legacy': |
56 with open(dest, 'wb') as dest_file: | 56 encoding = DetectEncoding(data) |
57 dest_file.write(data.decode(encoding).encode('UTF-16')) | 57 with open(dest, 'wb') as dest_file: |
| 58 dest_file.write(data.decode(encoding).encode('UTF-16')) |
| 59 else: |
| 60 cfformat = { |
| 61 'xml1': CF.kCFPropertyListXMLFormat_v1_0, |
| 62 'binary1': CF.kCFPropertyListBinaryFormat_v1_0, |
| 63 }[strings_format] |
| 64 cfdata, error = CF.CFPropertyListCreateData( |
| 65 None, plist, CF.kCFPropertyListBinaryFormat_v1_0, |
| 66 0, None) |
| 67 if error: |
| 68 raise ValueError(error) |
| 69 |
| 70 data = CF.CFDataGetBytes( |
| 71 cfdata, CF.CFRangeMake(0, CF.CFDataGetLength(cfdata)), None) |
| 72 with open(dest, 'wb') as dest_file: |
| 73 dest_file.write(data) |
58 | 74 |
59 | 75 |
60 def copy_file(source, dest): | 76 def CopyFile(source, dest, strings_format): |
61 """Copies a file or directory from |source| to |dest|. | 77 """Copies a file or directory from |source| to |dest|. |
62 | 78 |
63 Args: | 79 Args: |
64 source: string, path to the source file | 80 source: string, path to the source file |
65 dest: string, path to the destination file | 81 dest: string, path to the destination file |
66 """ | 82 """ |
67 if os.path.isdir(source): | 83 if os.path.isdir(source): |
68 if os.path.exists(dest): | 84 if os.path.exists(dest): |
69 shutil.rmtree(dest) | 85 shutil.rmtree(dest) |
70 # Copy tree. | 86 # Copy tree. |
71 # TODO(thakis): This copies file attributes like mtime, while the | 87 # TODO(thakis): This copies file attributes like mtime, while the |
72 # single-file branch below doesn't. This should probably be changed to | 88 # single-file branch below doesn't. This should probably be changed to |
73 # be consistent with the single-file branch. | 89 # be consistent with the single-file branch. |
74 shutil.copytree(source, dest, symlinks=True) | 90 shutil.copytree(source, dest, symlinks=True) |
75 return | 91 return |
76 | 92 |
77 if os.path.exists(dest): | 93 if os.path.exists(dest): |
78 os.unlink(dest) | 94 os.unlink(dest) |
79 | 95 |
80 _, extension = os.path.splitext(source) | 96 _, extension = os.path.splitext(source) |
81 if extension == '.strings': | 97 if extension == '.strings': |
82 copy_strings_file(source, dest) | 98 CopyStringsFile(source, dest, strings_format) |
83 return | 99 return |
84 | 100 |
85 shutil.copy(source, dest) | 101 shutil.copy(source, dest) |
86 | 102 |
87 | 103 |
88 def main(): | 104 def Main(): |
89 parser = argparse.ArgumentParser( | 105 parser = argparse.ArgumentParser( |
90 description='copy source to destination for the creation of a bundle') | 106 description='copy source to destination for the creation of a bundle') |
| 107 parser.add_argument('--strings-format', |
| 108 choices=('xml1', 'binary1', 'legacy'), default='legacy', |
| 109 help='convert .strings file to format (default: %(default)s)') |
91 parser.add_argument('source', help='path to source file or directory') | 110 parser.add_argument('source', help='path to source file or directory') |
92 parser.add_argument('dest', help='path to destination') | 111 parser.add_argument('dest', help='path to destination') |
93 args = parser.parse_args() | 112 args = parser.parse_args() |
94 | 113 |
95 copy_file(args.source, args.dest) | 114 CopyFile(args.source, args.dest, args.strings_format) |
96 | 115 |
97 if __name__ == '__main__': | 116 if __name__ == '__main__': |
98 main() | 117 sys.exit(Main()) |
OLD | NEW |