aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/run_ios_simulator.py
blob: b69ef81ccb638f9423036e844f2d41444ef59e8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import argparse
import json
import os
import subprocess
import sys


def just_do_it():
    parser = argparse.ArgumentParser()
    parser.add_argument("--action", choices=["create", "spawn", "kill"])
    parser.add_argument("--simctl", help="simctl binary path")
    parser.add_argument("--profiles", help="profiles path")
    parser.add_argument("--device-dir", help="devices directory")
    parser.add_argument("--device-name", help="temp device name")
    args, tail = parser.parse_known_args()
    if args.action == 'create':
        action_create(args.simctl, args.profiles, args.device_dir, args.device_name, tail)
    elif args.action == "spawn":
        action_spawn(args.simctl, args.profiles, args.device_dir, args.device_name, tail)
    elif args.action == "kill":
        action_kill(args.simctl, args.profiles, args.device_dir, args.device_name)


def action_create(simctl, profiles, device_dir, name, args):
    parser = argparse.ArgumentParser()
    parser.add_argument("--device-type", default="com.apple.CoreSimulator.SimDeviceType.iPhone-X")
    parser.add_argument("--device-runtime", default="com.apple.CoreSimulator.SimRuntime.iOS-12-1")
    args = parser.parse_args(args)
    all_devices = list(get_all_devices(simctl, profiles, device_dir))
    if filter(lambda x: x["name"] == name, all_devices):
        raise Exception("Device named {} already exists".format(name))
    subprocess.check_call(
        [simctl, "--profiles", profiles, "--set", device_dir, "create", name, args.device_type, args.device_runtime]
    )
    created = filter(lambda x: x["name"] == name, get_all_devices(simctl, profiles, device_dir))
    if not created:
        raise Exception("Creation error: temp device named {} not found".format(name))
    created = created[0]
    if created["availability"] != "(available)":
        raise Exception(
            "Creation error: temp device {} status is {} ((available) expected)".format(name, created["availability"])
        )


def action_spawn(simctl, profiles, device_dir, name, args):
    device = filter(lambda x: x["name"] == name, get_all_devices(simctl, profiles, device_dir))
    if not device:
        raise Exception("Can't spawn process: device named {} not found".format(name))
    if len(device) > 1:
        raise Exception("Can't spawn process: too many devices named {} found".format(name))
    device = device[0]
    os.execv(simctl, [simctl, "--profiles", profiles, "--set", device_dir, "spawn", device["udid"]] + args)


def action_kill(simctl, profiles, device_dir, name):
    device = filter(lambda x: x["name"] == name, get_all_devices(simctl, profiles, device_dir))
    if not device:
        print >> sys.stderr, "Device named {} not found; do nothing".format(name)
        return
    if len(device) > 1:
        raise Exception("Can't remove: too many devices named {}:\n{}".format(name, '\n'.join(i for i in device)))
    device = device[0]
    os.execv(simctl, [simctl, "--profiles", profiles, "--set", device_dir, "delete", device["udid"]])


def get_all_devices(simctl, profiles, device_dir):
    p = subprocess.Popen(
        [simctl, "--profiles", profiles, "--set", device_dir, "list", "--json", "devices"], stdout=subprocess.PIPE
    )
    out, _ = p.communicate()
    rc = p.wait()
    if rc:
        raise Exception("Devices list command return code is {}\nstdout:\n{}".format(rc, out))
    raw_object = json.loads(out)
    if "devices" not in raw_object:
        raise Exception("Devices not found in\n{}".format(json.dumps(raw_object)))
    raw_object = raw_object["devices"]
    for os_name, devices in raw_object.items():
        for device in devices:
            device["os_name"] = os_name
            yield device


if __name__ == '__main__':
    just_do_it()