1# Copyright 2013 Google Inc. All Rights Reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13#     * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29"""Shared utilities for writing scripts for Google Test/Mock."""
30
31__author__ = 'wan@google.com (Zhanyong Wan)'
32
33
34import os
35import re
36
37
38# Matches the line from 'svn info .' output that describes what SVN
39# path the current local directory corresponds to.  For example, in
40# a googletest SVN workspace's trunk/test directory, the output will be:
41#
42# URL: https://googletest.googlecode.com/svn/trunk/test
43_SVN_INFO_URL_RE = re.compile(r'^URL: https://(\w+)\.googlecode\.com/svn(.*)')
44
45
46def GetCommandOutput(command):
47  """Runs the shell command and returns its stdout as a list of lines."""
48
49  f = os.popen(command, 'r')
50  lines = [line.strip() for line in f.readlines()]
51  f.close()
52  return lines
53
54
55def GetSvnInfo():
56  """Returns the project name and the current SVN workspace's root path."""
57
58  for line in GetCommandOutput('svn info .'):
59    m = _SVN_INFO_URL_RE.match(line)
60    if m:
61      project = m.group(1)  # googletest or googlemock
62      rel_path = m.group(2)
63      root = os.path.realpath(rel_path.count('/') * '../')
64      return project, root
65
66  return None, None
67
68
69def GetSvnTrunk():
70  """Returns the current SVN workspace's trunk root path."""
71
72  _, root = GetSvnInfo()
73  return root + '/trunk' if root else None
74
75
76def IsInGTestSvn():
77  project, _ = GetSvnInfo()
78  return project == 'googletest'
79
80
81def IsInGMockSvn():
82  project, _ = GetSvnInfo()
83  return project == 'googlemock'
84