示例#1
0
def CreateTrackerDirIfNeeded():
    """Looks up or creates the gsutil tracker file directory.

  This is the configured directory where gsutil keeps its resumable transfer
  tracker files. This function creates it if it doesn't already exist.

  Returns:
    The pathname to the tracker directory.
  """
    tracker_dir = config.get(
        'GSUtil', 'resumable_tracker_dir',
        os.path.join(GetGsutilStateDir(), 'tracker-files'))
    CreateDirIfNeeded(tracker_dir)
    return tracker_dir
示例#2
0
def GetGsutilStateDir():
    """Returns the location of the directory for gsutil state files.

  Certain operations, such as cross-process credential sharing and
  resumable transfer tracking, need a known location for state files which
  are created by gsutil as-needed.

  This location should only be used for storing data that is required to be in
  a static location.

  Returns:
    Path to directory for gsutil static state files.
  """
    config_file_dir = config.get('GSUtil', 'state_dir',
                                 DEFAULT_GSUTIL_STATE_DIR)
    CreateDirIfNeeded(config_file_dir)
    return config_file_dir
def ReportMetrics(metrics_file_path, log_level, log_file_path=None):
    """Sends the specified anonymous usage event to the given analytics endpoint.

  Args:
      metrics_file_path: str, File with pickled metrics (list of tuples).
      log_level: int, The logging level of gsutil's root logger.
      log_file_path: str, The file that this module should write its logs to.
        This parameter is intended for use by tests that need to evaluate the
        contents of the file at this path.

  """
    logger = logging.getLogger()
    if log_file_path is not None:
        # Use a separate logger so that we don't add another handler to the default
        # module-level logger. This is intended to prevent multiple calls from tests
        # running in parallel from writing output to the same file.
        new_name = '%s.%s' % (logger.name, ''.join(
            random.choice(string.ascii_lowercase) for _ in range(8)))
        logger = logging.getLogger(new_name)

    log_file_path = log_file_path or LOG_FILE_PATH
    log_file_parent_dir = os.path.dirname(log_file_path)
    CreateDirIfNeeded(log_file_parent_dir)
    handler = logging.FileHandler(log_file_path, mode='w')
    logger.addHandler(handler)
    logger.setLevel(log_level)

    with open(metrics_file_path, 'rb') as metrics_file:
        metrics = pickle.load(metrics_file)
    os.remove(metrics_file_path)

    ConfigureCertsFile()
    http = GetNewHttp()

    for metric in metrics:
        try:
            headers = {'User-Agent': metric.user_agent}
            response = http.request(metric.endpoint,
                                    method=metric.method,
                                    body=metric.body,
                                    headers=headers)
            logger.debug(metric)
            logger.debug('RESPONSE: %s', response[0]['status'])
        except Exception as e:  # pylint: disable=broad-except
            logger.debug(e)
示例#4
0
def CheckAndMaybePromptForAnalyticsEnabling():
  """Asks a user to opt-in to data collection if a UUID file does not exist.

  If the user agrees, generates a UUID file. Will not prompt if part of SDK.
  """
  disable_prompt = boto.config.get_value('GSUtil', 'disable_analytics_prompt')
  if not os.path.exists(
      _UUID_FILE_PATH) and not disable_prompt and not os.environ.get(
          'CLOUDSDK_WRAPPER'):
    enable_analytics = raw_input('\n' + textwrap.fill(
        'gsutil developers rely on user feedback to make improvements to the '
        'tool. Would you like to send anonymous usage statistics to help '
        'improve gsutil? [y/N]') + ' ')

    text_to_write = _DISABLED_TEXT
    if enable_analytics.lower()[0] == 'y':
      text_to_write = uuid.uuid4().hex
    CreateDirIfNeeded(os.path.dirname(_UUID_FILE_PATH))
    with open(_UUID_FILE_PATH, 'w') as f:
      f.write(text_to_write)
示例#5
0
def GetTabCompletionCacheFilename():
    tab_completion_dir = os.path.join(GetGsutilStateDir(), 'tab-completion')
    # Limit read permissions on the directory to owner for privacy.
    CreateDirIfNeeded(tab_completion_dir, mode=0700)
    return os.path.join(tab_completion_dir, 'cache')