Skip to content

Commit 6f94390

Browse files
authored
Merge pull request #326 from aws/develop
release: 1.11.0
2 parents 3257a8f + fe72f73 commit 6f94390

File tree

85 files changed

+2612
-151
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2612
-151
lines changed

.appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ image:
66
environment:
77
GOVERSION: 1.11
88
GRADLE_OPTS: -Dorg.gradle.daemon=false
9-
nodejs_version: "10.10.0"
9+
nodejs_version: "14.17.6"
1010

1111
matrix:
1212
- PYTHON: "C:\\Python36-x64"
@@ -93,7 +93,7 @@ for:
9393
- sh: "source ${HOME}/venv${PYTHON_VERSION}/bin/activate"
9494
- sh: "rvm use 2.5"
9595
- sh: "nvm install ${nodejs_version}"
96-
- sh: "npm install npm@5.6.0 -g"
96+
- sh: "npm install npm@7.24.2 -g"
9797
- sh: "npm -v"
9898
- sh: "echo $PATH"
9999
- sh: "java --version"

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ init:
44
test:
55
# Run unit tests
66
# Fail if coverage falls below 94%
7-
LAMBDA_BUILDERS_DEV=1 pytest --cov aws_lambda_builders --cov-report term-missing --cov-fail-under 94 tests/unit tests/functional
7+
LAMBDA_BUILDERS_DEV=1 pytest -vv --cov aws_lambda_builders --cov-report term-missing --cov-fail-under 94 tests/unit tests/functional
88

99
func-test:
1010
LAMBDA_BUILDERS_DEV=1 pytest tests/functional

