Skip to content

Commit 61f74a2

Browse files
authored
Merge pull request #346 from aws/develop
chore: Merge develop into master
2 parents fef06ac + 1310812 commit 61f74a2

File tree

26 files changed

+397
-65
lines changed

26 files changed

+397
-65
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.13.0"
4+
__version__ = "1.14.0"
55
RPC_PROTOCOL_VERSION = "0.3"

aws_lambda_builders/workflows/go_dep/workflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import logging
6-
import os
6+
from warnings import warn
77

88
from aws_lambda_builders.actions import CopySourceAction
99
from aws_lambda_builders.workflow import BaseWorkflow, Capability
@@ -27,7 +27,7 @@ class GoDepWorkflow(BaseWorkflow):
2727
EXCLUDED_FILES = (".aws-sam", ".git")
2828

2929
def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=None, osutils=None, **kwargs):
30-
30+
warn(f"{self.__class__.__name__} will be removed on April 11, 2022.", DeprecationWarning, stacklevel=2)
3131
super(GoDepWorkflow, self).__init__(
3232
source_dir, artifacts_dir, scratch_dir, manifest_path, runtime=runtime, **kwargs
3333
)

aws_lambda_builders/workflows/nodejs_npm/actions.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ def execute(self):
161161
raise ActionFailedError(str(ex))
162162

163163

164-
class NodejsNpmrcCopyAction(BaseAction):
164+
class NodejsNpmrcAndLockfileCopyAction(BaseAction):
165165

166166
"""
167-
A Lambda Builder Action that copies NPM config file .npmrc
167+
A Lambda Builder Action that copies lockfile and NPM config file .npmrc
168168
"""
169169

170-
NAME = "CopyNpmrc"
171-
DESCRIPTION = "Copying configuration from .npmrc"
170+
NAME = "CopyNpmrcAndLockfile"
171+
DESCRIPTION = "Copying configuration from .npmrc and dependencies from lockfile"
172172
PURPOSE = Purpose.COPY_SOURCE
173173

