Add symlink support to zip script (#32257)
diff --git a/build/zip.py b/build/zip.py index 7936ea2..1a71242 100755 --- a/build/zip.py +++ b/build/zip.py
@@ -8,6 +8,7 @@ import json import zipfile import os +import stat import sys @@ -15,22 +16,52 @@ path = path.rstrip('/\\') for root, dirs, files in os.walk(path): for file in files: + if os.path.islink(os.path.join(root, file)): + add_symlink( + zip_file, + os.path.join(root, file), + os.path.join(root.replace(path, prefix), file) + ) + continue zip_file.write(os.path.join(root, file), os.path.join( root.replace(path, prefix), file)) +def add_symlink(zip_file, source, target): + """Adds a symlink to a zip file. + + Args: + zip_file: The ZipFile obj where the symlink will be added. + source: The full path to the symlink. + target: The target path for the symlink within the zip file. + """ + zip_info = zipfile.ZipInfo(target) + zip_info.create_system = 3 # Unix like system + unix_st_mode = (stat.S_IFLNK | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | + stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH | + stat.S_IWOTH | stat.S_IXOTH) + zip_info.external_attr = unix_st_mode << 16 + zip_file.writestr(zip_info, source) + + def main(args): zip_file = zipfile.ZipFile(args.output, 'w', zipfile.ZIP_DEFLATED) if args.source_file: with open(args.source_file) as source_file: file_dict_list = json.load(source_file) for file_dict in file_dict_list: + if os.path.islink(file_dict['source']): + add_symlink(zip_file, file_dict['source'], file_dict['destination']) + continue if os.path.isdir(file_dict['source']): _zip_dir(file_dict['source'], zip_file, file_dict['destination']) else: zip_file.write(file_dict['source'], file_dict['destination']) else: for path, archive_name in args.input_pairs: + if os.path.islink(path): + add_symlink(zip_file, path, archive_name) + continue if os.path.isdir(path): _zip_dir(path, zip_file, archive_name) else: