-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdevice.py
269 lines (223 loc) · 8.07 KB
/
device.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python
import subprocess
#dirty hack to backport check_output to python <2.7
# taken from: http://stackoverflow.com/questions/4814970/subprocess-check-output-doesnt-seem-to-exist-python-2-6-5/13160748#13160748
if "check_output" not in dir( subprocess ): # duck punch it in!
def f(*popenargs, **kwargs):
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd)
return output
subprocess.check_output = f
import plistlib
import re
import logging
import os
from store import AppStore
from enum import Enum
import deviceconnection
logger = logging.getLogger('worker.'+__name__)
class iDevice(object):
###### device info stuff
@staticmethod
def list_device_ids():
''' list of the connected devices udid's
'''
output = subprocess.check_output(["idevice_id", "--list"])
return filter(None, output.split("\n"));
@classmethod
def devices(cls):
''' list of connected devices.
'''
return list(iDevice(udid) for udid in iDevice.list_device_ids())
def __init__(self, udid):
self.udid = udid
self.deviceDict = {}
self.accountDict = {}
self.locale_val = ""
def __str__(self):
return "<iDevice: %s>" % self.udid
def __repr__(self):
return "<iDevice: %s>" % self.udid
def device_info_dict(self):
''' raw device information as dict
'''
if (len(self.deviceDict) == 0):
output = subprocess.check_output(["ideviceinfo", "--xml", "--udid", self.udid])
self.deviceDict = plistlib.readPlistFromString(output)
return self.deviceDict
DEVICE_INFO = Enum(['DeviceName', 'DeviceClass', 'ProductType', 'ProductVersion', 'WiFiAddress'])
def ios_version(self):
''' the devices iOS verison
'''
version = self.device_info_dict()["ProductVersion"]
return tuple(map(int, (version.split("."))))
def locale(self):
''' the devices locale setting
'''
if (self.locale_val == ""):
self.locale_val = subprocess.check_output(["ideviceinfo", "--udid", self.udid, "--domain", "com.apple.international", "--key", "Locale"]).strip()
return self.locale_val
def base_url(self):
''' the devices base url to access the devices pilot via http tunneled via USB
'''
baseUrl = None
device_handler = deviceconnection.shared_device_handler()
conn_tuple = device_handler.device_connection_info(self.udid)
if conn_tuple:
baseUrl = "http://%s:%s/" % conn_tuple
return baseUrl
### more device informations
def free_bytes(self):
''' get the free space left on the device in bytes
'''
output = subprocess.check_output(["ideviceinfo", "--udid", self.udid, "--domain", "com.apple.disk_usage", "--key", "TotalDataAvailable"])
free_bytes = 0
try:
free_bytes = long(output)
except ValueError:
logger.warning("Unable to get free space for device %s. Output: %s" % (self, output))
return free_bytes
###### Account stuff
def account_info_dict(self):
''' get raw account info from device as dict.
'''
if (len(self.accountDict) == 0):
output = subprocess.check_output(["ideviceinfo", "--xml", "--udid", self.udid, "--domain", "com.apple.mobile.iTunes.store", "--key", "KnownAccounts"])
if len(output) > 0:
self.accountDict = plistlib.readPlistFromString(output)
else:
logger.warning("No accounts found for device %s" % device)
self.accountDict = {}
return self.accountDict
ACCOUNT_INFO = Enum({
'APPLE_ID':'AppleID',
'UNIQUE_IDENTIFIER':'DSPersonID',
'STOREFRONT':'AccountStoreFront'
})
def accounts(self):
''' list all known accounts as list of account-dicts
'''
accList = self.account_info_dict()
accounts = []
for accInfo in accList:
storeFront = ""
if self.ACCOUNT_INFO.STOREFRONT in accInfo:
storeFront = accInfo[self.ACCOUNT_INFO.STOREFRONT].split(",")[0]
if '-' in storeFront:
storeFront = storeFront.split('-')[0]
storeCountry = AppStore.countryForStoreFrontId(storeFront)
if storeCountry == None or storeCountry == "":
storeCountry = self.locale().split('_')[-1]
acc = {
'uniqueIdentifier': str(accInfo[self.ACCOUNT_INFO.UNIQUE_IDENTIFIER]),
'appleId': accInfo[self.ACCOUNT_INFO.APPLE_ID],
'storeCountry': storeCountry.lower()
}
accounts.append(acc)
return accounts
###### App stuff
APP_INFO = Enum({
'NAME':'CFBundleName',
'DISPLAY_NAME':'CFBundleDisplayName',
'BUNDLE_ID':'CFBundleIdentifier',
'VERSION':'CFBundleShortVersionString',
'ACCOUNT_ID':'ApplicationDSID'
})
def installed_apps(self):
''' list all installed apps as dict.
'''
output = subprocess.check_output(["ideviceinstaller", "--udid", self.udid, "--list-apps", "-o", "list_user", "-o", "xml"])
if (len(output)==0):
return {}
apps = {}
plist = []
try:
plist = plistlib.readPlistFromString(output)
except Exception:
logger.warning("Failed to parse installed apps via xml output. Try to extract data via regex.")
output = subprocess.check_output(["ideviceinstaller", "--udid", self.udid, "--list-apps", "-o", "list_user"])
regex = re.compile("^(?P<bundleId>.*) - (?P<name>.*) (?P<version>(\d+\.*)+)$",re.MULTILINE)
# r = regex.search(output)
for i in regex.finditer(output):
results = i.groupdict()
data = {
self.APP_INFO.NAME: results['name'].decode('utf-8').encode("ascii", "ignore"),
self.APP_INFO.BUNDLE_ID: results['bundleId'],
self.APP_INFO.VERSION: results['version']
}
plist.append(data)
for entry in plist:
appData = {
'name': '',
'bundleId': entry[self.APP_INFO.BUNDLE_ID],
'version': '-2'
}
if not self.APP_INFO.DISPLAY_NAME in entry and not self.APP_INFO.NAME in entry:
logger.warning('Using last part of bundleId as app name! entry:<%s>', entry)
appData['name'] = appData['bundleId'].split('.')[-1]
if self.APP_INFO.NAME in entry:
appData['name'] = entry[self.APP_INFO.NAME]
if self.APP_INFO.DISPLAY_NAME in entry:
appData['name'] = entry[self.APP_INFO.DISPLAY_NAME]
if self.APP_INFO.VERSION in entry:
appData['version'] = entry[self.APP_INFO.VERSION]
if self.APP_INFO.ACCOUNT_ID in entry:
appData['accountId'] = entry[self.APP_INFO.ACCOUNT_ID]
apps[entry[self.APP_INFO.BUNDLE_ID]] = appData
return apps
def install(self, app_archive_path):
''' install an app on the device from given file
returns True or False
'''
result=True
try:
output = subprocess.check_output(["ideviceinstaller", "--udid", self.udid, "--install", app_archive_path])
logger.debug('output: %s' % output)
if (len(output)==0):
result=False
except subprocess.CalledProcessError as e:
logger.error('installing app %s failed with: %s <output: %s>' % (app_archive_path, e, output))
result=False
return result
def uninstall(self, bundleId):
''' uninstall an app on the device from given bundleId
returns True or False
'''
result=True
try:
output = subprocess.check_output(["ideviceinstaller", "--udid", self.udid, "--uninstall", bundleId])
logger.debug('output: %s' % output)
if (len(output)==0):
result=False
except subprocess.CalledProcessError as e:
logger.error('uninstalling app %s failed with: %s <output: %s>' % (bundleId, e, output))
result=False
return result
def archive(self, bundleId, app_archive_folder, app_only=True):
''' archives an app to `app_archive_folder`
returns True or False
'''
options = ["ideviceinstaller", "--udid", self.udid, "--archive", bundleId, "-o", "copy="+app_archive_folder, "-o", "remove"]
if app_only:
options.extend(["-o", "app_only"])
if not os.path.exists(app_archive_folder):
os.makedirs(app_archive_folder)
logger.debug('try archiving app %s with cmd: %s' % (bundleId, ' '.join(options)))
result=True
try:
output = subprocess.check_output(options)
logger.debug('output: %s' % output)
if (len(output)==0):
result=False
except subprocess.CalledProcessError as e:
logger.error('archiving app %s failed with: %s <output: %s>', bundleId, e, output)
result=False
return result