DevTools: introduce devtools_paths.py to manage paths

The new devtools_paths.py detects whether the frontend code
is hosted in the Chromium repository, in an external repository during
standalone build, or in an external repository during integrated build.

R=aerotwist@chromium.org

Change-Id: I800ab63f985d203359e387f9c81df9270929b437
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1839791
Reviewed-by: Benedikt Meurer <bmeurer@chromium.org>
Commit-Queue: Yang Guo <yangguo@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#703014}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: dd36b0f6b3fdbaa069facb239773596ae816febb
diff --git a/scripts/devtools_paths.py b/scripts/devtools_paths.py
new file mode 100644
index 0000000..14d721c
--- /dev/null
+++ b/scripts/devtools_paths.py
@@ -0,0 +1,73 @@
+# Copyright 2019 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+"""
+Helper to find the path to the correct third_party directory
+"""
+
+from os import path
+import sys
+
+
+# Find the root path of the checkout.
+# In the Chromium repository, this is the src/chromium directory.
+# In the external repository, standalone build, this is the devtools-frontend directory.
+# In the external repository, integrated build, this is the src/chromium directory.
+def root_path():
+    SCRIPTS_PATH = path.dirname(path.abspath(__file__))
+    ABS_DEVTOOLS_PATH = path.dirname(SCRIPTS_PATH)
+    PARENT_PATH = path.dirname(ABS_DEVTOOLS_PATH)
+    # TODO(1011259): remove Chromium repository handling
+    if path.basename(PARENT_PATH) == 'renderer':
+        # Chromium repository
+        return path.dirname(path.dirname(path.dirname(PARENT_PATH)))
+    elif path.basename(PARENT_PATH) == 'third_party':
+        # External repository, integrated build
+        return path.dirname(PARENT_PATH)
+    else:
+        # External repository, standalone build
+        return ABS_DEVTOOLS_PATH
+
+
+# This is the third_party path relative to the root of the checkout.
+def third_party_path():
+    return path.join(root_path(), 'third_party')
+
+
+# This points to the node binary downloaded as part of the checkout.
+def node_path():
+    try:
+        old_sys_path = sys.path[:]
+        sys.path.append(path.join(third_party_path(), 'node'))
+        import node
+    finally:
+        sys.path = old_sys_path
+    return node.GetBinaryPath()
+
+
+DEVTOOLS_ROOT_PATH = path.join(path.dirname(__file__), '..')
+
+
+def node_modules_path():
+    SCRIPTS_PATH = path.dirname(path.abspath(__file__))
+    ABS_DEVTOOLS_PATH = path.dirname(SCRIPTS_PATH)
+    PARENT_PATH = path.dirname(ABS_DEVTOOLS_PATH)
+    # TODO(1011259): remove Chromium repository handling
+    if path.basename(PARENT_PATH) == 'renderer':
+        # While in Chromium repo, node modules are hosted in //third_party/devtools-node-modules.
+        return path.join(root_path(), 'third_party', 'devtools-node-modules', 'third_party', 'node_modules')
+    else:
+        # In the external repo, node modules are hosted in root.
+        return path.join(root_path(), 'node_modules')
+
+
+def eslint_path():
+    return path.join(node_modules_path(), 'eslint', 'bin', 'eslint.js')
+
+
+def karma_path():
+    return path.join(node_modules_path(), 'karma', 'bin', 'karma')
+
+
+def package_json_path():
+    return path.join(DEVTOOLS_ROOT_PATH, 'package.json')