Skip to content
This repository was archived by the owner on Mar 15, 2026. It is now read-only.

Commit 07a5d42

Browse files
committed
Fixed binary file handling
1 parent defa8b9 commit 07a5d42

2 files changed

Lines changed: 37 additions & 16 deletions

File tree

aws_lambda/aws_lambda.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
import pip
1313
import yaml
1414
from . import project_template
15-
from .helpers import mkdir, read, archive, timestamp
15+
from .helpers import mkdir, read, readBinary, archive, timestamp
1616

1717

1818
log = logging.getLogger(__name__)
1919

2020

21-
def deploy(src, local_package=None):
21+
def deploy(src, local_package=None, subfolders=None):
2222
"""Deploys a new function to AWS Lambda.
2323
2424
:param str src:
@@ -36,7 +36,7 @@ def deploy(src, local_package=None):
3636
# folder then add the handler file in the root of this directory.
3737
# Zip the contents of this folder into a single file and output to the dist
3838
# directory.
39-
path_to_zip_file = build(src, local_package)
39+
path_to_zip_file = build(src, local_package, subfolders)
4040

4141
if function_exists(cfg, cfg.get('function_name')):
4242
update_function(cfg, path_to_zip_file)
@@ -104,7 +104,7 @@ def init(src, minimal=False):
104104
copy(path_to_file, src)
105105

106106

107-
def build(src, local_package=None):
107+
def build(src, local_package=None, subfolders=None):
108108
"""Builds the file bundle.
109109
110110
:param str src:
@@ -130,28 +130,46 @@ def build(src, local_package=None):
130130
output_filename = "{0}-{1}.zip".format(timestamp(), function_name)
131131

132132
path_to_temp = mkdtemp(prefix='aws-lambda')
133-
pip_install_to_target(path_to_temp, local_package)
133+
# DOM: Disabled pip install for now as it is too heavy
134+
# pip_install_to_target(path_to_temp, local_package)
134135

135136
# Gracefully handle whether ".zip" was included in the filename or not.
136137
output_filename = ('{0}.zip'.format(output_filename)
137138
if not output_filename.endswith('.zip')
138139
else output_filename)
139140

140141
files = []
141-
for filename in os.listdir(src):
142-
if os.path.isfile(filename):
143-
if filename == '.DS_Store':
144-
continue
145-
if filename == 'config.yaml':
146-
continue
147-
files.append(os.path.join(src, filename))
142+
if subfolders is None:
143+
for filename in os.listdir(src):
144+
if os.path.isfile(filename):
145+
if filename == '.DS_Store':
146+
continue
147+
if filename == 'config.yaml':
148+
continue
149+
if filename.endswith('.pyc'): continue
150+
files.append(os.path.join(src, filename))
151+
else:
152+
for filename in os.listdir(src):
153+
if os.path.isdir(os.path.join(src, filename)) and filename in subfolders:
154+
for root, subdirs, subfiles in os.walk(os.path.join(src, filename)):
155+
for subfilename in subfiles:
156+
if subfilename.endswith('.pyc'): continue
157+
files.append(os.path.join(root, subfilename))
148158

149159
# "cd" into `temp_path` directory.
150160
os.chdir(path_to_temp)
151161
for f in files:
152-
_, filename = os.path.split(f)
162+
# _, filename = os.path.split(f)
163+
if not f.startswith(src):
164+
logging.warn('Unable to determine relative path for %s', f)
165+
continue
166+
167+
filename = f[len(src)+1:]
153168

154169
# Copy handler file into root of the packages folder.
170+
targetDir = os.path.dirname(os.path.join(path_to_temp, filename))
171+
if not os.path.exists(targetDir):
172+
os.makedirs(targetDir)
155173
copyfile(f, os.path.join(path_to_temp, filename))
156174

157175
# Zip them together into a single file.
@@ -242,7 +260,7 @@ def create_function(cfg, path_to_zip_file):
242260
"""Register and upload a function to AWS Lambda."""
243261

244262
print("Creating your new Lambda function")
245-
byte_stream = read(path_to_zip_file)
263+
byte_stream = readBinary(path_to_zip_file)
246264
aws_access_key_id = cfg.get('aws_access_key_id')
247265
aws_secret_access_key = cfg.get('aws_secret_access_key')
248266

@@ -268,8 +286,8 @@ def create_function(cfg, path_to_zip_file):
268286
def update_function(cfg, path_to_zip_file):
269287
"""Updates the code of an existing Lambda function"""
270288

271-
print("Updating your Lambda function")
272-
byte_stream = read(path_to_zip_file)
289+
print("Updating your Lambda function using", path_to_zip_file)
290+
byte_stream = readBinary(path_to_zip_file)
273291
aws_access_key_id = cfg.get('aws_access_key_id')
274292
aws_secret_access_key = cfg.get('aws_secret_access_key')
275293

aws_lambda/helpers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def read(path, loader=None):
1515
return fh.read()
1616
return loader(fh.read())
1717

18+
def readBinary(path):
19+
with open(path, 'rb') as fh:
20+
return fh.read()
1821

1922
def archive(src, dest, filename):
2023
output = os.path.join(dest, filename)

0 commit comments

Comments
 (0)