1# Copyright 2013 Google Inc. All Rights Reserved. 2 3"""Does some initial setup and checks for all the bootstrapping scripts.""" 4 5 6from __future__ import absolute_import 7from __future__ import unicode_literals 8 9import os 10import sys 11 12 13# We don't want to import any libraries at this point so we handle py2/3 14# manually. 15SITE_PACKAGES = 'CLOUDSDK_PYTHON_SITEPACKAGES' 16VIRTUAL_ENV = 'VIRTUAL_ENV' 17if sys.version_info[0] == 2: 18 SITE_PACKAGES = SITE_PACKAGES.encode('utf-8') 19 VIRTUAL_ENV = VIRTUAL_ENV.encode('utf-8') 20 21 22# If we're in a virtualenv, always import site packages. Also, upon request. 23# We can't import anything from googlecloudsdk here so we are just going to 24# assume no one has done anything as silly as to put any unicode in either of 25# these env vars. 26import_site_packages = (os.environ.get(SITE_PACKAGES) or 27 os.environ.get(VIRTUAL_ENV)) 28 29if import_site_packages: 30 # pylint:disable=unused-import 31 # pylint:disable=g-import-not-at-top 32 import site 33 34# Put Cloud SDK libs on the path 35root_dir = os.path.normpath(os.path.join( 36 os.path.dirname(os.path.realpath(__file__)), '..', '..')) 37lib_dir = os.path.join(root_dir, 'lib') 38third_party_dir = os.path.join(lib_dir, 'third_party') 39 40sys.path = [lib_dir, third_party_dir] + sys.path 41 42# When python is not invoked with the -S option, it can preload google module 43# via .pth file setting its __path__. After this happens, our vendored google 44# package may not in the __path__. After our vendored dependency directory is 45# put at the first place in the sys.path, google module should be reloaded, 46# so that our vendored copy can be preferred. 47if 'google' in sys.modules: 48 import google # pylint: disable=g-import-not-at-top 49 try: 50 reload(google) 51 except NameError: 52 import importlib # pylint: disable=g-import-not-at-top 53 importlib.reload(google) 54 55 56# pylint: disable=g-import-not-at-top 57from googlecloudsdk.core.util import platforms 58 59 60# Add more methods to this list for universal checks that need to be performed 61def DoAllRequiredChecks(): 62 if not platforms.PythonVersion().IsCompatible(): 63 sys.exit(1) 64 65 66DoAllRequiredChecks() 67