download-deps.py will unzip the download zip file.

Output more detail informations
This commit is contained in:
James Chen 2014-05-07 13:36:08 +08:00
parent a4ce0687f4
commit b49b9d673c
1 changed files with 132 additions and 29 deletions

View File

@ -9,37 +9,83 @@
# be hosted separately. # be hosted separately.
# #
"""****************************************************************************
Copyright (c) 2014 cocos2d-x.org
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************"""
import urllib2 import urllib2
import os.path,zipfile import os.path,zipfile
import shutil import shutil
import sys import sys
import traceback import traceback
import distutils import distutils
import fileinput
from optparse import OptionParser
from time import time from time import time
from sys import stdout from sys import stdout
from distutils.errors import DistutilsError from distutils.errors import DistutilsError
from distutils.dir_util import copy_tree, remove_tree from distutils.dir_util import copy_tree, remove_tree
prefix = 'https://github.com/cocos2d/cocos2d-x-external/archive/' _workpath=''
filename = 'v3-deps-1'
extracted_folder_name='cocos2d-x-resources-' + filename
def download_file(url, file_name): # -- Section for configuring file url -----
print("--> Ready to download %s from %s" % (file_name, url)) # github server may not reponse a header information which contains `Content-Length`,
# therefore, the size needs to be written hardcode here. While server doesn't return
# `Content-Length`, use it instead
_zip_file_bytes = 57132755
_repo_name = 'cocos2d-x-external'
_filename_prefix = 'v3-deps-1'
_filename = _filename_prefix + '.zip'
_downloaded_file_url = 'https://github.com/cocos2d/' + _repo_name + '/archive/' + _filename
# 'v' letter was swallowed by github, so we need to substring it from the 2nd letter
_extracted_folder_name = _repo_name + '-' + _filename_prefix[1:]
# ----------------------------------------
_remove_downloaded_zip_file = None
def get_input_value(prompt):
ret = raw_input(prompt)
ret.rstrip(" \t")
return ret
def download_file(url, filename):
print("==> Ready to download '%s' from '%s'" % (filename, url))
u = urllib2.urlopen(url) u = urllib2.urlopen(url)
f = open(file_name, 'wb') f = open(filename, 'wb')
meta = u.info() meta = u.info()
content_len = meta.getheaders("Content-Length") content_len = meta.getheaders("Content-Length")
file_size = 0 file_size = 0
if content_len and len(content_len) > 0: if content_len and len(content_len) > 0:
file_size = int(content_len[0]) file_size = int(content_len[0])
print("--> Get file size succeed!")
else: else:
print("WARNING: Couldn't grab the file size, ignore it") print("==> WARNING: Couldn't grab the file size!")
file_size = _zip_file_bytes
print("--> Start to download, please wait ...") print("==> Start to download, please wait ...")
file_size_dl = 0 file_size_dl = 0
block_sz = 8192 block_sz = 8192
@ -70,7 +116,7 @@ def download_file(url, file_name):
block_size_per_second = 0 block_size_per_second = 0
old_time = new_time old_time = new_time
print("--> Downloading finished!") print("==> Downloading finished!")
f.close() f.close()
def default_filter(src,dst): def default_filter(src,dst):
@ -81,6 +127,13 @@ def ensure_directory(target):
if not os.path.exists(target): if not os.path.exists(target):
os.mkdir(target) os.mkdir(target)
class UnrecognizedFormat:
def __init__(self, prompt):
self._prompt = prompt
def __str__(self):
return self._prompt
def unpack_zipfile(filename, extract_dir, progress_filter=default_filter): def unpack_zipfile(filename, extract_dir, progress_filter=default_filter):
"""Unpack zip `filename` to `extract_dir` """Unpack zip `filename` to `extract_dir`
@ -90,8 +143,9 @@ def unpack_zipfile(filename, extract_dir, progress_filter=default_filter):
""" """
if not zipfile.is_zipfile(filename): if not zipfile.is_zipfile(filename):
raise UnrecognizedFormat("%s is not a zip file" % (filename,)) raise UnrecognizedFormat("%s is not a zip file" % (filename))
print("==> Extracting files, please wait ...")
z = zipfile.ZipFile(filename) z = zipfile.ZipFile(filename)
try: try:
for info in z.infolist(): for info in z.infolist():
@ -122,31 +176,80 @@ def unpack_zipfile(filename, extract_dir, progress_filter=default_filter):
os.chmod(target, unix_attributes) os.chmod(target, unix_attributes)
finally: finally:
z.close() z.close()
print("==> Extraction done!")
def ask_to_delete_downloaded_zip_file(filename):
ret = get_input_value("==> Whether to delete the downloaded zip file? It may be reused when you execute this script next time! (yes/no): ")
ret = ret.strip()
if ret != 'yes' and ret != 'no':
print("==> Invalid answer, please answer 'yes' or 'no'!")
ask_to_delete_downloaded_zip_file(filename)
if ret == "yes":
os.remove(filename)
def download_and_unzip_file(url, filename):
if not os.path.isfile(filename):
download_file(url, filename)
try:
unpack_zipfile(filename, _workpath)
except UnrecognizedFormat as e:
print("==> Unrecognized zip format from your local '%s' file!" % (filename))
if os.path.isfile(filename):
os.remove(filename)
print("==> Download it from internet again, please wait...")
download_and_unzip_file(url, filename)
def main(): def main():
workpath = os.path.dirname(os.path.realpath(__file__)) _workpath = os.path.dirname(os.path.realpath(__file__))
if os.path.exists(extracted_folder_name): if os.path.exists(_extracted_folder_name):
shutil.rmtree(extracted_folder_name) shutil.rmtree(_extracted_folder_name)
# if not os.path.isfile(filename + '.zip'): download_and_unzip_file(_downloaded_file_url, _filename)
download_file(prefix+filename+'.zip', filename+'.zip')
print("--> Extracting files, please wait ...") print("==> Copying files...")
# unpack_zipfile(filename+'.zip', workpath) folder_for_extracting = os.path.join(_workpath, 'external')
print("--> Extraction done!") if not os.path.exists(folder_for_extracting):
print("--> Copying files ...") os.mkdir(folder_for_extracting)
# distutils.dir_util.copy_tree(extracted_folder_name, workpath) distutils.dir_util.copy_tree(_extracted_folder_name, folder_for_extracting)
print("--> Cleaning ...") print("==> Cleaning...")
# if os.path.isfile(filename+'.zip'): if os.path.exists(_extracted_folder_name):
# os.remove(filename+'.zip') shutil.rmtree(_extracted_folder_name)
# if os.path.exists(extracted_folder_name): if _remove_downloaded_zip_file:
# shutil.rmtree(extracted_folder_name) if os.path.isfile(_filename):
print("--> DONE! Cheers!") os.remove(_filename)
else:
if os.path.isfile(_filename):
ask_to_delete_downloaded_zip_file(_filename)
print("==> DONE! Prebuilt libraries were installed successfully! Cheers!")
def _check_python_version():
major_ver = sys.version_info[0]
if major_ver > 2:
print ("The python version is %d.%d. But python 2.x is required. (Version 2.7 is well tested)\n"
"Download it here: https://www.python.org/" % (major_ver, sys.version_info[1]))
return False
return True
# -------------- main -------------- # -------------- main --------------
if __name__ == '__main__': if __name__ == '__main__':
try: try:
if not _check_python_version():
exit()
parser = OptionParser()
parser.add_option('-r', '--remove-download',
action="store_true", dest='to_remove_downloaded_zip_file', default=False,
help='Whether to remove downloaded zip file')
(opts, args) = parser.parse_args()
if opts.to_remove_downloaded_zip_file:
_remove_downloaded_zip_file = True
main() main()
except Exception as e: except Exception as e:
traceback.print_exc() traceback.print_exc()