Merge pull request #6564 from dumganhar/v3

Updates download-dep.py, linux, mac  and windows work ok
This commit is contained in:
James Chen 2014-05-04 23:51:31 +08:00
commit 81ced0fc9f
1 changed files with 59 additions and 27 deletions

View File

@ -14,7 +14,10 @@ import os.path,zipfile
import shutil import shutil
import sys import sys
import traceback import traceback
import distutils
from sys import stdout from sys import stdout
from distutils.errors import DistutilsError
from distutils.dir_util import copy_tree, remove_tree
prefix = 'https://codeload.github.com/minggo/cocos2d-x-resources/zip/' prefix = 'https://codeload.github.com/minggo/cocos2d-x-resources/zip/'
filename = 'cocos2d-x-deps-v3-1' filename = 'cocos2d-x-deps-v3-1'
@ -41,40 +44,69 @@ def download_file(url, file_name):
#print(status), #print(status),
f.close() f.close()
def unzip(src,dst): def default_filter(src,dst):
zf = zipfile.ZipFile(src) """The default progress/filter callback; returns True for all files"""
for file in zf.filelist: return dst
name = file.filename
perm = ((file.external_attr >> 16L) & 0777)
if name.endswith('/'): def ensure_directory(target):
outfile = os.path.join(dst, name) if not os.path.exists(target):
if not os.path.exists(outfile): os.mkdir(target)
os.mkdir(outfile, perm)
else:
outfile = os.path.join(dst, name)
fh = os.open(outfile, os.O_CREAT | os.O_WRONLY, perm)
os.write(fh, zf.read(name))
os.close(fh)
zf.close()
def copy_files(src, dst): def unpack_zipfile(filename, extract_dir, progress_filter=default_filter):
for item in os.listdir(src): """Unpack zip `filename` to `extract_dir`
path = os.path.join(src, item)
if os.path.isfile(path): Raises ``UnrecognizedFormat`` if `filename` is not a zipfile (as determined
shutil.copy(path, dst) by ``zipfile.is_zipfile()``). See ``unpack_archive()`` for an explanation
elif os.path.isdir(path): of the `progress_filter` argument.
new_dst = os.path.join(dst, item) """
if not os.path.exists(new_dst):
os.mkdir(new_dst) if not zipfile.is_zipfile(filename):
copy_files(path, new_dst) raise UnrecognizedFormat("%s is not a zip file" % (filename,))
z = zipfile.ZipFile(filename)
try:
for info in z.infolist():
name = info.filename
# don't extract absolute paths or ones with .. in them
if name.startswith('/') or '..' in name:
continue
target = os.path.join(extract_dir, *name.split('/'))
target = progress_filter(name, target)
if not target:
continue
if name.endswith('/'):
# directory
ensure_directory(target)
else:
# file
ensure_directory(target)
data = z.read(info.filename)
f = open(target,'wb')
try:
f.write(data)
finally:
f.close()
del data
unix_attributes = info.external_attr >> 16
if unix_attributes:
os.chmod(target, unix_attributes)
finally:
z.close()
def main(): def main():
download_file(prefix+filename, filename+'.zip') download_file(prefix+filename, filename+'.zip')
workpath = os.path.dirname(os.path.realpath(__file__)) workpath = os.path.dirname(os.path.realpath(__file__))
if os.path.exists(extracted_folder_name):
shutil.rmtree(extracted_folder_name)
print("Extracting files, please wait ...") print("Extracting files, please wait ...")
unzip(filename+'.zip', workpath) unpack_zipfile(filename+'.zip', workpath)
copy_files(extracted_folder_name, workpath) print("Extraction done!")
print("Copying files ...")
distutils.dir_util.copy_tree(extracted_folder_name, workpath)
print("Cleaning ...") print("Cleaning ...")
if os.path.isfile(filename+'.zip'): if os.path.isfile(filename+'.zip'):
os.remove(filename+'.zip') os.remove(filename+'.zip')