-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda_zip.py
71 lines (63 loc) · 2.37 KB
/
lambda_zip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import zipfile
import os
import argparse
def create_zip(zip_file_name):
current_path = os.path.dirname(os.path.realpath(__file__))
destination_zip = os.path.join(current_path, zip_file_name)
print('Creating zip file:', zip_file_name)
zip_file = zipfile.ZipFile(destination_zip, 'w')
root = os.listdir(current_path)
for item in root:
if os.path.isfile(os.path.join(current_path, item)) and item.endswith('.py'):
print('Compressing:', item)
zip_file.write(item, compress_type=zipfile.ZIP_DEFLATED)
for folder_path, subfolders, files in os.walk(current_path):
if os.path.basename(folder_path) == 'site-packages':
site_packages = subfolders
for package in site_packages:
print('Compressing package:', package)
package_path = os.path.join(folder_path, package)
zip_dir(package_path, folder_path, zip_file)
print('Compressing translators')
zip_file = zip_dir('translators', current_path, zip_file)
return zip_file
def zip_dir(path, parent_path, zip_file):
for folder_path, subfolders, files in os.walk(path):
for file in files:
absolute_path = os.path.join(folder_path, file)
relative_path = os.path.relpath(absolute_path, parent_path)
zip_file.write(absolute_path, relative_path, compress_type=zipfile.ZIP_DEFLATED)
return zip_file
def delete_zip(zip_file_name):
current_path = os.path.dirname(os.path.realpath(__file__))
zip_file_path = os.path.join(current_path, zip_file_name)
if os.path.isfile(zip_file_path):
print('Removing existing file:', zip_file_name)
os.remove(zip_file_path)
# Command-line functions
def main():
args = parse_args()
delete_zip(args.filename)
if args.delete:
return True
zip_file = create_zip(args.filename)
zip_file.close()
return True
def parse_args():
parser = argparse.ArgumentParser(
description='create or delete the lambda handler zip file')
parser.add_argument(
'-f', '--filename',
metavar='FILENAME',
type=str,
default='lambda_handler.zip',
help='The name of the zip file'
)
parser.add_argument(
'-d', '--delete',
action='store_true',
help='Delete the zip file'
)
return parser.parse_args()
if __name__ == "__main__":
main()