174174
def __init__(self, artifacts_dir, source_dir, osutils):
@@ -184,7 +184,7 @@ def __init__(self, artifacts_dir, source_dir, osutils):
184184
:param osutils: An instance of OS Utilities for file manipulation
185185
"""
186186

187-
super(NodejsNpmrcCopyAction, self).__init__()
187+
super(NodejsNpmrcAndLockfileCopyAction, self).__init__()
188188
self.artifacts_dir = artifacts_dir
189189
self.source_dir = source_dir
190190
self.osutils = osutils
@@ -193,14 +193,15 @@ def execute(self):
193193
"""
194194
Runs the action.
195195
196-
:raises lambda_builders.actions.ActionFailedError: when .npmrc copying fails
196+
:raises lambda_builders.actions.ActionFailedError: when copying fails
197197
"""
198198

199199
try:
200-
npmrc_path = self.osutils.joinpath(self.source_dir, ".npmrc")
201-
if self.osutils.file_exists(npmrc_path):
202-
LOG.debug(".npmrc copying in: %s", self.artifacts_dir)
203-
self.osutils.copy_file(npmrc_path, self.artifacts_dir)
200+
for filename in [".npmrc", "package-lock.json"]:
201+
file_path = self.osutils.joinpath(self.source_dir, filename)
202+
if self.osutils.file_exists(file_path):
203+
LOG.debug("%s copying in: %s", filename, self.artifacts_dir)
204+
self.osutils.copy_file(file_path, self.artifacts_dir)
204205

205206
except OSError as ex:
206207
raise ActionFailedError(str(ex))

aws_lambda_builders/workflows/nodejs_npm/workflow.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
NodejsNpmPackAction,
1818
NodejsNpmLockFileCleanUpAction,
1919
NodejsNpmInstallAction,
20-
NodejsNpmrcCopyAction,
20+
NodejsNpmrcAndLockfileCopyAction,
2121
NodejsNpmrcCleanUpAction,
22+
NodejsNpmCIAction,
2223
)
2324
from .utils import OSUtils
2425
from .npm import SubprocessNpm
@@ -91,17 +92,21 @@ def actions_without_bundler(self, source_dir, artifacts_dir, scratch_dir, manife
9192
npm_pack = NodejsNpmPackAction(
9293
tar_dest_dir, scratch_dir, manifest_path, osutils=osutils, subprocess_npm=subprocess_npm
9394
)
94-
npm_copy_npmrc = NodejsNpmrcCopyAction(tar_package_dir, source_dir, osutils=osutils)
95+
96+
npm_copy_npmrc_and_lockfile = NodejsNpmrcAndLockfileCopyAction(tar_package_dir, source_dir, osutils=osutils)
9597

9698
actions = [
9799
npm_pack,
98-
npm_copy_npmrc,
100+
npm_copy_npmrc_and_lockfile,
99101
CopySourceAction(tar_package_dir, artifacts_dir, excludes=self.EXCLUDED_FILES),
100102
]
101103

102104
if self.download_dependencies:
103105
# installed the dependencies into artifact folder
104-
actions.append(NodejsNpmInstallAction(artifacts_dir, subprocess_npm=subprocess_npm))
106+
install_action = NodejsNpmWorkflow.get_install_action(
107+
source_dir, artifacts_dir, subprocess_npm, osutils, self.options
108+
)
109+
actions.append(install_action)
105110

106111
# if dependencies folder exists, copy or move dependencies from artifact folder to dependencies folder
107112
# depends on the combine_dependencies flag
@@ -138,3 +143,41 @@ def get_resolvers(self):
138143
specialized path resolver that just returns the list of executable for the runtime on the path.
139144
"""
140145
return [PathResolver(runtime=self.runtime, binary="npm")]
146+
147+
@staticmethod
148+
def get_install_action(source_dir, artifacts_dir, subprocess_npm, osutils, build_options, is_production=True):
149+
"""
150+
Get the install action used to install dependencies at artifacts_dir
151+
152+
:type source_dir: str
153+
:param source_dir: an existing (readable) directory containing source files
154+
155+
:type artifacts_dir: str
156+
:param artifacts_dir: Dependencies will be installed in this directory.
157+
158+
:type osutils: aws_lambda_builders.workflows.nodejs_npm.utils.OSUtils
159+
:param osutils: An instance of OS Utilities for file manipulation
160+
161+
:type subprocess_npm: aws_lambda_builders.workflows.nodejs_npm.npm.SubprocessNpm
162+
:param subprocess_npm: An instance of the NPM process wrapper
163+
164+
:type build_options: Dict
165+
:param build_options: Object containing build options configurations
166+
167+
:type is_production: bool
168+
:param is_production: NPM installation mode is production (eg --production=false to force dev dependencies)
169+
170+
:rtype: BaseAction
171+
:return: Install action to use
172+
"""
173+
lockfile_path = osutils.joinpath(source_dir, "package-lock.json")
174+
shrinkwrap_path = osutils.joinpath(source_dir, "npm-shrinkwrap.json")
175+
176+
npm_ci_option = False
177+
if build_options and isinstance(build_options, dict):
178+
npm_ci_option = build_options.get("use_npm_ci", False)
179+
180+
if (osutils.file_exists(lockfile_path) or osutils.file_exists(shrinkwrap_path)) and npm_ci_option:
181+
return NodejsNpmCIAction(artifacts_dir, subprocess_npm=subprocess_npm)
182+
183+
return NodejsNpmInstallAction(artifacts_dir, subprocess_npm=subprocess_npm, is_production=is_production)

