Răsfoiți Sursa

gn_helper: find correct directory for //

gn finds // by searching .gn in parent directories.

To support out dir is not 2 directories from root (i.e. out/Default),
search .gn in parent directory for '//' location.

tools/licenses/licenses.py runs 'gn gen' in out/Default/$tmp.
for such case, '//' should be '../../..', or fail to import '//path'

Change-Id: I6e5ccbe93cb5e51704f31d4eb558c03560286865
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6051636
Reviewed-by: Philipp Wollermann <philwo@google.com>
Commit-Queue: Fumitoshi Ukai <ukai@google.com>
Reviewed-by: Takuto Ikuta <tikuta@chromium.org>
Fumitoshi Ukai 8 luni în urmă
părinte
comite
fd5161870a
2 a modificat fișierele cu 16 adăugiri și 1 ștergeri
  1. 13 1
      gn_helper.py
  2. 3 0
      tests/gn_helper_test.py

+ 13 - 1
gn_helper.py

@@ -7,6 +7,18 @@ import os
 import re
 import re
 
 
 
 
+def _find_root(output_dir):
+    curdir = output_dir
+    while True:
+        if os.path.exists(os.path.join(curdir, ".gn")):
+            return curdir
+        nextdir = os.path.join(curdir, "..")
+        if os.path.abspath(curdir) == os.path.abspath(nextdir):
+            raise Exception(
+                'Could not find checkout in any parent of the current path.')
+        curdir = nextdir
+
+
 def _gn_lines(output_dir, path):
 def _gn_lines(output_dir, path):
     """
     """
     Generator function that returns args.gn lines one at a time, following
     Generator function that returns args.gn lines one at a time, following
@@ -20,7 +32,7 @@ def _gn_lines(output_dir, path):
                 raw_import_path = match.groups()[0]
                 raw_import_path = match.groups()[0]
                 if raw_import_path[:2] == "//":
                 if raw_import_path[:2] == "//":
                     import_path = os.path.normpath(
                     import_path = os.path.normpath(
-                        os.path.join(output_dir, "..", "..",
+                        os.path.join(_find_root(output_dir),
                                      raw_import_path[2:]))
                                      raw_import_path[2:]))
                 else:
                 else:
                     import_path = os.path.normpath(
                     import_path = os.path.normpath(

+ 3 - 0
tests/gn_helper_test.py

@@ -36,6 +36,7 @@ class GnHelperTest(trial_dir.TestCase):
         super().tearDown()
         super().tearDown()
 
 
     def test_lines(self):
     def test_lines(self):
+        write('.gn', '')
         out_dir = os.path.join('out', 'dir')
         out_dir = os.path.join('out', 'dir')
         # Make sure nested import directives work. This is based on the
         # Make sure nested import directives work. This is based on the
         # reclient test.
         # reclient test.
@@ -52,6 +53,7 @@ class GnHelperTest(trial_dir.TestCase):
         ])
         ])
 
 
     def test_args(self):
     def test_args(self):
+        write('.gn', '')
         out_dir = os.path.join('out', 'dir')
         out_dir = os.path.join('out', 'dir')
         # Make sure nested import directives work. This is based on the
         # Make sure nested import directives work. This is based on the
         # reclient test.
         # reclient test.
@@ -68,6 +70,7 @@ class GnHelperTest(trial_dir.TestCase):
         ])
         ])
 
 
     def test_args_spaces(self):
     def test_args_spaces(self):
+        write('.gn', '')
         out_dir = os.path.join('out', 'dir')
         out_dir = os.path.join('out', 'dir')
         # Make sure nested import directives work. This is based on the
         # Make sure nested import directives work. This is based on the
         # reclient test.
         # reclient test.