-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiossim.py
More file actions
136 lines (109 loc) · 3.92 KB
/
Copy pathiossim.py
File metadata and controls
136 lines (109 loc) · 3.92 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright 2023 Breautek
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import subprocess
import json
def main():
if len(sys.argv) != 4:
print("Usage: python iossim.py <Sim Name> <Sim Runtime> <Device Type>")
sys.exit(1)
devName = sys.argv[1]
runtime = getRuntimeByName(sys.argv[2])
if runtime == None:
print("No runtime found by the name of " + sys.argv[2])
sys.exit(1)
# First check to make sure the simulator doesn't already exists
deviceUDID = getSimByName(devName, runtime)
if deviceUDID != None:
# Already has device
print(deviceUDID)
sys.exit(0)
# Otherwise create it
device = getDeviceTypeByName(sys.argv[3])
deviceUDID = createSim(devName, device, runtime)
if deviceUDID == None:
print("Could not create simulator with for " + device + "/" + runtime)
sys.exit(1)
print(deviceUDID)
sys.exit(0)
def createSim(name, device, runtime):
try:
subprocess.run([
"xcrun", "simctl", "create", name, device, runtime
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
except subprocess.CalledProcessError as ex:
print("xcrun failed with error:", ex)
raise ex
return getSimByName(name, runtime)
def getDeviceTypeByName(name):
result = None
try:
result = subprocess.run([
"xcrun", "simctl", "list", "devicetypes", "--json"
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
except subprocess.CalledProcessError as ex:
print("xcrun failed with error:", ex)
raise ex
types = None
if result.stdout:
try:
types = json.loads(result.stdout)["devicetypes"]
# runtimes = runtimes
except json.JSONDecodeError as ex:
print("JSON Error JSON:", ex)
raise ex
for dt in types:
if name == dt["name"]:
return dt["identifier"]
return None
def getRuntimeByName(name):
result = None
try:
result = subprocess.run([
"xcrun", "simctl", "list", "runtimes", "--json"
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
except subprocess.CalledProcessError as ex:
print("xcrun failed with error:", ex)
raise ex
runtimes = None
if result.stdout:
try:
runtimes = json.loads(result.stdout)["runtimes"]
# runtimes = runtimes
except json.JSONDecodeError as ex:
print("JSON Error JSON:", ex)
raise ex
for runtime in runtimes:
if name == runtime["name"]:
return runtime["identifier"]
return None
def getSimByName(name, runtime):
result = None
try:
result = subprocess.run([
"xcrun", "simctl", "list", "devices", "--json"
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
except subprocess.CalledProcessError as ex:
print("xcrun failed with error:", ex)
raise ex
devices = None
if result.stdout:
try:
devices = json.loads(result.stdout)["devices"][runtime]
except json.JSONDecodeError as ex:
print("JSON Error JSON:", ex)
raise ex
for device in devices:
if name == device["name"]:
return device["udid"]
return None
if __name__ == "__main__":
main()