1
1
#!/usr/bin/env python
2
- #coding=utf-8
2
+
3
+ # coding=utf-8
3
4
#
4
5
# ./download-deps.py
5
6
#
42
43
import sys
43
44
import traceback
44
45
import distutils
45
- import fileinput
46
46
import json
47
47
48
48
from optparse import OptionParser
49
49
from time import time
50
50
from time import sleep
51
51
from sys import stdout
52
- from distutils .errors import DistutilsError
53
52
from distutils .dir_util import copy_tree , remove_tree
54
53
55
54
@@ -92,10 +91,16 @@ def __init__(self, workpath, config_path, version_path, remote_version_key=None)
92
91
except :
93
92
self ._move_dirs = None
94
93
self ._filename = self ._current_version + '.zip'
95
- self ._url = data ["repo_parent" ] + self ._repo_name + '/archive/' + self ._filename
94
+ self ._url = data ["repo_parent" ] + \
95
+ self ._repo_name + '/archive/' + self ._filename
96
96
self ._zip_file_size = int (data ["zip_file_size" ])
97
97
# 'v' letter was swallowed by github, so we need to substring it from the 2nd letter
98
- self ._extracted_folder_name = os .path .join (self ._workpath , self ._repo_name + '-' + self ._current_version [1 :])
98
+ if self ._current_version [0 ] == 'v' :
99
+ self ._extracted_folder_name = os .path .join (
100
+ self ._workpath , self ._repo_name + '-' + self ._current_version [1 :])
101
+ else :
102
+ self ._extracted_folder_name = os .path .join (
103
+ self ._workpath , self ._repo_name + '-' + self ._current_version )
99
104
100
105
try :
101
106
data = self .load_json_file (version_path )
@@ -107,10 +112,10 @@ def __init__(self, workpath, config_path, version_path, remote_version_key=None)
107
112
print ("==> version file doesn't exist" )
108
113
109
114
def get_input_value (self , prompt ):
110
- if sys .version_info [0 ] > 2 :
111
- ret = input (prompt )
112
- else :
115
+ if (python_2 ):
113
116
ret = raw_input (prompt )
117
+ else :
118
+ ret = input (prompt )
114
119
ret .rstrip (" \t " )
115
120
return ret
116
121
@@ -120,27 +125,39 @@ def download_file(self):
120
125
os .remove (self ._filename )
121
126
except OSError :
122
127
pass
123
- print ("==> Ready to download '%s' from '%s'" % (self ._filename , self ._url ))
124
- import urllib2
128
+ print ("==> Ready to download '%s' from '%s'" %
129
+ (self ._filename , self ._url ))
130
+ if (python_2 ):
131
+ import urllib2 as urllib
132
+ else :
133
+ import urllib .request as urllib
125
134
try :
126
- u = urllib2 .urlopen (self ._url )
127
- except urllib2 . HTTPError as e :
135
+ u = urllib .urlopen (self ._url )
136
+ except Exception as e :
128
137
if e .code == 404 :
129
- print ("==> Error: Could not find the file from url: '%s'" % (self ._url ))
130
- print ("==> Http request failed, error code: " + str (e .code ) + ", reason: " + e .read ())
138
+ print ("==> Error: Could not find the file from url: '%s'" %
139
+ (self ._url ))
140
+ print ("==> Http request failed, error code: " +
141
+ str (e .code ) + ", reason: " + str (e .read ()))
131
142
sys .exit (1 )
132
143
133
144
f = open (self ._filename , 'wb' )
134
145
meta = u .info ()
135
- content_len = meta .getheaders ("Content-Length" )
146
+ content_len = 0
147
+ if (python_2 ):
148
+ content_len = meta .getheaders ("Content-Length" )
149
+ else :
150
+ content_len = meta ['Content-Length' ]
151
+
136
152
file_size = 0
137
153
if content_len and len (content_len ) > 0 :
138
154
file_size = int (content_len [0 ])
139
155
else :
140
156
# github server may not reponse a header information which contains `Content-Length`,
141
157
# therefore, the size needs to be written hardcode here. While server doesn't return
142
158
# `Content-Length`, use it instead
143
- print ("==> WARNING: Couldn't grab the file size from remote, use 'zip_file_size' section in '%s'" % self ._config_path )
159
+ print ("==> WARNING: Couldn't grab the file size from remote, use 'zip_file_size' section in '%s'" %
160
+ self ._config_path )
144
161
file_size = self ._zip_file_size
145
162
146
163
print ("==> Start to download, please wait ..." )
@@ -165,9 +182,11 @@ def download_file(self):
165
182
speed = block_size_per_second / (new_time - old_time ) / 1000.0
166
183
if file_size != 0 :
167
184
percent = file_size_dl * 100. / file_size
168
- status = r"Downloaded: %6dK / Total: %dK, Percent: %3.2f%%, Speed: %6.2f KB/S " % (file_size_dl / 1000 , file_size / 1000 , percent , speed )
185
+ status = r"Downloaded: %6dK / Total: %dK, Percent: %3.2f%%, Speed: %6.2f KB/S " % (
186
+ file_size_dl / 1000 , file_size / 1000 , percent , speed )
169
187
else :
170
- status = r"Downloaded: %6dK, Speed: %6.2f KB/S " % (file_size_dl / 1000 , speed )
188
+ status = r"Downloaded: %6dK, Speed: %6.2f KB/S " % (
189
+ file_size_dl / 1000 , speed )
171
190
print (status ),
172
191
sys .stdout .flush ()
173
192
print ("\r " ),
@@ -224,7 +243,8 @@ def unpack_zipfile(self, extract_dir):
224
243
print ("==> Extraction done!" )
225
244
226
245
def ask_to_delete_downloaded_zip_file (self ):
227
- ret = self .get_input_value ("==> Would you like to save '%s'? So you don't have to download it later. [Yes/no]: " % self ._filename )
246
+ ret = self .get_input_value (
247
+ "==> Would you like to save '%s'? So you don't have to download it later. [Yes/no]: " % self ._filename )
228
248
ret = ret .strip ()
229
249
if ret != 'yes' and ret != 'y' and ret != 'no' and ret != 'n' :
230
250
print ("==> Saving the dependency libraries by default" )
@@ -237,16 +257,17 @@ def download_zip_file(self):
237
257
self .download_file_with_retry (5 , 3 )
238
258
try :
239
259
if not zipfile .is_zipfile (self ._filename ):
240
- raise UnrecognizedFormat ("%s is not a zip file" % (self ._filename ))
260
+ raise UnrecognizedFormat (
261
+ "%s is not a zip file" % (self ._filename ))
241
262
except UnrecognizedFormat as e :
242
- print ("==> Unrecognized zip format from your local '%s' file!" % (self ._filename ))
263
+ print ("==> Unrecognized zip format from your local '%s' file!" %
264
+ (self ._filename ))
243
265
if os .path .isfile (self ._filename ):
244
266
os .remove (self ._filename )
245
267
print ("==> Download it from internet again, please wait..." )
246
268
self .download_zip_file ()
247
269
248
270
def download_file_with_retry (self , times , delay ):
249
- import urllib2
250
271
times_count = 0
251
272
while (times_count < times ):
252
273
times_count += 1
@@ -290,15 +311,17 @@ def fix_fmod_link(self, extract_dir):
290
311
import platform
291
312
if platform .system () != "Linux" :
292
313
return
293
- print ("==> Fix fmod link ... " )
294
- fmod_path = os .path .join (extract_dir , "linux-specific/fmod/prebuilt/64-bit" )
314
+ print ("==> Fix fmod link ... " )
315
+ fmod_path = os .path .join (
316
+ extract_dir , "linux-specific/fmod/prebuilt/64-bit" )
295
317
if os .path .exists (fmod_path ):
296
318
os .unlink (os .path .join (fmod_path , "libfmod.so.6" ))
297
319
os .unlink (os .path .join (fmod_path , "libfmodL.so.6" ))
298
320
os .symlink ("libfmod.so" , os .path .join (fmod_path , "libfmod.so.6" ))
299
321
os .symlink ("libfmodL.so" , os .path .join (fmod_path , "libfmodL.so.6" ))
300
322
else :
301
- print ("==> fmod directory not found `%s`, failed to fix fmod link!" % fmod_path )
323
+ print (
324
+ "==> fmod directory not found `%s`, failed to fix fmod link!" % fmod_path )
302
325
303
326
def run (self , workpath , folder_for_extracting , remove_downloaded , force_update , download_only ):
304
327
if not force_update and not self .need_to_update ():
@@ -318,13 +341,16 @@ def run(self, workpath, folder_for_extracting, remove_downloaded, force_update,
318
341
319
342
self .clean_external_folder (folder_for_extracting )
320
343
print ("==> Copying files..." )
321
- distutils .dir_util .copy_tree (self ._extracted_folder_name , folder_for_extracting )
344
+ distutils .dir_util .copy_tree (
345
+ self ._extracted_folder_name , folder_for_extracting )
322
346
if self ._move_dirs is not None :
323
347
for srcDir in self ._move_dirs .keys ():
324
- distDir = os .path .join ( os .path .join (workpath , self ._move_dirs [srcDir ]), srcDir )
348
+ distDir = os .path .join (os .path .join (
349
+ workpath , self ._move_dirs [srcDir ]), srcDir )
325
350
if os .path .exists (distDir ):
326
351
shutil .rmtree (distDir )
327
- shutil .move ( os .path .join (folder_for_extracting , srcDir ), distDir )
352
+ shutil .move (os .path .join (
353
+ folder_for_extracting , srcDir ), distDir )
328
354
self .fix_fmod_link (folder_for_extracting )
329
355
print ("==> Cleaning..." )
330
356
if os .path .exists (self ._extracted_folder_name ):
@@ -339,6 +365,14 @@ def run(self, workpath, folder_for_extracting, remove_downloaded, force_update,
339
365
print ("==> Download (%s) finish!" % self ._filename )
340
366
341
367
368
+ def _is_python_version_2 ():
369
+ major_ver = sys .version_info [0 ]
370
+ print ("The python version is %d.%d." % (major_ver , sys .version_info [1 ]))
371
+ if major_ver > 2 :
372
+ return False
373
+ return True
374
+
375
+
342
376
def main ():
343
377
workpath = os .path .dirname (os .path .realpath (__file__ ))
344
378
@@ -360,11 +394,15 @@ def main():
360
394
print ("=======================================================" )
361
395
print ("==> Prepare to download external libraries!" )
362
396
external_path = os .path .join (workpath , 'external' )
363
- installer = CocosZipInstaller (workpath , os .path .join (workpath , 'external' , 'config.json' ), os .path .join (workpath , 'external' , 'version.json' ), "prebuilt_libs_version" )
364
- installer .run (workpath , external_path , opts .remove_downloaded , opts .force_update , opts .download_only )
397
+ installer = CocosZipInstaller (workpath , os .path .join (workpath , 'external' , 'config.json' ), os .path .join (
398
+ workpath , 'external' , 'version.json' ), "prebuilt_libs_version" )
399
+ installer .run (workpath , external_path , opts .remove_downloaded ,
400
+ opts .force_update , opts .download_only )
401
+
365
402
366
403
# -------------- main --------------
367
404
if __name__ == '__main__' :
405
+ python_2 = _is_python_version_2 ()
368
406
try :
369
407
main ()
370
408
except Exception as e :
0 commit comments