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 stat |
| 9 import subprocess |
8 import sys | 10 import sys |
9 | 11 |
10 | 12 |
11 def DetectEncoding(data, default_encoding='UTF-8'): | 13 def DetectEncoding(data, default_encoding='UTF-8'): |
12 """Detects the encoding used by |data| from the Byte-Order-Mark if present. | 14 """Detects the encoding used by |data| from the Byte-Order-Mark if present. |
13 | 15 |
14 Args: | 16 Args: |
15 data: string whose encoding needs to be detected | 17 data: string whose encoding needs to be detected |
16 default_encoding: encoding returned if no BOM is found. | 18 default_encoding: encoding returned if no BOM is found. |
17 | 19 |
18 Returns: | 20 Returns: |
19 The encoding determined from the BOM if present or |default_encoding| if | 21 The encoding determined from the BOM if present or |default_encoding| if |
20 no BOM was found. | 22 no BOM was found. |
21 """ | 23 """ |
22 if data.startswith('\xFE\xFF'): | 24 if data.startswith('\xFE\xFF'): |
23 return 'UTF-16BE' | 25 return 'UTF-16BE' |
24 | 26 |
25 if data.startswith('\xFF\xFE'): | 27 if data.startswith('\xFF\xFE'): |
26 return 'UTF-16LE' | 28 return 'UTF-16LE' |
27 | 29 |
28 if data.startswith('\xEF\xBB\xBF'): | 30 if data.startswith('\xEF\xBB\xBF'): |
29 return 'UTF-8' | 31 return 'UTF-8' |
30 | 32 |
31 return default_encoding | 33 return default_encoding |
32 | 34 |
33 | 35 |
34 def CopyStringsFile(source, dest, strings_format): | 36 def CopyStringsFile(source, dest, strings_format): |
35 """Copies a .strings file from |source| to |dest| and convert it to UTF-16. | 37 """Copies a .strings file from |source| to |dest|. |
| 38 |
| 39 Args: |
| 40 source: string, path to the source file |
| 41 dest: string, path to the destination file |
| 42 strings_format: convert .strings file to format |
| 43 """ |
| 44 if strings_format == 'legacy': |
| 45 with open(source, 'rb') as source_file: |
| 46 data = source_file.read() |
| 47 encoding = DetectEncoding(data) |
| 48 with open(dest, 'wb') as dest_file: |
| 49 dest_file.write(data.decode(encoding).encode('UTF-16')) |
| 50 else: |
| 51 subprocess.check_call([ |
| 52 'xcrun', 'plutil', '-convert', strings_format, |
| 53 '-o', dest, source]) |
| 54 |
| 55 |
| 56 def CopyTree(source, dest): |
| 57 """Copies a directory from |source| to |dest|.""" |
| 58 os.makedirs(dest) |
| 59 for name in os.listdir(source): |
| 60 source_name = os.path.join(source, name) |
| 61 target_name = os.path.join(dest, name) |
| 62 if os.path.isdir(source_name): |
| 63 CopyTree(source_name, target_name) |
| 64 else: |
| 65 CopyFile(source_name, target_name) |
| 66 |
| 67 |
| 68 def CopyFile(source, dest): |
| 69 """Copies a file from |source| to |dest|. |
36 | 70 |
37 Args: | 71 Args: |
38 source: string, path to the source file | 72 source: string, path to the source file |
39 dest: string, path to the destination file | 73 dest: string, path to the destination file |
40 """ | 74 """ |
| 75 if os.path.islink(source): |
| 76 linkto = os.readink(source_name) |
| 77 os.symlink(linkto, dest) |
| 78 return |
| 79 |
| 80 source_stat = os.stat(source) |
| 81 if not stat.S_ISREG(source_stat.st_mode): |
| 82 raise OSError('cannot copy special file: %s' % source) |
| 83 |
41 with open(source, 'rb') as source_file: | 84 with open(source, 'rb') as source_file: |
42 data = source_file.read() | 85 with open(dest, 'wb') as dest_file: |
| 86 while True: |
| 87 data = source_file.read(16*1024) |
| 88 if not data: |
| 89 break |
| 90 dest_file.write(data) |
43 | 91 |
44 # Xcode's CpyCopyStringsFile / builtin-copyStrings seems to call | 92 os.chmod(dest, stat.S_IMODE(source_stat.st_mode)) |
45 # CFPropertyListCreateFromXMLData() behind the scenes; at least it prints | |
46 # CFPropertyListCreateFromXMLData(): Old-style plist parser: missing | |
47 # semicolon in dictionary. | |
48 # on invalid files. Do the same kind of validation. | |
49 import CoreFoundation as CF | |
50 cfdata = CF.CFDataCreate(None, data, len(data)) | |
51 plist, error = CF.CFPropertyListCreateFromXMLData(None, cfdata, 0, None) | |
52 if error: | |
53 raise ValueError(error) | |
54 | |
55 if strings_format == 'legacy': | |
56 encoding = DetectEncoding(data) | |
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) | |
74 | 93 |
75 | 94 |
76 def CopyFile(source, dest, strings_format): | 95 def Copy(source, dest, strings_format): |
77 """Copies a file or directory from |source| to |dest|. | 96 """Copies a file or directory from |source| to |dest|. |
78 | 97 |
79 Args: | 98 Args: |
80 source: string, path to the source file | 99 source: string, path to the source file |
81 dest: string, path to the destination file | 100 dest: string, path to the destination file |
| 101 strings_format: format to use when copying .strings files |
82 """ | 102 """ |
83 if os.path.isdir(source): | 103 if os.path.isdir(source): |
84 if os.path.exists(dest): | 104 if os.path.exists(dest): |
85 shutil.rmtree(dest) | 105 shutil.rmtree(dest) |
86 # Copy tree. | 106 |
87 # TODO(thakis): This copies file attributes like mtime, while the | 107 CopyTree(source, dest) |
88 # single-file branch below doesn't. This should probably be changed to | |
89 # be consistent with the single-file branch. | |
90 shutil.copytree(source, dest, symlinks=True) | |
91 return | 108 return |
92 | 109 |
93 if os.path.exists(dest): | 110 if os.path.exists(dest): |
94 os.unlink(dest) | 111 os.unlink(dest) |
95 | 112 |
96 _, extension = os.path.splitext(source) | 113 if os.path.splitext(source)[-1] == '.strings': |
97 if extension == '.strings': | |
98 CopyStringsFile(source, dest, strings_format) | 114 CopyStringsFile(source, dest, strings_format) |
99 return | 115 return |
100 | 116 |
101 shutil.copy(source, dest) | 117 CopyFile(source, dest) |
102 | 118 |
103 | 119 |
104 def Main(): | 120 def Main(): |
105 parser = argparse.ArgumentParser( | 121 parser = argparse.ArgumentParser( |
106 description='copy source to destination for the creation of a bundle') | 122 description='copy source to destination for the creation of a bundle') |
| 123 parser.add_argument( |
| 124 '--version-for-gn', choices=('1',), |
| 125 help='version need to be increased in this script and in the file ' |
| 126 '//build/toolchain/mac/BUILD.gn to force build until issue ' |
| 127 'http://crbug.com/619083 is fixed') |
107 parser.add_argument('--strings-format', | 128 parser.add_argument('--strings-format', |
108 choices=('xml1', 'binary1', 'legacy'), default='legacy', | 129 choices=('xml1', 'binary1', 'legacy'), default='legacy', |
109 help='convert .strings file to format (default: %(default)s)') | 130 help='convert .strings file to format (default: %(default)s)') |
110 parser.add_argument('source', help='path to source file or directory') | 131 parser.add_argument('source', help='path to source file or directory') |
111 parser.add_argument('dest', help='path to destination') | 132 parser.add_argument('dest', help='path to destination') |
112 args = parser.parse_args() | 133 args = parser.parse_args() |
113 | 134 |
114 CopyFile(args.source, args.dest, args.strings_format) | 135 Copy(args.source, args.dest, args.strings_format) |
115 | 136 |
116 if __name__ == '__main__': | 137 if __name__ == '__main__': |
117 sys.exit(Main()) | 138 sys.exit(Main()) |
OLD | NEW |