forked from dosoudil/maven-repository-builder
-
Notifications
You must be signed in to change notification settings - Fork 12
/
filter.py
266 lines (231 loc) · 12.9 KB
/
filter.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
import copy
import logging
from multiprocessing.pool import ThreadPool
import maven_repo_util
from maven_artifact import MavenArtifact
class Filter:
def __init__(self, config):
self.config = config
def filter(self, artifactList, threadnum):
"""
Filter artifactList removing excluded GAVs, duplicates and GAVs that exists in
excluded repositories.
:param artifactList: artifactList from ArtifactListBuilder.
:returns: filtered artifactList.
"""
if self.config.excludedGAVs:
artifactList = self._filterExcludedGAVs(artifactList)
if self.config.excludedTypes:
artifactList = self._filterExcludedTypes(artifactList)
artifactList = self._filterDuplicates(artifactList)
if self.config.singleVersion:
artifactList = self._filterMultipleVersions(artifactList)
if self.config.excludedRepositories:
artifactList = self._filterExcludedRepositories(artifactList, threadnum)
return artifactList
def _filterExcludedGAVs(self, artifactList):
"""
Filter artifactList removing specified GAVs.
:param artifactList: artifactList to be filtered.
:returns: artifactList without artifacts that matched specified GAVs.
"""
logging.debug("Filtering artifacts with excluded GAVs.")
regExps = maven_repo_util.getRegExpsFromStrings(self.config.excludedGAVs)
gavRegExps = []
gatcvRegExps = []
for regExp in regExps:
if regExp.pattern.count(":") > 2:
gatcvRegExps.append(regExp)
else:
gavRegExps.append(regExp)
for ga in artifactList.keys():
for priority in artifactList[ga].keys():
for version in artifactList[ga][priority].keys():
gav = "%s:%s" % (ga, version)
if maven_repo_util.somethingMatch(gavRegExps, gav):
logging.debug("Dropping GAV %s:%s from priority %i because it matches an excluded "
"GAV pattern.", ga, version, priority)
del artifactList[ga][priority][version]
else:
artSpec = artifactList[ga][priority][version]
for artType in copy.deepcopy(artSpec.artTypes.keys()):
at = artSpec.artTypes[artType]
for classifier in copy.deepcopy(at.classifiers):
if classifier:
gatcv = "%s:%s:%s:%s" % (ga, artType, classifier, version)
else:
gatcv = "%s:%s:%s" % (ga, artType, version)
if maven_repo_util.somethingMatch(gatcvRegExps, gatcv):
logging.debug("Dropping GATCV %s from priority %i because it matches an excluded "
"GAV pattern.", gatcv, priority)
at.classifiers.remove(classifier)
if not at.classifiers:
logging.debug("Dropping GATV %s:%s:%s from priority %i because of no classifiers left.",
ga, artType, version, priority)
del artSpec.artTypes[artType]
if not artSpec.containsMain():
logging.debug("Dropping GAV %s:%s from priority %i because of no main artifact left.",
ga, version, priority)
del artifactList[ga][priority][version]
if not artifactList[ga][priority]:
logging.debug("Dropping GA %s from priority %i because of no version left.", ga, priority)
del artifactList[ga][priority]
if not artifactList[ga]:
logging.debug("Dropping GA %s because of no priority left.", ga)
del artifactList[ga]
return artifactList
def _filterExcludedTypes(self, artifactList):
'''
Filter artifactList removing GAVs with specified main types only, otherwise keeping GAVs with
not-excluded artifact types only.
:param artifactList: artifactList to be filtered.
:param exclTypes: list of excluded types
:returns: artifactList without artifacts that matched specified types and had no other main types.
'''
logging.debug("Filtering artifacts with excluded types.")
regExps = maven_repo_util.getRegExpsFromStrings(self.config.gatcvWhitelist)
exclTypes = self.config.excludedTypes
for ga in artifactList.keys():
for priority in artifactList[ga].keys():
for version in artifactList[ga][priority].keys():
artSpec = artifactList[ga][priority][version]
for artType in list(artSpec.artTypes.keys()):
if artType in exclTypes:
artTypeObj = artSpec.artTypes[artType]
classifiers = artTypeObj.classifiers
(groupId, artifactId) = ga.split(':')
for classifier in list(classifiers):
art = MavenArtifact(groupId, artifactId, artType, version, classifier)
gatcv = art.getGATCV()
if not maven_repo_util.somethingMatch(regExps, gatcv):
logging.debug("Dropping classifier \"%s\" of %s:%s:%s from priority %i because of "
"excluded type.", classifier, ga, artType, version, priority)
classifiers.remove(classifier)
else:
logging.debug("Skipping drop of %s:%s:%s:%s from priority %i because it matches a "
"whitelist pattern.", ga, artType, classifier, version, priority)
if not classifiers:
logging.debug("Dropping %s:%s:%s from priority %i because of no classifier left.", ga,
artType, version, priority)
del(artSpec.artTypes[artType])
noMain = True
for artType in artSpec.artTypes.keys():
artTypeObj = artSpec.artTypes[artType]
if artTypeObj.mainType:
noMain = False
break
if not artSpec.artTypes or noMain:
if noMain:
logging.debug("Dropping GAV %s:%s from priority %i because of no main artifact left.",
ga, version, priority)
else:
logging.debug("Dropping GAV %s:%s from priority %i because of no artifact type left.",
ga, version, priority)
del artifactList[ga][priority][version]
if not artifactList[ga][priority]:
logging.debug("Dropping GA %s from priority %i because of no version left.", ga, priority)
del artifactList[ga][priority]
if not artifactList[ga]:
logging.debug("Dropping GA %s because of no priority left.", ga)
del artifactList[ga]
return artifactList
def _filterExcludedRepositories(self, artifactList, threadnum):
"""
Filter artifactList removing artifacts existing in specified repositories.
:param artifactList: artifactList to be filtered.
:returns: artifactList without artifacts that exists in specified repositories.
"""
logging.debug("Filtering artifacts contained in excluded repositories.")
pool = ThreadPool(threadnum)
# Contains artifact to be removed
delArtifacts = []
for ga in artifactList.keys():
groupId = ga.split(':')[0]
artifactId = ga.split(':')[1]
for priority in artifactList[ga].keys():
for version in artifactList[ga][priority].keys():
artifact = MavenArtifact(groupId, artifactId, "pom", version)
pool.apply_async(
_artifactInRepos,
[self.config.excludedRepositories, artifact, priority, delArtifacts]
)
# Close the pool and wait for the workers to finnish
pool.close()
pool.join()
for artifact, priority in delArtifacts:
ga = artifact.getGA()
logging.debug("Dropping GAV %s:%s from priority %i because it was found in an excluded repository.",
ga, artifact.version, priority)
del artifactList[ga][priority][artifact.version]
if not artifactList[ga][priority]:
logging.debug("Dropping GA %s from priority %i because of no version left.", ga, priority)
del artifactList[ga][priority]
if not artifactList[ga]:
logging.debug("Dropping GA %s because of no priority left.", ga)
del artifactList[ga]
return artifactList
def _filterDuplicates(self, artifactList):
"""
Filter artifactList removing duplicate artifacts.
:param artifactList: artifactList to be filtered.
:returns: artifactList without duplicate artifacts from lower priorities.
"""
logging.debug("Filtering duplicate artifacts.")
for ga in artifactList.keys():
for priority in sorted(artifactList[ga].keys()):
for version in artifactList[ga][priority].keys():
for pr in artifactList[ga].keys():
if pr <= priority:
continue
if version in artifactList[ga][pr]:
logging.debug("Dropping GAV %s:%s from priority %i because its duplicate was found in "
"priority %s.", ga, version, pr, priority)
if len(artifactList[ga][pr][version].paths):
artifactList[ga][priority][version].paths.extend(artifactList[ga][pr][version].paths)
del artifactList[ga][pr][version]
if not artifactList[ga][priority]:
logging.debug("Dropping GA %s from priority %i because of no version left.", ga, priority)
del artifactList[ga][priority]
if not artifactList[ga]:
logging.debug("Dropping GA %s because of no priority left.", ga)
del artifactList[ga]
return artifactList
def _filterMultipleVersions(self, artifactList):
logging.debug("Filtering multi-version artifacts to have just a single version.")
regExps = maven_repo_util.getRegExpsFromStrings(self.config.multiVersionGAs, False)
for ga in sorted(artifactList.keys()):
if maven_repo_util.somethingMatch(regExps, ga):
continue
# Gather all priorities
priorities = sorted(artifactList[ga].keys())
priority = priorities[0]
# Gather all versions
versions = list(artifactList[ga][priority].keys())
if len(versions) > 1: # list of 1 is sorted by definition
versions = maven_repo_util._sortVersionsWithAtlas(versions)
# Remove version, priorities and gats from artifactList as necessary
for version in versions[1:]:
logging.debug("Dropping GAV %s:%s from priority %i because only single version is allowed.", ga,
version, priority)
del artifactList[ga][priority][version]
for p in priorities[1:]:
logging.debug("Dropping GA %s from priority %i because of no version left.", ga, p)
del artifactList[ga][p]
if not artifactList[ga]:
logging.debug("Dropping GA %s because of no priority left.", ga)
del artifactList[ga]
return artifactList
def _artifactInRepos(repositories, artifact, priority, artifacts):
"""
Checks if artifact is available in one of the repositories, if so, appends
it with priority in list of pairs - artifacts. Used for multi-threading.
:param repositories: list of repository urls
:param artifact: searched MavenArtifact
:param priority: value of dictionary artifacts
:param artifacts: list with (artifact, priority) tuples
"""
for repoUrl in repositories:
if maven_repo_util.gavExists(repoUrl, artifact):
#Critical section?
artifacts.append((artifact, priority))
break