Skip to content

Commit 826e4ef

Browse files
authored
Merge pull request #381 from aws/develop
aws-lambda-builders release 1.19.0
2 parents 7c29f65 + cec166c commit 826e4ef

File tree

39 files changed

+1584
-650
lines changed

39 files changed

+1584
-650
lines changed

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.18.0"
4+
__version__ = "1.19.0"
55
RPC_PROTOCOL_VERSION = "0.3"

aws_lambda_builders/actions.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import logging
66
import os
77
import shutil
8+
from pathlib import Path
89
from typing import Set, Iterator, Tuple
910

11+
from aws_lambda_builders import utils
1012
from aws_lambda_builders.utils import copytree
1113

1214
LOG = logging.getLogger(__name__)
@@ -31,6 +33,9 @@ class Purpose(object):
3133
# Action is copying source code
3234
COPY_SOURCE = "COPY_SOURCE"
3335

36+
# Action is linking source code
37+
LINK_SOURCE = "LINK_SOURCE"
38+
3439
# Action is copying dependencies
3540
COPY_DEPENDENCIES = "COPY_DEPENDENCIES"
3641

@@ -111,6 +116,31 @@ def execute(self):
111116
copytree(self.source_dir, self.dest_dir, ignore=shutil.ignore_patterns(*self.excludes))
112117

113118

119+
class LinkSourceAction(BaseAction):
120+
121+
NAME = "LinkSource"
122+
123+
DESCRIPTION = "Linking source code to the target folder"
124+
125+
PURPOSE = Purpose.LINK_SOURCE
126+
127+
def __init__(self, source_dir, dest_dir):
128+
self._source_dir = source_dir
129+
self._dest_dir = dest_dir
130+
131+
def execute(self):
132+
source_files = set(os.listdir(self._source_dir))
133+
134+
for source_file in source_files:
135+
source_path = Path(self._source_dir, source_file)
136+
destination_path = Path(self._dest_dir, source_file)
137+
if destination_path.exists():
138+
os.remove(destination_path)
139+
else:
140+
os.makedirs(destination_path.parent, exist_ok=True)
141+
utils.create_symlink_or_copy(str(source_path), str(destination_path))
142+
143+
114144
class CopyDependenciesAction(BaseAction):
115145

116146
NAME = "CopyDependencies"
@@ -175,10 +205,10 @@ def __init__(self, target_dir):
175205

176206
def execute(self):
177207
if not os.path.isdir(self.target_dir):
178-
LOG.info("Clean up action: %s does not exist and will be skipped.", str(self.target_dir))
208+
LOG.debug("Clean up action: %s does not exist and will be skipped.", str(self.target_dir))
179209
return
180210
targets = os.listdir(self.target_dir)
181-
LOG.info("Clean up action: folder %s will be cleaned", str(self.target_dir))
211+
LOG.debug("Clean up action: folder %s will be cleaned", str(self.target_dir))
182212

183213
for name in targets:
184214
target_path = os.path.join(self.target_dir, name)

aws_lambda_builders/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class MisMatchRuntimeError(LambdaBuilderError):
1919
MESSAGE = (
2020
"{language} executable found in your path does not "
2121
"match runtime. "
22-
"\n Expected version: {required_runtime}, Found version: {runtime_path}. "
22+
"\n Expected version: {required_runtime}, Found a different version at {runtime_path}. "
2323
"\n Possibly related: https://github.com/awslabs/aws-lambda-builders/issues/30"
2424
)
2525

aws_lambda_builders/path_resolver.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66

77

88
class PathResolver(object):
9-
def __init__(self, binary, runtime, executable_search_paths=None):
9+
def __init__(self, binary, runtime, additional_binaries=None, executable_search_paths=None):
1010
self.binary = binary
1111
self.runtime = runtime
1212
self.executables = [self.runtime, self.binary]
13+
self.additional_binaries = additional_binaries
14+
if isinstance(additional_binaries, list):
15+
self.executables = self.executables + self.additional_binaries
16+
1317
self.executable_search_paths = executable_search_paths
1418

1519
def _which(self):

aws_lambda_builders/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77
import os
88
import logging
9+
from pathlib import Path
910

