Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(248)

Side by Side Diff: build/toolchain/mac/copy_bundle_data.py

Issue 2052333002: [iOS/GN] Convert .strings file to binary1 plist format. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« build/toolchain/mac/BUILD.gn ('K') | « build/toolchain/mac/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
Robert Sesek 2016/06/13 16:50:57 "as CF" could shorten up some of your callsites.
sdefresne 2016/06/14 07:27:39 Done.
50 cfdata = CFDataCreate(None, data, len(data)) 50 cfdata = CoreFoundation.CFDataCreate(None, data, len(data))
51 _, error = CFPropertyListCreateFromXMLData(None, cfdata, 0, None) 51 plist, error = CoreFoundation.CFPropertyListCreateFromXMLData(
52 None, cfdata, 0, None)
52 if error: 53 if error:
53 raise ValueError(error) 54 raise ValueError(error)
54 55
55 encoding = detect_encoding(data) 56 if strings_format == 'legacy':
56 with open(dest, 'wb') as dest_file: 57 encoding = DetectEncoding(data)
57 dest_file.write(data.decode(encoding).encode('UTF-16')) 58 with open(dest, 'wb') as dest_file:
59 dest_file.write(data.decode(encoding).encode('UTF-16'))
60 else:
61 cfformat = {
62 'xml1': CoreFoundation.kCFPropertyListXMLFormat_v1_0,
63 'binary1': CoreFoundation.kCFPropertyListBinaryFormat_v1_0,
64 }[strings_format]
65 cfdata, error = CoreFoundation.CFPropertyListCreateData(
66 None, plist, CoreFoundation.kCFPropertyListBinaryFormat_v1_0,
67 0, None)
68 if error:
69 raise ValueError(error)
70
71 data = CoreFoundation.CFDataGetBytes(
Robert Sesek 2016/06/13 16:50:57 GetBytes will do a copy. Is it possible to use Get
sdefresne 2016/06/14 07:27:40 I tried, but when I pass the value to dest_file.wr
Robert Sesek 2016/06/14 13:40:26 Eesh, what you have here is fine, then.
72 cfdata,
73 CoreFoundation.CFRangeMake(0, CoreFoundation.CFDataGetLength(cfdata)),
74 None)
75 with open(dest, 'wb') as dest_file:
76 dest_file.write(data)
58 77
59 78
60 def copy_file(source, dest): 79 def CopyFile(source, dest, strings_format):
61 """Copies a file or directory from |source| to |dest|. 80 """Copies a file or directory from |source| to |dest|.
62 81
63 Args: 82 Args:
64 source: string, path to the source file 83 source: string, path to the source file
65 dest: string, path to the destination file 84 dest: string, path to the destination file
66 """ 85 """
67 if os.path.isdir(source): 86 if os.path.isdir(source):
68 if os.path.exists(dest): 87 if os.path.exists(dest):
69 shutil.rmtree(dest) 88 shutil.rmtree(dest)
70 # Copy tree. 89 # Copy tree.
71 # TODO(thakis): This copies file attributes like mtime, while the 90 # TODO(thakis): This copies file attributes like mtime, while the
72 # single-file branch below doesn't. This should probably be changed to 91 # single-file branch below doesn't. This should probably be changed to
73 # be consistent with the single-file branch. 92 # be consistent with the single-file branch.
74 shutil.copytree(source, dest, symlinks=True) 93 shutil.copytree(source, dest, symlinks=True)
75 return 94 return
76 95
77 if os.path.exists(dest): 96 if os.path.exists(dest):
78 os.unlink(dest) 97 os.unlink(dest)
79 98
80 _, extension = os.path.splitext(source) 99 _, extension = os.path.splitext(source)
81 if extension == '.strings': 100 if extension == '.strings':
82 copy_strings_file(source, dest) 101 CopyStringsFile(source, dest, strings_format)
83 return 102 return
84 103
85 shutil.copy(source, dest) 104 shutil.copy(source, dest)
86 105
87 106
88 def main(): 107 def Main():
89 parser = argparse.ArgumentParser( 108 parser = argparse.ArgumentParser(
90 description='copy source to destination for the creation of a bundle') 109 description='copy source to destination for the creation of a bundle')
110 parser.add_argument('--strings-format',
111 choices=('xml1', 'binary1', 'legacy'), default='legacy',
112 help='convert .strings file to format (default: %(default)s)')
91 parser.add_argument('source', help='path to source file or directory') 113 parser.add_argument('source', help='path to source file or directory')
92 parser.add_argument('dest', help='path to destination') 114 parser.add_argument('dest', help='path to destination')
93 args = parser.parse_args() 115 args = parser.parse_args()
94 116
95 copy_file(args.source, args.dest) 117 CopyFile(args.source, args.dest, args.strings_format)
96 118
97 if __name__ == '__main__': 119 if __name__ == '__main__':
98 main() 120 sys.exit(Main())
OLDNEW
« build/toolchain/mac/BUILD.gn ('K') | « build/toolchain/mac/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698