 Chromium Code Reviews
 Chromium Code Reviews Issue 1104703003:
  Actually use --extra-r-text-files in process_resources.py  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1104703003:
  Actually use --extra-r-text-files in process_resources.py  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: build/android/gyp/process_resources.py | 
| diff --git a/build/android/gyp/process_resources.py b/build/android/gyp/process_resources.py | 
| index 4e6c27dcb8fb71dba499d33e2ee5962460fd0bce..a3ef8c61a4fb4a42583fd5144cf2b67117906b42 100755 | 
| --- a/build/android/gyp/process_resources.py | 
| +++ b/build/android/gyp/process_resources.py | 
| @@ -10,11 +10,11 @@ This will crunch images and generate v14 compatible resources | 
| (see generate_v14_compatible_resources.py). | 
| """ | 
| -import codecs | 
| import optparse | 
| import os | 
| import re | 
| import shutil | 
| +from string import Template | 
| import sys | 
| import zipfile | 
| @@ -57,6 +57,8 @@ def ParseArgs(args): | 
| help='directory to hold generated R.java.') | 
| parser.add_option('--srcjar-out', | 
| help='Path to srcjar to contain generated R.java.') | 
| + parser.add_option('--r-text-out', | 
| + help='Path to store the R.txt file generated by appt.') | 
| parser.add_option('--proguard-file', | 
| help='Path to proguard.txt generated file') | 
| @@ -71,8 +73,6 @@ def ParseArgs(args): | 
| parser.add_option( | 
| '--extra-res-packages', | 
| help='Additional package names to generate R.java files for') | 
| - # TODO(cjhopman): Actually use --extra-r-text-files. We currently include all | 
| - # the resources in all R.java files for a particular apk. | 
| parser.add_option( | 
| '--extra-r-text-files', | 
| help='For each additional package, the R.txt file should contain a ' | 
| @@ -108,24 +108,80 @@ def ParseArgs(args): | 
| return options | 
| -def CreateExtraRJavaFiles(r_dir, extra_packages): | 
| - java_files = build_utils.FindInDirectory(r_dir, "R.java") | 
| - if len(java_files) != 1: | 
| +def CreateExtraRJavaFiles(r_dir, extra_packages, extra_r_text_files): | 
| + r_txt_files = build_utils.FindInDirectory(r_dir, 'R.txt') | 
| 
cjhopman
2015/04/27 18:34:47
Below you find the R.txt by just doing os.path.joi
 
Ian Cullinan
2015/04/27 18:58:46
Acknowledged.
 | 
| + if len(r_txt_files) != 1: | 
| return | 
| - r_java_file = java_files[0] | 
| - r_java_contents = codecs.open(r_java_file, encoding='utf-8').read() | 
| - | 
| - for package in extra_packages: | 
| + if len(extra_packages) != len(extra_r_text_files): | 
| + raise Exception('Need one R.txt file per extra package') | 
| + | 
| + all_resources = {} | 
| + with open(r_txt_files[0]) as f: | 
| + for line in f: | 
| + m = re.match(r'(int(?:\[\])?) (\w+) (\w+) (.+)$', line) | 
| + if not m: | 
| + raise Exception('Unexpected line in R.txt: %s' % line) | 
| + java_type, resource_type, name, value = m.groups() | 
| + all_resources[(resource_type, name)] = (java_type, value) | 
| + | 
| + for package, r_text_file in zip(extra_packages, extra_r_text_files): | 
| package_r_java_dir = os.path.join(r_dir, *package.split('.')) | 
| build_utils.MakeDirectory(package_r_java_dir) | 
| package_r_java_path = os.path.join(package_r_java_dir, 'R.java') | 
| - new_r_java = re.sub(r'package [.\w]*;', u'package %s;' % package, | 
| - r_java_contents) | 
| - codecs.open(package_r_java_path, 'w', encoding='utf-8').write(new_r_java) | 
| - # TODO(cjhopman): These extra package's R.java files should be filtered to | 
| - # only contain the resources listed in their R.txt files. At this point, we | 
| - # have already compiled those other libraries, so doing this would only | 
| - # affect how the code in this .apk target could refer to the resources. | 
| + CreateExtraRJavaFile( | 
| + package, package_r_java_path, r_text_file, all_resources) | 
| + | 
| + | 
| +def CreateExtraRJavaFile(package, r_java_path, r_text_file, all_resources): | 
| + resources = {} | 
| + with open(r_text_file) as f: | 
| + for line in f: | 
| + m = re.match(r'int(?:\[\])? (\w+) (\w+) ', line) | 
| + if not m: | 
| + raise Exception('Unexpected line in R.txt: %s' % line) | 
| + resource_type, name = m.groups() | 
| + if resource_type not in resources: | 
| + resources[resource_type] = [] | 
| + resources[resource_type].append(name) | 
| + | 
| + template = Template("""/* AUTO-GENERATED FILE. DO NOT MODIFY. */ | 
| + | 
| +package ${PACKAGE}; | 
| + | 
| +public final class R { | 
| +${RESOURCE_CLASSES} | 
| +} | 
| +""") | 
| + | 
| + class_template = Template(""" public static final class ${RESOURCE_TYPE} { | 
| +${RESOURCES} | 
| + } | 
| +""") | 
| + | 
| + resource_template = Template( | 
| + ' public static final ${TYPE} ${NAME} = ${VALUE};') | 
| + resource_classes = [] | 
| + for resource_type in sorted(resources.keys()): | 
| + resource_strings = [] | 
| + for name in resources[resource_type]: | 
| + java_type, value = all_resources[(resource_type, name)] | 
| + values = { | 
| + 'TYPE': java_type, | 
| + 'NAME': name, | 
| + 'VALUE': value | 
| + } | 
| + resource_strings.append(resource_template.substitute(values)) | 
| + values = { | 
| + 'RESOURCE_TYPE': resource_type, | 
| + 'RESOURCES': '\n'.join(resource_strings) | 
| + } | 
| + resource_classes.append(class_template.substitute(values)) | 
| + values = { | 
| + 'PACKAGE': package, | 
| + 'RESOURCE_CLASSES': '\n'.join(resource_classes) | 
| + } | 
| + with open(r_java_path, 'w') as f: | 
| + f.write(template.substitute(values)) | 
| def CrunchDirectory(aapt, input_dir, output_dir): | 
| @@ -280,7 +336,8 @@ def main(): | 
| if options.extra_res_packages: | 
| CreateExtraRJavaFiles( | 
| gen_dir, | 
| - build_utils.ParseGypList(options.extra_res_packages)) | 
| + build_utils.ParseGypList(options.extra_res_packages), | 
| + build_utils.ParseGypList(options.extra_r_text_files)) | 
| # This is the list of directories with resources to put in the final .zip | 
| # file. The order of these is important so that crunched/v14 resources | 
| @@ -310,6 +367,10 @@ def main(): | 
| else: | 
| build_utils.ZipDir(options.srcjar_out, gen_dir) | 
| + if options.r_text_out: | 
| + r_text_path = os.path.join(gen_dir, 'R.txt') | 
| + shutil.copyfile(r_text_path, options.r_text_out) | 
| + | 
| if options.depfile: | 
| input_files += build_utils.GetPythonDependencies() | 
| build_utils.WriteDepfile(options.depfile, input_files) |