Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
zip.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2#
3# Copyright 2013 The Flutter Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import argparse
8import json
9import os
10import stat
11import sys
12import zipfile
13
14
15def _zip_dir(path, zip_file, prefix):
16 path = path.rstrip('/\\')
17 for root, directories, files in os.walk(path):
18 for directory in directories:
19 if os.path.islink(os.path.join(root, directory)):
21 zip_file,
22 os.path.join(root, directory),
23 os.path.join(root.replace(path, prefix), directory),
24 )
25 for file in files:
26 if os.path.islink(os.path.join(root, file)):
28 zip_file, os.path.join(root, file), os.path.join(root.replace(path, prefix), file)
29 )
30 continue
31 zip_file.write(os.path.join(root, file), os.path.join(root.replace(path, prefix), file))
32
33
34def add_symlink(zip_file, source, target):
35 """Adds a symlink to a zip file.
36
37 Args:
38 zip_file: The ZipFile obj where the symlink will be added.
39 source: The full path to the symlink.
40 target: The target path for the symlink within the zip file.
41 """
42 zip_info = zipfile.ZipInfo(target)
43 zip_info.create_system = 3 # Unix like system
44 unix_st_mode = (
45 stat.S_IFLNK | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP
46 | stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH
47 )
48 zip_info.external_attr = unix_st_mode << 16
49 zip_file.writestr(zip_info, os.readlink(source))
50
51
52def main(args):
53 zip_file = zipfile.ZipFile(args.output, 'w', zipfile.ZIP_DEFLATED)
54 if args.source_file:
55 with open(args.source_file) as source_file:
56 file_dict_list = json.load(source_file)
57 for file_dict in file_dict_list:
58 if os.path.islink(file_dict['source']):
59 add_symlink(zip_file, file_dict['source'], file_dict['destination'])
60 continue
61 if os.path.isdir(file_dict['source']):
62 _zip_dir(file_dict['source'], zip_file, file_dict['destination'])
63 else:
64 zip_file.write(file_dict['source'], file_dict['destination'])
65 else:
66 for path, archive_name in args.input_pairs:
67 if os.path.islink(path):
68 add_symlink(zip_file, path, archive_name)
69 continue
70 if os.path.isdir(path):
71 _zip_dir(path, zip_file, archive_name)
72 else:
73 zip_file.write(path, archive_name)
74 zip_file.close()
75
76
77if __name__ == '__main__':
78 parser = argparse.ArgumentParser(description='This script creates zip files.')
79 parser.add_argument('-o', dest='output', action='store', help='The name of the output zip file.')
80 parser.add_argument(
81 '-i',
82 dest='input_pairs',
83 nargs=2,
84 action='append',
85 help='The input file and its destination location in the zip archive.'
86 )
87 parser.add_argument(
88 '-f', dest='source_file', action='store', help='The path to the file list to zip.'
89 )
90 sys.exit(main(parser.parse_args()))
Definition main.py:1
_zip_dir(path, zip_file, prefix)
Definition zip.py:15
add_symlink(zip_file, source, target)
Definition zip.py:34