1#!/usr/bin/python3
2# Copyright 2020 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""
6Wrapper around xcrun adding support for --developer-dir parameter to set
7the DEVELOPER_DIR environment variable, and for converting paths relative
8to absolute (since this is required by most of the tool run via xcrun).
9"""
10
11import argparse
12import os
13import subprocess
14import sys
15
16
17def xcrun(command, developer_dir):
18  environ = dict(os.environ)
19  if developer_dir:
20    environ['DEVELOPER_DIR'] = os.path.abspath(developer_dir)
21
22  processed_args = ['/usr/bin/xcrun']
23  for arg in command:
24    if os.path.exists(arg):
25      arg = os.path.abspath(arg)
26    processed_args.append(arg)
27
28  process = subprocess.Popen(processed_args,
29                             stdout=subprocess.PIPE,
30                             stderr=subprocess.PIPE,
31                             universal_newlines=True,
32                             env=environ)
33
34  stdout, stderr = process.communicate()
35  sys.stdout.write(stdout)
36  if process.returncode:
37    sys.stderr.write(stderr)
38    sys.exit(process.returncode)
39
40
41def main(args):
42  parser = argparse.ArgumentParser(add_help=False)
43  parser.add_argument(
44      '--developer-dir',
45      help='path to developer dir to use for the invocation of xcrun')
46
47  parsed, remaining_args = parser.parse_known_args(args)
48  xcrun(remaining_args, parsed.developer_dir)
49
50
51if __name__ == '__main__':
52  main(sys.argv[1:])
53