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 # xcodebuild doesn't guarantee that the CoreSimulatorService daemon is
224 # running prior to using a simulator, but calling `xcrun simctl list` does.
225 # Determining the default simulator that *would* be used is a cheap action;
226 # so use that as a way to ensure that the CoreSimulatorService daemon is
227 # running.
228 default_simulator = select_simulator_device()
229 if simulator is None:
230 simulator = default_simulator
231 print(f"Running test on {simulator}")
232
233 xcode_test(location, simulator=simulator, verbose=verbose)
234
235
236 def main():
237 parser = argparse.ArgumentParser(
238 description=(
239 "Manages the process of testing a Python project in the iOS simulator."
240 ),
241 )
242
243 subcommands = parser.add_subparsers(dest="subcommand")
244
245 clone = subcommands.add_parser(
246 "clone",
247 description=(
248 "Clone the testbed project, copying in an iOS Python framework and"
249 "any specified application code."
250 ),
251 help="Clone a testbed project to a new location.",
252 )
253 clone.add_argument(
254 "--framework",
255 help=(
256 "The location of the XCFramework (or simulator-only slice of an "
257 "XCFramework) to use when running the testbed"
258 ),
259 )
260 clone.add_argument(
261 "--app",
262 dest="apps",
263 action="append",
264 default=[],
265 help="The location of any code to include in the testbed project",
266 )
267 clone.add_argument(
268 "location",
269 help="The path where the testbed will be cloned.",
270 )
271
272 run = subcommands.add_parser(
273 "run",
274 usage="%(prog)s [-h] [--simulator SIMULATOR] -- <test arg> [<test arg> ...]",
275 description=(
276 "Run a testbed project. The arguments provided after `--` will be "
277 "passed to the running iOS process as if they were arguments to "
278 "`python -m`."
279 ),
280 help="Run a testbed project",
281 )
282 run.add_argument(
283 "--simulator",
284 help=(
285 "The name of the simulator to use (eg: 'iPhone 16e'). Defaults to "
286 "the most recently released 'entry level' iPhone device. Device "
287 "architecture and OS version can also be specified; e.g., "
288 "`--simulator 'iPhone 16 Pro,arch=arm64,OS=26.0'` would run on "
289 "an ARM64 iPhone 16 Pro simulator running iOS 26.0."
290 ),
291 )
292 run.add_argument(
293 "-v",
294 "--verbose",
295 action="store_true",
296 help="Enable verbose output",
297 )
298
299 try:
300 pos = sys.argv.index("--")
301 testbed_args = sys.argv[1:pos]
302 test_args = sys.argv[pos + 1 :]
303 except ValueError:
304 testbed_args = sys.argv[1:]
305 test_args = []
306
307 context = parser.parse_args(testbed_args)
308
309 if context.subcommand == "clone":
310 clone_testbed(
311 source=Path(__file__).parent.resolve(),
312 target=Path(context.location).resolve(),
313 framework=Path(context.framework).resolve()
314 if context.framework
315 else None,
316 apps=[Path(app) for app in context.apps],
317 )
318 elif context.subcommand == "run":
319 if test_args:
320 if not (
321 Path(__file__).parent
322 / "Python.xcframework/ios-arm64_x86_64-simulator/bin"
323 ).is_dir():
324 print(
325 f"Testbed does not contain a compiled iOS framework. Use "
326 f"`python {sys.argv[0]} clone ...` to create a runnable "
327 f"clone of this testbed."
328 )
329 sys.exit(20)
330
331 run_testbed(
332 simulator=context.simulator,
333 verbose=context.verbose,
334 args=test_args,
335 )
336 else:
337 print(
338 f"Must specify test arguments (e.g., {sys.argv[0]} run -- test)"
339 )
340 print()
341 parser.print_help(sys.stderr)
342 sys.exit(21)
343 else:
344 parser.print_help(sys.stderr)
345 sys.exit(1)
346
347
348 if __name__ == "__main__":
349 main()