1011
from aws_lambda_builders.architecture import X86_64, ARM64
1112

@@ -182,3 +183,16 @@ def get_goarch(architecture):
182183
returns a valid GO Architecture value
183184
"""
184185
return "arm64" if architecture == ARM64 else "amd64"
186+
187+
188+
def create_symlink_or_copy(source: str, destination: str) -> None:
189+
"""Tries to create symlink, if it fails it will copy source into destination"""
190+
LOG.debug("Creating symlink; source: %s, destination: %s", source, destination)
191+
try:
192+
os.symlink(Path(source).absolute(), Path(destination).absolute())
193+
except OSError as ex:
194+
LOG.warning(
195+
"Symlink operation is failed, falling back to copying files",
196+
exc_info=ex if LOG.isEnabledFor(logging.DEBUG) else None,
197+
)
198+
copytree(source, destination)

aws_lambda_builders/workflows/custom_make/actions.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ class CustomMakeAction(BaseAction):
2222
DESCRIPTION = "Running build target on Makefile"
2323
PURPOSE = Purpose.COMPILE_SOURCE
2424

25-
def __init__(self, artifacts_dir, scratch_dir, manifest_path, osutils, subprocess_make, build_logical_id):
25+
def __init__(
26+
self,
27+
artifacts_dir,
28+
scratch_dir,
29+
manifest_path,
30+
osutils,
31+
subprocess_make,
32+
build_logical_id,
33+
working_directory=None,
34+
):
2635
"""
2736
:type artifacts_dir: str
2837
:param artifacts_dir: directory where artifacts needs to be stored.
@@ -38,6 +47,13 @@ def __init__(self, artifacts_dir, scratch_dir, manifest_path, osutils, subproces
3847
3948
:type subprocess_make aws_lambda_builders.workflows.custom_make.make.SubprocessMake
4049
:param subprocess_make: An instance of the Make process wrapper
50+
51+
:type build_logical_id: str
52+
:param build_logical_id: the lambda resource logical id that will be built by the custom action.
53+
54+
:type working_directory: str
55+
:param working_directory: path to the working directory where the Makefile will be executed. Use the scratch_dir
56+
as the working directory if the input working_directory is None
4157
"""
4258
super(CustomMakeAction, self).__init__()
4359
self.artifacts_dir = artifacts_dir
@@ -46,6 +62,7 @@ def __init__(self, artifacts_dir, scratch_dir, manifest_path, osutils, subproces
4662
self.osutils = osutils
4763
self.subprocess_make = subprocess_make
4864
self.build_logical_id = build_logical_id
65+
self.working_directory = working_directory if working_directory else scratch_dir
4966

5067
@property
5168
def artifact_dir_path(self):
@@ -91,7 +108,7 @@ def execute(self):
91108
"build-{logical_id}".format(logical_id=self.build_logical_id),
92109
],
93110
env=current_env,
94-
cwd=self.scratch_dir,
111+
cwd=self.working_directory,
95112
)
96113
except MakeExecutionError as ex:
97114
raise ActionFailedError(str(ex))

aws_lambda_builders/workflows/custom_make/workflow.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim
3434
# Find the logical id of the function to be built.
3535
options = kwargs.get("options") or {}
3636
build_logical_id = options.get("build_logical_id", None)
37+
working_directory = options.get("working_directory", scratch_dir)
3738

3839
if not build_logical_id:
3940
raise WorkflowFailedError(
@@ -51,6 +52,7 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim
5152
osutils=self.os_utils,
5253
subprocess_make=subprocess_make,
5354
build_logical_id=build_logical_id,
55+
working_directory=working_directory,
5456
)
5557

5658
self.actions = [CopySourceAction(source_dir, scratch_dir, excludes=self.EXCLUDED_FILES), make_action]

aws_lambda_builders/workflows/nodejs_npm/actions.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class NodejsNpmInstallAction(BaseAction):
8080
DESCRIPTION = "Installing dependencies from NPM"
8181
PURPOSE = Purpose.RESOLVE_DEPENDENCIES
8282

83-
def __init__(self, artifacts_dir, subprocess_npm, is_production=True):
83+
def __init__(self, artifacts_dir, subprocess_npm):
8484
"""
8585
:type artifacts_dir: str
8686
:param artifacts_dir: an existing (writable) directory with project source files.
@@ -96,22 +96,18 @@ def __init__(self, artifacts_dir, subprocess_npm, is_production=True):
9696
super(NodejsNpmInstallAction, self).__init__()
9797
self.artifacts_dir = artifacts_dir
9898
self.subprocess_npm = subprocess_npm
99-
self.is_production = is_production
10099

101100
def execute(self):
102101
"""
103102
Runs the action.
104103
105104
:raises lambda_builders.actions.ActionFailedError: when NPM execution fails
106105
"""
107-
108-
mode = "--production" if self.is_production else "--production=false"
109-
110106
try:
111107
LOG.debug("NODEJS installing in: %s", self.artifacts_dir)
112108

113109
self.subprocess_npm.run(
114-
["install", "-q", "--no-audit", "--no-save", mode, "--unsafe-perm"], cwd=self.artifacts_dir
110+
["install", "-q", "--no-audit", "--no-save", "--unsafe-perm", "--production"], cwd=self.artifacts_dir
115111
)
116112

117113
except NpmExecutionError as ex:

aws_lambda_builders/workflows/nodejs_npm/workflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def get_resolvers(self):
145145
return [PathResolver(runtime=self.runtime, binary="npm")]
146146

147147
@staticmethod
148-
def get_install_action(source_dir, artifacts_dir, subprocess_npm, osutils, build_options, is_production=True):
148+
def get_install_action(source_dir, artifacts_dir, subprocess_npm, osutils, build_options):
149149
"""
150150
Get the install action used to install dependencies at artifacts_dir
151151
@@ -180,4 +180,4 @@ def get_install_action(source_dir, artifacts_dir, subprocess_npm, osutils, build
180180
if (osutils.file_exists(lockfile_path) or osutils.file_exists(shrinkwrap_path)) and npm_ci_option:
181181
return NodejsNpmCIAction(artifacts_dir, subprocess_npm=subprocess_npm)
182182

183-
return NodejsNpmInstallAction(artifacts_dir, subprocess_npm=subprocess_npm, is_production=is_production)
183+
return NodejsNpmInstallAction(artifacts_dir, subprocess_npm=subprocess_npm)

aws_lambda_builders/workflows/nodejs_npm_esbuild/DESIGN.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,23 @@ testing flow before invoking `sam build`. For additional typescript caveats with
117117

118118
#### Configuring the bundler
119119

120-
The Lambda builder invokes `esbuild` with sensible defaults that will work for the majority of cases. Importantly, the following three parameters are set by default
120+
The Lambda builder invokes `esbuild` with sensible defaults that will work for the majority of cases. Importantly, the following parameters are set by default
121121

122122
* `--minify`, as it [produces a smaller runtime package](https://esbuild.github.io/api/#minify)
123-
* `--sourcemap`, as it generates a [source map that allows for correct stack trace reporting](https://esbuild.github.io/api/#sourcemap) in case of errors (see the [Error reporting](#error-reporting) section above)
124123
* `--target es2020`, as it allows for javascript features present in Node 14
125124

126125
Users might want to tweak some of these runtime arguments for a specific project, for example not including the source map to further reduce the package size, or restricting javascript features to an older version. The Lambda builder allows this with optional sub-properties of the `aws_sam` configuration property.
127126

128127
* `target`: string, corresponding to a supported [esbuild target](https://esbuild.github.io/api/#target) property
129128
* `minify`: boolean, defaulting to `true`
130-
* `sourcemap`: boolean, defaulting to `true`
129+
* `sourcemap`: boolean, defaulting to `false`
131130

132-
Here is an example that deactivates minification and source maps, and supports JavaScript features compatible with Node.js version 10.
131+
Here is an example that deactivates minification, enables source maps, and supports JavaScript features compatible with Node.js version 10.
133132

134133
```json
135134
{
136135
"entry_points": ["included.ts"],
137136
"target": "node10",
138137
"minify": false,
139-
"sourcemap": false
138+
"sourcemap": true
140139
}

0 commit comments

Comments
 (0)