Chromium Code Reviews| OLD | NEW |
|---|---|
| 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. |
| 46 if 'SDKROOT' in os.environ: | 49 if 'SDKROOT' in os.environ: |
| 47 ibtool_env = os.environ.copy() | 50 ibtool_env = os.environ.copy() |
| 48 del ibtool_env['SDKROOT'] | 51 del ibtool_env['SDKROOT'] |
| 49 else: | 52 else: |
| 50 ibtool_env = os.environ | 53 ibtool_env = os.environ |
| 54 if xcode_path: | |
| 55 ibtool_env['DEVELOPER_DIR'] = xcode_path | |
|
Dirk Pranke
2016/10/04 20:25:40
This logic is a little strange, in that you copy()
erikchen
2016/10/05 19:57:19
Good point, done.
| |
| 51 | 56 |
| 52 ibtool_cmd = subprocess.Popen(['xcrun', 'ibtool', | 57 command = ['xcrun', 'ibtool', '--localizable-strings', xib_path] |
| 53 '--localizable-strings', xib_path], | 58 ibtool_cmd = subprocess.Popen(command, |
| 54 stdout=subprocess.PIPE, stderr=subprocess.PIPE, | 59 stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 55 env=ibtool_env) | 60 env=ibtool_env) |
| 56 (cmd_out, cmd_err) = ibtool_cmd.communicate() | 61 (cmd_out, cmd_err) = ibtool_cmd.communicate() |
| 57 if ibtool_cmd.returncode: | 62 if ibtool_cmd.returncode: |
| 58 sys.stderr.write('%s:0: error: ibtool on "%s" failed (%d):\n%s\n' % | 63 sys.stderr.write('%s:0: error: ibtool on "%s" failed (%d):\n%s\n' % |
| 59 (generate_localizer, xib_path, ibtool_cmd.returncode, | 64 (generate_localizer, xib_path, ibtool_cmd.returncode, |
| 60 cmd_err)) | 65 cmd_err)) |
| 61 return None | 66 return None |
| 62 return cmd_out | 67 return cmd_out |
| 63 | 68 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 'xib_files': "\n// ".join(xib_paths), | 102 'xib_files': "\n// ".join(xib_paths), |
| 98 } | 103 } |
| 99 h_file = localizer_template_h % values_dict | 104 h_file = localizer_template_h % values_dict |
| 100 return h_file | 105 return h_file |
| 101 | 106 |
| 102 | 107 |
| 103 def Main(argv=None): | 108 def Main(argv=None): |
| 104 global generate_localizer | 109 global generate_localizer |
| 105 generate_localizer = os.path.basename(argv[0]) | 110 generate_localizer = os.path.basename(argv[0]) |
| 106 | 111 |
| 107 # Args | 112 parser = argparse.ArgumentParser( |
| 108 if len(argv) < 3: | 113 description='Generates a header file that localizes libs.') |
| 109 sys.stderr.write('%s:0: error: Expected output file and then xibs\n' % | 114 parser.add_argument('--output_path', required=True, |
| 110 generate_localizer); | 115 help='Path to output header file.') |
| 111 return 1 | 116 parser.add_argument('--developer_dir', required=False, |
| 112 output_path = argv[1]; | 117 help='Path to Xcode.') |
| 113 xib_paths = argv[2:] | 118 parser.add_argument('xibs', nargs='+', |
| 119 help='A list of xibs to localize') | |
| 120 args = parser.parse_args() | |
| 121 | |
| 122 output_path = args.output_path | |
| 123 xib_paths = args.xibs | |
| 114 | 124 |
| 115 full_constants_list = [] | 125 full_constants_list = [] |
| 116 for xib_path in xib_paths: | 126 for xib_path in xib_paths: |
| 117 # Run ibtool and convert to something Python can deal with | 127 # Run ibtool and convert to something Python can deal with |
| 118 plist_string = xib_localizable_strings(xib_path) | 128 plist_string = xib_localizable_strings(xib_path, args.developer_dir) |
| 119 if not plist_string: | 129 if not plist_string: |
| 120 return 2 | 130 return 2 |
| 121 plist = plistlib.readPlistFromString(plist_string) | 131 plist = plistlib.readPlistFromString(plist_string) |
| 122 | 132 |
| 123 # Extract the resource constant strings | 133 # Extract the resource constant strings |
| 124 localizable_strings = plist['com.apple.ibtool.document.localizable-strings'] | 134 localizable_strings = plist['com.apple.ibtool.document.localizable-strings'] |
| 125 constants_list = extract_resource_constants(localizable_strings, xib_path) | 135 constants_list = extract_resource_constants(localizable_strings, xib_path) |
| 126 if not constants_list: | 136 if not constants_list: |
| 127 sys.stderr.write("%s:0: warning: %s didn't find any resource strings\n" % | 137 sys.stderr.write("%s:0: warning: %s didn't find any resource strings\n" % |
| 128 (xib_path, generate_localizer)); | 138 (xib_path, generate_localizer)); |
| 129 full_constants_list.extend(constants_list) | 139 full_constants_list.extend(constants_list) |
| 130 | 140 |
| 131 # Generate our file contents | 141 # Generate our file contents |
| 132 h_file_content = \ | 142 h_file_content = \ |
| 133 generate_file_contents(full_constants_list, xib_paths) | 143 generate_file_contents(full_constants_list, xib_paths) |
| 134 | 144 |
| 135 # Write out the file | 145 # Write out the file |
| 136 file_fd = open(output_path, 'w') | 146 file_fd = open(output_path, 'w') |
| 137 file_fd.write(h_file_content) | 147 file_fd.write(h_file_content) |
| 138 file_fd.close() | 148 file_fd.close() |
| 139 | 149 |
| 140 return 0 | 150 return 0 |
| 141 | 151 |
| 142 if __name__ == '__main__': | 152 if __name__ == '__main__': |
| 143 sys.exit(Main(sys.argv)) | 153 sys.exit(Main(sys.argv)) |
| OLD | NEW |