mirror of
https://chromium.googlesource.com/chromium/tools/depot_tools.git
synced 2026-01-11 10:41:31 +00:00
depot_tools: Add tests for gclient_paths
Change-Id: I544aa85b040544508df8ed16310def982356fbcc Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1949417 Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org> Reviewed-by: Anthony Polito <apolito@google.com>
This commit is contained in:
124
gclient_paths.py
124
gclient_paths.py
@@ -11,7 +11,9 @@
|
||||
from __future__ import print_function
|
||||
|
||||
import gclient_utils
|
||||
import logging
|
||||
import os
|
||||
import subprocess2
|
||||
import sys
|
||||
|
||||
|
||||
@@ -25,66 +27,66 @@ def FindGclientRoot(from_dir, filename='.gclient'):
|
||||
return None
|
||||
path = split_path[0]
|
||||
|
||||
logging.info('Found gclient root at ' + path)
|
||||
|
||||
if path == real_from_dir:
|
||||
return path
|
||||
|
||||
# If we did not find the file in the current directory, make sure we are in a
|
||||
# sub directory that is controlled by this configuration.
|
||||
if path != real_from_dir:
|
||||
entries_filename = os.path.join(path, filename + '_entries')
|
||||
if not os.path.exists(entries_filename):
|
||||
# If .gclient_entries does not exist, a previous call to gclient sync
|
||||
# might have failed. In that case, we cannot verify that the .gclient
|
||||
# is the one we want to use. In order to not to cause too much trouble,
|
||||
# just issue a warning and return the path anyway.
|
||||
print(
|
||||
"%s missing, %s file in parent directory %s might not be the file "
|
||||
"you want to use." % (entries_filename, filename, path),
|
||||
file=sys.stderr)
|
||||
return path
|
||||
scope = {}
|
||||
try:
|
||||
import io
|
||||
with io.open(entries_filename, encoding='utf-8') as f:
|
||||
exec(f.read(), scope)
|
||||
except SyntaxError as e:
|
||||
SyntaxErrorToError(filename, e)
|
||||
all_directories = scope['entries'].keys()
|
||||
path_to_check = real_from_dir[len(path)+1:]
|
||||
while path_to_check:
|
||||
if path_to_check in all_directories:
|
||||
return path
|
||||
path_to_check = os.path.dirname(path_to_check)
|
||||
return None
|
||||
entries_filename = os.path.join(path, filename + '_entries')
|
||||
if not os.path.exists(entries_filename):
|
||||
# If .gclient_entries does not exist, a previous call to gclient sync
|
||||
# might have failed. In that case, we cannot verify that the .gclient
|
||||
# is the one we want to use. In order to not to cause too much trouble,
|
||||
# just issue a warning and return the path anyway.
|
||||
print(
|
||||
"%s missing, %s file in parent directory %s might not be the file "
|
||||
"you want to use." % (entries_filename, filename, path),
|
||||
file=sys.stderr)
|
||||
return path
|
||||
|
||||
import logging
|
||||
logging.info('Found gclient root at ' + path)
|
||||
return path
|
||||
entries_content = gclient_utils.FileRead(entries_filename)
|
||||
scope = {}
|
||||
try:
|
||||
exec(entries_content, scope)
|
||||
except (SyntaxError, Exception) as e:
|
||||
gclient_utils.SyntaxErrorToError(filename, e)
|
||||
|
||||
all_directories = scope['entries'].keys()
|
||||
path_to_check = os.path.relpath(real_from_dir, path)
|
||||
while path_to_check:
|
||||
if path_to_check in all_directories:
|
||||
return path
|
||||
path_to_check = os.path.dirname(path_to_check)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def GetPrimarySolutionPath():
|
||||
"""Returns the full path to the primary solution. (gclient_root + src)"""
|
||||
|
||||
gclient_root = FindGclientRoot(os.getcwd())
|
||||
if not gclient_root:
|
||||
# Some projects might not use .gclient. Try to see whether we're in a git
|
||||
# checkout.
|
||||
top_dir = [os.getcwd()]
|
||||
def filter_fn(line):
|
||||
repo_root_path = os.path.normpath(line.rstrip('\n'))
|
||||
if os.path.exists(repo_root_path):
|
||||
top_dir[0] = repo_root_path
|
||||
try:
|
||||
gclient_utils.CheckCallAndFilter(["git", "rev-parse", "--show-toplevel"],
|
||||
print_stdout=False,
|
||||
filter_fn=filter_fn)
|
||||
except Exception:
|
||||
pass
|
||||
top_dir = top_dir[0]
|
||||
if os.path.exists(os.path.join(top_dir, 'buildtools')):
|
||||
return top_dir
|
||||
return None
|
||||
if gclient_root:
|
||||
# Some projects' top directory is not named 'src'.
|
||||
source_dir_name = GetGClientPrimarySolutionName(gclient_root) or 'src'
|
||||
return os.path.join(gclient_root, source_dir_name)
|
||||
|
||||
# Some projects' top directory is not named 'src'.
|
||||
source_dir_name = GetGClientPrimarySolutionName(gclient_root) or 'src'
|
||||
return os.path.join(gclient_root, source_dir_name)
|
||||
# Some projects might not use .gclient. Try to see whether we're in a git
|
||||
# checkout that contains a 'buildtools' subdir.
|
||||
top_dir = os.getcwd()
|
||||
try:
|
||||
top_dir = subprocess2.check_output(
|
||||
['git', 'rev-parse', '--show-toplevel'])
|
||||
if sys.version_info.major == 3:
|
||||
top_dir = top_dir.decode('utf-8', 'replace')
|
||||
top_dir = os.path.normpath(top_dir.strip())
|
||||
except subprocess2.CalledProcessError:
|
||||
pass
|
||||
|
||||
if os.path.exists(os.path.join(top_dir, 'buildtools')):
|
||||
return top_dir
|
||||
return None
|
||||
|
||||
|
||||
def GetBuildtoolsPath():
|
||||
@@ -100,12 +102,18 @@ def GetBuildtoolsPath():
|
||||
primary_solution = GetPrimarySolutionPath()
|
||||
if not primary_solution:
|
||||
return None
|
||||
|
||||
buildtools_path = os.path.join(primary_solution, 'buildtools')
|
||||
if not os.path.exists(buildtools_path):
|
||||
# Buildtools may be in the gclient root.
|
||||
gclient_root = FindGclientRoot(os.getcwd())
|
||||
buildtools_path = os.path.join(gclient_root, 'buildtools')
|
||||
return buildtools_path
|
||||
if os.path.exists(buildtools_path):
|
||||
return buildtools_path
|
||||
|
||||
# buildtools may be in the gclient root.
|
||||
gclient_root = FindGclientRoot(os.getcwd())
|
||||
buildtools_path = os.path.join(gclient_root, 'buildtools')
|
||||
if os.path.exists(buildtools_path):
|
||||
return buildtools_path
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def GetBuildtoolsPlatformBinaryPath():
|
||||
@@ -121,7 +129,7 @@ def GetBuildtoolsPlatformBinaryPath():
|
||||
elif sys.platform.startswith('linux'):
|
||||
subdir = 'linux64'
|
||||
else:
|
||||
raise Error('Unknown platform: ' + sys.platform)
|
||||
raise gclient_utils.Error('Unknown platform: ' + sys.platform)
|
||||
return os.path.join(buildtools_path, subdir)
|
||||
|
||||
|
||||
@@ -135,9 +143,9 @@ def GetExeSuffix():
|
||||
def GetGClientPrimarySolutionName(gclient_root_dir_path):
|
||||
"""Returns the name of the primary solution in the .gclient file specified."""
|
||||
gclient_config_file = os.path.join(gclient_root_dir_path, '.gclient')
|
||||
gclient_config_contents = gclient_utils.FileRead(gclient_config_file)
|
||||
env = {}
|
||||
exec(compile(open(gclient_config_file).read(), gclient_config_file, 'exec'),
|
||||
env)
|
||||
exec(gclient_config_contents, env)
|
||||
solutions = env.get('solutions', [])
|
||||
if solutions:
|
||||
return solutions[0].get('name')
|
||||
|
||||
Reference in New Issue
Block a user