-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport.py
201 lines (170 loc) · 6.47 KB
/
export.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
#Imports
import json
import urllib2
import re
import os
import errno
import yaml
import sys
# from http://stackoverflow.com/a/3382869
def require_dir(path):
try:
os.makedirs(path)
except OSError, exc:
if exc.errno != errno.EEXIST:
raise
# http://stackoverflow.com/questions/1883980/find-the-nth-occurrence-of-substring-in-a-string
def findnth(haystack, needle, n):
start = haystack.find(needle)
while start >= 0 and n > 1:
start = haystack.find(needle, start+len(needle))
n -= 1
return start
# Get the plugin details from the slug
def parse_plugin_details(slug, version):
page = urllib2.urlopen("http://api.bukget.org/3/plugins/bukkit/" + slug + "/" + version)
return json.load(page)
# Download the plugin jar from the given website and save it to the file
def download_plugin(filename, url_source):
# Make sure filename is not empty
if filename and not os.path.exists(filename):
f = urllib2.urlopen(url_source)
data = f.read()
with open(filename, "wb") as code:
code.write(data)
# Validate the given plugin item
def validate_plugin_item(item):
valid = True
if not "versions" in item:
print "!!! No versions"
valid = False
elif not "main" in item:
print "!!! missing main"
valid = False
return valid
# Export the file to maven repository
def export_to_maven(repo_url, repo_id, group_id, artifact_id, version, filename):
command = "mvn deploy:deploy-file \
-Durl=" + repo_url + " \
-DrepositoryId=" + repo_id + " \
-DgroupId=" + group_id + " \
-DartifactId=" + artifact_id + " \
-Dversion=" + version +" \
-Dpackaging=jar \
-Dfile=\"" + os.path.abspath(filename) +"\""
return os.system(command)
# Main class
class MainClass:
# load configuration from config.yml
def load_configuration(self):
conf = yaml.load(open('config.yml'))
repo_conf = conf.get("repository", "")
if repo_conf:
self.repository_id = repo_conf.get("id", self.repository_id)
self.repository_url = repo_conf.get("url", self.repository_url)
self.short_main_length = conf.get("main_class_length", self.short_main_length)
plugin_list = conf.get("plugins", "")
if plugin_list:
for plugin in plugin_list:
self.target_plugins.append(plugin.lower())
self.release_flag = conf.get("release", self.release_flag).lower()
# load default configuration
def load_defaults(self):
self.repository_id = "internal"
self.repository_url = "UNKNOWN"
self.short_main_length = 3
self.target_plugins = []
self.release_flag = "latest"
def validate_configuration(self):
#Check that the shortened length is not less than or equal to 0
if self.short_main_length <= 0:
self.short_main_length = 3
# run function
def run(self):
# Get configuration options
self.load_defaults()
self.load_configuration()
self.validate_configuration()
if len(self.target_plugins) == 0:
print "!!! No target plugins to download..."
sys.exit()
# Get the downloads directory
# TODO configurable?
downloads_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "downloads")
require_dir(downloads_dir)
# Using BukGet
# API: http://bukget.org/pages/docs/API3.html
#Get the plugin slugs and names
slug_page = urllib2.urlopen("http://api.bukget.org/3/plugins/bukkit?fields=slug,plugin_name")
slug_json = json.load(slug_page)
# Iterate over the slugs
for slug_item in slug_json:
# Check if we're done
if len(self.target_plugins) == 0:
print "=== Done"
break
slug = slug_item["slug"]
plugin_name = slug_item.get('plugin_name', "UNKNOWN")
plugin_website = slug_item.get('webpage',"")
# Check if this is a plugin we want
found_slug = False
found_name = False
if plugin_name.lower() in self.target_plugins:
self.target_plugins.remove(plugin_name.lower())
found_name = True
elif slug.lower() in self.target_plugins:
self.target_plugins.remove(slug.lower())
found_slug = True
if not found_name and not found_slug:
continue
if found_slug:
print "=== Attempting: " + slug + " (" + self.release_flag + ")"
elif found_name:
print "=== Attempting: " + plugin_name + " (" + self.release_flag + ")"
#TODO from config, allow specifiers for latest, stable, beta, alpha versions
#Use API to get these versions
try:
plugin_details = parse_plugin_details(slug, self.release_flag)
# Validate plugin
if validate_plugin_item(plugin_details):
main_class_path = plugin_details["main"]
short_cutoff = findnth(main_class_path, ".", self.short_main_length)
short_main = main_class_path
# Need to check in case of ridiculously short main class paths
if short_cutoff > 0:
short_main = main_class_path[:short_cutoff]
# Grab version(s)
for version_details in plugin_details["versions"]:
download_link = version_details["download"]
filename = version_details["filename"]
version = version_details["version"]
extension = os.path.splitext(filename)[1]
if extension != ".jar":
print "!!! Skipping " + plugin_name + " v" + version + " because it is not a jar..."
continue
print filename + " : " + version + " : " + download_link
# download file
download_plugin(filename, download_link)
# export jar to maven
export_to_maven(self.repository_url, self.repository_id, short_main, plugin_name, version, filename)
# delete downloaded file
os.remove(filename)
else:
if found_slug:
self.target_plugins.append(slug.lower())
elif found_name:
self.target_plugins.append(plugin_name.lower())
print "!!! Errors in validating '" + slug + "' | '" + plugin_name + "' @ " + plugin_website
except urllib2.HTTPError, err:
print "!!! HTTP error: " + str(err.code) + " : " + err.reason
# Notify user of any plugins we could not download
if len(self.target_plugins) != 0:
print "!!! Could not find / export the following plugins: "
for plugin in self.target_plugins:
print " - " + plugin
# Main function
def main():
mc = MainClass()
mc.run()
if __name__ == "__main__":
main()