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"""
6TestMac.py:  a collection of helper function shared between test on Mac OS X.
7"""
8
9import re
10import subprocess
11
12__all__ = ['Xcode', 'CheckFileType']
13
14
15def CheckFileType(test, file, archs):
16  """Check that |file| contains exactly |archs| or fails |test|."""
17  proc = subprocess.Popen(['lipo', '-info', file], stdout=subprocess.PIPE)
18  o = proc.communicate()[0].strip()
19  assert not proc.returncode
20  if len(archs) == 1:
21    pattern = re.compile('^Non-fat file: (.*) is architecture: (.*)$')
22  else:
23    pattern = re.compile('^Architectures in the fat file: (.*) are: (.*)$')
24  match = pattern.match(o)
25  if match is None:
26    print 'Ouput does not match expected pattern: %s' % (pattern.pattern)
27    test.fail_test()
28  else:
29    found_file, found_archs = match.groups()
30    if found_file != file or set(found_archs.split()) != set(archs):
31      print 'Expected file %s with arch %s, got %s with arch %s' % (
32          file, ' '.join(archs), found_file, found_archs)
33      test.fail_test()
34
35
36class XcodeInfo(object):
37  """Simplify access to Xcode informations."""
38
39  def __init__(self):
40    self._cache = {}
41
42  def _XcodeVersion(self):
43    lines = subprocess.check_output(['xcodebuild', '-version']).splitlines()
44    version = ''.join(lines[0].split()[-1].split('.'))
45    version = (version + '0' * (3 - len(version))).zfill(4)
46    return version, lines[-1].split()[-1]
47
48  def Version(self):
49    if 'Version' not in self._cache:
50      self._cache['Version'], self._cache['Build'] = self._XcodeVersion()
51    return self._cache['Version']
52
53  def Build(self):
54    if 'Build' not in self._cache:
55      self._cache['Version'], self._cache['Build'] = self._XcodeVersion()
56    return self._cache['Build']
57
58  def SDKBuild(self):
59    if 'SDKBuild' not in self._cache:
60      self._cache['SDKBuild'] = subprocess.check_output(
61          ['xcodebuild', '-version', '-sdk', '', 'ProductBuildVersion'])
62      self._cache['SDKBuild'] = self._cache['SDKBuild'].rstrip('\n')
63    return self._cache['SDKBuild']
64
65  def SDKVersion(self):
66    if 'SDKVersion' not in self._cache:
67      self._cache['SDKVersion'] = subprocess.check_output(
68          ['xcodebuild', '-version', '-sdk', '', 'SDKVersion'])
69      self._cache['SDKVersion'] = self._cache['SDKVersion'].rstrip('\n')
70    return self._cache['SDKVersion']
71
72
73Xcode = XcodeInfo()
74