forked from opengapps/apkcrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporthelper.py
219 lines (189 loc) · 9.01 KB
/
reporthelper.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
import logging
import re
from apkhelper import ApkVersionInfo
class ReportHelper(object):
"""ReportHelper"""
def __init__(self, lines):
self.dAllApks = {}
self.maxVerEachApk = {}
self.minSdkEachApk = {}
self.appsNeeded = []
# Fill member dict and lists
self.processReportSourcesOutput(lines)
self.getMaxVersionDict()
self.getMinSdkDict()
self.showMissingApks()
# END: __init__
def processReportSourcesOutput(self, lines):
"""
processReportSourcesOutput(lines): Populate a dictionary of all APKs and versions in report
created by report_sources.sh
"""
self.dAllApks = {}
sColumns = ['(?P<name>[a-z][^|]*)', '(?P<arch>[^|]*)', '(?P<sdk>[^|]*)', '(?P<dpi>[^|]*)',
'(?P<ver>[^|]*)', '(?P<code>[^|]*)', '(?P<mib>[^|]*)', '(?P<sig>[^|]*)']
pattern = '^\s+' + '\|'.join(sColumns) + '$'
reLine = re.compile(pattern)
for line in lines:
m = reLine.match(line)
if m:
name = m.group('name').strip()
arch = m.group('arch').strip()
sdk = m.group('sdk').strip()
dpi = m.group('dpi').strip()
ver = m.group('ver').strip()
code = m.group('code').strip()
avi = ApkVersionInfo(name=name,
arch=arch,
sdk=sdk,
dpi=dpi,
ver=ver,
vercode=code)
# Check if supported and add if it is
if avi.vercode in [1, 19, 21, 22, 23, 24, 25, 26]: # Ignore factory image files
continue
if avi.name not in list(self.dAllApks.keys()):
self.dAllApks[avi.name] = []
self.dAllApks[avi.name].append(avi)
# END: if m:
# END: for line
# END: def processReportSourcesOutput
def getAllApkIds(self, beta=False, playstoreCaps=False):
apkIds = self.dAllApks.keys()
regexStr = '^.*$'
if not beta:
regexStr = '^.*\.beta$'
reBeta = re.compile(regexStr)
if playstoreCaps:
apkIds = [apkid.replace('googlecamera', 'GoogleCamera') for apkid in apkIds]
return list(filter(lambda x: not reBeta.match(x), apkIds))
# END: def getAllApkIds
def getMaxVersionDict(self):
"""
getMaxVersionDict(): Populate a dictionary of the max version of each APK
"""
self.maxVerEachApk = {}
for k in sorted(self.dAllApks.keys()):
k2 = self.dAllApks[k][0].name
if k not in self.maxVerEachApk:
max1 = max(apk for apk in self.dAllApks[k]).ver
max2 = max1
# Check for "non-leanback" versions for max comparison
if k2 in self.dAllApks:
max2 = max(apk for apk in self.dAllApks[k2]).ver
self.maxVerEachApk[k] = max(max1, max2)
# END: if not k
logging.debug('{0} - maxVer: {1}'.format(k, self.maxVerEachApk[k]))
# END: for k
# END: def getMaxVersionDict
def getMinSdkDict(self):
"""
getMinSdkDict(): Populate a dictionary of the minimum sdk for each APK
"""
self.minSdkEachApk = {}
for k in sorted(self.dAllApks.keys()):
if k not in self.minSdkEachApk:
minSdk = min(int(apk.sdk) for apk in self.dAllApks[k])
minSdk = min(minSdk, 19) # We support down to 19
self.minSdkEachApk[k] = minSdk
# END: if not k
logging.debug('{0} - minSdk: {1}'.format(k, self.minSdkEachApk[k]))
# END: for k
# END: def getMinSdkDict
def showMissingApks(self):
"""
showMissingApks(): Populate a list of the needed APKs
"""
self.appsNeeded = []
# NOTE: This code currently only shows older apks (that need updating).
# @mfonville has another scheme based up vercode rules for each
# apkid that would be more complete
for k in list(self.dAllApks.keys()):
thisappsneeded = []
for a in self.dAllApks[k]:
maxApk = ApkVersionInfo(ver=self.maxVerEachApk[k])
if a.ver < maxApk.ver:
logging.debug('{0}: {1} < maxApk.ver: {2}'.format(k, a.ver, maxApk.ver))
thisappsneeded.append(a.fullString(self.maxVerEachApk[k]))
if len(thisappsneeded) != 0:
self.appsNeeded.extend(thisappsneeded)
# END: for k in
for a in sorted(self.appsNeeded):
logging.info(a)
# END: def showMissingApks
def isThisApkNeeded(self, avi):
"""
def isThisApkNeeded(): Return true if this information passed in is needed per the report data
that this class was initialized with
"""
# Against the list we are looking for
if avi.lowername not in list(self.dAllApks.keys()):
return False
logging.debug(avi.fullString(avi.ver))
logging.debug('Do we have already vercode?')
# Do we have the requested vercode already? Or do we have a higher vercode while there is only one variant of these apps?
if avi.vercode != 0:
if ([apk for apk in self.dAllApks[avi.lowername] if apk.vercode == avi.vercode]) or (avi.isVercodeAbsolute() and ([apk for apk in self.dAllApks[avi.lowername] if apk.vercode >= avi.vercode])):
logging.debug(' DON\'T NEED')
return False
else: # We only need to run the realvername match if we could not compare the vercode itself
logging.debug('Can we use an absolute realvername match?')
if avi.isRealverAbsolute():
logging.debug('Do we have already a matching absolute realvername?')
# Do we have the requested realver already?
if avi.realver != '':
if [apk for apk in self.dAllApks[avi.lowername] if apk.realver == avi.realver]:
logging.debug(' DON\'T NEED')
return False
logging.debug('Is it less than maxVersion?')
# Is it < maxVersion?
if avi.ver != '':
maxApkInfo = ApkVersionInfo(name=avi.lowername, ver=self.maxVerEachApk[avi.lowername])
if avi < maxApkInfo:
logging.debug(' DON\'T NEED')
return False
logging.debug('Is SDK a number?') # If it is not a number, but a letter it is a preview and undesired by Open GApps
if avi.sdk and not isinstance(avi.sdk, int):
logging.debug('SdkNotNumber: {0}({1})'.format(avi.name, avi.sdk))
return False
logging.debug('Is Target a number?') # If it is not a number, but a letter it is a preview and undesired by Open GApps
if avi.target and not isinstance(avi.target, int):
logging.debug('TargetNotNumber: {0}({1})'.format(avi.name, avi.target))
return False
logging.debug('Is it less than minSdk?')
# Is it < minSdk?
if avi.sdk != 0:
if avi.sdk < self.minSdkEachApk[avi.lowername]:
logging.debug('SdkTooLow: {0}({1})'.format(avi.name, avi.sdk))
return False
# Are we dealing with a app that has beta support?
# Examples: WebView, GoogleApp
if self.needsBetaSupport(avi):
logging.debug('beta support ...')
# TODO: Needs more thought (?)
if not avi.lowername.endswith('.beta'): # Make sure we don't promote a beta app to non-beta
logging.debug('Do we have already vercode? (beta)')
# Do we have the requested vercode (in beta) already?
if avi.vercode != 0:
if [apk for apk in self.dAllApks[avi.lowername + '.beta'] if apk.vercode == avi.vercode]:
logging.debug(' DON\'T NEED')
return False
logging.debug('Is it greater than or equal to maxVersion?')
# Is it >= maxVersion (for beta)?
if avi.ver != '':
maxApkInfo = ApkVersionInfo(name=avi.lowername, ver=self.maxVerEachApk[avi.lowername + '.beta'])
if avi >= maxApkInfo:
logging.debug(' DON\'T NEED')
return False
logging.debug('++++ NEED IT ... (beta)')
# END: if self.needsBetaSupport(avi):
logging.debug('++++ NEED IT ...')
return True
# END: def isThisApkNeeded():
def needsBetaSupport(self, avi):
"""
def needsBetaSupport(): Returns True if beta support is needed, else False
"""
return (avi.lowername.endswith('.beta') or avi.lowername + '.beta' in self.dAllApks)
# END: def needsBetaSupport(self, avi):
# END: class ReportHelper