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

Side by Side Diff: chrome/app/nibs/generate_localizer.py

Issue 2388063003: Add a variable use_system_xcode to GN. (Closed)
Patch Set: Comments from dpranke. Created 4 years, 2 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
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 # Copyright (c) 2009 The Chromium Authors. All rights reserved. 3 # Copyright (c) 2009 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 6
7 # Usage: generate_localizer [xib_path] [output_dot_h_path] [output_dot_mm_path] 7 # Usage: generate_localizer [xib_path] [output_dot_h_path] [output_dot_mm_path]
8 # 8 #
9 # Extracts all the localizable strings that start with "^IDS" from the given 9 # Extracts all the localizable strings that start with "^IDS" from the given
10 # xib file, and then generates a localizer to process those strings. 10 # xib file, and then generates a localizer to process those strings.
11 11
12
13 import argparse
14 import logging
12 import os 15 import os
13 import plistlib 16 import plistlib
14 import subprocess 17 import subprocess
15 import sys 18 import sys
16 19
17 generate_localizer = "me" 20 generate_localizer = "me"
18 21
19 localizer_template_h = \ 22 localizer_template_h = \
20 '''// ---------- WARNING ---------- 23 '''// ---------- WARNING ----------
21 // THIS IS A GENERATED FILE, DO NOT EDIT IT DIRECTLY! 24 // THIS IS A GENERATED FILE, DO NOT EDIT IT DIRECTLY!
22 // 25 //
23 // This header includes the table used by ui_localizer.mm. Nothing else should 26 // This header includes the table used by ui_localizer.mm. Nothing else should
24 // be including this file. 27 // be including this file.
25 // 28 //
26 // Generated by %(generate_localizer)s. 29 // Generated by %(generate_localizer)s.
27 // Generated from: 30 // Generated from:
28 // %(xib_files)s 31 // %(xib_files)s
29 // 32 //
30 33
31 #ifndef CHROME_APP_NIBS_LOCALIZER_TABLE_H_ 34 #ifndef CHROME_APP_NIBS_LOCALIZER_TABLE_H_
32 #define CHROME_APP_NIBS_LOCALIZER_TABLE_H_ 35 #define CHROME_APP_NIBS_LOCALIZER_TABLE_H_
33 36
34 static const UILocalizerResourceMap kUIResources[] = { 37 static const UILocalizerResourceMap kUIResources[] = {
35 %(resource_map_list)s }; 38 %(resource_map_list)s };
36 static const size_t kUIResourcesSize = arraysize(kUIResources); 39 static const size_t kUIResourcesSize = arraysize(kUIResources);
37 40
38 #endif // CHROME_APP_NIBS_LOCALIZER_TABLE_H_ 41 #endif // CHROME_APP_NIBS_LOCALIZER_TABLE_H_
39 ''' 42 '''
40 43
41 def xib_localizable_strings(xib_path): 44 def xib_localizable_strings(xib_path, xcode_path):
42 """Runs ibtool to extract the localizable strings data from the xib.""" 45 """Runs ibtool to extract the localizable strings data from the xib."""
43 # Take SDKROOT out of the environment passed to ibtool. ibtool itself has 46 # Take SDKROOT out of the environment passed to ibtool. ibtool itself has
44 # no need for it, but when ibtool runs via xcrun and Xcode isn't aware of 47 # no need for it, but when ibtool runs via xcrun and Xcode isn't aware of
45 # the SDK in use, its presence causes an error. 48 # the SDK in use, its presence causes an error.
49 ibtool_env = os.environ.copy()
46 if 'SDKROOT' in os.environ: 50 if 'SDKROOT' in os.environ:
47 ibtool_env = os.environ.copy()
48 del ibtool_env['SDKROOT'] 51 del ibtool_env['SDKROOT']
49 else: 52 if xcode_path:
50 ibtool_env = os.environ 53 ibtool_env['DEVELOPER_DIR'] = xcode_path
51 54
52 ibtool_cmd = subprocess.Popen(['xcrun', 'ibtool', 55 command = ['xcrun', 'ibtool', '--localizable-strings', xib_path]
53 '--localizable-strings', xib_path], 56 ibtool_cmd = subprocess.Popen(command,
54 stdout=subprocess.PIPE, stderr=subprocess.PIPE, 57 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
55 env=ibtool_env) 58 env=ibtool_env)
56 (cmd_out, cmd_err) = ibtool_cmd.communicate() 59 (cmd_out, cmd_err) = ibtool_cmd.communicate()
57 if ibtool_cmd.returncode: 60 if ibtool_cmd.returncode:
58 sys.stderr.write('%s:0: error: ibtool on "%s" failed (%d):\n%s\n' % 61 sys.stderr.write('%s:0: error: ibtool on "%s" failed (%d):\n%s\n' %
59 (generate_localizer, xib_path, ibtool_cmd.returncode, 62 (generate_localizer, xib_path, ibtool_cmd.returncode,
60 cmd_err)) 63 cmd_err))
61 return None 64 return None
62 return cmd_out 65 return cmd_out
63 66
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 'xib_files': "\n// ".join(xib_paths), 100 'xib_files': "\n// ".join(xib_paths),
98 } 101 }
99 h_file = localizer_template_h % values_dict 102 h_file = localizer_template_h % values_dict
100 return h_file 103 return h_file
101 104
102 105
103 def Main(argv=None): 106 def Main(argv=None):
104 global generate_localizer 107 global generate_localizer
105 generate_localizer = os.path.basename(argv[0]) 108 generate_localizer = os.path.basename(argv[0])
106 109
107 # Args 110 parser = argparse.ArgumentParser(
108 if len(argv) < 3: 111 description='Generates a header file that localizes libs.')
109 sys.stderr.write('%s:0: error: Expected output file and then xibs\n' % 112 parser.add_argument('--output_path', required=True,
110 generate_localizer); 113 help='Path to output header file.')
111 return 1 114 parser.add_argument('--developer_dir', required=False,
112 output_path = argv[1]; 115 help='Path to Xcode.')
113 xib_paths = argv[2:] 116 parser.add_argument('xibs', nargs='+',
117 help='A list of xibs to localize')
118 args = parser.parse_args()
119
120 output_path = args.output_path
121 xib_paths = args.xibs
114 122
115 full_constants_list = [] 123 full_constants_list = []
116 for xib_path in xib_paths: 124 for xib_path in xib_paths:
117 # Run ibtool and convert to something Python can deal with 125 # Run ibtool and convert to something Python can deal with
118 plist_string = xib_localizable_strings(xib_path) 126 plist_string = xib_localizable_strings(xib_path, args.developer_dir)
119 if not plist_string: 127 if not plist_string:
120 return 2 128 return 2
121 plist = plistlib.readPlistFromString(plist_string) 129 plist = plistlib.readPlistFromString(plist_string)
122 130
123 # Extract the resource constant strings 131 # Extract the resource constant strings
124 localizable_strings = plist['com.apple.ibtool.document.localizable-strings'] 132 localizable_strings = plist['com.apple.ibtool.document.localizable-strings']
125 constants_list = extract_resource_constants(localizable_strings, xib_path) 133 constants_list = extract_resource_constants(localizable_strings, xib_path)
126 if not constants_list: 134 if not constants_list:
127 sys.stderr.write("%s:0: warning: %s didn't find any resource strings\n" % 135 sys.stderr.write("%s:0: warning: %s didn't find any resource strings\n" %
128 (xib_path, generate_localizer)); 136 (xib_path, generate_localizer));
129 full_constants_list.extend(constants_list) 137 full_constants_list.extend(constants_list)
130 138
131 # Generate our file contents 139 # Generate our file contents
132 h_file_content = \ 140 h_file_content = \
133 generate_file_contents(full_constants_list, xib_paths) 141 generate_file_contents(full_constants_list, xib_paths)
134 142
135 # Write out the file 143 # Write out the file
136 file_fd = open(output_path, 'w') 144 file_fd = open(output_path, 'w')
137 file_fd.write(h_file_content) 145 file_fd.write(h_file_content)
138 file_fd.close() 146 file_fd.close()
139 147
140 return 0 148 return 0
141 149
142 if __name__ == '__main__': 150 if __name__ == '__main__':
143 sys.exit(Main(sys.argv)) 151 sys.exit(Main(sys.argv))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698