blob: e13cbcb4fe47375f89757098346a023375cf55f8 [file] [log] [blame]
# -*- coding: utf-8 -*-
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from __future__ import print_function
import os
import sys
import pkg_resources
# Add the third_party/ dir to our search path so that we can find the
# modules in there automatically. This isn't normal, so don't replicate
# this pattern elsewhere.
_chromite_dir = os.path.normpath(os.path.dirname(os.path.realpath(__file__)))
_containing_dir = os.path.dirname(_chromite_dir)
_third_party_dirs = [os.path.join(_chromite_dir, 'third_party')]
# If chromite is living inside the Chrome checkout under
# <chrome_root>/src/third_party/chromite, its dependencies will be checked out
# to <chrome_root>/src/third_party instead of the normal chromite/third_party
# location due to git-submodule limitations (a submodule cannot be contained
# inside another submodule's workspace), so we want to add that to the
# search path.
if os.path.basename(_containing_dir) == 'third_party':
# List of third_party directories that might need subpaths added to search.
_extra_import_paths = [
# Allow Python 2 or 3 specific modules under a separate subpath.
'python%s' % sys.version_info.major,
for _path in _extra_import_paths:
for _third_party_dir in _third_party_dirs[:]:
_component = os.path.join(_third_party_dir, _path)
if os.path.isdir(_component):
_insert_at = 0
if hasattr(sys, 'real_prefix'):
# sys.real_prefix should only exist in virtualenv (
# questions/1871549/python-determine-if-running-inside-virtualenv#1883251)
# When it's in virtualenv, make sure the packages installed by the virtualenv
# have precedence over the third_party libraries.
virtual_env_path = sys.prefix
for _i, _path in reversed(list(enumerate(sys.path))):
if virtual_env_path in _path:
_insert_at = _i + 1
sys.path[_insert_at:_insert_at] = _third_party_dirs
# Fix the .__path__ attributes of these submodules to correspond with sys.path.
# This prevents globally installed packages from shadowing the third_party
# packages. See and
# for more context.
# TODO(phobbs) this won't be necessary when we use venv everywhere.
_pkg_resources = __import__('pkg_resources')
for _package in ['google', 'google.protobuf']:
for _path in _third_party_dirs:
# Make sure we're only using the local google.protobuf.
# pylint: disable=wrong-import-position
import google.protobuf
google.protobuf.__path__ = [_p for _p in google.protobuf.__path__
if any(_dir in _p for _dir in _third_party_dirs)]