Update scripts to use DEPS-pinned depot_tools

Always use gn.py in depot_tools instead of just gn.
The https://cs.chromium.org/chromium/src/build/find_depot_tools.py
is looking up the DEPS-pinned copy in third_party/depot_tools
and adds it to the path when add_depot_tools_to_path() is called.

Similar use:
 

https: //cs.chromium.org/search/?q=%22find_depot_tools.add_depot_tools_to_path()%22&sq=package:chromium&type=cs
Bug: webrtc:8393
Change-Id: I3cfa3d96b4d0f60e8099e556876bc94340b1bbb5
Reviewed-on: https://webrtc-review.googlesource.com/12540
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Reviewed-by: Patrik Höglund <phoglund@google.com>
Commit-Queue: Henrik Kjellander <kjellander@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20333}
diff --git a/examples/androidtests/gradle_project_test.py b/examples/androidtests/gradle_project_test.py
index cd8fcd8..3ef6e06 100644
--- a/examples/androidtests/gradle_project_test.py
+++ b/examples/androidtests/gradle_project_test.py
@@ -60,10 +60,15 @@
   project_dir = os.path.abspath(project_dir)
 
   try:
+    env = os.environ.copy()
+    env['PATH'] = os.pathsep.join([
+        os.path.join(SRC_DIR, 'third_party', 'depot_tools'), env.get('PATH', '')
+    ])
     _RunCommand([GENERATE_GRADLE_SCRIPT, '--output-directory', output_dir,
         '--target', '//examples:AppRTCMobile',
         '--project-dir', project_dir,
-        '--use-gradle-process-resources', '--split-projects', '--canary'])
+        '--use-gradle-process-resources', '--split-projects', '--canary'],
+        env=env)
     _RunCommand([GRADLEW_BIN, 'assembleDebug'], project_dir)
   finally:
     # Do not delete temporary directory if user specified it manually.
diff --git a/examples/androidtests/video_quality_loopback_test.py b/examples/androidtests/video_quality_loopback_test.py
index 0b55878..6991a0a 100755
--- a/examples/androidtests/video_quality_loopback_test.py
+++ b/examples/androidtests/video_quality_loopback_test.py
@@ -34,6 +34,8 @@
 BAD_DEVICES_JSON = os.path.join(SRC_DIR,
                                 os.environ.get('CHROMIUM_OUT_DIR', 'out'),
                                 'bad_devices.json')
+sys.path.append(os.path.join(SRC_DIR, 'build'))
+import find_depot_tools
 
 
 class Error(Exception):
@@ -95,8 +97,13 @@
 
   if not build_dir_x86:
     build_dir_x86 = os.path.join(temp_dir, 'LocalBuild')
-    _RunCommand(['gn', 'gen', build_dir_x86])
-    _RunCommand(['ninja', '-C', build_dir_x86, 'frame_analyzer'])
+
+    def DepotToolPath(*args):
+      return os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, *args)
+
+    _RunCommand([sys.executable, DepotToolPath('gn.py'), 'gen', build_dir_x86])
+    _RunCommand([DepotToolPath('ninja'), '-C', build_dir_x86,
+                 'frame_analyzer'])
 
   tools_dir = os.path.join(SRC_DIR, 'tools_webrtc')
   toolchain_dir = os.path.join(tools_dir, 'video_quality_toolchain')
diff --git a/rtc_tools/testing/utils.py b/rtc_tools/testing/utils.py
index 3b82fc8..8b9a81b 100755
--- a/rtc_tools/testing/utils.py
+++ b/rtc_tools/testing/utils.py
@@ -18,6 +18,14 @@
 import zipfile
 
 
+SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
+SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
+sys.path.append(os.path.join(SRC_DIR, 'build'))
+
+
+import find_depot_tools
+
+
 def RunSubprocessWithRetry(cmd):
   """Invokes the subprocess and backs off exponentially on fail."""
   for i in range(5):
@@ -36,10 +44,14 @@
 def DownloadFilesFromGoogleStorage(path, auto_platform=True):
   print 'Downloading files in %s...' % path
 
