Skip to content

Commit

Permalink
Fixed archive being written to incorrect location
Browse files Browse the repository at this point in the history
  • Loading branch information
Drew Sonne committed Jun 26, 2017
1 parent 719266c commit 84183cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 109 deletions.
19 changes: 11 additions & 8 deletions src/main/python/awslambdahelper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self):
Add the cli argument schema
"""
super(BundlerArgumentParser, self).__init__()
self.add_argument('--directory', help='Path to the directory to bundle for AWS lambda.')
self.add_argument('--directory', help='Path to the directory to bundle for AWS lambda.', required=True)
self.add_argument('--requirements_name', default='requirements.txt',
help='Name of the requirements file in the target directory')

Expand Down Expand Up @@ -148,7 +148,9 @@ def run(self, args=None):
"-r", self.requirements_path
])

DirectoryZipFile(self.working_directory).create_archive()
temp_archive = DirectoryZipFile(self.working_directory).create_archive().archive_path

shutil.move(temp_archive, self.target_directory + ".zip")

def copy_lambda_package_files(self):
"""
Expand All @@ -168,7 +170,10 @@ def parse_args(args=None):
:param args:
:return:
"""
cli_args = BundlerArgumentParser().parse_args(sys.argv[1:] if args is None else args)
parser = BundlerArgumentParser()
cli_args = parser.parse_args(sys.argv[1:] if args is None else args)
if not cli_args.directory:
parser.print_help()
return (
cli_args.directory,
tempfile.mkdtemp(),
Expand Down Expand Up @@ -214,7 +219,7 @@ def write(self):
self.add_section('install')
self.set('install', 'prefix', '')

with open(self.temp_setup_cfg) as cfg:
with open(self.temp_setup_cfg, 'w') as cfg:
super(SetupCfgFile, self).write(cfg)


Expand All @@ -241,6 +246,8 @@ def create_archive(self):
"""
self.zipdir(self.source_path, self.source_path.rstrip('/') + '/')

return self

def zipdir(self, path, zip_path_prefix):
"""
Recursively walk our directory path, and add files to the zip archive.
Expand All @@ -259,7 +266,3 @@ def zipdir(self, path, zip_path_prefix):
archive_file = source_file.replace(zip_path_prefix, '')
print " Adding file: '" + archive_file + "'"
self.write(source_file, archive_file)


if __name__ == '__main__':
LambdahelperBundler().run()
103 changes: 2 additions & 101 deletions src/main/scripts/lambdahelper-bundler
Original file line number Diff line number Diff line change
@@ -1,104 +1,5 @@
#! /usr/bin/env python

import ConfigParser
import glob
import os
import shutil
import sys
import tempfile
from argparse import ArgumentParser
import pip
import zipfile


def main(args=sys.argv[1:]):
cli_parser = ArgumentParser()
cli_parser.add_argument('--directory', help='Path to the directory to bundle for AWS lambda.')
cli_parser.add_argument('--requirements_name', default='requirements.txt',
help='Name of the requirements file in the target directory')
cli_args = cli_parser.parse_args(args)

target_directory = full_path(cli_args.directory)
requirements_file = cli_args.requirements_name

requirements_path = os.path.join(target_directory, requirements_file)

if not os.path.exists(target_directory):
print "Could not find `--directory={dir}`.".format(dir=target_directory)
exit(1)
elif not os.path.isdir(target_directory):
print "`--directory={dir}` is not a directory.".format(dir=target_directory)
exit(2)
elif not os.path.exists(requirements_path):
print "Could not find requirements file at `{path}`.".format(path=requirements_path)
exit(3)

# Create temporary working directory
working_directory = tempfile.mkdtemp()
print "working in: " + working_directory

for file in glob.glob(target_directory + os.path.sep + "*.py"):
shutil.copy(file, working_directory)

shutil.copy(requirements_path, working_directory)

process_setup_cfg(target_directory, working_directory)

pip.main([
"install",
"-t", working_directory,
"-r", requirements_path
])

create_zip(target_directory, working_directory)


def full_path(dir_):
if dir_[0] == '~' and not os.path.exists(dir_):
dir_ = os.path.expanduser(dir_)
return os.path.abspath(dir_)


def create_zip(target_directory, working_directory):
zip_destination = target_directory.rstrip(os.path.sep) + '.zip'
print "Creating zip archive: '" + zip_destination + "'"

zipf = zipfile.ZipFile(zip_destination, 'w', zipfile.ZIP_DEFLATED)
zipdir(working_directory, zipf, working_directory.rstrip('/') + '/')

zipf.close()


def zipdir(path, ziph, zip_path_prefix):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
source_file = os.path.join(root, file)
archive_file = source_file.replace(zip_path_prefix, '')
print " Adding file: '" + archive_file + "'"
ziph.write(source_file, archive_file)


def process_setup_cfg(project_dir, working_directory):
if sys.platform == 'darwin':
setup_cfg_path = os.path.join(project_dir, 'setup.cfg')
temporary_cfg_path = os.path.join(working_directory, 'setup.cfg')

# If we already have a setup.cfg, modify it
if os.path.exists(setup_cfg_path):
setup_cfg = ConfigParser.ConfigParser()
setup_cfg.read(setup_cfg_path)
if 'install' not in setup_cfg.sections():
setup_cfg.add_section('install')
setup_cfg.set('install', 'prefix', '')

with open(temporary_cfg_path, 'w') as fp:
setup_cfg.write(fp)
# If we don't, just write a blank file out.
else:
with open(temporary_cfg_path, 'w+') as fp:
fp.write("""[install]\nprefix= """)

from awslambdahelper.cli import LambdahelperBundler

if __name__ == '__main__':
main()
LambdahelperBundler().run()

0 comments on commit 84183cb

Please sign in to comment.