Blame googletest/scripts/common.py

Packit bd1cd8
# Copyright 2013 Google Inc. All Rights Reserved.
Packit bd1cd8
#
Packit bd1cd8
# Redistribution and use in source and binary forms, with or without
Packit bd1cd8
# modification, are permitted provided that the following conditions are
Packit bd1cd8
# met:
Packit bd1cd8
#
Packit bd1cd8
#     * Redistributions of source code must retain the above copyright
Packit bd1cd8
# notice, this list of conditions and the following disclaimer.
Packit bd1cd8
#     * Redistributions in binary form must reproduce the above
Packit bd1cd8
# copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
# in the documentation and/or other materials provided with the
Packit bd1cd8
# distribution.
Packit bd1cd8
#     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
# contributors may be used to endorse or promote products derived from
Packit bd1cd8
# this software without specific prior written permission.
Packit bd1cd8
#
Packit bd1cd8
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
Packit bd1cd8
"""Shared utilities for writing scripts for Google Test/Mock."""
Packit bd1cd8
Packit bd1cd8
__author__ = 'wan@google.com (Zhanyong Wan)'
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
import os
Packit bd1cd8
import re
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
# Matches the line from 'svn info .' output that describes what SVN
Packit bd1cd8
# path the current local directory corresponds to.  For example, in
Packit bd1cd8
# a googletest SVN workspace's trunk/test directory, the output will be:
Packit bd1cd8
#
Packit bd1cd8
# URL: https://googletest.googlecode.com/svn/trunk/test
Packit bd1cd8
_SVN_INFO_URL_RE = re.compile(r'^URL: https://(\w+)\.googlecode\.com/svn(.*)')
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def GetCommandOutput(command):
Packit bd1cd8
  """Runs the shell command and returns its stdout as a list of lines."""
Packit bd1cd8
Packit bd1cd8
  f = os.popen(command, 'r')
Packit bd1cd8
  lines = [line.strip() for line in f.readlines()]
Packit bd1cd8
  f.close()
Packit bd1cd8
  return lines
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def GetSvnInfo():
Packit bd1cd8
  """Returns the project name and the current SVN workspace's root path."""
Packit bd1cd8
Packit bd1cd8
  for line in GetCommandOutput('svn info .'):
Packit bd1cd8
    m = _SVN_INFO_URL_RE.match(line)
Packit bd1cd8
    if m:
Packit bd1cd8
      project = m.group(1)  # googletest or googlemock
Packit bd1cd8
      rel_path = m.group(2)
Packit bd1cd8
      root = os.path.realpath(rel_path.count('/') * '../')
Packit bd1cd8
      return project, root
Packit bd1cd8
Packit bd1cd8
  return None, None
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def GetSvnTrunk():
Packit bd1cd8
  """Returns the current SVN workspace's trunk root path."""
Packit bd1cd8
Packit bd1cd8
  _, root = GetSvnInfo()
Packit bd1cd8
  return root + '/trunk' if root else None
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def IsInGTestSvn():
Packit bd1cd8
  project, _ = GetSvnInfo()
Packit bd1cd8
  return project == 'googletest'
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def IsInGMockSvn():
Packit bd1cd8
  project, _ = GetSvnInfo()
Packit bd1cd8
  return project == 'googlemock'