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

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

Issue 1946203002: [GN/iOS/Mac] Decouple copy_bundle_data from gyp mac_tool.py. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
« no previous file with comments | « 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
(Empty)
1 # 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
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import argparse
6 import os
7 import shutil
8 import sys
9
10 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.
11
12
13 def detect_encoding(data, default_encoding='UTF-8'):
14 '''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.
15
16 Args:
17 data: string whose encoding needs to be detected
18 default_encoding: encoding returned if no BOM is found.
19
20 Returns:
21 The encoding determined from the BOM if present or |default_encoding| if
22 no BOM was found.
23 '''
24 if data.startswith('\xFE\xFF'):
25 return 'UTF-16BE'
26
27 if data.startswith('\xFF\xFE'):
28 return 'UTF-16LE'
29
30 if data.startswith('\xEF\xBB\xBF'):
31 return 'UTF-8'
32
33 return default_encoding
34
35
36 def copy_strings_file(source, dest):
37 '''Copies a .strings file from |source| to |dest| and convert it to UTF-16.
38
39 Args:
40 source: string, path to the source file
41 dest: string, path to the destination file
42 '''
43 with open(source, 'rb') as source_file:
44 data = source_file.read()
45
46 # Xcode's CpyCopyStringsFile / builtin-copyStrings seems to call
47 # CFPropertyListCreateFromXMLData() behind the scenes; at least it prints
48 # CFPropertyListCreateFromXMLData(): Old-style plist parser: missing
49 # semicolon in dictionary.
50 # on invalid files. Do the same kind of validation.
51 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.
52 _, error = CFPropertyListCreateFromXMLData(None, core_data, 0, None)
53 if error:
54 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.
55
56 encoding = detect_encoding(data)
57 with open(dest, 'wb') as dest_file:
58 dest_file.write(data.decode(encoding).encode('UTF-16'))
59
60
61 def copy_file(source, dest):
62 '''Copies a file or directory from |source| to |dest|.
63
64 Args:
65 source: string, path to the source file
66 dest: string, path to the destination file
67 '''
68 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.
69 if os.path.exists(dest):
70 shutil.rmtree(dest)
71 shutil.copytree(source, dest)
72 return
73
74 if os.path.exists(dest):
75 os.unlink(dest)
76
77 _, extension = os.path.splitext(source)
78 if extension == '.strings':
79 copy_strings_file(source, dest)
80 return
81
82 shutil.copy(source, dest)
83
84
85 def main():
86 parser = argparse.ArgumentParser(
87 description='copy source to destination for the creation of a bundle')
88 parser.add_argument('source', help='path to source file or directory')
89 parser.add_argument('dest', help='path to destination')
90 args = parser.parse_args()
91
92 copy_file(args.source, args.dest)
93
94 if __name__ == '__main__':
95 main()
OLDNEW
« no previous file with comments | « build/toolchain/mac/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698