Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .samignore for excludes in python_pip_workfow #183

Closed
wants to merge 6 commits into from
Closed
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
16 changes: 14 additions & 2 deletions aws_lambda_builders/workflows/python_pip/workflow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Python PIP Workflow
"""
import pathlib
from aws_lambda_builders.workflow import BaseWorkflow, Capability
from aws_lambda_builders.actions import CopySourceAction
from aws_lambda_builders.workflows.python_pip.validator import PythonRuntimeValidator
Expand All @@ -16,7 +17,7 @@ class PythonPipWorkflow(BaseWorkflow):

# Common source files to exclude from build artifacts output
# Trimmed version of https://github.com/github/gitignore/blob/master/Python.gitignore
EXCLUDED_FILES = (
_DEFAULT_EXCLUDED_FILES = (
".aws-sam",
".chalice",
".git",
Expand Down Expand Up @@ -65,8 +66,19 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim

self.actions = [
PythonPipBuildAction(artifacts_dir, scratch_dir, manifest_path, runtime, binaries=self.binaries),
CopySourceAction(source_dir, artifacts_dir, excludes=self.EXCLUDED_FILES),
CopySourceAction(source_dir, artifacts_dir, excludes=self.excluded_files),
]

@property
def excluded_files(self):
"""
Use .samignore falling back to default exclude files
"""
sam_ignore = pathlib.Path("./.samignore")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.samignore is a new directory for users to have files that are excluded from the build correct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually a file, akin to .dockerignore, .gitignore and the like. Which users could place sam specific files/directories to ignore

if sam_ignore.exists():
return sam_ignore.read_text().splitlines()
else:
return self._DEFAULT_EXCLUDED_FILES

def get_validators(self):
return [PythonRuntimeValidator(runtime=self.runtime)]
28 changes: 28 additions & 0 deletions tests/unit/workflows/python_pip/test_workflow.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest
import pathlib
import mock
from unittest import TestCase

from aws_lambda_builders.actions import CopySourceAction
Expand All @@ -17,3 +20,28 @@ def test_workflow_sets_up_actions(self):
def test_workflow_validator(self):
for validator in self.workflow.get_validators():
self.assertTrue(isinstance(validator, PythonRuntimeValidator))


@pytest.mark.parametrize("use_samignore", (False, False))
def test_use_sam_ignore(tmpdir, use_samignore):
"""
Verify if there is a `.samignore` workflow will give that preference
over the default files to exclude.
"""

sam_ignore = pathlib.Path("{tmpdir}/.samignore".format(tmpdir=tmpdir))

if use_samignore:
sam_ignore.write_text("foo*.txt\n*-bar.so\n")

pathlib.Path = mock.MagicMock(return_value=sam_ignore)
mock.patch.object(sam_ignore, "exists", return_value=use_samignore)

_workflow = PythonPipWorkflow("source", "artifacts", "scratch_dir", "manifest", runtime="python3.7")

pathlib.Path.assert_called_once_with("./.samignore")

if use_samignore:
assert _workflow.excluded_files == ["foo*.txt", "*-bar.so"]
else:
assert _workflow.excluded_files == _workflow._DEFAULT_EXCLUDED_FILES