199a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# Copyright 2013 Google Inc. All Rights Reserved.
299a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com#
399a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# Redistribution and use in source and binary forms, with or without
499a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# modification, are permitted provided that the following conditions are
599a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# met:
699a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com#
799a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com#     * Redistributions of source code must retain the above copyright
899a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# notice, this list of conditions and the following disclaimer.
999a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com#     * Redistributions in binary form must reproduce the above
1099a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# copyright notice, this list of conditions and the following disclaimer
1199a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# in the documentation and/or other materials provided with the
1299a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# distribution.
1399a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com#     * Neither the name of Google Inc. nor the names of its
1499a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# contributors may be used to endorse or promote products derived from
1599a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# this software without specific prior written permission.
1699a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com#
1799a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1899a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1999a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2099a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2199a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2299a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2399a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2499a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2599a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2699a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2799a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2899a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
2999a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com"""Shared utilities for writing scripts for Google Test/Mock."""
3099a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
3199a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com__author__ = 'wan@google.com (Zhanyong Wan)'
3299a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
3399a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
3499a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.comimport os
3599a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.comimport re
3699a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
3799a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
3899a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# Matches the line from 'svn info .' output that describes what SVN
3999a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# path the current local directory corresponds to.  For example, in
4099a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# a googletest SVN workspace's trunk/test directory, the output will be:
4199a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com#
4299a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com# URL: https://googletest.googlecode.com/svn/trunk/test
4399a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com_SVN_INFO_URL_RE = re.compile(r'^URL: https://(\w+)\.googlecode\.com/svn(.*)')
4499a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
4599a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
4699a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.comdef GetCommandOutput(command):
4799a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  """Runs the shell command and returns its stdout as a list of lines."""
4899a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
4999a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  f = os.popen(command, 'r')
5099a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  lines = [line.strip() for line in f.readlines()]
5199a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  f.close()
5299a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  return lines
5399a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
5499a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
5599a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.comdef GetSvnInfo():
5699a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  """Returns the project name and the current SVN workspace's root path."""
5799a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
5899a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  for line in GetCommandOutput('svn info .'):
5999a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com    m = _SVN_INFO_URL_RE.match(line)
6099a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com    if m:
6199a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com      project = m.group(1)  # googletest or googlemock
6299a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com      rel_path = m.group(2)
6399a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com      root = os.path.realpath(rel_path.count('/') * '../')
6499a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com      return project, root
6599a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
6699a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  return None, None
6799a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
6899a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
6999a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.comdef GetSvnTrunk():
7099a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  """Returns the current SVN workspace's trunk root path."""
7199a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
7299a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  _, root = GetSvnInfo()
7399a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  return root + '/trunk' if root else None
7499a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
7599a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
7699a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.comdef IsInGTestSvn():
7799a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  project, _ = GetSvnInfo()
7899a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  return project == 'googletest'
7999a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
8099a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com
8199a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.comdef IsInGMockSvn():
8299a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  project, _ = GetSvnInfo()
8399a24f0f1e94e3d08809261e1617f3ad275dc69fkosak@google.com  return project == 'googlemock'
84