1 # -*- coding: utf-8 -*- 2 # Copyright 2010 Google Inc. All Rights Reserved. 3 # 4 # Permission is hereby granted, free of charge, to any person obtaining a 5 # copy of this software and associated documentation files (the 6 # "Software"), to deal in the Software without restriction, including 7 # without limitation the rights to use, copy, modify, merge, publish, dis- 8 # tribute, sublicense, and/or sell copies of the Software, and to permit 9 # persons to whom the Software is furnished to do so, subject to the fol- 10 # lowing conditions: 11 # 12 # The above copyright notice and this permission notice shall be included 13 # in all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 # IN THE SOFTWARE. 22 """Package marker file.""" 23 24 from __future__ import absolute_import 25 26 import os 27 import pkgutil 28 import sys 29 import tempfile 30 31 import gslib.exception 32 33 coverage_outfile = os.getenv('GSUTIL_COVERAGE_OUTPUT_FILE', None) 34 if coverage_outfile: 35 try: 36 import coverage # pylint: disable=g-import-not-at-top 37 coverage_controller = coverage.coverage( 38 data_file=coverage_outfile, data_suffix=True, auto_data=True, 39 source=['gslib'], omit=['gslib/third_party/*', 'gslib/tests/*', 40 tempfile.gettempdir() + '*']) 41 coverage_controller.start() 42 except ImportError: 43 pass 44 45 46 # Directory containing the gslib module. 47 GSLIB_DIR = os.path.dirname(os.path.realpath(__file__)) 48 # Path to gsutil executable. This assumes gsutil is the running script. 49 GSUTIL_PATH = os.path.realpath(sys.argv[0]) 50 # The directory that contains the gsutil executable. 51 GSUTIL_DIR = os.path.dirname(GSUTIL_PATH) 52 53 # Whether or not this was installed via a package manager like pip, deb, rpm, 54 # etc. If installed by just extracting a tarball or zip file, this will be 55 # False. 56 IS_PACKAGE_INSTALL = True 57 58 # Whether or not this was installed via setup.py develop mode. This creates a 59 # symlink directly to the source directory. 60 IS_EDITABLE_INSTALL = False 61 62 # Directory where program files like VERSION and CHECKSUM will be. When 63 # installed via tarball, this is the gsutil directory, but the files are moved 64 # to the gslib directory when installed via setup.py. 65 PROGRAM_FILES_DIR = GSLIB_DIR 66 67 # The gslib directory will be underneath the gsutil directory when installed 68 # from a tarball, but somewhere else on the machine if installed via setup.py. 69 if (not os.path.isfile(os.path.join(PROGRAM_FILES_DIR, 'VERSION')) and 70 os.path.commonprefix((GSUTIL_DIR, GSLIB_DIR)) == GSUTIL_DIR): 71 IS_PACKAGE_INSTALL = False 72 PROGRAM_FILES_DIR = GSUTIL_DIR 73 74 # If the module was installed from source using editable mode 75 # (i.e. pip install -e) then the files might be one directory up. 76 if not os.path.isfile(os.path.join(PROGRAM_FILES_DIR, 'VERSION')): 77 PROGRAM_FILES_DIR = os.path.normpath(os.path.join(GSLIB_DIR, '..')) 78 IS_EDITABLE_INSTALL = True 79 80 # If installed via editable mode, we have to add the mock_storage_service 81 # module to the Python path, since the gsutil script path munging is not 82 # executed in this mode. 83 if IS_EDITABLE_INSTALL: 84 mock_storage_location = os.path.join( 85 PROGRAM_FILES_DIR, 'third_party', 'boto', 'tests', 'integration', 's3') 86 sys.path.append(mock_storage_location) 87 88 89 def _GetFileContents(filename): 90 """Tries to find the given filename on disk or via pkgutil.get_data. 91 92 Args: 93 filename: String name of the file. 94 95 Returns: 96 A tuple containing the absolute path to the requested file and the file's 97 contents. If the file is not actually on disk, the file path will be None. 98 """ 99 fpath = os.path.join(PROGRAM_FILES_DIR, filename) 100 if os.path.isfile(fpath): 101 with open(fpath, 'r') as f: 102 content = f.read() 103 else: 104 content = pkgutil.get_data('gslib', filename) 105 fpath = None 106 return (fpath, content.strip()) 107 108 # Get the version file and store it. 109 VERSION_FILE, VERSION = _GetFileContents('VERSION') 110 if not VERSION: 111 raise gslib.exception.CommandException( 112 'VERSION file not found. Please reinstall gsutil from scratch') 113 __version__ = VERSION 114 115 # Get the checksum file and store it. 116 CHECKSUM_FILE, CHECKSUM = _GetFileContents('CHECKSUM') 117 if not CHECKSUM: 118 raise gslib.exception.CommandException( 119 'CHECKSUM file not found. Please reinstall gsutil from scratch') 120