11#!/usr/bin/env python
2- #coding=utf-8
2+
3+ # coding=utf-8
34#
45# ./download-deps.py
56#
4243import sys
4344import traceback
4445import distutils
45- import fileinput
4646import json
4747
4848from optparse import OptionParser
4949from time import time
5050from time import sleep
5151from sys import stdout
52- from distutils .errors import DistutilsError
5352from distutils .dir_util import copy_tree , remove_tree
5453
5554
@@ -92,10 +91,16 @@ def __init__(self, workpath, config_path, version_path, remote_version_key=None)
9291 except :
9392 self ._move_dirs = None
9493 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
9696 self ._zip_file_size = int (data ["zip_file_size" ])
9797 # '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 )
99104
100105 try :
101106 data = self .load_json_file (version_path )
@@ -107,7 +112,10 @@ def __init__(self, workpath, config_path, version_path, remote_version_key=None)
107112 print ("==> version file doesn't exist" )
108113
109114 def get_input_value (self , prompt ):
110- ret = raw_input (prompt )
115+ if (python_2 ):
116+ ret = raw_input (prompt )
117+ else :
118+ ret = input (prompt )
111119 ret .rstrip (" \t " )
112120 return ret
113121
@@ -117,27 +125,39 @@ def download_file(self):
117125 os .remove (self ._filename )
118126 except OSError :
119127 pass
120- print ("==> Ready to download '%s' from '%s'" % (self ._filename , self ._url ))
121- 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
122134 try :
123- u = urllib2 .urlopen (self ._url )
124- except urllib2 . HTTPError as e :
135+ u = urllib .urlopen (self ._url )
136+ except Exception as e :
125137 if e .code == 404 :
126- print ("==> Error: Could not find the file from url: '%s'" % (self ._url ))
127- 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 ()))
128142 sys .exit (1 )
129143
130144 f = open (self ._filename , 'wb' )
131145 meta = u .info ()
132- 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+
133152 file_size = 0
134153 if content_len and len (content_len ) > 0 :
135154 file_size = int (content_len [0 ])
136155 else :
137156 # github server may not reponse a header information which contains `Content-Length`,
138157 # therefore, the size needs to be written hardcode here. While server doesn't return
139158 # `Content-Length`, use it instead
140- 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 )
141161 file_size = self ._zip_file_size
142162
143163 print ("==> Start to download, please wait ..." )
@@ -162,9 +182,11 @@ def download_file(self):
162182 speed = block_size_per_second / (new_time - old_time ) / 1000.0
163183 if file_size != 0 :
164184 percent = file_size_dl * 100. / file_size
165- 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 )
166187 else :
167- 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 )
168190 print (status ),
169191 sys .stdout .flush ()
170192 print ("\r " ),
@@ -221,7 +243,8 @@ def unpack_zipfile(self, extract_dir):
221243 print ("==> Extraction done!" )
222244
223245 def ask_to_delete_downloaded_zip_file (self ):
224- 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 )
225248 ret = ret .strip ()
226249 if ret != 'yes' and ret != 'y' and ret != 'no' and ret != 'n' :
227250 print ("==> Saving the dependency libraries by default" )
@@ -234,16 +257,17 @@ def download_zip_file(self):
234257 self .download_file_with_retry (5 , 3 )
235258 try :
236259 if not zipfile .is_zipfile (self ._filename ):
237- raise UnrecognizedFormat ("%s is not a zip file" % (self ._filename ))
260+ raise UnrecognizedFormat (
261+ "%s is not a zip file" % (self ._filename ))
238262 except UnrecognizedFormat as e :
239- print ("==> Unrecognized zip format from your local '%s' file!" % (self ._filename ))
263+ print ("==> Unrecognized zip format from your local '%s' file!" %
264+ (self ._filename ))
240265 if os .path .isfile (self ._filename ):
241266 os .remove (self ._filename )
242267 print ("==> Download it from internet again, please wait..." )
243268 self .download_zip_file ()
244269
245270 def download_file_with_retry (self , times , delay ):
246- import urllib2
247271 times_count = 0
248272 while (times_count < times ):
249273 times_count += 1
@@ -287,15 +311,17 @@ def fix_fmod_link(self, extract_dir):
287311 import platform
288312 if platform .system () != "Linux" :
289313 return
290- print ("==> Fix fmod link ... " )
291- 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" )
292317 if os .path .exists (fmod_path ):
293318 os .unlink (os .path .join (fmod_path , "libfmod.so.6" ))
294319 os .unlink (os .path .join (fmod_path , "libfmodL.so.6" ))
295320 os .symlink ("libfmod.so" , os .path .join (fmod_path , "libfmod.so.6" ))
296321 os .symlink ("libfmodL.so" , os .path .join (fmod_path , "libfmodL.so.6" ))
297322 else :
298- 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 )
299325
300326 def run (self , workpath , folder_for_extracting , remove_downloaded , force_update , download_only ):
301327 if not force_update and not self .need_to_update ():
@@ -315,13 +341,16 @@ def run(self, workpath, folder_for_extracting, remove_downloaded, force_update,
315341
316342 self .clean_external_folder (folder_for_extracting )
317343 print ("==> Copying files..." )
318- 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 )
319346 if self ._move_dirs is not None :
320347 for srcDir in self ._move_dirs .keys ():
321- 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 )
322350 if os .path .exists (distDir ):
323351 shutil .rmtree (distDir )
324- shutil .move ( os .path .join (folder_for_extracting , srcDir ), distDir )
352+ shutil .move (os .path .join (
353+ folder_for_extracting , srcDir ), distDir )
325354 self .fix_fmod_link (folder_for_extracting )
326355 print ("==> Cleaning..." )
327356 if os .path .exists (self ._extracted_folder_name ):
@@ -336,22 +365,17 @@ def run(self, workpath, folder_for_extracting, remove_downloaded, force_update,
336365 print ("==> Download (%s) finish!" % self ._filename )
337366
338367
339- def _check_python_version ():
368+ def _is_python_version_2 ():
340369 major_ver = sys .version_info [0 ]
370+ print ("The python version is %d.%d." % (major_ver , sys .version_info [1 ]))
341371 if major_ver > 2 :
342- print ("The python version is %d.%d. But python 2.x is required. (Version 2.7 is well tested)\n "
343- "Download it here: https://www.python.org/" % (major_ver , sys .version_info [1 ]))
344372 return False
345-
346373 return True
347374
348375
349376def main ():
350377 workpath = os .path .dirname (os .path .realpath (__file__ ))
351378
352- if not _check_python_version ():
353- exit ()
354-
355379 parser = OptionParser ()
356380 parser .add_option ('-r' , '--remove-download' ,
357381 action = "store" , type = "string" , dest = 'remove_downloaded' , default = None ,
@@ -370,11 +394,15 @@ def main():
370394 print ("=======================================================" )
371395 print ("==> Prepare to download external libraries!" )
372396 external_path = os .path .join (workpath , 'external' )
373- installer = CocosZipInstaller (workpath , os .path .join (workpath , 'external' , 'config.json' ), os .path .join (workpath , 'external' , 'version.json' ), "prebuilt_libs_version" )
374- 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+
375402
376403# -------------- main --------------
377404if __name__ == '__main__' :
405+ python_2 = _is_python_version_2 ()
378406 try :
379407 main ()
380408 except Exception as e :
0 commit comments