Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions lambda_uploader/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import sys

from subprocess import Popen, PIPE
from lambda_uploader import utils

LOG = logging.getLogger(__name__)
TEMP_WORKSPACE_NAME = ".lambda_package"
Expand Down Expand Up @@ -102,28 +103,9 @@ def package(self):

shutil.copytree(os.path.join(self._pkg_venv, site_packages),
package)
self._copy_src_files(package)
utils.copy_tree(self._path, package, ignore=[TEMP_WORKSPACE_NAME])
self._create_zip(package)

def _copy_src_files(self, package):
LOG.info('Copying source files')
# Re-create cwd directory structure
for root, subdirs, files in os.walk(self._path):
for subdir in subdirs:
subdir_path = os.path.join(root, subdir)
if TEMP_WORKSPACE_NAME in subdir_path:
continue
fpath = os.path.join(package, subdir)
LOG.debug("Creating directory %s" % fpath)
os.mkdir(fpath)

for filename in files:
path = os.path.join(root, filename)
if TEMP_WORKSPACE_NAME in path:
continue

shutil.copy(path, package)

def _create_zip(self, src):
zfile = os.path.join(self._path, ZIPFILE_NAME)
LOG.info('Creating zipfile')
Expand Down
50 changes: 50 additions & 0 deletions lambda_uploader/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2015 Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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
import shutil
import logging


LOG = logging.getLogger(__name__)


def copy_tree(src, dest, ignore=[]):
LOG.info('Copying source files')
# Re-create directory structure
for root, _, files in os.walk(src):
for filename in files:
path = os.path.join(root, filename)
if _ignore_file(path, ignore):
continue

sub_dirs = os.path.dirname(os.path.relpath(path,
start=src))
pkg_path = os.path.join(dest, sub_dirs)
if not os.path.isdir(pkg_path):
os.makedirs(pkg_path)

LOG.debug("Copying %s to %s" % (path, pkg_path))
shutil.copy(path, pkg_path)


# Iterate through every item in ignore
# and check for matches in the path
def _ignore_file(path, ignore=[]):
if not ignore:
return False
for ign in ignore:
if ign in path:
return True
return False
47 changes: 47 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os

from os import path
from shutil import rmtree
from lambda_uploader import utils

TESTING_TEMP_DIR = '.testing_temp'

TEST_TREE = [
'foo.py',
'bar/foo.py',
'bar/bar/foo.py',
'ignore/foo.py'
]

TEST_IGNORE = ['ignore/']


def test_copy_tree():
os.mkdir(TESTING_TEMP_DIR)
for fil in TEST_TREE:
dir = path.dirname(fil)
test_pth = path.join(TESTING_TEMP_DIR, dir)
if dir is not None and not path.isdir(test_pth):
os.makedirs(test_pth)
with open(path.join(TESTING_TEMP_DIR, fil), 'w') as tfile:
tfile.write(fil)

copy_dir = '.copy_of_test'
utils.copy_tree(TESTING_TEMP_DIR, copy_dir, TEST_IGNORE)
for fil in TEST_TREE:
pth = path.join(copy_dir, fil)
if utils._ignore_file(fil, TEST_IGNORE):
assert path.isfile(pth) is not True
else:
assert path.isfile(pth)

rmtree(TESTING_TEMP_DIR)
rmtree(copy_dir)


def test_ignore_file():
result = utils._ignore_file('ignore/foo.py', TEST_IGNORE)
assert result

res = utils._ignore_file('bar/foo.py', TEST_IGNORE)
assert res is False