-  extension = 'bat' if 'win32' in sys.platform else 'py'
-  cmd = ['download_from_google_storage.%s' % extension,
-         '--bucket=chromium-webrtc-resources',
-         '--directory', path]
+  cmd = [
+    sys.executable,
+    os.path.join(find_depot_tools.DEPOT_TOOLS_PATH,
+                 'download_from_google_storage.py'),
+    '--bucket=chromium-webrtc-resources',
+    '--directory',
+    path,
+  ]
   if auto_platform:
     cmd += ['--auto_platform', '--recursive']
   subprocess.check_call(cmd)
diff --git a/tools_webrtc/android/build_aar.py b/tools_webrtc/android/build_aar.py
index c86d266..0ddca2f 100755
--- a/tools_webrtc/android/build_aar.py
+++ b/tools_webrtc/android/build_aar.py
@@ -35,6 +35,7 @@
 
 
 SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
+SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
 DEFAULT_ARCHS = ['armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64']
 NEEDED_SO_FILES = ['libjingle_peerconnection_so.so']
 JAR_FILE = 'lib.java/sdk/android/libwebrtc.jar'
@@ -47,6 +48,10 @@
 sys.path.append(os.path.join(SCRIPT_DIR, '..', 'libs'))
 from generate_licenses import LicenseBuilder
 
+sys.path.append(os.path.join(SRC_DIR, 'build'))
+import find_depot_tools
+
+
 
 def _ParseArgs():
   parser = argparse.ArgumentParser(description='libwebrtc.aar generator.')
@@ -66,14 +71,16 @@
 
 
 def _RunGN(args):
-  cmd = ['gn']
+  cmd = [sys.executable,
+         os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py')]
   cmd.extend(args)
   logging.debug('Running: %r', cmd)
   subprocess.check_call(cmd)
 
 
 def _RunNinja(output_directory, args):
-  cmd = ['ninja', '-C', output_directory]
+  cmd = [os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'),
+         '-C', output_directory]
   cmd.extend(args)
   logging.debug('Running: %r', cmd)
   subprocess.check_call(cmd)
diff --git a/tools_webrtc/download_tools.py b/tools_webrtc/download_tools.py
index 5cc8de2..7324245f 100755
--- a/tools_webrtc/download_tools.py
+++ b/tools_webrtc/download_tools.py
@@ -20,10 +20,11 @@
 
 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
 SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir))
-DEPOT_TOOLS_DIR = os.path.join(SRC_DIR, 'third_party', 'depot_tools')
-sys.path.insert(0, DEPOT_TOOLS_DIR)
+sys.path.append(os.path.join(SRC_DIR, 'build'))
 
 
+import find_depot_tools
+find_depot_tools.add_depot_tools_to_path()
 import gclient_utils
 import subprocess2
 
@@ -35,7 +36,8 @@
   for path in directories:
     cmd = [
       sys.executable,
-      os.path.join(DEPOT_TOOLS_DIR, 'download_from_google_storage.py'),
+      os.path.join(find_depot_tools.DEPOT_TOOLS_PATH,
+                   'download_from_google_storage.py'),
       '--directory',
       '--num_threads=10',
       '--bucket', 'chrome-webrtc-resources',
diff --git a/tools_webrtc/ios/build_ios_libs.py b/tools_webrtc/ios/build_ios_libs.py
index 0051982..b48304e 100755
--- a/tools_webrtc/ios/build_ios_libs.py
+++ b/tools_webrtc/ios/build_ios_libs.py
@@ -25,8 +25,11 @@
 os.environ['PATH'] = '/usr/libexec' + os.pathsep + os.environ['PATH']
 
 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
-WEBRTC_SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, '..', '..'))
-SDK_OUTPUT_DIR = os.path.join(WEBRTC_SRC_DIR, 'out_ios_libs')
+SRC_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, '..', '..'))
+sys.path.append(os.path.join(SRC_DIR, 'build'))
+import find_depot_tools
+
+SDK_OUTPUT_DIR = os.path.join(SRC_DIR, 'out_ios_libs')
 SDK_LIB_NAME = 'librtc_sdk_objc.a'
 SDK_FRAMEWORK_NAME = 'WebRTC.framework'
 
@@ -75,7 +78,7 @@
 
 def _RunCommand(cmd):
   logging.debug('Running: %r', cmd)
-  subprocess.check_call(cmd, cwd=WEBRTC_SRC_DIR)
+  subprocess.check_call(cmd, cwd=SRC_DIR)
 
 
 def _CleanArtifacts(output_dir):
