TestMac.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (c) 2014 Google Inc. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """
  5. TestMac.py: a collection of helper function shared between test on Mac OS X.
  6. """
  7. import re
  8. import subprocess
  9. __all__ = ['Xcode', 'CheckFileType']
  10. def CheckFileType(test, file, archs):
  11. """Check that |file| contains exactly |archs| or fails |test|."""
  12. proc = subprocess.Popen(['lipo', '-info', file], stdout=subprocess.PIPE)
  13. o = proc.communicate()[0].strip()
  14. assert not proc.returncode
  15. if len(archs) == 1:
  16. pattern = re.compile('^Non-fat file: (.*) is architecture: (.*)$')
  17. else:
  18. pattern = re.compile('^Architectures in the fat file: (.*) are: (.*)$')
  19. match = pattern.match(o)
  20. if match is None:
  21. print 'Ouput does not match expected pattern: %s' % (pattern.pattern)
  22. test.fail_test()
  23. else:
  24. found_file, found_archs = match.groups()
  25. if found_file != file or set(found_archs.split()) != set(archs):
  26. print 'Expected file %s with arch %s, got %s with arch %s' % (
  27. file, ' '.join(archs), found_file, found_archs)
  28. test.fail_test()
  29. class XcodeInfo(object):
  30. """Simplify access to Xcode informations."""
  31. def __init__(self):
  32. self._cache = {}
  33. def _XcodeVersion(self):
  34. lines = subprocess.check_output(['xcodebuild', '-version']).splitlines()
  35. version = ''.join(lines[0].split()[-1].split('.'))
  36. version = (version + '0' * (3 - len(version))).zfill(4)
  37. return version, lines[-1].split()[-1]
  38. def Version(self):
  39. if 'Version' not in self._cache:
  40. self._cache['Version'], self._cache['Build'] = self._XcodeVersion()
  41. return self._cache['Version']
  42. def Build(self):
  43. if 'Build' not in self._cache:
  44. self._cache['Version'], self._cache['Build'] = self._XcodeVersion()
  45. return self._cache['Build']
  46. def SDKBuild(self):
  47. if 'SDKBuild' not in self._cache:
  48. self._cache['SDKBuild'] = subprocess.check_output(
  49. ['xcodebuild', '-version', '-sdk', '', 'ProductBuildVersion'])
  50. self._cache['SDKBuild'] = self._cache['SDKBuild'].rstrip('\n')
  51. return self._cache['SDKBuild']
  52. def SDKVersion(self):
  53. if 'SDKVersion' not in self._cache:
  54. self._cache['SDKVersion'] = subprocess.check_output(
  55. ['xcodebuild', '-version', '-sdk', '', 'SDKVersion'])
  56. self._cache['SDKVersion'] = self._cache['SDKVersion'].rstrip('\n')
  57. return self._cache['SDKVersion']
  58. Xcode = XcodeInfo()