Upload WebRTC CLs from Chromium.

This CL removes some assumptions that were making it difficult to
upload a patch from the directory //third_party/webrtc in a
Chromium checkout.

Bug: webrtc:9705
Change-Id: I227ca492d5cf03875474ffd4d31abf387f947e5e
Reviewed-on: https://webrtc-review.googlesource.com/97600
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Patrik Höglund <phoglund@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24549}
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index 1b435a7..ae5303d 100755
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -110,6 +110,14 @@
 FILE_PATH_RE = re.compile(r'"(?P<file_path>(\w|\/)+)(?P<extension>\.\w+)"')
 
 
+def FindSrcDirPath(starting_dir):
+  """Returns the abs path to the src/ dir of the project."""
+  src_dir = starting_dir
+  while os.path.basename(src_dir) != 'src':
+    src_dir = os.path.normpath(os.path.join(src_dir, os.pardir))
+  return src_dir
+
+
 @contextmanager
 def _AddToPath(*paths):
   original_sys_path = sys.path
@@ -554,7 +562,7 @@
   with _AddToPath(input_api.os_path.join(
       input_api.PresubmitLocalPath(), 'tools_webrtc', 'presubmit_checks_lib')):
     from gn_check import RunGnCheck
-  errors = RunGnCheck(input_api.PresubmitLocalPath())[:5]
+  errors = RunGnCheck(FindSrcDirPath(input_api.PresubmitLocalPath()))[:5]
   if errors:
     return [output_api.PresubmitPromptWarning(
         'Some #includes do not match the build dependency graph. Please run:\n'
@@ -573,8 +581,8 @@
   # We need to wait until we have an input_api object and use this
   # roundabout construct to import checkdeps because this file is
   # eval-ed and thus doesn't have __file__.
-  checkdeps_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
-                                          'buildtools', 'checkdeps')
+  src_path = FindSrcDirPath(input_api.PresubmitLocalPath())
+  checkdeps_path = input_api.os_path.join(src_path, 'buildtools', 'checkdeps')
   if not os.path.exists(checkdeps_path):
     return [output_api.PresubmitError(
         'Cannot find checkdeps at %s\nHave you run "gclient sync" to '
diff --git a/tools_webrtc/autoroller/roll_deps.py b/tools_webrtc/autoroller/roll_deps.py
index 11e1fb8..75a8add 100755
--- a/tools_webrtc/autoroller/roll_deps.py
+++ b/tools_webrtc/autoroller/roll_deps.py
@@ -19,6 +19,13 @@
 import sys
 import urllib2
 
+def FindSrcDirPath():
+  """Returns the abs path to the src/ dir of the project."""
+  src_dir = os.path.dirname(os.path.abspath(__file__))
+  while os.path.basename(src_dir) != 'src':
+    src_dir = os.path.normpath(os.path.join(src_dir, os.pardir))
+  return src_dir
+
 # Skip these dependencies (list without solution name prefix).
 DONT_AUTOROLL_THESE = [
   'src/examples/androidtests/third_party/gradle',
@@ -41,8 +48,7 @@
 ROLL_BRANCH_NAME = 'roll_chromium_revision'
 
 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
-CHECKOUT_SRC_DIR = os.path.realpath(os.path.join(SCRIPT_DIR, os.pardir,
-                                                 os.pardir))
+CHECKOUT_SRC_DIR = FindSrcDirPath()
 CHECKOUT_ROOT_DIR = os.path.realpath(os.path.join(CHECKOUT_SRC_DIR, os.pardir))
 
 sys.path.append(os.path.join(CHECKOUT_SRC_DIR, 'build'))
diff --git a/tools_webrtc/libs/generate_licenses.py b/tools_webrtc/libs/generate_licenses.py
index 7f2ec46..892fb46 100755
--- a/tools_webrtc/libs/generate_licenses.py
+++ b/tools_webrtc/libs/generate_licenses.py
@@ -21,6 +21,14 @@
 import subprocess
 
 
+def FindSrcDirPath():
+  """Returns the abs path to the src/ dir of the project."""
+  src_dir = os.path.dirname(os.path.abspath(__file__))
+  while os.path.basename(src_dir) != 'src':
+    src_dir = os.path.normpath(os.path.join(src_dir, os.pardir))
+  return src_dir
+
+
 LIB_TO_LICENSES_DICT = {
     'abseil-cpp': ['third_party/abseil-cpp/LICENSE'],
     'android_tools': ['third_party/android_tools/LICENSE'],
@@ -62,8 +70,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'))
+WEBRTC_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
+SRC_DIR = FindSrcDirPath()
+sys.path.append(os.path.join(SRC_DIR, 'build'))
 import find_depot_tools
 
 THIRD_PARTY_LIB_REGEX = r'^.*/third_party/([\w\-+]+).*$'
@@ -101,7 +110,7 @@
       target,
     ]
     logging.debug("Running: %r", cmd)
-    output_json = subprocess.check_output(cmd, cwd=CHECKOUT_ROOT)
+    output_json = subprocess.check_output(cmd, cwd=WEBRTC_ROOT)
     logging.debug("Output: %s", output_json)
     return output_json
 
@@ -147,7 +156,7 @@
       output_license_file.write('# %s\n' % license_lib)
       output_license_file.write('```\n')
       for path in LIB_TO_LICENSES_DICT[license_lib]:
-        license_path = os.path.join(CHECKOUT_ROOT, path)
+        license_path = os.path.join(WEBRTC_ROOT, path)
         with open(license_path, 'r') as license_file:
           license_text = cgi.escape(license_file.read(), quote=True)
           output_license_file.write(license_text)
diff --git a/tools_webrtc/presubmit_checks_lib/gn_check.py b/tools_webrtc/presubmit_checks_lib/gn_check.py
index a81cc1c..459dcd8 100644
--- a/tools_webrtc/presubmit_checks_lib/gn_check.py
+++ b/tools_webrtc/presubmit_checks_lib/gn_check.py
@@ -14,8 +14,15 @@
 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))
+def FindSrcDirPath():
+  """Returns the abs path to the src/ dir of the project."""
+  src_dir = os.path.dirname(os.path.abspath(__file__))
+  while os.path.basename(src_dir) != 'src':
+    src_dir = os.path.normpath(os.path.join(src_dir, os.pardir))
+  return src_dir
+
+
+SRC_DIR = FindSrcDirPath()
 sys.path.append(os.path.join(SRC_DIR, 'build'))
 import find_depot_tools