1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2016 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import argparse
8import os
9import subprocess
10import sys
11
12if __name__ == '__main__':
13    parser = argparse.ArgumentParser(
14        description='A script to execute a command via xcrun.')
15    parser.add_argument('--stamp', action='store', type=str,
16                        help='Write a stamp file to this path on success.')
17    parser.add_argument('--developer_dir', required=False,
18                        help='Path to Xcode.')
19    args, unknown_args = parser.parse_known_args()
20
21    if args.developer_dir:
22        os.environ['DEVELOPER_DIR'] = args.developer_dir
23
24    rv = subprocess.check_call(['xcrun'] + unknown_args)
25    flags = os.O_RDWR | os.O_CREAT | os.O_EXCL
26    modes = stat.S_IWUSR | stat.S_IRUSR
27    if rv == 0 and args.stamp:
28        if os.path.exists(args.stamp):
29            os.unlink(args.stamp)
30    with os.fdopen(os.open(args.stamp, flags, modes), 'w+') as fp:
31        sys.exit(rv)
32