@@ -122,11 +125,22 @@
   args_string = ' '.join(gn_args + extra_gn_args)
   logging.info('Building WebRTC with args: %s', args_string)
 
-  cmd = ['gn', 'gen', output_dir, '--args=' + args_string]
+  cmd = [
+    sys.executable,
+    os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
+    'gen',
+    output_dir,
+    '--args=' + args_string,
+  ]
   _RunCommand(cmd)
   logging.info('Building target: %s', gn_target_name)
 
-  cmd = ['ninja', '-C', output_dir, gn_target_name]
+  cmd = [
+    os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'),
+    '-C',
+    output_dir,
+    gn_target_name,
+  ]
   if use_goma:
     cmd.extend(['-j', '200'])
   _RunCommand(cmd)
diff --git a/tools_webrtc/libs/generate_licenses.py b/tools_webrtc/libs/generate_licenses.py
index 1bfcb64..6c2a50a 100755
--- a/tools_webrtc/libs/generate_licenses.py
+++ b/tools_webrtc/libs/generate_licenses.py
@@ -47,6 +47,9 @@
 
 SCRIPT_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
 CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
+sys.path.append(os.path.join(CHECKOUT_ROOT, 'build'))
+import find_depot_tools
+
 THIRD_PARTY_LIB_REGEX = r'^.*/third_party/([\w+]+).*$'
 
 class LicenseBuilder(object):
@@ -72,8 +75,15 @@
 
   @staticmethod
   def _RunGN(buildfile_dir, target):
-    cmd = ['gn', 'desc', '--all', '--format=json',
-        os.path.abspath(buildfile_dir), target]
+    cmd = [
+      sys.executable,
+      os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
+      'desc',
+      '--all',
+      '--format=json',
+      os.path.abspath(buildfile_dir),
+      target,
+    ]
     logging.debug("Running: %r", cmd)
     output_json = subprocess.check_output(cmd, cwd=CHECKOUT_ROOT)
     logging.debug("Output: %s", output_json)
diff --git a/tools_webrtc/mb/mb.py b/tools_webrtc/mb/mb.py
index d2bba88..ca021d3 100755
--- a/tools_webrtc/mb/mb.py
+++ b/tools_webrtc/mb/mb.py
@@ -36,6 +36,7 @@
 SRC_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR))
 sys.path = [os.path.join(SRC_DIR, 'build')] + sys.path
 
+import find_depot_tools
 import gn_helpers
 
 
@@ -1405,7 +1406,11 @@
 
   def Build(self, target):
     build_dir = self.ToSrcRelPath(self.args.path[0])
-    ninja_cmd = ['ninja', '-C', build_dir]
+    ninja_cmd = [
+      os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'ninja'),
+      '-C',
+      build_dir,
+    ]
     if self.args.jobs:
       ninja_cmd.extend(['-j', '%d' % self.args.jobs])
     ninja_cmd.append(target)
diff --git a/tools_webrtc/presubmit_checks_lib/gn_check.py b/tools_webrtc/presubmit_checks_lib/gn_check.py
index 7270bdd..a81cc1c 100644
--- a/tools_webrtc/presubmit_checks_lib/gn_check.py
+++ b/tools_webrtc/presubmit_checks_lib/gn_check.py
@@ -6,12 +6,20 @@
 # in the file PATENTS.  All contributing project authors may
 # be found in the AUTHORS file in the root of the source tree.
 
+import os
 import re
 import shutil
 import subprocess
+import sys
 import tempfile
 
 
+SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
+SRC_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
+sys.path.append(os.path.join(SRC_DIR, 'build'))
+import find_depot_tools
+
+
 # GN_ERROR_RE matches the summary of an error output by `gn check`.
 # Matches "ERROR" and following lines until it sees an empty line or a line
 # containing just underscores.
@@ -27,7 +35,13 @@
   """
   out_dir = tempfile.mkdtemp('gn')
   try:
-    command = ['gn', 'gen', '--check', out_dir]
+    command = [
+      sys.executable,
+      os.path.join(find_depot_tools.DEPOT_TOOLS_PATH, 'gn.py'),
+      'gen',
+      '--check',
+      out_dir,
+    ]
     subprocess.check_output(command, cwd=root_dir)
   except subprocess.CalledProcessError as err:
     return GN_ERROR_RE.findall(err.output)