aws_lambda_builders/workflows/nodejs_npm_esbuild/workflow.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from .node import SubprocessNodejs
2323
from .utils import is_experimental_esbuild_scope
2424
from .esbuild import SubprocessEsbuild, EsbuildExecutionError
25-
from ..nodejs_npm.actions import NodejsNpmCIAction, NodejsNpmInstallAction
25+
from ..nodejs_npm import NodejsNpmWorkflow
2626
from ..nodejs_npm.npm import SubprocessNpm
2727
from ..nodejs_npm.utils import OSUtils
2828
from ...path_resolver import PathResolver
@@ -101,9 +101,6 @@ def actions_with_bundler(
101101
:rtype: list
102102
:return: List of build actions to execute
103103
"""
104-
lockfile_path = osutils.joinpath(source_dir, "package-lock.json")
105-
shrinkwrap_path = osutils.joinpath(source_dir, "npm-shrinkwrap.json")
106-
107104
actions: List[BaseAction] = [
108105
CopySourceAction(source_dir, scratch_dir, excludes=self.EXCLUDED_FILES + tuple(["node_modules"]))
109106
]
@@ -126,10 +123,9 @@ def actions_with_bundler(
126123
]
127124
esbuild_with_deps = EsbuildBundleAction(scratch_dir, artifacts_dir, bundler_config, osutils, subprocess_esbuild)
128125

129-
if osutils.file_exists(lockfile_path) or osutils.file_exists(shrinkwrap_path):
130-
install_action = NodejsNpmCIAction(scratch_dir, subprocess_npm=subprocess_npm)
131-
else:
132-
install_action = NodejsNpmInstallAction(scratch_dir, subprocess_npm=subprocess_npm, is_production=False)
126+
install_action = NodejsNpmWorkflow.get_install_action(
127+
source_dir, scratch_dir, subprocess_npm, osutils, self.options, is_production=False
128+
)
133129

134130
if self.download_dependencies and not self.dependencies_dir:
135131
return actions + [install_action, esbuild_with_deps]

aws_lambda_builders/workflows/python_pip/packager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ class DependencyBuilder(object):
171171

172172
_COMPATIBLE_PLATFORM_ARM64 = {
173173
"any",
174+
"linux_aarch64",
174175
"manylinux2014_aarch64",
175176
}
176177

tests/integration/workflows/nodejs_npm/test_nodejs_npm.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from unittest import TestCase
77

88
import mock
9+
from parameterized import parameterized
10+
911
from aws_lambda_builders.builder import LambdaBuilder
1012
from aws_lambda_builders.exceptions import WorkflowFailedError
1113

@@ -120,6 +122,31 @@ def test_builds_project_with_npmrc(self):
120122
output_modules = set(os.listdir(os.path.join(self.artifacts_dir, "node_modules")))
121123
self.assertEqual(expected_modules, output_modules)
122124

125+
@parameterized.expand(["package-lock", "shrinkwrap", "package-lock-and-shrinkwrap"])
126+
def test_builds_project_with_lockfile(self, dir_name):
127+
expected_files_common = {"package.json", "included.js", "node_modules"}
128+
expected_files_by_dir_name = {
129+
"package-lock": {"package-lock.json"},
130+
"shrinkwrap": {"npm-shrinkwrap.json"},
131+
"package-lock-and-shrinkwrap": {"package-lock.json", "npm-shrinkwrap.json"},
132+
}
133+
134+
source_dir = os.path.join(self.TEST_DATA_FOLDER, dir_name)
135+
136+
self.builder.build(
137+
source_dir,
138+
self.artifacts_dir,
139+
self.scratch_dir,
140+
os.path.join(source_dir, "package.json"),
141+
runtime=self.runtime,
142+
)
143+
144+
expected_files = expected_files_common.union(expected_files_by_dir_name[dir_name])
145+
146+
output_files = set(os.listdir(self.artifacts_dir))
147+
148+
self.assertEqual(expected_files, output_files)
149+
123150
def test_fails_if_npm_cannot_resolve_dependencies(self):
124151

125152
source_dir = os.path.join(self.TEST_DATA_FOLDER, "broken-deps")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//excluded
2+
const x = 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//included
2+
const x = 1;

tests/integration/workflows/nodejs_npm/testdata/package-lock-and-shrinkwrap/npm-shrinkwrap.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/workflows/nodejs_npm/testdata/package-lock-and-shrinkwrap/package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "package-lock-and-shrinkwrap",
3+
"version": "1.0.0",
4+
"description": "",
5+
"files": ["included.js"],
6+
"keywords": [],
7+
"author": "",
8+
"license": "APACHE2.0",
9+
"dependencies": {
10+
"minimal-request-promise": "*"
11+
}
12+
}

tests/integration/workflows/nodejs_npm/testdata/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//excluded
2+
const x = 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//included
2+
const x = 1;

tests/integration/workflows/nodejs_npm/testdata/package-lock/package-lock.json

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "package-lock",
3+
"version": "1.0.0",
4+
"description": "",
5+
"files": ["included.js"],
6+
"keywords": [],
7+
"author": "",
8+
"license": "APACHE2.0",
9+
"dependencies": {
10+
"minimal-request-promise": "*"
11+
}
12+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//excluded
2+
const x = 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//included
2+
const x = 1;

0 commit comments

Comments
 (0)