Add ability to print golem config from run_on_app_dump

Bug: 173189095, 181732467
Change-Id: I91701d2815e8558645c16d535681a9cdac703ade
diff --git a/tools/run_on_app_dump.py b/tools/run_on_app_dump.py
index e035e46..4426f2b 100755
--- a/tools/run_on_app_dump.py
+++ b/tools/run_on_app_dump.py
@@ -8,6 +8,7 @@
 import as_utils
 import compiledump
 import gradle
+import hashlib
 import optparse
 import os
 import shutil
@@ -464,11 +465,11 @@
         f.write(line)
 
 
-def download_app(app_sha, internal):
+def download_app(app_sha, internal, quiet=False):
   if internal:
     utils.DownloadFromX20(app_sha)
   else:
-    utils.DownloadFromGoogleCloudStorage(app_sha)
+    utils.DownloadFromGoogleCloudStorage(app_sha, quiet=quiet)
 
 
 def is_logging_enabled_for(app, options):
@@ -804,6 +805,11 @@
         warn('  {}-#{}: {}'.format(shrinker, compilation_index, build_status))
         continue
 
+      if options.golem:
+        print('%s(RunTimeRaw): %s ms' % (app.name, result.get('duration')))
+        print('%s(CodeSize): %s' % (app.name, result.get('dex_size')))
+        continue
+
       print('  {}-#{}:'.format(shrinker, compilation_index))
       dex_size = result.get('dex_size')
       msg = '    dex size: {}'.format(dex_size)
@@ -861,6 +867,10 @@
                     help='Running on bot, use third_party dependency.',
                     default=False,
                     action='store_true')
+  result.add_option('--generate-golem-config', '--generate_golem_config',
+                    help='Generate a new config for golem.',
+                    default=False,
+                    action='store_true')
   result.add_option('--debug-agent',
                     help='Enable Java debug agent and suspend compilation '
                          '(default disabled)',
@@ -979,6 +989,63 @@
   return (options, args)
 
 
+def print_indented(s, indent):
+  print(' ' * indent + s)
+
+
+def get_sha256(gz_file):
+  with open(gz_file, 'rb') as f:
+    bytes = f.read() # read entire file as bytes
+    return hashlib.sha256(bytes).hexdigest();
+
+
+def get_sha_from_file(sha_file):
+  with open(sha_file, 'r') as f:
+    return f.readlines()[0]
+
+
+def print_golem_config(options):
+  print('// AUTOGENERATED FILE from tools/run_on_app_dump.py in R8 repo')
+  print('part of r8_config')
+  print('final Suite dumpsSuite = new Suite("OpenSourceAppDumps");')
+  print('')
+  print('createOpenSourceAppBenchmarks() {')
+  print_indented('var cpus = ["Lenovo M90"];', 2)
+  for app in options.apps:
+    if app.folder and not app.internal:
+      indentation = 2;
+      print_indented('{', indentation)
+      indentation = 4
+      print_indented('var name = "%s";' % app.name, indentation)
+      print_indented('var benchmark = ', indentation)
+      print_indented(
+          'new StandardBenchmark(name, [Metric.RunTimeRaw, Metric.CodeSize]);',
+          indentation + 4)
+      print_indented(
+          'var options = benchmark.addTargets(noImplementation, ["R8"]);',
+          indentation)
+      print_indented('options.cpus = cpus;', indentation)
+      print_indented('options.isScript = true;', indentation)
+      print_indented('options.mainFile = "tools/run_on_app_dump.py"', indentation)
+      print_indented('"--golem --shrinker r8 --app %s";' % app.name, indentation + 4)
+      app_gz = os.path.join(utils.OPENSOURCE_DUMPS_DIR, app.folder + '.tar.gz')
+      app_sha = app_gz + '.sha1'
+      # Golem uses a sha256 of the file in the cache, and you need to specify that.
+      download_app(app_sha, app.internal, quiet=True)
+      sha256 = get_sha256(app_gz)
+      sha = get_sha_from_file(app_sha)
+      print_indented('var resource = BenchmarkResource("",', indentation)
+      print_indented('type: BenchmarkResourceType.Storage,', indentation + 4)
+      print_indented('uri: "gs://r8-deps/%s",' % sha, indentation + 4)
+      print_indented('hash:', indentation + 4)
+      print_indented('"%s"' % sha256, indentation + 8)
+      print_indented('extract: "gz")', indentation + 4);
+      print_indented('options.resources.add(resource);', indentation)
+      print_indented('dumpsSuite.addBenchmark(name);', indentation)
+      indentation = 2
+      print_indented('}', indentation)
+  print('}')
+
 def main(argv):
   (options, args) = parse_options(argv)
 
@@ -995,6 +1062,10 @@
     options.quiet = True
     options.no_logging = True
 
+  if options.generate_golem_config:
+    print_golem_config(options)
+    return 0
+
   with utils.TempDir() as temp_dir:
     if options.hash:
       # Download r8-<hash>.jar from
diff --git a/tools/utils.py b/tools/utils.py
index 1ebcfb7..1820991 100644
--- a/tools/utils.py
+++ b/tools/utils.py
@@ -238,15 +238,19 @@
   PrintCmd(cmd)
   subprocess.check_call(cmd)
 
-def DownloadFromGoogleCloudStorage(sha1_file, bucket='r8-deps', auth=False):
+def DownloadFromGoogleCloudStorage(sha1_file, bucket='r8-deps', auth=False,
+                                   quiet=False):
   suffix = '.bat' if IsWindows() else ''
   download_script = 'download_from_google_storage%s' % suffix
   cmd = [download_script]
   if not auth:
     cmd.append('-n')
   cmd.extend(['-b', bucket, '-u', '-s',  sha1_file])
-  PrintCmd(cmd)
-  subprocess.check_call(cmd)
+  if not quiet:
+    PrintCmd(cmd)
+    subprocess.check_call(cmd)
+  else:
+    subprocess.check_output(cmd)
 
 def get_sha1(filename):
   sha1 = hashlib.sha1()