1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # Copyright 2010 Google Inc. All Rights Reserved. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 """Wrapper module for running gslib.__main__.main() from the command line.""" 18 19 import os 20 import sys 21 import warnings 22 23 # TODO: gsutil-beta: Distribute a pylint rc file. 24 25 if not (2, 6) <= sys.version_info[:3] < (3,): 26 sys.exit('gsutil requires python 2.6 or 2.7.') 27 28 29 def UsingCrcmodExtension(crcmod_module): 30 return (getattr(crcmod_module, 'crcmod', None) and 31 getattr(crcmod_module.crcmod, '_usingExtension', None)) 32 33 34 def OutputAndExit(message): 35 sys.stderr.write('%s\n' % message) 36 sys.exit(1) 37 38 39 GSUTIL_DIR = os.path.dirname(os.path.abspath(os.path.realpath(__file__))) 40 if not GSUTIL_DIR: 41 OutputAndExit('Unable to determine where gsutil is installed. Sorry, ' 42 'cannot run correctly without this.\n') 43 44 # The wrapper script adds all third_party libraries to the Python path, since 45 # we don't assume any third party libraries are installed system-wide. 46 THIRD_PARTY_DIR = os.path.join(GSUTIL_DIR, 'third_party') 47 48 49 # Filter out "module was already imported" warnings that get printed after we 50 # add our bundled version of modules to the Python path. 51 warnings.filterwarnings('ignore', category=UserWarning, 52 message=r'.* httplib2 was already imported from') 53 warnings.filterwarnings('ignore', category=UserWarning, 54 message=r'.* oauth2client was already imported from') 55 56 57 # List of third-party libraries. The first element of the tuple is the name of 58 # the directory under third_party and the second element is the subdirectory 59 # that needs to be added to sys.path. 60 THIRD_PARTY_LIBS = [ 61 ('oauth2client', ''), # oauth2client and dependencies must be before boto. 62 ('pyasn1', ''), # oauth2client dependency 63 ('pyasn1-modules', ''), # oauth2client dependency 64 ('rsa', ''), # oauth2client dependency 65 ('apitools', ''), 66 ('boto', ''), 67 ('gcs-oauth2-boto-plugin', ''), 68 ('httplib2', 'python2'), 69 ('protorpc', ''), 70 ('python-gflags', ''), 71 ('retry-decorator', ''), 72 ('six', ''), 73 ('socksipy-branch', ''), 74 ] 75 for libdir, subdir in THIRD_PARTY_LIBS: 76 if not os.path.isdir(os.path.join(THIRD_PARTY_DIR, libdir)): 77 OutputAndExit( 78 'There is no %s library under the gsutil third-party directory (%s).\n' 79 'The gsutil command cannot work properly when installed this way.\n' 80 'Please re-install gsutil per the installation instructions.' % ( 81 libdir, THIRD_PARTY_DIR)) 82 sys.path.insert(0, os.path.join(THIRD_PARTY_DIR, libdir, subdir)) 83 84 # The wrapper script adds all third_party libraries to the Python path, since 85 # we don't assume any third party libraries are installed system-wide. 86 THIRD_PARTY_DIR = os.path.join(GSUTIL_DIR, 'third_party') 87 88 CRCMOD_PATH = os.path.join(THIRD_PARTY_DIR, 'crcmod', 'python2') 89 CRCMOD_OSX_PATH = os.path.join(THIRD_PARTY_DIR, 'crcmod_osx') 90 91 try: 92 # pylint: disable=g-import-not-at-top 93 import crcmod 94 except ImportError: 95 crcmod = None 96 97 if not UsingCrcmodExtension(crcmod): 98 local_crcmod_path = (CRCMOD_OSX_PATH 99 if 'darwin' in str(sys.platform).lower() 100 else CRCMOD_PATH) 101 sys.path.insert(0, local_crcmod_path) 102 103 104 def RunMain(): 105 # pylint: disable=g-import-not-at-top 106 import gslib.__main__ 107 sys.exit(gslib.__main__.main()) 108 109 if __name__ == '__main__': 110 RunMain() 111