Index: build/toolchain/mac/copy_data.py |
diff --git a/build/toolchain/mac/copy_data.py b/build/toolchain/mac/copy_data.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6809b79f3111dc58b2fe3f0c361c9e93e4b343f3 |
--- /dev/null |
+++ b/build/toolchain/mac/copy_data.py |
@@ -0,0 +1,95 @@ |
+# Copyright 2016 The Chromium Authors. All rights reserved. |
Robert Sesek
2016/05/05 14:38:10
Why not name this copy_bundle_data.py to match the
sdefresne
2016/05/09 07:47:37
Renamed (there was no other reason than lazyness f
|
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import argparse |
+import os |
+import shutil |
+import sys |
+ |
+from CoreFoundation import CFDataCreate, CFPropertyListCreateFromXMLData |
Robert Sesek
2016/05/05 14:38:10
Moving this import into copy_strings_file will spe
sdefresne
2016/05/09 07:47:37
Done.
|
+ |
+ |
+def detect_encoding(data, default_encoding='UTF-8'): |
+ '''Detects the encoding used by |data| from the Byte-Order-Mark if present. |
Robert Sesek
2016/05/05 14:38:10
Docstrings should use """ per PEP-8.
sdefresne
2016/05/09 07:47:37
Done.
|
+ |
+ Args: |
+ data: string whose encoding needs to be detected |
+ default_encoding: encoding returned if no BOM is found. |
+ |
+ Returns: |
+ The encoding determined from the BOM if present or |default_encoding| if |
+ no BOM was found. |
+ ''' |
+ if data.startswith('\xFE\xFF'): |
+ return 'UTF-16BE' |
+ |
+ if data.startswith('\xFF\xFE'): |
+ return 'UTF-16LE' |
+ |
+ if data.startswith('\xEF\xBB\xBF'): |
+ return 'UTF-8' |
+ |
+ return default_encoding |
+ |
+ |
+def copy_strings_file(source, dest): |
+ '''Copies a .strings file from |source| to |dest| and convert it to UTF-16. |
+ |
+ Args: |
+ source: string, path to the source file |
+ dest: string, path to the destination file |
+ ''' |
+ with open(source, 'rb') as source_file: |
+ data = source_file.read() |
+ |
+ # Xcode's CpyCopyStringsFile / builtin-copyStrings seems to call |
+ # CFPropertyListCreateFromXMLData() behind the scenes; at least it prints |
+ # CFPropertyListCreateFromXMLData(): Old-style plist parser: missing |
+ # semicolon in dictionary. |
+ # on invalid files. Do the same kind of validation. |
+ core_data = CFDataCreate(None, data, len(data)) |
Robert Sesek
2016/05/05 14:38:10
naming: cfdata?
sdefresne
2016/05/09 07:47:37
Done.
|
+ _, error = CFPropertyListCreateFromXMLData(None, core_data, 0, None) |
+ if error: |
+ sys.exit(1) |
Robert Sesek
2016/05/05 14:38:10
Maybe raise instead, so you get a stack trace?
sdefresne
2016/05/09 07:47:37
Done.
|
+ |
+ 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): |
+ '''Copies a file or directory from |source| to |dest|. |
+ |
+ Args: |
+ source: string, path to the source file |
+ dest: string, path to the destination file |
+ ''' |
+ if os.path.isdir(source): |
Robert Sesek
2016/05/05 14:38:10
There's a TODO(thakis) about mtimes in mac_tool.py
sdefresne
2016/05/09 07:47:37
I think it still apply, copied.
|
+ if os.path.exists(dest): |
+ shutil.rmtree(dest) |
+ shutil.copytree(source, dest) |
+ return |
+ |
+ if os.path.exists(dest): |
+ os.unlink(dest) |
+ |
+ _, extension = os.path.splitext(source) |
+ if extension == '.strings': |
+ copy_strings_file(source, dest) |
+ return |
+ |
+ shutil.copy(source, dest) |
+ |
+ |
+def main(): |
+ parser = argparse.ArgumentParser( |
+ description='copy source to destination for the creation of a bundle') |
+ 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) |
+ |
+if __name__ == '__main__': |
+ main() |