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/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..7324245 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)