00001 # Copyright 2013 Google Inc. All Rights Reserved. 00002 # 00003 # Redistribution and use in source and binary forms, with or without 00004 # modification, are permitted provided that the following conditions are 00005 # met: 00006 # 00007 # * Redistributions of source code must retain the above copyright 00008 # notice, this list of conditions and the following disclaimer. 00009 # * Redistributions in binary form must reproduce the above 00010 # copyright notice, this list of conditions and the following disclaimer 00011 # in the documentation and/or other materials provided with the 00012 # distribution. 00013 # * Neither the name of Google Inc. nor the names of its 00014 # contributors may be used to endorse or promote products derived from 00015 # this software without specific prior written permission. 00016 # 00017 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00018 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00019 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00020 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00021 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00022 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00023 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00024 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00025 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00026 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00027 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00028 00029 """Shared utilities for writing scripts for Google Test/Mock.""" 00030 00031 __author__ = 'wan@google.com (Zhanyong Wan)' 00032 00033 00034 import os 00035 import re 00036 00037 00038 # Matches the line from 'svn info .' output that describes what SVN 00039 # path the current local directory corresponds to. For example, in 00040 # a googletest SVN workspace's trunk/test directory, the output will be: 00041 # 00042 # URL: https://googletest.googlecode.com/svn/trunk/test 00043 _SVN_INFO_URL_RE = re.compile(r'^URL: https://(\w+)\.googlecode\.com/svn(.*)') 00044 00045 00046 def GetCommandOutput(command): 00047 """Runs the shell command and returns its stdout as a list of lines.""" 00048 00049 f = os.popen(command, 'r') 00050 lines = [line.strip() for line in f.readlines()] 00051 f.close() 00052 return lines 00053 00054 00055 def GetSvnInfo(): 00056 """Returns the project name and the current SVN workspace's root path.""" 00057 00058 for line in GetCommandOutput('svn info .'): 00059 m = _SVN_INFO_URL_RE.match(line) 00060 if m: 00061 project = m.group(1) # googletest or googlemock 00062 rel_path = m.group(2) 00063 root = os.path.realpath(rel_path.count('/') * '../') 00064 return project, root 00065 00066 return None, None 00067 00068 00069 def GetSvnTrunk(): 00070 """Returns the current SVN workspace's trunk root path.""" 00071 00072 _, root = GetSvnInfo() 00073 return root + '/trunk' if root else None 00074 00075 00076 def IsInGTestSvn(): 00077 project, _ = GetSvnInfo() 00078 return project == 'googletest' 00079 00080 00081 def IsInGMockSvn(): 00082 project, _ = GetSvnInfo() 00083 return project == 'googlemock'