forked from awslabs/kinesis-aggregation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_lambda_build.py
More file actions
160 lines (118 loc) · 5.35 KB
/
make_lambda_build.py
File metadata and controls
160 lines (118 loc) · 5.35 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#Kinesis Aggregation/Deaggregation Libraries for Python
#
#Copyright 2014, Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
#Licensed under the Amazon Software License (the "License").
#You may not use this file except in compliance with the License.
#A copy of the License is located at
#
# http://aws.amazon.com/asl/
#
#or in the "license" file accompanying this file. This file is distributed
#on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
#express or implied. See the License for the specific language governing
#permissions and limitations under the License.
import os.path
import shutil
import subprocess
import sys
import zipfile
#Add your PIP dependencies here
PIP_DEPENDENCIES = ['protobuf']
BUILD_DIR_NAME = 'build'
cur_dir = None
proj_dir = None
build_dir = None
def is_python_file(filename):
'''Returns True if the input flie path has a .py extension. False otherwise.'''
return os.path.isfile(filename) and os.path.splitext(filename)[-1] == '.py'
def initialize_current_working_dir():
'''Forces the current working directory to be set to the directory where
this build script lives (which should be the python project root).'''
global cur_dir, proj_dir
cur_dir = os.path.normpath(os.getcwd())
print 'Current Working Directory = %s' % (os.getcwd())
proj_dir = os.path.normpath(os.path.dirname(os.path.realpath(__file__)))
print 'Project Directory = %s' % (proj_dir)
if cur_dir != proj_dir:
print 'Changing CWD to script directory...'
os.chdir(proj_dir)
print 'Current Working Directory = %s' % (os.getcwd())
def setup_build_dir():
'''Removes any existing build directories and creates a new one. Due to issues with
running "pip install <foo> -t <build_dir>" multiple times to the same directory, it's
easier to just create a new build dir every time.'''
global BUILD_DIR_NAME, build_dir
print ''
build_dir = os.path.join(os.getcwd(),BUILD_DIR_NAME)
print 'Setting up build directory: %s' % (build_dir)
if os.path.exists(build_dir):
shutil.rmtree(build_dir,True)
os.mkdir(build_dir)
def copy_source_to_build_dir():
'''Copy all Python source files to the build directory.'''
global BUILD_DIR_NAME, build_dir
print ''
print 'Looking for Python source files...'
for source in os.listdir(os.getcwd()):
if is_python_file(source) and source != __file__:
print 'Copy file %s to build directory...' % (source)
shutil.copy(source, build_dir)
elif os.path.isdir(source) and source != BUILD_DIR_NAME:
print 'Copy folder %s to build directory...' % (source)
shutil.copytree(source, os.path.join(build_dir,source))
def install_dependencies():
'''Using PIP, install all dependencies to the build directory.'''
global PIP_DEPENDENCIES, build_dir
#Make sure PIP is available on the command line
print ''
print 'Verifying PIP installation...'
with open(os.devnull,'w') as devnull:
aws_cmd_line_result = subprocess.call('pip', shell=True, stdout=devnull, stderr=subprocess.STDOUT)
if aws_cmd_line_result != 0:
print>>sys.stderr,'You do not have "pip" installed or it is not on your PATH. This script requires access to it.'
sys.exit(1)
print 'Successfully located "pip".'
#Install PIP dependencies to the build directory
print ''
print 'Installing necessary modules from pip...'
for dependency in PIP_DEPENDENCIES:
pip_install_cmd = 'pip install %s -t "%s"' % (dependency, build_dir)
print pip_install_cmd
pip_install_cmd_line_result = subprocess.call(pip_install_cmd, shell=True)
if pip_install_cmd_line_result != 0:
print>>sys.stderr,'Failed to install module via pip. Try running \'%s\' to debug the issue.' % (pip_install_cmd)
sys.exit(1)
print 'Successfully installed %s from pip.' % (dependency)
#AWS Lambda has issues with the normal protobuf install lacking a root level __init__.py
protobuf_install_dir = os.path.join(build_dir,'google')
protobuf_init_file = os.path.join(protobuf_install_dir,'__init__.py')
if os.path.exists(protobuf_install_dir) and not os.path.exists(protobuf_init_file):
open(protobuf_init_file, 'a').close()
def create_zip():
'''Zip up the contents of the build directory into a zip file that can be deployed
to AWS Lambda.'''
global build_dir
print ''
print 'Building zip file for AWS Lambda...'
zip_file_path = os.path.join(os.getcwd(),'python_lambda_build.zip')
os.chdir(build_dir)
with zipfile.PyZipFile(zip_file_path, 'w') as output_zip:
for item in os.listdir(build_dir):
if os.path.isdir(item) or is_python_file(item):
print 'Adding %s to zip...' % (item)
output_zip.writepy(item)
return zip_file_path
if __name__ == '__main__':
print ''
print 'Creating build for AWS Lambda...'
print ''
initialize_current_working_dir()
setup_build_dir()
copy_source_to_build_dir()
install_dependencies()
zip_file_path = create_zip()
print ''
print 'Successfully created Lambda build zip file: %s' % (zip_file_path)
print ''
sys.exit(0)