This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
/
build.py
168 lines (129 loc) · 4.02 KB
/
build.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
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
161
162
163
164
165
166
167
168
# Builds a zip file from the source_dir or source_file.
# Installs dependencies with pip automatically.
import os
import shlex
import shutil
import subprocess
import sys
import tempfile
from contextlib import contextmanager
@contextmanager
def cd(path):
"""
Changes the working directory.
"""
cwd = os.getcwd()
print('cd', path)
try:
os.chdir(path)
yield
finally:
os.chdir(cwd)
def format_command(command):
"""
Formats a command for displaying on screen.
"""
args = []
for arg in command:
if ' ' in arg:
args.append('"' + arg + '"')
else:
args.append(arg)
return ' '.join(args)
def list_files(top_path):
"""
Returns a sorted list of all files in a directory.
"""
results = []
for root, dirs, files in os.walk(top_path):
if root.endswith("__pycache__"):
continue
for file_name in files:
file_path = os.path.join(root, file_name)
relative_path = os.path.relpath(file_path, top_path)
results.append(relative_path)
results.sort()
return results
def run(*args, **kwargs):
"""
Runs a command.
"""
print(format(format_command(args)))
sys.stdout.flush()
subprocess.check_call(args, **kwargs)
@contextmanager
def tempdir():
"""
Creates a temporary directory and then deletes it afterwards.
"""
print('mktemp -d')
path = tempfile.mkdtemp(prefix='terraform-aws-lambda-')
print(path)
try:
yield path
finally:
shutil.rmtree(path)
def create_zip_file(source_dir, target_file):
"""
Creates a zip file from a directory.
"""
target_file = os.path.abspath(target_file)
target_dir = os.path.dirname(target_file)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
target_base, _ = os.path.splitext(target_file)
shutil.make_archive(
target_base,
format='zip',
root_dir=source_dir,
)
def dequote(value):
"""
Handles quotes around values in a shell-compatible fashion.
"""
return ' '.join(shlex.split(value))
filename = dequote(sys.argv[1])
runtime = dequote(sys.argv[2])
source_path = dequote(sys.argv[3])
absolute_filename = os.path.abspath(filename)
# Create a temporary directory for building the archive,
# so no changes will be made to the source directory.
with tempdir() as temp_dir:
# Find all source files.
if os.path.isdir(source_path):
source_dir = source_path
source_files = list_files(source_path)
else:
source_dir = os.path.dirname(source_path)
source_files = [os.path.basename(source_path)]
# Copy them into the temporary directory.
with cd(source_dir):
for file_name in source_files:
target_path = os.path.join(temp_dir, file_name)
target_dir = os.path.dirname(target_path)
if not os.path.exists(target_dir):
print('mkdir -p {}'.format(target_dir))
os.makedirs(target_dir)
print('cp {} {}'.format(file_name, target_path))
shutil.copyfile(file_name, target_path)
shutil.copymode(file_name, target_path)
# Install dependencies into the temporary directory.
if runtime.startswith('python'):
requirements = os.path.join(temp_dir, 'requirements.txt')
if os.path.exists(requirements):
with cd(temp_dir):
if runtime.startswith('python3'):
pip_command = 'pip3'
else:
pip_command = 'pip2'
run(
pip_command,
'install',
'--prefix=',
'--target=.',
'--requirement=requirements.txt',
)
# Zip up the temporary directory and write it to the target filename.
# This will be used by the Lambda function as the source code package.
create_zip_file(temp_dir, absolute_filename)
print('Created {}'.format(filename))