Chromium Code Reviews| Index: build/toolchain/mac/copy_bundle_data.py |
| diff --git a/build/toolchain/mac/copy_bundle_data.py b/build/toolchain/mac/copy_bundle_data.py |
| index 976591c08ee309a1df86d71d3bd6ce66d3865edc..51810d23257ca15e88731ed301e11e1e765097cd 100644 |
| --- a/build/toolchain/mac/copy_bundle_data.py |
| +++ b/build/toolchain/mac/copy_bundle_data.py |
| @@ -8,7 +8,7 @@ import shutil |
| import sys |
| -def detect_encoding(data, default_encoding='UTF-8'): |
| +def DetectEncoding(data, default_encoding='UTF-8'): |
| """Detects the encoding used by |data| from the Byte-Order-Mark if present. |
| Args: |
| @@ -31,7 +31,7 @@ def detect_encoding(data, default_encoding='UTF-8'): |
| return default_encoding |
| -def copy_strings_file(source, dest): |
| +def CopyStringsFile(source, dest, strings_format): |
| """Copies a .strings file from |source| to |dest| and convert it to UTF-16. |
| Args: |
| @@ -46,18 +46,37 @@ def copy_strings_file(source, dest): |
| # CFPropertyListCreateFromXMLData(): Old-style plist parser: missing |
| # semicolon in dictionary. |
| # on invalid files. Do the same kind of validation. |
| - from CoreFoundation import CFDataCreate, CFPropertyListCreateFromXMLData |
| - cfdata = CFDataCreate(None, data, len(data)) |
| - _, error = CFPropertyListCreateFromXMLData(None, cfdata, 0, None) |
| + 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.
|
| + cfdata = CoreFoundation.CFDataCreate(None, data, len(data)) |
| + plist, error = CoreFoundation.CFPropertyListCreateFromXMLData( |
| + None, cfdata, 0, None) |
| if error: |
| raise ValueError(error) |
| - encoding = detect_encoding(data) |
| - with open(dest, 'wb') as dest_file: |
| - dest_file.write(data.decode(encoding).encode('UTF-16')) |
| - |
| - |
| -def copy_file(source, dest): |
| + if strings_format == 'legacy': |
| + encoding = DetectEncoding(data) |
| + with open(dest, 'wb') as dest_file: |
| + dest_file.write(data.decode(encoding).encode('UTF-16')) |
| + else: |
| + cfformat = { |
| + 'xml1': CoreFoundation.kCFPropertyListXMLFormat_v1_0, |
| + 'binary1': CoreFoundation.kCFPropertyListBinaryFormat_v1_0, |
| + }[strings_format] |
| + cfdata, error = CoreFoundation.CFPropertyListCreateData( |
| + None, plist, CoreFoundation.kCFPropertyListBinaryFormat_v1_0, |
| + 0, None) |
| + if error: |
| + raise ValueError(error) |
| + |
| + 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.
|
| + cfdata, |
| + CoreFoundation.CFRangeMake(0, CoreFoundation.CFDataGetLength(cfdata)), |
| + None) |
| + with open(dest, 'wb') as dest_file: |
| + dest_file.write(data) |
| + |
| + |
| +def CopyFile(source, dest, strings_format): |
| """Copies a file or directory from |source| to |dest|. |
| Args: |
| @@ -79,20 +98,23 @@ def copy_file(source, dest): |
| _, extension = os.path.splitext(source) |
| if extension == '.strings': |
| - copy_strings_file(source, dest) |
| + CopyStringsFile(source, dest, strings_format) |
| return |
| shutil.copy(source, dest) |
| -def main(): |
| +def Main(): |
| parser = argparse.ArgumentParser( |
| description='copy source to destination for the creation of a bundle') |
| + parser.add_argument('--strings-format', |
| + choices=('xml1', 'binary1', 'legacy'), default='legacy', |
| + help='convert .strings file to format (default: %(default)s)') |
| parser.add_argument('source', help='path to source file or directory') |
| parser.add_argument('dest', help='path to destination') |
| args = parser.parse_args() |
| - copy_file(args.source, args.dest) |
| + CopyFile(args.source, args.dest, args.strings_format) |
| if __name__ == '__main__': |
| - main() |
| + sys.exit(Main()) |