forked from dosoudil/maven-repository-builder
-
Notifications
You must be signed in to change notification settings - Fork 12
/
configuration.py
290 lines (248 loc) · 12.4 KB
/
configuration.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import json
import logging
import os
import sys
import maven_repo_util
class Configuration:
"""
Class holding Artifact List Generator configuration. It can be loaded
from a json configuration file.
"""
ALL_CLASSIFIERS_VALUE = "__all__"
singleVersion = None
artifactSources = []
excludedGAVs = []
excludedRepositories = []
excludedTypes = []
multiVersionGAs = []
_configFiles = set()
addClassifiers = set()
gatcvWhitelist = []
useCache = True
analyze = False
def load(self, opts):
"""
Load configuration from command line arguments using requested config file.
:param opts: options parsed by an OptionParser
"""
if opts.config is None:
logging.error('You must specify a config file')
sys.exit(1)
self.addClassifiers = self._parseClassifiers(opts.classifiers)
self.excludedTypes = opts.excludedtypes.split(':')
if opts.whitelist:
self.gatcvWhitelist = maven_repo_util.loadArtifactFile(opts.whitelist)
if hasattr(opts, "cache"):
self.useCache = opts.cache
self.analyze = (not opts.reportdir == None)
self.loadFromFile(opts.config)
def create(self, opts, args):
"""
Creates configuration from command line parameters like url, artifact list files etc.
:param opts: options parsed by an OptionParser
"""
self.singleVersion = False
includedGatcvs = []
for filename in args:
includedGatcvs.extend(maven_repo_util.loadArtifactFile(filename))
self.artifactSources = [{
"type": "repository",
"repo-url": opts.url.split(","),
"included-gatcvs": includedGatcvs
}]
self._setDefaults()
self._validate()
self.addClassifiers = self._parseClassifiers(opts.classifiers)
self.excludedTypes = opts.excludedtypes.split(':')
if opts.whitelist:
# TODO read the file properly (skip comments, enable regexps, ...)
self.gatcvWhitelist = maven_repo_util.loadArtifactFile(opts.whitelist)
if hasattr(opts, "cache"):
self.useCache = opts.cache
def loadFromFile(self, filename):
self._loadFromFile(filename)
self._setDefaults()
self._validate()
def isAllClassifiers(self):
"""
Checks if all available classifiers should be downloaded.
"""
return self.addClassifiers == self.ALL_CLASSIFIERS_VALUE
def _setDefaults(self):
if self.singleVersion is None:
self.singleVersion = True
for source in self.artifactSources:
if source['type'] == 'dependency-list':
if 'recursive' not in source:
source['recursive'] = True
if 'include-scope' not in source:
source['include-scope'] = None
if 'skip-missing' not in source:
source['skip-missing'] = True
elif source['type'] == 'dependency-graph':
if 'wsid' not in source:
source['wsid'] = None
if 'excluded-sources' not in source:
source['excluded-sources'] = []
if 'excluded-subgraphs' not in source:
source['excluded-subgraphs'] = []
if 'preset' not in source:
source['preset'] = None
if 'mutator' not in source:
source['mutator'] = None
if 'patcher-ids' not in source:
source['patcher-ids'] = []
if 'injected-boms' not in source:
source['injected-boms'] = []
elif source['type'] == 'repository':
if 'included-gav-patterns' not in source:
source['included-gav-patterns'] = []
def _validate(self):
valid = True
if self.singleVersion is None:
logging.error("Option single-version not set in configuration file.")
valid = False
if not self.artifactSources:
logging.error("No artifact-sources set in configuration file.")
valid = False
else:
for source in self.artifactSources:
if source['type'] == 'dependency-graph':
if 'carto-url' not in source:
logging.error("No carto-url specified for source with type dependency-graph.")
valid = False
if 'source-key' not in source:
logging.error("No source-key specified for source with type dependency-graph.")
valid = False
if not len(source['top-level-gavs']):
logging.error("No top-level GAV specified for source with type dependency-graph.")
valid = False
if not valid:
sys.exit(1)
def _loadFromFile(self, filename, rewrite=True):
""" Load configuration from json config file. """
logging.debug("Loading configuration file %s", filename)
if filename in self._configFiles:
raise Exception("Config file '%s' is already included." % filename +
" Check your config files for circular inclusions.")
self._configFiles.add(filename)
data = json.load(open(filename))
filePath = os.path.dirname(filename)
if filePath:
filePath += '/'
if 'include-high-priority' in data and data['include-high-priority']:
inclFile = self._getRelativeFilename(data['include-high-priority'], filePath)
self._loadFromFile(inclFile, True)
if (rewrite or self.singleVersion is None) and 'single-version' in data:
self.singleVersion = maven_repo_util.str2bool(data['single-version'])
if 'artifact-sources' in data:
self._loadArtifactSources(data['artifact-sources'], filePath)
if 'excluded-gav-patterns-ref' in data:
for filename in data['excluded-gav-patterns-ref']:
relFilename = self._getRelativeFilename(filename, filePath)
self.excludedGAVs.extend(maven_repo_util.loadFlatFile(relFilename))
if 'excluded-repositories' in data:
self.excludedRepositories.extend(data['excluded-repositories'])
if 'multi-version-ga-patterns-ref' in data:
for filename in data['multi-version-ga-patterns-ref']:
relFilename = self._getRelativeFilename(filename, filePath)
self.multiVersionGAs.extend(maven_repo_util.loadFlatFile(relFilename))
if 'multi-version-ga-patterns' in data:
self.multiVersionGAs.extend(data['multi-version-ga-patterns'])
if 'include-low-priority' in data and data['include-low-priority']:
inclFile = self._getRelativeFilename(data['include-low-priority'], filePath)
self._loadFromFile(inclFile, False)
def _loadArtifactSources(self, artifactSources, filePath):
for source in artifactSources:
if not 'type' in source:
logging.error("Source doesn't have type.\n %s", str(source))
sys.exit(1)
if source['type'] == 'mead-tag':
source['included-gav-patterns'] = self._loadFlatFileBySourceParameter(source,
'included-gav-patterns-ref', filePath)
elif source['type'] == 'dependency-list':
if 'recursive' in source:
source['recursive'] = maven_repo_util.str2bool(source['recursive'])
if 'skip-missing' in source:
source['skip-missing'] = maven_repo_util.str2bool(source['skip-missing'])
source['repo-url'] = self._getRepoUrl(source)
source['top-level-gavs'] = self._loadFlatFileBySourceParameter(source, 'top-level-gavs-ref',
filePath)
elif source['type'] == 'dependency-graph':
source['top-level-gavs'] = self._loadFlatFileBySourceParameter(source, 'top-level-gavs-ref',
filePath)
excluded_subgraphs = self._loadFlatFileBySourceParameter(source, 'excluded-subgraphs-ref',
filePath)
if 'excluded-subgraphs' in source:
if isinstance(source['excluded-subgraphs'], basestring):
excluded_subgraphs.append(source['excluded-subgraphs'])
else:
excluded_subgraphs.extend(source['excluded-subgraphs'])
source['excluded-subgraphs'] = excluded_subgraphs
elif source['type'] == 'repository':
source['repo-url'] = self._getRepoUrl(source)
source['included-gav-patterns'] = self._loadFlatFileBySourceParameter(source,
'included-gav-patterns-ref',
filePath)
source['included-gatcvs'] = self._loadArtifactFileBySourceParameter(source, 'included-gatcvs-ref',
filePath)
source["excludedGAVs"] = []
if 'excluded-gav-patterns-ref' in source:
for filename in source['excluded-gav-patterns-ref']:
relFilename = self._getRelativeFilename(filename, filePath)
source["excludedGAVs"].extend(maven_repo_util.loadFlatFile(relFilename))
self.artifactSources.append(source)
def _loadFlatFileBySourceParameter(self, source, parameter, filePath):
if parameter in source:
relFilename = self._getRelativeFilename(source[parameter], filePath)
return maven_repo_util.loadFlatFile(relFilename)
else:
return []
def _loadArtifactFileBySourceParameter(self, source, parameter, filePath):
if parameter in source:
relFilename = self._getRelativeFilename(source[parameter], filePath)
return maven_repo_util.loadArtifactFile(relFilename)
else:
return []
def _getRelativeFilename(self, filename, path):
"""Checks, if the given filename has absolute path, and if not, it preppends to it given path."""
if os.path.isabs(filename):
return filename
else:
return path + filename
def _getRepoUrl(self, source):
if not 'repo-url' in source:
logging.error("Source %s must have specified repo-url.", source['type'])
sys.exit(1)
if isinstance(source['repo-url'], basestring):
return [source['repo-url']]
else:
return source['repo-url']
def _parseClassifiers(self, classifiersString):
"""
Parses classifiers and types from command-line argument value. The result is list of dictionaries. Each
dictionary contains classifier value under "classifier" key and type under "type" key. If no type is specified,
then the value "jar" is used for key "type".
:param classifiersString: comma-separated list of classifiers with an optional prepended type separated by colon,
if no type specified, then the default type ("jar") is used
:returns: list of dictionaries with structure e.g. {"classifiers": "sources", "type": "jar"}
"""
if not classifiersString:
result = []
elif classifiersString == self.ALL_CLASSIFIERS_VALUE:
result = self.ALL_CLASSIFIERS_VALUE
else:
classifiers = classifiersString.split(",")
result = []
if len(classifiers) == 1 and classifiers.count(":") > 1:
for classifier in classifiersString.split(":"):
result.append({"classifier": classifier, "type": "jar"})
else:
for classifier in classifiers:
colonCount = classifier.count(":")
if colonCount == 0:
result.append({"classifier": classifier, "type": "jar"})
elif colonCount == 1:
parts = classifier.split(":")
result.append({"classifier": parts[1], "type": parts[0]})
return result