Skip to content

Commit 1ef66a4

Browse files
committed
keep symblinks while make dist
1 parent 02e5f28 commit 1ef66a4

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

tools/package_binaries.py

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -347,21 +347,53 @@ def generate_target_others(platform_name, arch, version):
347347

348348
################################
349349
# Make packages
350+
351+
#https://gist.github.com/kgn/610907
352+
def ZipDir(inputDir, outputZip):
353+
'''Zip up a directory and preserve symlinks and empty directories'''
354+
zipOut = zipfile.ZipFile(outputZip, 'w', compression=zipfile.ZIP_DEFLATED)
355+
356+
rootLen = len(os.path.dirname(inputDir))
357+
def _ArchiveDirectory(parentDirectory):
358+
contents = os.listdir(parentDirectory)
359+
#store empty directories
360+
if not contents:
361+
#http://www.velocityreviews.com/forums/t318840-add-empty-directory-using-zipfile.html
362+
archiveRoot = parentDirectory[rootLen:].replace('\\', '/').lstrip('/')
363+
zipInfo = zipfile.ZipInfo(archiveRoot+'/')
364+
zipOut.writestr(zipInfo, '')
365+
for item in contents:
366+
fullPath = os.path.join(parentDirectory, item)
367+
if os.path.isdir(fullPath) and not os.path.islink(fullPath):
368+
_ArchiveDirectory(fullPath)
369+
else:
370+
archiveRoot = fullPath[rootLen:].replace('\\', '/').lstrip('/')
371+
if os.path.islink(fullPath):
372+
# http://www.mail-archive.com/python-list@python.org/msg34223.html
373+
zipInfo = zipfile.ZipInfo(archiveRoot)
374+
zipInfo.create_system = 3
375+
# long type of hex val of '0xA1ED0000L',
376+
# say, symlink attr magic...
377+
zipInfo.external_attr = 2716663808L
378+
zipOut.writestr(zipInfo, os.readlink(fullPath))
379+
else:
380+
zipOut.write(fullPath, archiveRoot, zipfile.ZIP_DEFLATED)
381+
_ArchiveDirectory(inputDir)
382+
383+
zipOut.close()
384+
350385
def compress(from_dir, to_dir, fname, compress):
351386
from_dir = os.path.normpath(from_dir)
352387
to_dir = os.path.normpath(to_dir)
353388
_from = os.path.join(from_dir, fname)
354389
_to = os.path.join(to_dir, fname)
355390
if compress == 'zip':
356-
z = zipfile.ZipFile(_to + '.zip', 'w', compression=zipfile.ZIP_DEFLATED)
357-
if os.path.isdir(_from):
358-
for root, dirs, files in os.walk(_from):
359-
for f in files:
360-
_path = os.path.join(root, f)
361-
z.write(_path, _path.replace(from_dir+os.sep, ''))
391+
if os.path.isdir(_from):
392+
ZipDir(_from, _to + '.zip')
362393
else:
394+
z = zipfile.ZipFile(_to + '.zip', 'w', compression=zipfile.ZIP_DEFLATED)
363395
z.write(_from, fname)
364-
z.close()
396+
z.close()
365397
elif compress == 'tar.gz': # only for folders
366398
if not os.path.isdir(_from):
367399
print 'Will not create tar.gz for a single file: ' + _from
@@ -424,13 +456,13 @@ def make_packages(targets):
424456
src = os.path.join(binaries_location, f)
425457
dest = os.path.join(folder, f)
426458
if os.path.isdir(src): # like nw.app
427-
shutil.copytree(src, dest)
459+
shutil.copytree(src, dest, symlinks=True)
428460
else:
429461
shutil.copy(src, dest)
430462
compress(dist_dir, dist_dir, t['output'], t['compress'])
431463
# remove temp folders
432464
if (t.has_key('keep4test')) :
433-
shutil.copytree(folder, nwfolder)
465+
shutil.copytree(folder, nwfolder, symlinks=True)
434466

435467
shutil.rmtree(folder)
436468
else:

0 commit comments

Comments
 (0)