-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin-updater.py
More file actions
181 lines (143 loc) · 5.88 KB
/
plugin-updater.py
File metadata and controls
181 lines (143 loc) · 5.88 KB
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
#!/usr/bin/env python3
# Copyright 2021 Jakub Kukielka
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import yaml
import click
import signal
import shutil
import requests
from glob import glob
from time import sleep
from bs4 import BeautifulSoup
from rich.console import Console
from rich.progress import Progress
# pretty text in terminal
console = Console()
class Downloader:
def __init__(self, config):
self.downloads = []
self.pluginDir = config["plugin-dir"]
self.oldPluginDir = config["old-plugin-dir"]
def matchAll(self, dls, link):
for dl in dls:
if "match" in link:
if any([m in dl for m in link["match"]]):
if "unmatch" in link:
if any([not u in dl for u in link["unmatch"]]):
yield dl
else:
yield dl
else:
yield dl
def bukkit(self, link):
page = requests.get(link["url"] + "/files/latest")
self.downloads.append(page.url)
def jenkins(self, link):
page = requests.get(link["url"])
soup = BeautifulSoup(page.content, "html.parser")
filelist = soup.find("table", {"class": "fileList"})
results = filelist.findAll(lambda tag: tag.name == "a" and \
"href" in tag.attrs and \
not "fingerprint" in tag.attrs["href"] and \
not "view" in tag.attrs["href"])
# get all download links
dls = []
for result in results:
dls.append(result.attrs["href"])
for matching in self.matchAll(dls, link):
self.downloads.append(link["url"] + "/" + matching)
def github(self, link):
page = requests.get(link["url"])
soup = BeautifulSoup(page.content, "html.parser")
results = soup.findAll(lambda tag: tag.name == "a" and \
"href" in tag.attrs and \
"/releases/download/" in tag.attrs["href"])
# get all download links
dls = []
for result in results:
dls.append(result.attrs["href"])
# we only want download links for the latest version
versions = []
for dl in dls:
versions.append("/".join(dl.split("/")[:-1]))
versions.sort(reverse=True)
dls = [ dl for dl in dls if versions[0] in dl ]
for matching in self.matchAll(dls, link):
self.downloads.append("https://github.com" + matching)
def downloadFile(self, url):
localFile = url.split('/')[-1]
downFile = requests.get(url, stream=True)
with open(os.path.join(self.pluginDir, localFile), "wb") as f:
shutil.copyfileobj(downFile.raw, f)
def downloadAll(self, progress, task, download = True):
# first move all the old .jar files from the plugins folder to a safe place
# i.e. old-plugin-dir
if download:
if not os.path.isdir(self.pluginDir):
progress.console.print("plugin-dir does not exist! please verify plugins.yml!", style="bold red")
exit()
oldPluginFiles = glob(os.path.join(self.pluginDir, "*.jar"))
if os.path.isdir(self.oldPluginDir):
for oldPluginFile in oldPluginFiles:
movefrom = oldPluginFile
moveto = os.path.join(self.oldPluginDir, os.path.split(oldPluginFile)[-1:][0])
progress.console.print("moving " + movefrom + " to " + moveto + "...", style="italic blue")
os.rename(movefrom, moveto)
else:
progress.console.print("old-plugin-dir does not exist! please verify plugins.yml!", style="bold red")
exit()
for downloadURL in self.downloads:
if download:
self.downloadFile(downloadURL)
progress.advance(task)
else:
progress.console.print(downloadURL)
@click.command()
@click.option("--config", default="plugins.yml", help="path to plugins.yml or compatible config file")
@click.option("--download/--urls",
default=True,
help="automatically download (default behaviour) or only return plugin URLs")
def updatePlugins(config, download):
with open(config, "r") as stream:
try:
data = yaml.safe_load(stream)
downloader = Downloader(data["config"])
# find the total number of URLs
totalURLs = 0
for key in data["downloads"]:
for item in data["downloads"][key]:
totalURLs += 1
with Progress() as progress:
urlTask = progress.add_task("[green]Getting plugin URLs...", total=totalURLs)
for key in data["downloads"]:
if key == "bukkit":
for value in data["downloads"][key]:
progress.console.print(f"Getting URL from {value['url']} ...", style="blue")
downloader.bukkit(value)
progress.advance(urlTask)
if key == "jenkins":
for value in data["downloads"][key]:
progress.console.print(f"Getting URL from {value['url']} ...", style="blue")
downloader.jenkins(value)
progress.advance(urlTask)
if key == "github":
for value in data["downloads"][key]:
progress.console.print(f"Getting URL from {value['url']} ...", style="blue")
downloader.github(value)
progress.advance(urlTask)
downloadTask = progress.add_task("[green]Downloading plugins...", total=totalURLs)
downloader.downloadAll(progress, downloadTask, download=download)
progress.console.print(f"done \o/", style="green bold")
except yaml.YAMLError as exc:
print(exc)
if __name__ == "__main__":
updatePlugins()