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
19import os
20import sys
21import warnings
22
23# TODO: gsutil-beta: Distribute a pylint rc file.
24
25if not (2, 6) <= sys.version_info[:3] < (3,):
26  sys.exit('gsutil requires python 2.6 or 2.7.')
27
28
29def UsingCrcmodExtension(crcmod_module):
30  return (getattr(crcmod_module, 'crcmod', None) and
31          getattr(crcmod_module.crcmod, '_usingExtension', None))
32
33
34def OutputAndExit(message):
35  sys.stderr.write('%s\n' % message)
36  sys.exit(1)
37
38
39GSUTIL_DIR = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
40if 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.
46THIRD_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.
51warnings.filterwarnings('ignore', category=UserWarning,
52                        message=r'.* httplib2 was already imported from')
53warnings.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.
60THIRD_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]
75for 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.
86THIRD_PARTY_DIR = os.path.join(GSUTIL_DIR, 'third_party')
87
88CRCMOD_PATH = os.path.join(THIRD_PARTY_DIR, 'crcmod', 'python2')
89CRCMOD_OSX_PATH = os.path.join(THIRD_PARTY_DIR, 'crcmod_osx')
90
91try:
92  # pylint: disable=g-import-not-at-top
93  import crcmod
94except ImportError:
95  crcmod = None
96
97if 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
104def RunMain():
105  # pylint: disable=g-import-not-at-top
106  import gslib.__main__
107  sys.exit(gslib.__main__.main())
108
109if __name__ == '__main__':
110  RunMain()
111