common.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. """Shared utilities for writing scripts for Google Test/Mock."""
  29. __author__ = 'wan@google.com (Zhanyong Wan)'
  30. import os
  31. import re
  32. # Matches the line from 'svn info .' output that describes what SVN
  33. # path the current local directory corresponds to. For example, in
  34. # a googletest SVN workspace's trunk/test directory, the output will be:
  35. #
  36. # URL: https://googletest.googlecode.com/svn/trunk/test
  37. _SVN_INFO_URL_RE = re.compile(r'^URL: https://(\w+)\.googlecode\.com/svn(.*)')
  38. def GetCommandOutput(command):
  39. """Runs the shell command and returns its stdout as a list of lines."""
  40. f = os.popen(command, 'r')
  41. lines = [line.strip() for line in f.readlines()]
  42. f.close()
  43. return lines
  44. def GetSvnInfo():
  45. """Returns the project name and the current SVN workspace's root path."""
  46. for line in GetCommandOutput('svn info .'):
  47. m = _SVN_INFO_URL_RE.match(line)
  48. if m:
  49. project = m.group(1) # googletest or googlemock
  50. rel_path = m.group(2)
  51. root = os.path.realpath(rel_path.count('/') * '../')
  52. return project, root
  53. return None, None
  54. def GetSvnTrunk():
  55. """Returns the current SVN workspace's trunk root path."""
  56. _, root = GetSvnInfo()
  57. return root + '/trunk' if root else None
  58. def IsInGTestSvn():
  59. project, _ = GetSvnInfo()
  60. return project == 'googletest'
  61. def IsInGMockSvn():
  62. project, _ = GetSvnInfo()
  63. return project == 'googlemock'