/
update_plugins.py
executable file
·73 lines (60 loc) · 2.7 KB
/
update_plugins.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
#! /usr/bin/python3
import fnmatch
import getpass
import optparse
import os
import re
import socket
import string
import subprocess
import sys
import tempfile
import docker_build
#-----------------------------------------------------------------------
def update_plugins(args = []):
help_text = """%prog [options] [host(s)]
Build docker images. Use -h for help."""
parser = optparse.OptionParser(usage=help_text)
# keep at optparse for 2.6. compatibility
parser.add_option("-r", "--report", action="store_true", default=False, help="Report the command to update plugins and exit without building the image")
options, arg_hosts = parser.parse_args()
original_branch = docker_build.get_current_branch()
base_jenkins_version = docker_build.compute_jenkins_base_version(original_branch, True)
if options.report:
docker_build.report_update_plugins_commands(base_jenkins_version)
quit()
existing_plugins = []
with open('plugins.txt', 'r+') as f:
for line in f.readlines():
existing_plugins.append(line.strip())
existing_plugins.sort()
get_available_updates_command = docker_build.get_available_updates_command(base_jenkins_version)
get_available_updates_command += [ '-o', 'txt' ]
available_updates = subprocess.check_output(get_available_updates_command).decode('utf-8').split('\n')
available_updates.sort()
if available_updates[0] == '':
available_updates = available_updates[1:]
print("existing - available")
old_plugins = list(set(existing_plugins) - set(available_updates))
old_plugins.sort()
print(old_plugins)
print("available - existing")
new_plugins = list(set(available_updates) - set(existing_plugins))
new_plugins.sort()
print(new_plugins)
if len(old_plugins) == len(new_plugins) and len(old_plugins) > 0:
for old_plugin, new_plugin in zip(old_plugins, new_plugins):
with open('plugins.txt', 'r+') as f:
data = f.read()
data = data.replace(old_plugin, new_plugin)
lines = data.splitlines()
lines.sort()
data = '\n'.join(lines) + '\n'
with open('plugins.txt', 'wt') as f:
f.write(data)
get_download_updates_command = docker_build.get_download_updates_command(base_jenkins_version)
subprocess.check_call(get_download_updates_command)
message = 'Use ' + new_plugin.replace('-', ' ').replace(':', ' plugin ')
subprocess.check_call(['git', 'commit', '-m', message, 'plugins.txt', os.path.join('ref', 'plugins')])
#-----------------------------------------------------------------------
if __name__ == "__main__": update_plugins(sys.argv[1:])