Skip to content

Commit 893e6e8

Browse files
committed
Related to issue nficano#64 "Subdirectories not included in zip package".
Added the ability to explicitly define the list of "source" subdirectories in the config.yaml file under a new "build" configuration section. Decided it was perhaps best to allow explicit declaration of the folder list so we don't build undesirable (.git) folders into the zipfile. Modified the template config.yaml with this setting as well.
1 parent 8169e46 commit 893e6e8

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

aws_lambda/aws_lambda.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
import time
1010
from imp import load_source
1111
from shutil import copy
12-
from shutil import copyfile
12+
from shutil import copyfile, copytree
1313
from tempfile import mkdtemp
14+
from collections import defaultdict
1415

1516
import boto3
1617
import botocore
@@ -242,24 +243,32 @@ def build(src, requirements=False, local_package=None):
242243
else output_filename
243244
)
244245

246+
# Allow definition of source code directories we want to build into our zipped package.
247+
build_config = defaultdict(**cfg.get('build', {}))
248+
source_directories = [d.strip() for d in build_config.get('source_directories', '').split(',')]
249+
245250
files = []
246251
for filename in os.listdir(src):
247252
if os.path.isfile(filename):
248253
if filename == '.DS_Store':
249254
continue
250255
if filename == 'config.yaml':
251256
continue
252-
# TODO: Check subdirectories for '.DS_Store' files
253-
print('Bundling: %r' % filename)
254-
files.append(os.path.join(src, filename))
257+
elif os.path.isdir(filename) and filename in source_directories:
258+
print('Bundling directory: %r' % filename)
259+
files.append(os.path.join(src, filename))
255260

256261
# "cd" into `temp_path` directory.
257262
os.chdir(path_to_temp)
258263
for f in files:
259-
_, filename = os.path.split(f)
260-
261-
# Copy handler file into root of the packages folder.
262-
copyfile(f, os.path.join(path_to_temp, filename))
264+
if os.path.isfile(f):
265+
_, filename = os.path.split(f)
266+
267+
# Copy handler file into root of the packages folder.
268+
copyfile(f, os.path.join(path_to_temp, filename))
269+
elif os.path.isdir(f):
270+
destination_folder = os.path.join(path_to_temp, f[len(src) + 1:])
271+
copytree(f, destination_folder)
263272

264273
# Zip them together into a single file.
265274
# TODO: Delete temp directory created once the archive has been compiled.

aws_lambda/project_templates/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ aws_secret_access_key:
2525
environment_variables:
2626
env_1: foo
2727
env_2: baz
28+
29+
# Build options
30+
build:
31+
include_source_directories: lib # a common delimited list of directories in your project root that contains source to package.

0 commit comments

Comments
 (0)