-
Notifications
You must be signed in to change notification settings - Fork 113
/
device_guids.py
executable file
·97 lines (74 loc) · 2.84 KB
/
device_guids.py
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
#!/usr/bin/env python
import re
import subprocess
import platform
import os
import sys
def is_version_higher(orig_version, new_version) :
if new_version[0] > orig_version[0] :
return True
if new_version[0] < orig_version[0] :
return False
if new_version[1] > orig_version[1] :
return True
if new_version[1] < orig_version[1] :
return False
if new_version[2] > orig_version[2] :
return True
return False
def get_guid_i(device) :
device_regex = re.compile("[A-Za-z0-9 ]+ ?(?:\\(([0-9.]+)\\))? \\(([A-F0-9-]+)\\)")
version_regex = re.compile("([0-9]+)\\.([0-9]+)(?:\\.([0-9]+))?")
command = "xcrun xctrace list devices"
print("##[group]Devices")
p = subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True)
# Sometimes the hostname comes back with the proper casing, sometimes not. Using a
# case insensitive regex ensures we work either way
dev_name_regex = re.compile("^" + device + "( Simulator)?" " \\(", re.I)
latest_os_device = None
latest_os_version = None
for line in p.stdout :
sys.stdout.buffer.write(line)
strLine = line.decode(sys.stdout.encoding)
if (dev_name_regex.match(strLine) == None) :
continue
match = device_regex.match(strLine)
# Regex won't match simulators with apple watches...
if (match == None) :
continue
version_match = version_regex.match(match.group(1))
minor_version = version_match.group(3)
if (minor_version == None) :
minor_version = 0
else :
minor_version = int(minor_version)
version_tuple = (int(version_match.group(1)), int(version_match.group(2)), minor_version)
if latest_os_version == None or is_version_higher(latest_os_version, version_tuple) :
latest_os_device = match.group(2)
latest_os_version = version_tuple
print("##[endgroup]Devices")
return latest_os_device
def get_guid(device) :
guid = get_guid_i(device)
if (guid == None) :
print_failure(device)
return guid
def print_failure(device) :
print("Failed to find GUID for device : " + device)
subprocess.call("xcrun xctrace list devices", shell=True)
raise Exception("Failed to get device GUID")
def get_ios(device) :
if (device in get_ios.guid) :
return get_ios.guid[device]
guid = get_guid(device)
get_ios.guid[device] = guid
return guid
get_ios.guid = {}
def get_mac() :
if (get_mac.guid != None) :
return get_mac.guid
guid = subprocess.check_output("system_profiler SPHardwareDataType | awk '/UUID/ { print $3; }'", shell=True)
guid = guid.strip()
get_mac.guid = guid
return guid
get_mac.guid = None