|
| 1 | +# This source file is part of the Swift.org open source project |
| 2 | +# |
| 3 | +# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 4 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +# |
| 6 | +# See https://swift.org/LICENSE.txt for license information |
| 7 | +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | + |
| 9 | + |
| 10 | +""" |
| 11 | +Wrapper module around the 'xcrun' command-line utility. |
| 12 | +""" |
| 13 | + |
| 14 | + |
| 15 | +from __future__ import absolute_import, unicode_literals |
| 16 | + |
| 17 | +import functools |
| 18 | +import re |
| 19 | +import shlex |
| 20 | + |
| 21 | +import six |
| 22 | + |
| 23 | +from .. import shell |
| 24 | +from ..versions import Version |
| 25 | + |
| 26 | + |
| 27 | +__all__ = [ |
| 28 | + 'XcrunWrapper', |
| 29 | +] |
| 30 | + |
| 31 | + |
| 32 | +# ----------------------------------------------------------------------------- |
| 33 | +# Constants |
| 34 | + |
| 35 | +_VERSION_PATTERN = re.compile(r'^xcrun version (?P<version>[0-9.]+)\.$') |
| 36 | + |
| 37 | + |
| 38 | +# ----------------------------------------------------------------------------- |
| 39 | +# Helpers |
| 40 | + |
| 41 | +def _catch_return_none(exceptions): |
| 42 | + """Decorator used to catch exceptions and return None. |
| 43 | + """ |
| 44 | + |
| 45 | + def decorator(func): |
| 46 | + @functools.wraps(func) |
| 47 | + def wrapper(*args, **kwargs): |
| 48 | + try: |
| 49 | + return func(*args, **kwargs) |
| 50 | + except exceptions: |
| 51 | + return None |
| 52 | + |
| 53 | + return wrapper |
| 54 | + return decorator |
| 55 | + |
| 56 | + |
| 57 | +def _prepend_sdk_and_toolchain(func): |
| 58 | + """Method decorator used to prepend the sdk and toolchain arguments to the |
| 59 | + final command passed to xcrun. |
| 60 | + """ |
| 61 | + |
| 62 | + @functools.wraps(func) |
| 63 | + def wrapper(self, args, sdk=None, toolchain=None, **kwargs): |
| 64 | + if isinstance(args, six.string_types): |
| 65 | + args = shlex.split(args) |
| 66 | + if toolchain: |
| 67 | + args = ['--toolchain', toolchain] + args |
| 68 | + if sdk: |
| 69 | + args = ['--sdk', sdk] + args |
| 70 | + |
| 71 | + return func(self, args, **kwargs) |
| 72 | + return wrapper |
| 73 | + |
| 74 | + |
| 75 | +# ----------------------------------------------------------------------------- |
| 76 | + |
| 77 | +class XcrunWrapper(shell.ExecutableWrapper): |
| 78 | + """Wrapper class around the 'xcrun' command-line utility. |
| 79 | + """ |
| 80 | + |
| 81 | + EXECUTABLE = shell.which('xcrun') or 'xcrun' |
| 82 | + |
| 83 | + @_prepend_sdk_and_toolchain |
| 84 | + def Popen(self, args, **kwargs): |
| 85 | + return super(XcrunWrapper, self).Popen(args, **kwargs) |
| 86 | + |
| 87 | + @_prepend_sdk_and_toolchain |
| 88 | + def call(self, args, **kwargs): |
| 89 | + return super(XcrunWrapper, self).call(args, **kwargs) |
| 90 | + |
| 91 | + @_prepend_sdk_and_toolchain |
| 92 | + def check_call(self, args, **kwargs): |
| 93 | + return super(XcrunWrapper, self).check_call(args, **kwargs) |
| 94 | + |
| 95 | + @_prepend_sdk_and_toolchain |
| 96 | + def check_output(self, args, **kwargs): |
| 97 | + return super(XcrunWrapper, self).check_output(args, **kwargs) |
| 98 | + |
| 99 | + # ------------------------------------------------------------------------- |
| 100 | + |
| 101 | + @property |
| 102 | + @_catch_return_none(shell.CalledProcessError) |
| 103 | + def version(self): |
| 104 | + """Returns the xcrun version. |
| 105 | + """ |
| 106 | + |
| 107 | + output = self.check_output('--version') |
| 108 | + matches = _VERSION_PATTERN.match(output.rstrip()) |
| 109 | + if matches: |
| 110 | + return Version(matches.group('version')) |
| 111 | + |
| 112 | + return None |
| 113 | + |
| 114 | + # ------------------------------------------------------------------------- |
| 115 | + # Subcommands |
| 116 | + |
| 117 | + @_catch_return_none(shell.CalledProcessError) |
| 118 | + def find(self, tool, sdk=None, toolchain=None): |
| 119 | + """Finds and returns the path to tool using xcrun. Returns None if the |
| 120 | + tool cannot be found. |
| 121 | + """ |
| 122 | + |
| 123 | + return self.check_output( |
| 124 | + ['--find', tool], |
| 125 | + sdk=sdk, |
| 126 | + toolchain=toolchain, |
| 127 | + stderr=shell.DEVNULL).rstrip() |
| 128 | + |
| 129 | + def kill_cache(self): |
| 130 | + """Kills the xcrun cache. Returns the status code from the command. |
| 131 | + """ |
| 132 | + |
| 133 | + return self.call('--kill-cache') |
| 134 | + |
| 135 | + @_catch_return_none(shell.CalledProcessError) |
| 136 | + def sdk_path(self, sdk=None, toolchain=None): |
| 137 | + """Returns the SDK path. Returns None if the SDK cannot be found. |
| 138 | + """ |
| 139 | + |
| 140 | + return self.check_output( |
| 141 | + '--show-sdk-path', |
| 142 | + sdk=sdk, |
| 143 | + toolchain=toolchain, |
| 144 | + stderr=shell.DEVNULL).rstrip() |
| 145 | + |
| 146 | + @_catch_return_none(shell.CalledProcessError) |
| 147 | + def sdk_version(self, sdk=None, toolchain=None): |
| 148 | + """Returns the SDK version. Returns None if the SDK cannot be found. |
| 149 | + """ |
| 150 | + |
| 151 | + output = self.check_output( |
| 152 | + '--show-sdk-version', |
| 153 | + sdk=sdk, |
| 154 | + toolchain=toolchain, |
| 155 | + stderr=shell.DEVNULL) |
| 156 | + |
| 157 | + return Version(output.rstrip()) |
| 158 | + |
| 159 | + @_catch_return_none(shell.CalledProcessError) |
| 160 | + def sdk_build_version(self, sdk=None, toolchain=None): |
| 161 | + """Returns the SDK build version. Returns None if the SDK cannot be |
| 162 | + found. |
| 163 | + """ |
| 164 | + |
| 165 | + output = self.check_output( |
| 166 | + '--show-sdk-build-version', |
| 167 | + sdk=sdk, |
| 168 | + toolchain=toolchain, |
| 169 | + stderr=shell.DEVNULL) |
| 170 | + |
| 171 | + return Version(output.rstrip()) |
| 172 | + |
| 173 | + @_catch_return_none(shell.CalledProcessError) |
| 174 | + def sdk_platform_path(self, sdk=None, toolchain=None): |
| 175 | + """Returns the SDK platform path. Returns None if the SDK cannot be |
| 176 | + found. |
| 177 | + """ |
| 178 | + |
| 179 | + return self.check_output( |
| 180 | + '--show-sdk-platform-path', |
| 181 | + sdk=sdk, |
| 182 | + toolchain=toolchain, |
| 183 | + stderr=shell.DEVNULL).rstrip() |
| 184 | + |
| 185 | + @_catch_return_none(shell.CalledProcessError) |
| 186 | + def sdk_platform_version(self, sdk=None, toolchain=None): |
| 187 | + """Returns the SDK platform version. Returns None if the SDK cannot be |
| 188 | + found. |
| 189 | + """ |
| 190 | + |
| 191 | + output = self.check_output( |
| 192 | + '--show-sdk-platform-version', |
| 193 | + sdk=sdk, |
| 194 | + toolchain=toolchain, |
| 195 | + stderr=shell.DEVNULL) |
| 196 | + |
| 197 | + return Version(output.rstrip()) |
0 commit comments