-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins_discover.py
executable file
·51 lines (43 loc) · 1.61 KB
/
plugins_discover.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
#!/usr/bin/env python
import zipfile
import io
import re
import argparse
def discover_plugins(jenkins_war):
"""
discover_plugins
extracts metadata for each plugin included in jenkins.war
:param jenkins_war: path to jenkins.war
:returns: a list of dicts for each plugin
"""
plugins = []
with zipfile.ZipFile(jenkins_war) as j_war:
for pn_hpi in j_war.infolist():
regex = r'^WEB-INF/(detached-plugins|plugins)/(.*).hpi$'
if re.match(regex, pn_hpi.filename):
pn_data = j_war.read(pn_hpi.filename)
pn_stm = io.BytesIO(pn_data)
with zipfile.ZipFile(pn_stm) as pn_zip:
meta_inf = pn_zip.read('META-INF/MANIFEST.MF')
plugin = {}
for line in meta_inf.split('\n'):
kv_rx = '^(?P<key>.*):(?P<value>.*?)$'
res = re.match(kv_rx, line)
if res:
key = res.group('key')
value = res.group('value')
plugin[key.strip()] = value.strip()
plugins.append(plugin)
return plugins
def main():
"""main"""
parser = argparse.ArgumentParser()
parser.add_argument('--jenkins_war', required=True,
help='path to jenkins.war')
args = parser.parse_args()
plugins = discover_plugins(args.jenkins_war)
for plugin in plugins:
print('%s : %s' % (plugin.get('Short-Name'),
plugin.get('Plugin-Version')))
if __name__ == '__main__':
main()