aws_lambda_builders/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
22
AWS Lambda Builder Library
33
"""
4-
__version__ = "1.10.0"
4+
__version__ = "1.11.0"
55
RPC_PROTOCOL_VERSION = "0.3"

aws_lambda_builders/builder.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ def build(
6969
dependencies_dir=None,
7070
combine_dependencies=True,
7171
architecture=X86_64,
72+
is_building_layer=False,
73+
experimental_flags=None,
7274
):
75+
# pylint: disable-msg=too-many-locals
7376
"""
7477
Actually build the code by running workflows
7578
@@ -127,6 +130,14 @@ def build(
127130
:type architecture: str
128131
:param architecture:
129132
Type of architecture x86_64 and arm64 for Lambda Function
133+
134+
:type is_building_layer: bool
135+
:param is_building_layer:
136+
Boolean flag which will be set True if current build operation is being executed for layers
137+
138+
:type experimental_flags: list
139+
:param experimental_flags:
140+
List of strings, which will indicate enabled experimental flags for the current build session
130141
"""
131142

132143
if not os.path.exists(scratch_dir):
@@ -146,6 +157,8 @@ def build(
146157
dependencies_dir=dependencies_dir,
147158
combine_dependencies=combine_dependencies,
148159
architecture=architecture,
160+
is_building_layer=is_building_layer,
161+
experimental_flags=experimental_flags,
149162
)
150163

151164
return workflow.run()

aws_lambda_builders/utils.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
LOG = logging.getLogger(__name__)
1313

1414

15-
def copytree(source, destination, ignore=None):
15+
def copytree(source, destination, ignore=None, include=None):
1616
"""
1717
Similar to shutil.copytree except that it removes the limitation that the destination directory should
1818
be present.
@@ -29,17 +29,25 @@ def copytree(source, destination, ignore=None):
2929
:param ignore:
3030
A function that returns a set of file names to ignore, given a list of available file names. Similar to the
3131
``ignore`` property of ``shutils.copytree`` method
32+
33+
:type include: Callable[[str], bool]
34+
:param include:
35+
A function that will decide whether a file should be copied or skipped it. It accepts file name as parameter
36+
and return True or False. Returning True will continue copy operation, returning False will skip copy operation
37+
for that file
3238
"""
3339

3440
if not os.path.exists(source):
3541
LOG.warning("Skipping copy operation since source %s does not exist", source)
3642
return
3743

3844
if not os.path.exists(destination):
45+
LOG.debug("Creating target folders at %s", destination)
3946
os.makedirs(destination)
4047

4148
try:
4249
# Let's try to copy the directory metadata from source to destination
50+
LOG.debug("Copying directory metadata from source (%s) to destination (%s)", source, destination)
4351
shutil.copystat(source, destination)
4452
except OSError as ex:
4553
# Can't copy file access times in Windows
@@ -54,14 +62,20 @@ def copytree(source, destination, ignore=None):
5462
for name in names:
5563
# Skip ignored names
5664
if name in ignored_names:
65+
LOG.debug("File (%s) is in ignored set, skipping it", name)
5766
continue
5867

5968
new_source = os.path.join(source, name)
6069
new_destination = os.path.join(destination, name)
6170

71+
if include and not os.path.isdir(new_source) and not include(name):
72+
LOG.debug("File (%s) doesn't satisfy the include rule, skipping it", name)
73+
continue
74+
6275
if os.path.isdir(new_source):
63-
copytree(new_source, new_destination, ignore=ignore)
76+
copytree(new_source, new_destination, ignore=ignore, include=include)
6477
else:
78+
LOG.debug("Copying source file (%s) to destination (%s)", new_source, new_destination)
6579
shutil.copy2(new_source, new_destination)
6680

6781

aws_lambda_builders/workflow.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ def __init__(
164164
dependencies_dir=None,
165165
combine_dependencies=True,
166166
architecture=X86_64,
167+
is_building_layer=False,
168+
experimental_flags=None,
167169
):
170+
# pylint: disable-msg=too-many-locals
168171
"""
169172
Initialize the builder with given arguments. These arguments together form the "public API" that each
170173
build action must support at the minimum.
@@ -200,6 +203,12 @@ def __init__(
200203
from dependency_folder into build folder
201204
architecture : str, optional
202205
Architecture type either arm64 or x86_64 for which the build will be based on in AWS lambda, by default X86_64
206+
207+
is_building_layer: bool, optional
208+
Boolean flag which will be set True if current build operation is being executed for layers
209+
210+
experimental_flags: list, optional
211+
List of strings, which will indicate enabled experimental flags for the current build session
203212
"""
204213

205214
self.source_dir = source_dir
@@ -215,6 +224,8 @@ def __init__(
215224
self.dependencies_dir = dependencies_dir
216225
self.combine_dependencies = combine_dependencies
217226
self.architecture = architecture
227+
self.is_building_layer = is_building_layer
228+
self.experimental_flags = experimental_flags if experimental_flags else []
218229

219230
# Actions are registered by the subclasses as they seem fit
220231
self.actions = []

aws_lambda_builders/workflows/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
import aws_lambda_builders.workflows.java_maven
1212
import aws_lambda_builders.workflows.dotnet_clipackage
1313
import aws_lambda_builders.workflows.custom_make
14+
import aws_lambda_builders.workflows.nodejs_npm_esbuild

aws_lambda_builders/workflows/java/utils.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import platform
77
import shutil
88
import subprocess
9-
from aws_lambda_builders.utils import which
9+
from aws_lambda_builders.utils import which, copytree
10+
11+
12+
EXPERIMENTAL_MAVEN_SCOPE_AND_LAYER_FLAG = "experimentalMavenScopeAndLayer"
1013

1114

1215
class OSUtils(object):
@@ -37,17 +40,8 @@ def exists(self, p):
3740
def which(self, executable, executable_search_paths=None):
3841
return which(executable, executable_search_paths=executable_search_paths)
3942

40-
def copytree(self, source, destination):
41-
if not os.path.exists(destination):
42-
self.makedirs(destination)
43-
names = self.listdir(source)
44-
for name in names:
45-
new_source = os.path.join(source, name)
46-
new_destination = os.path.join(destination, name)
47-
if os.path.isdir(new_source):
48-
self.copytree(new_source, new_destination)
49-
else:
50-
self.copy(new_source, new_destination)
43+
def copytree(self, source, destination, ignore=None, include=None):
44+
copytree(source, destination, ignore=ignore, include=include)
5145

5246
def makedirs(self, d):
5347
return os.makedirs(d)
@@ -58,3 +52,21 @@ def rmtree(self, d):
5852
@property
5953
def pipe(self):
6054
return subprocess.PIPE
55+
56+
57+
def jar_file_filter(file_name):
58+
"""
59+
A function that will filter .jar files for copy operation
60+
61+
:type file_name: str
62+
:param file_name:
63+
Name of the file that will be checked against if it ends with .jar or not
64+
"""
65+
return bool(file_name) and isinstance(file_name, str) and file_name.endswith(".jar")
66+
67+
68+
def is_experimental_maven_scope_and_layers_active(experimental_flags):
69+
"""
70+
A function which will determine if experimental maven scope and layer changes are active
71+
"""
72+
return bool(experimental_flags) and EXPERIMENTAL_MAVEN_SCOPE_AND_LAYER_FLAG in experimental_flags

aws_lambda_builders/workflows/java_gradle/actions.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
from aws_lambda_builders.actions import ActionFailedError, BaseAction, Purpose
77
from .gradle import GradleExecutionError
8+
from ..java.utils import jar_file_filter
89

910

1011
class JavaGradleBuildAction(BaseAction):
@@ -56,7 +57,7 @@ def _build_project(self, init_script_file):
5657

5758

5859
class JavaGradleCopyArtifactsAction(BaseAction):
59-
NAME = "CopyArtifacts"
60+
NAME = "JavaGradleCopyArtifacts"
6061
DESCRIPTION = "Copying the built artifacts"
6162
PURPOSE = Purpose.COPY_SOURCE
6263

@@ -77,3 +78,26 @@ def _copy_artifacts(self):
7778
self.os_utils.copytree(lambda_build_output, self.artifacts_dir)
7879
except Exception as ex:
7980
raise ActionFailedError(str(ex))
81+
82+
83+
class JavaGradleCopyLayerArtifactsAction(JavaGradleCopyArtifactsAction):
84+
"""
85+
Java layers does not support using .class files in it.
86+
This action (different from the parent one) copies contents of the layer as jar files and place it
87+
into the artifact folder
88+
"""
89+
90+
NAME = "JavaGradleCopyLayerArtifacts"
91+
92+
def _copy_artifacts(self):
93+
lambda_build_output = os.path.join(self.build_dir, "build", "libs")
94+
layer_dependencies = os.path.join(self.build_dir, "build", "distributions", "lambda-build", "lib")
95+
try:
96+
if not self.os_utils.exists(self.artifacts_dir):
97+
self.os_utils.makedirs(self.artifacts_dir)
98+
self.os_utils.copytree(
99+
lambda_build_output, os.path.join(self.artifacts_dir, "lib"), include=jar_file_filter
100+
)
101+
self.os_utils.copytree(layer_dependencies, os.path.join(self.artifacts_dir, "lib"), include=jar_file_filter)
102+
except Exception as ex:
103+
raise ActionFailedError(str(ex))

aws_lambda_builders/workflows/java_gradle/workflow.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from aws_lambda_builders.actions import CleanUpAction
77
from aws_lambda_builders.workflow import BaseWorkflow, Capability
88
from aws_lambda_builders.workflows.java.actions import JavaCopyDependenciesAction, JavaMoveDependenciesAction
9-
from aws_lambda_builders.workflows.java.utils import OSUtils
9+
from aws_lambda_builders.workflows.java.utils import OSUtils, is_experimental_maven_scope_and_layers_active
1010

11-
from .actions import JavaGradleBuildAction, JavaGradleCopyArtifactsAction
11+
from .actions import JavaGradleBuildAction, JavaGradleCopyArtifactsAction, JavaGradleCopyLayerArtifactsAction
1212
from .gradle import SubprocessGradle
1313
from .gradle_resolver import GradleResolver
1414
from .gradle_validator import GradleValidator
@@ -33,9 +33,16 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, **kwar
3333

3434
subprocess_gradle = SubprocessGradle(gradle_binary=self.binaries["gradle"], os_utils=self.os_utils)
3535

36+
copy_artifacts_action = JavaGradleCopyArtifactsAction(
37+
source_dir, artifacts_dir, self.build_output_dir, self.os_utils
38+
)
39+
if self.is_building_layer and is_experimental_maven_scope_and_layers_active(self.experimental_flags):
40+
copy_artifacts_action = JavaGradleCopyLayerArtifactsAction(
41+
source_dir, artifacts_dir, self.build_output_dir, self.os_utils
42+
)
3643
self.actions = [
3744
JavaGradleBuildAction(source_dir, manifest_path, subprocess_gradle, scratch_dir, self.os_utils),
38-
JavaGradleCopyArtifactsAction(source_dir, artifacts_dir, self.build_output_dir, self.os_utils),
45+
copy_artifacts_action,
3946
]
4047

4148
if self.dependencies_dir:

0 commit comments

Comments
 (0)