Allow specifying --version to d8.py

Change-Id: I6bd97ee69b7d892b85380d2ec479a708c423f2e8
diff --git a/tools/d8.py b/tools/d8.py
index 18a4a67..833f7f5 100755
--- a/tools/d8.py
+++ b/tools/d8.py
@@ -3,8 +3,38 @@
 # for details. All rights reserved. Use of this source code is governed by a
 # BSD-style license that can be found in the LICENSE file.
 
+import utils
+
+import optparse
 import sys
 import toolhelper
 
+def ParseOptions(argv):
+  parser = optparse.OptionParser(usage='%prog [options] -- [D8 options]')
+  parser.add_option(
+      '-c',
+      '--commit-hash',
+      '--commit_hash',
+      help='Commit hash of D8 to use.',
+      default=None)
+  parser.add_option(
+      '--version',
+      help='Version of D8 to use.',
+      default=None)
+  parser.add_option(
+      '--tag',
+      help='Tag of D8 to use.',
+      default=None)
+  return parser.parse_args(argv)
+
+def main(argv):
+  (options, args) = ParseOptions(sys.argv)
+  d8_args = args[1:]
+  return toolhelper.run(
+      'd8',
+      d8_args,
+      jar=utils.find_r8_jar_from_options(options),
+      main='com.android.tools.r8.D8')
+
 if __name__ == '__main__':
-  sys.exit(toolhelper.run('d8', sys.argv[1:]))
+  sys.exit(main(sys.argv[1:]))
diff --git a/tools/retrace.py b/tools/retrace.py
index b0a1ec8..ea06566 100755
--- a/tools/retrace.py
+++ b/tools/retrace.py
@@ -53,52 +53,24 @@
   return parser.parse_args()
 
 
-def find_version_or_hash_from_tag(tag_or_hash):
-  info = subprocess.check_output([
-      'git',
-      'show',
-      tag_or_hash,
-      '-s',
-      '--format=oneline']).splitlines()[-1].split()
-  # The info should be on the following form [hash,"Version",version]
-  if len(info) == 3 and len(info[0]) == 40 and info[1] == "Version":
-    return info[2]
-  return None
-
-
 def main():
   args = parse_arguments()
-  if args.tag:
-    hash_or_version = find_version_or_hash_from_tag(args.tag)
-  else:
-    hash_or_version = args.commit_hash or args.version
+  map_path = utils.find_cloud_storage_file_from_options(
+      'r8lib.jar.map', args, orElse=args.map)
   return run(
-      args.map,
-      hash_or_version,
+      map_path,
       args.stacktrace,
       args.commit_hash is not None,
       args.no_r8lib,
       quiet=args.quiet,
       debug=args.debug_agent)
 
-def run(map_path, hash_or_version, stacktrace, is_hash, no_r8lib, quiet=False,
-        debug=False):
-  if hash_or_version:
-    download_path = archive.GetUploadDestination(
-        hash_or_version,
-        'r8lib.jar.map',
-        is_hash)
-    if utils.file_exists_on_cloud_storage(download_path):
-      map_path = tempfile.NamedTemporaryFile().name
-      utils.download_file_from_cloud_storage(download_path, map_path)
-    else:
-      print('Could not find map file from argument: %s.' % hash_or_version)
-      return 1
-
+def run(map_path, stacktrace, is_hash, no_r8lib, quiet=False, debug=False):
   retrace_args = [jdk.GetJavaExecutable()]
 
   if debug:
-    retrace_args.append('-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005')
+    retrace_args.append(
+        '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005')
 
   retrace_args += [
     '-cp',
diff --git a/tools/utils.py b/tools/utils.py
index 5c1b32b..bd28ef3 100644
--- a/tools/utils.py
+++ b/tools/utils.py
@@ -103,6 +103,46 @@
       f.write(str(value))
     archive_file(name, gs_dir, tempfile)
 
+def find_cloud_storage_file_from_options(name, options, orElse=None):
+  # Import archive on-demand since archive depends on utils.
+  from archive import GetUploadDestination
+  hash_or_version = find_hash_or_version_from_options(options)
+  if not hash_or_version:
+    return orElse
+  is_hash = options.commit_hash is not None
+  download_path = GetUploadDestination(hash_or_version, name, is_hash)
+  if file_exists_on_cloud_storage(download_path):
+    out = tempfile.NamedTemporaryFile().name
+    download_file_from_cloud_storage(download_path, out)
+    return out
+  else:
+    raise Exception('Could not find file {} from hash/version: {}.'
+                  .format(name, hash_or_version))
+
+def find_r8_jar_from_options(options):
+  return find_cloud_storage_file_from_options('r8.jar', options)
+
+def find_r8_lib_jar_from_options(options):
+  return find_cloud_storage_file_from_options('r8lib.jar', options)
+
+def find_hash_or_version_from_options(options):
+  if options.tag:
+    return find_hash_or_version_from_tag(options.tag)
+  else:
+    return options.commit_hash or options.version
+
+def find_hash_or_version_from_tag(tag_or_hash):
+  info = subprocess.check_output([
+      'git',
+      'show',
+      tag_or_hash,
+      '-s',
+      '--format=oneline']).splitlines()[-1].split()
+  # The info should be on the following form [hash,"Version",version]
+  if len(info) == 3 and len(info[0]) == 40 and info[1] == "Version":
+    return info[2]
+  return None
+
 def getAndroidHome():
   return os.environ.get(
       ANDROID_HOME_ENVIROMENT_NAME, os.path.join(USER_HOME, 'Android', 'Sdk'))