Skip to content

Commit

Permalink
Update import_util.py to work from chrome/chrome_cleaner/internal
Browse files Browse the repository at this point in the history
This prepares for a pending commit to the internal chrome_cleaner repo
which will delete the internal copy of import_util.py:

* Add separate GetChromiumRootDirectory and GetInternalRootDirectory
functions to clarify the ambiguous term "root directory" when called
from the internal repo mounted at //chrome/chrome_cleaner/internal.

* Move all the functions that depend on being called from inside the
chromium source tree to a separate import_paths.py script. (All the
current callers of these functions happen to be in
//chrome/chrome_cleaner/internal.)

* Delete code to find a local copy of find_depot_tools. It's no longer
supported to run this script without calling gclient sync, which will
update depot_tools.

R=proberge

Bug: 1004848
Change-Id: I829851f332975c950f1dcde59735ee96f34f295e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2451810
Reviewed-by: proberge <proberge@chromium.org>
Commit-Queue: proberge <proberge@chromium.org>
Auto-Submit: Joe Mason <joenotcharles@chromium.org>
Cr-Commit-Position: refs/heads/master@{#814390}
  • Loading branch information
JoeNotCharlesGoogle authored and Commit Bot committed Oct 6, 2020
1 parent 415c28c commit 1673a55
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 45 deletions.
53 changes: 53 additions & 0 deletions chrome/chrome_cleaner/tools/import_paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2020 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.
"""Paths for python proto modules and depot_tools.
This script must be used from inside a chromium checkout because it depends on
paths relative to the root of the checkout.
"""

import import_util
import os
import sys


# Expect this script to be located in <root_dir>/chrome/chrome_cleaner/tools.
_CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
_ROOT_DIRECTORY = os.path.normpath(os.path.join(_CURRENT_DIRECTORY, os.pardir,
os.pardir, os.pardir))
assert(_CURRENT_DIRECTORY == os.path.join(_ROOT_DIRECTORY, 'chrome',
'chrome_cleaner', 'tools'))


def GetBuildDirectory(build_configuration):
"""Returns the path to the build directory relative to this file.
Args:
build_configuration: name of the build configuration whose directory
should be looked up, e.g. 'Debug' or 'Release'.
"""
return os.path.join(_ROOT_DIRECTORY, 'out', build_configuration)


def GetChromiumRootDirectory():
"""Returns the path to root directory of the chromium checkout."""
return _ROOT_DIRECTORY


def GetInternalRootDirectory():
"""Returns the path to //chrome/chrome_cleaner/internal."""
return os.path.join(_ROOT_DIRECTORY, 'chrome', 'chrome_cleaner', 'internal')


def AddDepotToolsToPath():
"""Locates a depot_tools checkout and adds it to the Python import path.
Returns:
The path to depot_tools.
"""

# Try to import find_depot_tools from the build subdir.
import_util.AddImportPath(os.path.join(_ROOT_DIRECTORY, 'build'))
import find_depot_tools
return find_depot_tools.add_depot_tools_to_path()
46 changes: 1 addition & 45 deletions chrome/chrome_cleaner/tools/import_util.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
# Copyright 2018 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.
"""Utilities for importing python proto modules."""
"""Utilities for importing python scripts and proto modules."""

import os
import sys


_CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
_ROOT_DIRECTORY = os.path.join(_CURRENT_DIRECTORY, os.pardir, os.pardir)


def _InSourceTree():
return os.path.basename(os.path.abspath(_ROOT_DIRECTORY)) == 'src'


def AddImportPath(path):
"""Adds the given path to the front of the Python import path."""
sys.path.insert(0, os.path.normpath(path))


def GetBuildDirectory(build_configuration):
"""Returns the path to the build directory relative to this file.
Args:
build_configuration: name of the build configuration whose directory
should be looked up, e.g. 'Debug' or 'Release'.
Returns:
Path to the build directory or None if used from outside the source tree.
"""
if not _InSourceTree():
return None
return os.path.join(_ROOT_DIRECTORY, 'out', build_configuration)


def AddProtosToPath(root_path):
"""Adds generated protobuf modules to the Python import path.
Expand All @@ -55,24 +32,3 @@ def AddProtosToPath(root_path):
AddImportPath(os.path.join(pyproto_dir, 'chrome', 'chrome_cleaner', 'proto'))
AddImportPath(os.path.join(pyproto_dir, 'chrome', 'chrome_cleaner', 'logging',
'proto'))


def AddDepotToolsToPath():
"""Locates a depot_tools checkout and adds it to the Python import path.
Returns:
The path to depot_tools.
"""

# Try to import the local copy of find_depot_tools from the import subdir, in
# case this is being run from a checkout that hasn't been synced.
AddImportPath(os.path.join(_CURRENT_DIRECTORY, 'import'))

# But prefer to use the copy synced into the build dir which might be more
# up-to-date. (This path will take priority over the path added above.)
if _InSourceTree():
AddImportPath(os.path.join(_ROOT_DIRECTORY, 'build'))

# Import the first copy of find_depot_tools found in the above paths.
import find_depot_tools
return find_depot_tools.add_depot_tools_to_path()

0 comments on commit 1673a55

Please sign in to comment.