-
Notifications
You must be signed in to change notification settings - Fork 4
/
libgdx-updater.py
executable file
·377 lines (331 loc) · 13 KB
/
libgdx-updater.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/python
__appname__ = 'libgdx_library_updater'
__version__ = "0.91"
__author__ = "Jon Renner <[email protected]>"
__url__ = "http://github.com/jrenner/libgdx-updater"
__licence__ = "MIT"
import os, time, sys, urllib2, re, datetime, tempfile, zipfile, argparse
# error handling functions and utils
def fatal_error(msg):
print "ERROR: %s" % msg
sys.exit(1)
def warning_error(msg):
print "WARNING: %s" % msg
if not FORCE:
answer = confirm("abort? (Y/n): ")
if answer in YES:
fatal_error("USER QUIT")
def confirm(msg):
answer = raw_input(msg)
return answer.lower()
def human_time(t):
minutes = t / 60
seconds = t % 60
return "%.0fm %.1fs" % (minutes, seconds)
# constants
YES = ['y', 'ye', 'yes', '']
# for finding the time of the latest nightly build from the web page html
DATE_RE = r"[0-9]{1,2}-[A-Za-z]{3,4}-[0-9]{4}\s[0-9]+:[0-9]+"
REMOTE_DATE_FORMAT = "%d-%b-%Y %H:%M"
SUPPORTED_PLATFORMS = ['android', 'android_x86', 'desktop', 'gwt', 'ios', 'box2d', 'bullet']
CORE_LIBS = [
"gdx.jar",
"gdx-sources.jar",
]
DESKTOP_LIBS = [
"gdx-backend-lwjgl.jar",
"gdx-backend-lwjgl-natives.jar",
"gdx-natives.jar",
]
ANDROID_LIBS = [
"gdx-backend-android.jar",
"armeabi/libgdx.so",
"armeabi-v7a/libgdx.so",
]
ANDROID_X86_LIBS = [
"x86/libgdx.so",
]
GWT_LIBS = ["gdx-backend-gwt.jar"]
ROBOVM_LIBS = [
"gdx-backend-robovm.jar",
"ios/libgdx.a",
"ios/libObjectAL.a"
]
BOX2D = [
"gdx-box2d.jar",
"gdx-box2d-natives.jar",
"gdx-box2d-gwt.jar",
"armeabi/libgdx-box2d.so",
"armeabi-v7a/libgdx-box2d.so",
"x86/libgdx-box2d.so",
"gdx-box2d-sources.jar",
"gdx-box2d-gwt-sources.jar",
"ios/libgdx-box2d.a"
]
BULLET = [
"gdx-bullet.jar",
"gdx-bullet-natives.jar",
"armeabi/libgdx-bullet.so",
"armeabi-v7a/libgdx-bullet.so",
"ios/libdgx-bullet.a",
"x86/libgdx-bullet.so",
"sources/gdx-bullet-sources.jar",
]
# parse arguments
EPILOGUE_TEXT = "%s\n%s" % (__author__, __url__) + "\nUSE AT YOUR OWN RISK!"
parser = argparse.ArgumentParser(description='LibGDX Library Updater %s' % __version__, epilog=EPILOGUE_TEXT)
parser.add_argument('-d', '--directory', help='set the libgdx project/workspace directory', default=os.getcwd())
parser.add_argument('-i', '--interactive', action='store_true', help='ask for confirmation for every file', default=False)
parser.add_argument('-f', '--force-update', action='store_true', help='no confirmations, just update without checking nightly\'s datetime', default=False)
parser.add_argument('-a', '--archive', help='specify libgdx zip file to use for update', default=None)
args = parser.parse_args()
PROJECT_DIR = args.directory
INTERACTIVE = args.interactive
FORCE = args.force_update
ARCHIVE = args.archive
# mutually exclusive
if FORCE:
INTERACTIVE = False
# check the time of the latest archive on the nightlies server
def get_remote_archive_mtime():
index_page = urllib2.urlopen("http://libgdx.badlogicgames.com/nightlies/")
contents = index_page.read()
print "-- OK --"
# regex for filename
regex = r"libgdx-nightly-latest\.zip"
# add regex for anything followed by the nighlty html time format
regex += r".*%s" % DATE_RE
try:
result = re.findall(regex, contents)[0]
except IndexError as e:
print "REGEX ERROR: failed to find '%s' in:\n%s" % (regex, contents)
fatal_error("regex failure to match")
try:
mtime = re.findall(DATE_RE, result)[0]
except IndexError as e:
print "REGEX ERROR: failed to find datetime in: %s" % result
fatal_error("regex failure to match")
dtime = datetime.datetime.strptime(mtime, REMOTE_DATE_FORMAT)
return dtime
# downloads and returns a temporary file contained the latest nightly archive
def download_libgdx_zip():
libgdx = tempfile.TemporaryFile()
url = "http://libgdx.badlogicgames.com/nightlies/libgdx-nightly-latest.zip"
# testing url - don't hammer badlogic server, host the file on localhost instead
# url = "http://localhost/libgdx-nightly-latest.zip"
resp = urllib2.urlopen(url)
print "downloading file: %s" % url
total_size = resp.info().getheader('Content-Length').strip()
total_size = int(total_size)
# base 10 SI units - following Ubuntu policy because it makes sense - https://wiki.ubuntu.com/UnitsPolicy
total_size_megabytes = total_size / 1000000.0
bytes_read = 0
chunk_size = 10000 # 10kB per chunk
while True:
chunk = resp.read(chunk_size)
libgdx.write(chunk)
bytes_read += len(chunk)
bytes_read_megabytes = bytes_read / 1000000.0
percent = (bytes_read / float(total_size)) * 100
sys.stdout.write("\rprogress: {:>8}{:.2f} / {:.2f} MB ({:.0f}% complete)".format(
"", bytes_read_megabytes, total_size_megabytes, percent))
sys.stdout.flush()
if bytes_read >= total_size:
print "\nfinished download"
break
return libgdx
def update_files(libs, locations, archive):
for lib in libs:
# check to see if the lib exists in the project
if locations[lib] is None:
continue
# it's time for a dirty hack - shame on me
if lib == "gdx-sources.jar":
archive_name = "sources/gdx-sources.jar"
elif 'box2d' in lib:
archive_name = "extensions/gdx-box2d/" + lib
elif 'bullet' in lib:
archive_name = "extensions/gdx-bullet/" + lib
else:
archive_name = lib
# end dirty hack
if archive_name in archive.namelist():
if INTERACTIVE:
answer = confirm("overwrite %s? (Y/n): " % lib)
if answer not in YES:
print "skipped: %s" % lib
continue
with archive.open(archive_name, "r") as fin:
filename = os.path.basename(lib)
final_path = os.path.join(locations[lib], filename)
with open(final_path, "w") as fout:
fout.write(fin.read())
print "extracted to %s" % final_path
else:
warning_error("Couldn't find %s in .zip archive" % lib)
def run_core(locations, archive):
title("CORE")
update_files(CORE_LIBS, locations, archive)
def run_android(locations, archive):
title("ANDROID")
update_files(ANDROID_LIBS, locations, archive)
def run_android_x86(locations, archive):
title("ANDROID_X86")
update_files(ANDROID_X86_LIBS, locations, archive)
def run_desktop(locations, archive):
title("DESKTOP")
update_files(DESKTOP_LIBS, locations, archive)
def run_gwt(locations, archive):
title("GWT")
update_files(GWT_LIBS, locations, archive)
def run_ios(locations, archive):
title("IOS-ROBOVM")
update_files(ROBOVM_LIBS, locations, archive)
def run_box2d(locations, archive):
title("EXTENSION: BOX2D")
update_files(BOX2D, locations, archive)
def run_bullet(locations, archive):
title("EXTENSION: BULLET")
update_files(BULLET, locations, archive)
def search_for_lib_locations(directory):
platforms = []
search_list = CORE_LIBS + DESKTOP_LIBS + ANDROID_LIBS + ANDROID_X86_LIBS + GWT_LIBS + ROBOVM_LIBS + BOX2D + BULLET
locations = {}
for element in search_list:
locations[element] = None
for (this_dir, dirs, files) in os.walk(directory):
for element in search_list:
split_path = os.path.split(element)
path = os.path.split(split_path[0])[-1]
filename = split_path[1]
for f in files:
match = False
if filename == f:
f_dir = os.path.split(this_dir)[-1]
if path == "":
match = True
else:
if path == f_dir:
match = True
if match:
if locations[element] != None:
print "WARNING: found %s in more than one place!" % element
if not FORCE:
answer = confirm("continue? (Y/n): ")
if answer not in YES:
fatal_error("USER ABORT")
locations[element] = this_dir
found_libraries = [lib for lib, loc in locations.items() if locations[lib] != None]
if found_all_in_set(CORE_LIBS, found_libraries):
platforms.append("core")
if found_all_in_set(ANDROID_LIBS, found_libraries):
platforms.append("android")
if found_all_in_set(ANDROID_X86_LIBS, found_libraries):
platforms.append("android_x86")
if found_all_in_set(DESKTOP_LIBS, found_libraries):
platforms.append("desktop")
if found_all_in_set(GWT_LIBS, found_libraries):
platforms.append("gwt")
if found_all_in_set(ROBOVM_LIBS, found_libraries):
platforms.append("ios")
if found_any_in_set(BOX2D, found_libraries):
platforms.append("box2d")
if found_any_in_set(BULLET, found_libraries):
platforms.append("bullet")
print "WARNING - did not find the following:"
for lib, loc in locations.items():
if loc == None:
print "\t%s not found" % lib
for lib, loc in locations.items():
if loc != None:
print "found %s -> %s" % (lib, loc)
return platforms, locations
def found_all_in_set(lib_set, found_list):
for lib in lib_set:
if lib not in found_list:
return False
return True
def found_any_in_set(lib_set, found_list):
for lib in lib_set:
if lib in found_list:
return True
return False
def main():
global ARCHIVE
start_time = time.time()
print "finding local libraries in %s" % PROJECT_DIR
platforms, locations = search_for_lib_locations(PROJECT_DIR)
if "core" not in platforms:
fatal_error("did not find CORE libraries %s in project directory tree" % str(CORE_LIBS))
else:
print "found CORE libraries"
for supported in SUPPORTED_PLATFORMS:
if supported in platforms:
print "found libraries for platform: %s" % supported.upper()
else:
pass
#print "WARNING: did not find libraries for platform: %s - WILL NOT UPDATE" % supported.upper()
if ARCHIVE == None:
print "checking latest nightly..."
mtime = get_remote_archive_mtime()
print "lastest nightly from server: %s" % mtime
if not FORCE:
answer = confirm("replace local libraries with files from latest nightly?(Y/n): ")
if answer not in YES:
fatal_error("USER QUIT")
libgdx = download_libgdx_zip()
else:
if not os.path.exists(ARCHIVE):
fatal_error("archive file not found: %s" % ARCHIVE)
if os.path.isdir(ARCHIVE):
selected = select_archive_from_dir(ARCHIVE)
if selected == None:
fatal_error("no archive chosen to update with, check supplied argument: '%s'" % ARCHIVE)
else:
ARCHIVE = selected
if not FORCE:
answer = confirm("replace local libraries with files from '%s'?(Y/n): " % os.path.basename(ARCHIVE))
if answer not in YES:
fatal_error("USER QUIT")
libgdx = open(ARCHIVE, "r")
with zipfile.ZipFile(libgdx) as archive:
if "core" in platforms:
run_core(locations, archive)
if "desktop" in platforms:
run_desktop(locations, archive)
if "android" in platforms:
run_android(locations, archive)
if "gwt" in platforms:
run_gwt(locations, archive)
if "ios" in platforms:
run_ios(locations, archive)
if "box2d" in platforms:
run_box2d(locations, archive)
if "bullet" in platforms:
run_bullet(locations,archive)
duration = time.time() - start_time
print "finished updates in %s" % human_time(duration)
libgdx.close()
def find_zips(path):
zips = []
for item in os.listdir(path):
pieces = item.split(".")
if len(pieces) > 0:
if (pieces[-1] == "zip"):
zips.append(item)
return zips
def select_archive_from_dir(archive_dir):
zips = find_zips(archive_dir)
if len(zips) == 0:
return None
for z in zips:
full_path = os.path.join(archive_dir, z)
answer = confirm("use archive '%s'? (Y/n): " % full_path)
if answer in YES:
return full_path
return None
def title(text):
dashes = "-" * 10
print dashes + " %s " % text + dashes
if __name__ == "__main__":
main()