run_ios_simulator.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import argparse
  2. import json
  3. import os
  4. import subprocess
  5. import sys
  6. def just_do_it():
  7. parser = argparse.ArgumentParser()
  8. parser.add_argument("--action", choices=["create", "spawn", "kill"])
  9. parser.add_argument("--simctl", help="simctl binary path")
  10. parser.add_argument("--profiles", help="profiles path")
  11. parser.add_argument("--device-dir", help="devices directory")
  12. parser.add_argument("--device-name", help="temp device name")
  13. args, tail = parser.parse_known_args()
  14. if args.action == 'create':
  15. action_create(args.simctl, args.profiles, args.device_dir, args.device_name, tail)
  16. elif args.action == "spawn":
  17. action_spawn(args.simctl, args.profiles, args.device_dir, args.device_name, tail)
  18. elif args.action == "kill":
  19. action_kill(args.simctl, args.profiles, args.device_dir, args.device_name)
  20. def action_create(simctl, profiles, device_dir, name, args):
  21. parser = argparse.ArgumentParser()
  22. parser.add_argument("--device-type", default="com.apple.CoreSimulator.SimDeviceType.iPhone-X")
  23. parser.add_argument("--device-runtime", default="com.apple.CoreSimulator.SimRuntime.iOS-12-1")
  24. args = parser.parse_args(args)
  25. all_devices = list(get_all_devices(simctl, profiles, device_dir))
  26. if filter(lambda x: x["name"] == name, all_devices):
  27. raise Exception("Device named {} already exists".format(name))
  28. subprocess.check_call(
  29. [simctl, "--profiles", profiles, "--set", device_dir, "create", name, args.device_type, args.device_runtime]
  30. )
  31. created = filter(lambda x: x["name"] == name, get_all_devices(simctl, profiles, device_dir))
  32. if not created:
  33. raise Exception("Creation error: temp device named {} not found".format(name))
  34. created = created[0]
  35. if created["availability"] != "(available)":
  36. raise Exception(
  37. "Creation error: temp device {} status is {} ((available) expected)".format(name, created["availability"])
  38. )
  39. def action_spawn(simctl, profiles, device_dir, name, args):
  40. device = filter(lambda x: x["name"] == name, get_all_devices(simctl, profiles, device_dir))
  41. if not device:
  42. raise Exception("Can't spawn process: device named {} not found".format(name))
  43. if len(device) > 1:
  44. raise Exception("Can't spawn process: too many devices named {} found".format(name))
  45. device = device[0]
  46. os.execv(simctl, [simctl, "--profiles", profiles, "--set", device_dir, "spawn", device["udid"]] + args)
  47. def action_kill(simctl, profiles, device_dir, name):
  48. device = filter(lambda x: x["name"] == name, get_all_devices(simctl, profiles, device_dir))
  49. if not device:
  50. print >> sys.stderr, "Device named {} not found; do nothing".format(name)
  51. return
  52. if len(device) > 1:
  53. raise Exception("Can't remove: too many devices named {}:\n{}".format(name, '\n'.join(i for i in device)))
  54. device = device[0]
  55. os.execv(simctl, [simctl, "--profiles", profiles, "--set", device_dir, "delete", device["udid"]])
  56. def get_all_devices(simctl, profiles, device_dir):
  57. p = subprocess.Popen(
  58. [simctl, "--profiles", profiles, "--set", device_dir, "list", "--json", "devices"], stdout=subprocess.PIPE
  59. )
  60. out, _ = p.communicate()
  61. rc = p.wait()
  62. if rc:
  63. raise Exception("Devices list command return code is {}\nstdout:\n{}".format(rc, out))
  64. raw_object = json.loads(out)
  65. if "devices" not in raw_object:
  66. raise Exception("Devices not found in\n{}".format(json.dumps(raw_object)))
  67. raw_object = raw_object["devices"]
  68. for os_name, devices in raw_object.items():
  69. for device in devices:
  70. device["os_name"] = os_name
  71. yield device
  72. if __name__ == '__main__':
  73. just_do_it()