Debian Codegraph @

iOS/testbed/__main__.py

raw
1 import argparse
2 import json
3 import re
4 import shlex
5 import shutil
6 import subprocess
7 import sys
8 from pathlib import Path
9
10
11 DECODE_ARGS = ("UTF-8", "backslashreplace")
12
13 # The system log prefixes each line:
14 # 2025-01-17 16:14:29.093742+0800 iOSTestbed[23987:1fd393b4] ...
15 # 2025-01-17 16:14:29.093742+0800 iOSTestbed[23987:1fd393b4] ...
16
17 LOG_PREFIX_REGEX = re.compile(
18 r"^\d{4}-\d{2}-\d{2}" # YYYY-MM-DD
19 r"\s+\d+:\d{2}:\d{2}\.\d+\+\d{4}" # HH:MM:SS.ssssss+ZZZZ
20 r"\s+iOSTestbed\[\d+:\w+\]" # Process/thread ID
21 )
22
23
24 # Select a simulator device to use.
25 def select_simulator_device():
26 # List the testing simulators, in JSON format
27 raw_json = subprocess.check_output(["xcrun", "simctl", "list", "-j"])
28 json_data = json.loads(raw_json)
29
30 # Any device will do; we'll look for "SE" devices - but the name isn't
31 # consistent over time. Older Xcode versions will use "iPhone SE (Nth
32 # generation)"; As of 2025, they've started using "iPhone 16e".
33 #
34 # When Xcode is updated after a new release, new devices will be available
35 # and old ones will be dropped from the set available on the latest iOS
36 # version. Select the one with the highest minimum runtime version - this
37 # is an indicator of the "newest" released device, which should always be
38 # supported on the "most recent" iOS version.
39 se_simulators = sorted(
40 (devicetype["minRuntimeVersion"], devicetype["name"])
41 for devicetype in json_data["devicetypes"]
42 if devicetype["productFamily"] == "iPhone"
43 and (
44 (
45 "iPhone " in devicetype["name"]
46 and devicetype["name"].endswith("e")
47 )
48 or "iPhone SE " in devicetype["name"]
49 )
50 )
51
52 return se_simulators[-1][1]
53
54
55 def xcode_test(location, simulator, verbose):
56 # Build and run the test suite on the named simulator.
57 args = [
58 "-project",
59 str(location / "iOSTestbed.xcodeproj"),
60 "-scheme",
61 "iOSTestbed",
62 "-destination",
63 f"platform=iOS Simulator,name={simulator}",
64 "-derivedDataPath",
65 str(location / "DerivedData"),
66 ]
67 verbosity_args = [] if verbose else ["-quiet"]
68
69 print("Building test project...")
70 subprocess.run(
71 ["xcodebuild", "build-for-testing"] + args + verbosity_args,
72 check=True,
73 )
74
75 print("Running test project...")
76 # Test execution *can't* be run -quiet; verbose mode
77 # is how we see the output of the test output.
78 process = subprocess.Popen(
79 ["xcodebuild", "test-without-building"] + args,
80 stdout=subprocess.PIPE,
81 stderr=subprocess.STDOUT,
82 )
83 while line := (process.stdout.readline()).decode(*DECODE_ARGS):
84 # Strip the timestamp/process prefix from each log line
85 line = LOG_PREFIX_REGEX.sub("", line)
86 sys.stdout.write(line)
87 sys.stdout.flush()
88
89 status = process.wait(timeout=5)
90 exit(status)
91
92
93 def clone_testbed(
94 source: Path,
95 target: Path,
96 framework: Path,
97 apps: list[Path],
98 ) -> None:
99 if target.exists():
100 print(f"{target} already exists; aborting without creating project.")
101 sys.exit(10)
102
103 if framework is None:
104 if not (
105 source / "Python.xcframework/ios-arm64_x86_64-simulator/bin"
106 ).is_dir():
107 print(
108 f"The testbed being cloned ({source}) does not contain "
109 f"a simulator framework. Re-run with --framework"
110 )
111 sys.exit(11)
112 else:
113 if not framework.is_dir():
114 print(f"{framework} does not exist.")
115 sys.exit(12)
116 elif not (
117 framework.suffix == ".xcframework"
118 or (framework / "Python.framework").is_dir()
119 ):
120 print(
121 f"{framework} is not an XCframework, "
122 f"or a simulator slice of a framework build."
123 )
124 sys.exit(13)
125
126 print("Cloning testbed project:")
127 print(f" Cloning {source}...", end="")
128 shutil.copytree(source, target, symlinks=True)
129 print(" done")
130
131 xc_framework_path = target / "Python.xcframework"
132 sim_framework_path = xc_framework_path / "ios-arm64_x86_64-simulator"
133 if framework is not None:
134 if framework.suffix == ".xcframework":
135 print(" Installing XCFramework...", end="")
136 if xc_framework_path.is_dir():
137 shutil.rmtree(xc_framework_path)
138 else:
139 xc_framework_path.unlink(missing_ok=True)
140 xc_framework_path.symlink_to(
141 framework.relative_to(xc_framework_path.parent, walk_up=True)
142 )
143 print(" done")
144 else:
145 print(" Installing simulator framework...", end="")
146 if sim_framework_path.is_dir():
147 shutil.rmtree(sim_framework_path)
148 else:
149 sim_framework_path.unlink(missing_ok=True)
150 sim_framework_path.symlink_to(
151 framework.relative_to(sim_framework_path.parent, walk_up=True)
152 )
153 print(" done")
154 else:
155 if (
156 xc_framework_path.is_symlink()
157 and not xc_framework_path.readlink().is_absolute()
158 ):
159 # XCFramework is a relative symlink. Rewrite the symlink relative
160 # to the new location.
161 print(" Rewriting symlink to XCframework...", end="")
162 orig_xc_framework_path = (
163 source / xc_framework_path.readlink()
164 ).resolve()
165 xc_framework_path.unlink()
166 xc_framework_path.symlink_to(
167 orig_xc_framework_path.relative_to(
168 xc_framework_path.parent, walk_up=True
169 )
170 )
171 print(" done")
172 elif (
173 sim_framework_path.is_symlink()
174 and not sim_framework_path.readlink().is_absolute()
175 ):
176 print(" Rewriting symlink to simulator framework...", end="")
177 # Simulator framework is a relative symlink. Rewrite the symlink
178 # relative to the new location.
179 orig_sim_framework_path = (
180 source / "Python.XCframework" / sim_framework_path.readlink()
181 ).resolve()
182 sim_framework_path.unlink()
183 sim_framework_path.symlink_to(
184 orig_sim_framework_path.relative_to(
185 sim_framework_path.parent, walk_up=True
186 )
187 )
188 print(" done")
189 else:
190 print(" Using pre-existing iOS framework.")
191
192 for app_src in apps:
193 print(f" Installing app {app_src.name!r}...", end="")
194 app_target = target / f"iOSTestbed/app/{app_src.name}"
195 if app_target.is_dir():
196 shutil.rmtree(app_target)
197 shutil.copytree(app_src, app_target)
198 print(" done")
199
200 print(f"Successfully cloned testbed: {target.resolve()}")
201
202
203 def update_test_plan(testbed_path, args):
204 # Modify the test plan to use the requested test arguments.
205 test_plan_path = testbed_path / "iOSTestbed.xctestplan"
206 with test_plan_path.open("r", encoding="utf-8") as f:
207 test_plan = json.load(f)
208
209 test_plan["defaultOptions"]["commandLineArgumentEntries"] = [
210 {"argument": shlex.quote(arg)} for arg in args
211 ]
212
213 with test_plan_path.open("w", encoding="utf-8") as f:
214 json.dump(test_plan, f, indent=2)
215
216
217 def run_testbed(simulator: str | None, args: list[str], verbose: bool = False):
218 location = Path(__file__).parent
219 print("Updating test plan...", end="")
220 update_test_plan(location, args)
221 print(" done.")
222
223 if simulator is None:
224 simulator = select_simulator_device()
225 print(f"Running test on {simulator}")
226
227 xcode_test(location, simulator=simulator, verbose=verbose)
228
229
230 def main():
231 parser = argparse.ArgumentParser(
232 description=(
233 "Manages the process of testing a Python project in the iOS simulator."
234 ),
235 )
236
237 subcommands = parser.add_subparsers(dest="subcommand")
238
239 clone = subcommands.add_parser(
240 "clone",
241 description=(
242 "Clone the testbed project, copying in an iOS Python framework and"
243 "any specified application code."
244 ),
245 help="Clone a testbed project to a new location.",
246 )
247 clone.add_argument(
248 "--framework",
249 help=(
250 "The location of the XCFramework (or simulator-only slice of an "
251 "XCFramework) to use when running the testbed"
252 ),
253 )
254 clone.add_argument(
255 "--app",
256 dest="apps",
257 action="append",
258 default=[],
259 help="The location of any code to include in the testbed project",
260 )
261 clone.add_argument(
262 "location",
263 help="The path where the testbed will be cloned.",
264 )
265
266 run = subcommands.add_parser(
267 "run",
268 usage="%(prog)s [-h] [--simulator SIMULATOR] -- <test arg> [<test arg> ...]",
269 description=(
270 "Run a testbed project. The arguments provided after `--` will be "
271 "passed to the running iOS process as if they were arguments to "
272 "`python -m`."
273 ),
274 help="Run a testbed project",
275 )
276 run.add_argument(
277 "--simulator",
278 help=(
279 "The name of the simulator to use (eg: 'iPhone 16e'). Defaults to "
280 "the most recently released 'entry level' iPhone device. Device "
281 "architecture and OS version can also be specified; e.g., "
282 "`--simulator 'iPhone 16 Pro,arch=arm64,OS=26.0'` would run on "
283 "an ARM64 iPhone 16 Pro simulator running iOS 26.0."
284 ),
285 )
286 run.add_argument(
287 "-v",
288 "--verbose",
289 action="store_true",
290 help="Enable verbose output",
291 )
292
293 try:
294 pos = sys.argv.index("--")
295 testbed_args = sys.argv[1:pos]
296 test_args = sys.argv[pos + 1 :]
297 except ValueError:
298 testbed_args = sys.argv[1:]
299 test_args = []
300
301 context = parser.parse_args(testbed_args)
302
303 if context.subcommand == "clone":
304 clone_testbed(
305 source=Path(__file__).parent.resolve(),
306 target=Path(context.location).resolve(),
307 framework=Path(context.framework).resolve()
308 if context.framework
309 else None,
310 apps=[Path(app) for app in context.apps],
311 )
312 elif context.subcommand == "run":
313 if test_args:
314 if not (
315 Path(__file__).parent
316 / "Python.xcframework/ios-arm64_x86_64-simulator/bin"
317 ).is_dir():
318 print(
319 f"Testbed does not contain a compiled iOS framework. Use "
320 f"`python {sys.argv[0]} clone ...` to create a runnable "
321 f"clone of this testbed."
322 )
323 sys.exit(20)
324
325 run_testbed(
326 simulator=context.simulator,
327 verbose=context.verbose,
328 args=test_args,
329 )
330 else:
331 print(
332 f"Must specify test arguments (e.g., {sys.argv[0]} run -- test)"
333 )
334 print()
335 parser.print_help(sys.stderr)
336 sys.exit(21)
337 else:
338 parser.print_help(sys.stderr)
339 sys.exit(1)
340
341
342 if __name__ == "__main__":
343 main()