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,13 +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
9898 if self ._current_version [0 ] == 'v' :
99- self ._extracted_folder_name = os .path .join (self ._workpath , self ._repo_name + '-' + self ._current_version [1 :])
99+ self ._extracted_folder_name = os .path .join (
100+ self ._workpath , self ._repo_name + '-' + self ._current_version [1 :])
100101 else :
101- self ._extracted_folder_name = os .path .join (self ._workpath , self ._repo_name + '-' + self ._current_version )
102+ self ._extracted_folder_name = os .path .join (
103+ self ._workpath , self ._repo_name + '-' + self ._current_version )
102104
103105 try :
104106 data = self .load_json_file (version_path )
@@ -110,7 +112,10 @@ def __init__(self, workpath, config_path, version_path, remote_version_key=None)
110112 print ("==> version file doesn't exist" )
111113
112114 def get_input_value (self , prompt ):
113- ret = raw_input (prompt )
115+ if (python_2 ):
116+ ret = raw_input (prompt )
117+ else :
118+ ret = input (prompt )
114119 ret .rstrip (" \t " )
115120 return ret
116121
@@ -120,27 +125,39 @@ def download_file(self):
120125 os .remove (self ._filename )
121126 except OSError :
122127 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
125134 try :
126- u = urllib2 .urlopen (self ._url )
127- except urllib2 . HTTPError as e :
135+ u = urllib .urlopen (self ._url )
136+ except Exception as e :
128137 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 ()))
131142 sys .exit (1 )
132143
133144 f = open (self ._filename , 'wb' )
134145 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+
136152 file_size = 0
137153 if content_len and len (content_len ) > 0 :
138154 file_size = int (content_len [0 ])
139155 else :
140156 # github server may not reponse a header information which contains `Content-Length`,
141157 # therefore, the size needs to be written hardcode here. While server doesn't return
142158 # `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 )
144161 file_size = self ._zip_file_size
145162
146163 print ("==> Start to download, please wait ..." )
@@ -165,9 +182,11 @@ def download_file(self):
165182 speed = block_size_per_second / (new_time - old_time ) / 1000.0
166183 if file_size != 0 :
167184 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 )
169187 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 )
171190 print (status ),
172191 sys .stdout .flush ()
173192 print ("\r " ),
@@ -224,7 +243,8 @@ def unpack_zipfile(self, extract_dir):
224243 print ("==> Extraction done!" )
225244
226245 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 )
228248 ret = ret .strip ()
229249 if ret != 'yes' and ret != 'y' and ret != 'no' and ret != 'n' :
230250 print ("==> Saving the dependency libraries by default" )
@@ -237,16 +257,17 @@ def download_zip_file(self):
237257 self .download_file_with_retry (5 , 3 )
238258 try :
239259 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 ))
241262 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 ))
243265 if os .path .isfile (self ._filename ):
244266 os .remove (self ._filename )
245267 print ("==> Download it from internet again, please wait..." )
246268 self .download_zip_file ()
247269
248270 def download_file_with_retry (self , times , delay ):
249- import urllib2
250271 times_count = 0
251272 while (times_count < times ):
252273 times_count += 1
@@ -290,15 +311,17 @@ def fix_fmod_link(self, extract_dir):
290311 import platform
291312 if platform .system () != "Linux" :
292313 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" )
295317 if os .path .exists (fmod_path ):
296318 os .unlink (os .path .join (fmod_path , "libfmod.so.6" ))
297319 os .unlink (os .path .join (fmod_path , "libfmodL.so.6" ))
298320 os .symlink ("libfmod.so" , os .path .join (fmod_path , "libfmod.so.6" ))
299321 os .symlink ("libfmodL.so" , os .path .join (fmod_path , "libfmodL.so.6" ))
300322 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 )
302325
303326 def run (self , workpath , folder_for_extracting , remove_downloaded , force_update , download_only ):
304327 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,
318341
319342 self .clean_external_folder (folder_for_extracting )
320343 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 )
322346 if self ._move_dirs is not None :
323347 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 )
325350 if os .path .exists (distDir ):
326351 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 )
328354 self .fix_fmod_link (folder_for_extracting )
329355 print ("==> Cleaning..." )
330356 if os .path .exists (self ._extracted_folder_name ):
@@ -339,22 +365,17 @@ def run(self, workpath, folder_for_extracting, remove_downloaded, force_update,
339365 print ("==> Download (%s) finish!" % self ._filename )
340366
341367
342- def _check_python_version ():
368+ def _is_python_version_2 ():
343369 major_ver = sys .version_info [0 ]
370+ print ("The python version is %d.%d." % (major_ver , sys .version_info [1 ]))
344371 if major_ver > 2 :
345- print ("The python version is %d.%d. But python 2.x is required. (Version 2.7 is well tested)\n "
346- "Download it here: https://www.python.org/" % (major_ver , sys .version_info [1 ]))
347372 return False
348-
349373 return True
350374
351375
352376def main ():
353377 workpath = os .path .dirname (os .path .realpath (__file__ ))
354378
355- if not _check_python_version ():
356- exit ()
357-
358379 parser = OptionParser ()
359380 parser .add_option ('-r' , '--remove-download' ,
360381 action = "store" , type = "string" , dest = 'remove_downloaded' , default = None ,
@@ -373,11 +394,15 @@ def main():
373394 print ("=======================================================" )
374395 print ("==> Prepare to download external libraries!" )
375396 external_path = os .path .join (workpath , 'external' )
376- installer = CocosZipInstaller (workpath , os .path .join (workpath , 'external' , 'config.json' ), os .path .join (workpath , 'external' , 'version.json' ), "prebuilt_libs_version" )
377- 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+
378402
379403# -------------- main --------------
380404if __name__ == '__main__' :
405+ python_2 = _is_python_version_2 ()
381406 try :
382407 main ()
383408 except Exception as e :
0 commit comments