mojo_run: infer --origin from `MOJO_VERSION` file if present.

This allows mojo consumers that use pinned mojo version not to set
--origin to point to the correct google storage directory manually - it
will be set based on MOJO_VERSION if MOJO_VERSION is present AND
--origin is not passed explicitly.

R=tonyg@chromium.org

Review URL: https://codereview.chromium.org/1581773005 .

Cr-Mirrored-From: https://github.com/domokit/mojo
Cr-Mirrored-Commit: d34ebee9950ba27b50ef8f78d29c2bb3e6d6dc51
diff --git a/devtoolslib/paths.py b/devtoolslib/paths.py
index 502636a..8c592ae 100644
--- a/devtoolslib/paths.py
+++ b/devtoolslib/paths.py
@@ -37,7 +37,7 @@
   return os.path.join(ancestor, target_relpath)
 
 
-def infer_paths(is_android, is_debug, target_cpu):
+def infer_params(is_android, is_debug, target_cpu):
   """Infers the locations of select build output artifacts in a regular
   Chromium-like checkout. This should grow thinner or disappear as we introduce
   per-repo config files, see https://github.com/domokit/devtools/issues/28.
@@ -51,19 +51,24 @@
   out_build_dir = os.path.join('out', build_dir)
 
   root_path = find_ancestor_with(out_build_dir)
-  paths = collections.defaultdict(lambda: None)
+  params = collections.defaultdict(lambda: None)
   if not root_path:
-    return paths
+    return params
 
   build_dir_path = os.path.join(root_path, out_build_dir)
-  paths['build_dir_path'] = build_dir_path
+  params['build_dir_path'] = build_dir_path
   if is_android:
-    paths['shell_path'] = os.path.join(build_dir_path, 'apks', 'MojoShell.apk')
-    paths['adb_path'] = os.path.join(root_path, 'third_party', 'android_tools',
+    params['shell_path'] = os.path.join(build_dir_path, 'apks', 'MojoShell.apk')
+    params['adb_path'] = os.path.join(root_path, 'third_party', 'android_tools',
                                 'sdk', 'platform-tools', 'adb')
   else:
-    paths['shell_path'] = os.path.join(build_dir_path, 'mojo_shell')
-  return paths
+    params['shell_path'] = os.path.join(build_dir_path, 'mojo_shell')
+
+  mojo_version_file = find_within_ancestors('MOJO_VERSION')
+  if mojo_version_file:
+    with open(mojo_version_file) as f:
+      params['mojo_version'] = f.read().strip()
+  return params
 
 
 # Based on Chromium //tools/find_depot_tools.py.
diff --git a/devtoolslib/shell_config.py b/devtoolslib/shell_config.py
index ace522b..e1c04fe 100644
--- a/devtoolslib/shell_config.py
+++ b/devtoolslib/shell_config.py
@@ -61,7 +61,10 @@
                       action='store_true')
   parser.add_argument('--shell-path', help='Path of the Mojo shell binary.')
   parser.add_argument('--origin', help='Origin for mojo: URLs. This can be a '
-                      'web url or a local directory path.')
+                      'web url or a local directory path. If MOJO_VERSION file '
+                      'is among ancestors of this file, by default the origin '
+                      'will point to mojo services prebuilt at the '
+                      'specified version in Google Storage')
   parser.add_argument('--map-url', action='append',
                       help='Define a mapping for a url in the format '
                       '<url>=<url-or-local-file-path>')
@@ -143,22 +146,34 @@
   """
   # Infer paths based on the Chromium configuration options
   # (--debug/--release, etc.), if running within a Chromium-like checkout.
-  inferred_paths = paths.infer_paths(script_args.android, script_args.debug,
-                                     script_args.target_cpu)
-  shell_config = ShellConfig()
+  inferred_params = paths.infer_params(script_args.android, script_args.debug,
+                                       script_args.target_cpu)
+  inferred_origin = None
+  if inferred_params['mojo_version']:
+    inferred_origin = "https://storage.googleapis.com/mojo/services"
+    if script_args.android:
+      inferred_origin += "/android-arm"
+    else:
+      inferred_origin += "/linux-x64"
+    # Get the versions that were built against Modular's version of the SDK.
+    inferred_origin += "/%s" % inferred_params['mojo_version']
+    if script_args.verbose:
+      print('Inferring origin from `MOJO_VERSION` as: ' +
+            inferred_origin)
 
+  shell_config = ShellConfig()
   shell_config.android = script_args.android
   shell_config.shell_path = (script_args.shell_path or
-                             inferred_paths['shell_path'])
-  shell_config.origin = script_args.origin
+                             inferred_params['shell_path'])
+  shell_config.origin = script_args.origin or inferred_origin
   shell_config.map_url_list = script_args.map_url
   shell_config.map_origin_list = script_args.map_origin
   shell_config.reuse_servers = script_args.reuse_servers
   shell_config.verbose = script_args.verbose
 
   # Android-only.
-  shell_config.adb_path = (script_args.adb_path or inferred_paths['adb_path'] or
-                           'adb')
+  shell_config.adb_path = (script_args.adb_path or inferred_params['adb_path']
+                           or 'adb')
   shell_config.target_device = script_args.target_device
   shell_config.logcat_tags = script_args.logcat_tags
 
@@ -166,8 +181,8 @@
   shell_config.use_osmesa = script_args.use_osmesa
 
   if (shell_config.android and not shell_config.origin and
-      inferred_paths['build_dir_path']):
-    shell_config.origin = inferred_paths['build_dir_path']
+      inferred_params['build_dir_path']):
+    shell_config.origin = inferred_params['build_dir_path']
 
   # Read the mojoconfig file.
   config_file = script_args.config_file
@@ -182,9 +197,9 @@
           alias_from, alias_to = alias_spec.split('=')
           config_file_aliases.append(('@{%s}' % alias_from, alias_to))
 
-      if inferred_paths['build_dir_path']:
+      if inferred_params['build_dir_path']:
         config_file_aliases.append(('@{BUILD_DIR}',
-                                    inferred_paths['build_dir_path']))
+                                    inferred_params['build_dir_path']))
 
       config = None
       try:
diff --git a/docs/mojo_run.md b/docs/mojo_run.md
index 11758b6..ad4f0e2 100644
--- a/docs/mojo_run.md
+++ b/docs/mojo_run.md
@@ -69,3 +69,12 @@
 mkdir ~/another_home
 HOME=~/another_home mojo_run APP_URL --reuse-servers
 ```
+
+## Setting default mojo origin
+
+When run outside of the `domokit/mojo` repository, `mojo_run` needs `--origin`
+parameter to indicate where binaries of the core mojo services come from. If a
+`MOJO_VERSION` file is present among ancestors of `mojo_run` and `--origin`
+parameter is not set, origin will point to Google Storage location storing
+binaries of core mojo services built at the git revision indicated in
+`MOJO_VERSION`.