Skip to content

Commit 21f031b

Browse files
authored
Better mixed runtime & function deploy handling (#180)
* Better mixed runtime & function deploy handling fixes #161 and fixes #179 * another tweak * fix again? * Fix corrupted zip archive in case of same module * Do not try to install requirements for non-python runtime * Fix lint * format * update test for merging #181 * @AndrewFarley's fix * huh. depcheck sucks. * fixix syntax error
1 parent 5a72d5c commit 21f031b

File tree

5 files changed

+60
-32
lines changed

5 files changed

+60
-32
lines changed

index.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,36 @@ class ServerlessPythonRequirements {
108108
}
109109
};
110110

111-
const before = () =>
112-
BbPromise.bind(this)
111+
const before = () => {
112+
if (
113+
arguments[1].functionObj &&
114+
arguments[1].functionObj.runtime &&
115+
!arguments[1].functionObj.runtime.startsWith('python')
116+
)
117+
return;
118+
return BbPromise.bind(this)
113119
.then(pipfileToRequirements)
114120
.then(addVendorHelper)
115121
.then(installAllRequirements)
116122
.then(packRequirements);
123+
};
117124

118-
const after = () =>
119-
BbPromise.bind(this)
125+
const after = () => {
126+
if (
127+
arguments[1].functionObj &&
128+
arguments[1].functionObj.runtime &&
129+
!arguments[1].functionObj.runtime.startsWith('python')
130+
)
131+
return;
132+
return BbPromise.bind(this)
120133
.then(removeVendorHelper)
121-
.then(injectAllRequirements);
134+
.then(() =>
135+
injectAllRequirements.bind(this)(
136+
arguments[1].functionObj &&
137+
arguments[1].functionObj.package.artifact
138+
)
139+
);
140+
};
122141

123142
const invalidateCaches = () => {
124143
if (this.options.invalidateCaches) {

lib/inject.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function moveModuleUp(source, target, module) {
7272
* Inject requirements into packaged application.
7373
* @return {Promise} the combined promise for requirements injection.
7474
*/
75-
function injectAllRequirements() {
75+
function injectAllRequirements(funcArtifact) {
7676
this.serverless.cli.log('Injecting required Python packages to package...');
7777

7878
if (this.options.zip) {
@@ -94,8 +94,11 @@ function injectAllRequirements() {
9494
})
9595
.map(func => {
9696
if (func.module !== '.') {
97-
const artifact = func.package.artifact;
98-
const newArtifact = path.join('.serverless', `${func.module}.zip`);
97+
const artifact = func.package ? func.package.artifact : funcArtifact;
98+
const newArtifact = path.join(
99+
'.serverless',
100+
`${func.module}-${func.name}.zip`
101+
);
99102
func.package.artifact = newArtifact;
100103
return moveModuleUp(artifact, newArtifact, func.module).then(
101104
() => func
@@ -114,7 +117,7 @@ function injectAllRequirements() {
114117
} else {
115118
return injectRequirements(
116119
path.join('.serverless', 'requirements'),
117-
this.serverless.service.package.artifact,
120+
this.serverless.service.package.artifact || funcArtifact,
118121
this.options
119122
);
120123
}

lib/pip.js

+26-20
Original file line numberDiff line numberDiff line change
@@ -201,29 +201,35 @@ function installAllRequirements() {
201201
fse.ensureDirSync(path.join(this.servicePath, '.serverless'));
202202
if (this.serverless.service.package.individually) {
203203
let doneModules = [];
204-
values(this.serverless.service.functions).forEach(f => {
205-
if (!get(f, 'module')) {
206-
set(f, ['module'], '.');
207-
}
208-
if (!doneModules.includes(f.module)) {
209-
installRequirements(
210-
path.join(f.module, this.options.fileName),
211-
path.join('.serverless', f.module),
212-
this.serverless,
213-
this.servicePath,
214-
this.options
215-
);
216-
if (f.vendor) {
217-
// copy vendor libraries to requirements folder
218-
copyVendors(
219-
f.vendor,
204+
values(this.serverless.service.functions)
205+
.filter(func =>
206+
(func.runtime || this.serverless.service.provider.runtime).match(
207+
/^python.*/
208+
)
209+
)
210+
.map(f => {
211+
if (!get(f, 'module')) {
212+
set(f, ['module'], '.');
213+
}
214+
if (!doneModules.includes(f.module)) {
215+
installRequirements(
216+
path.join(f.module, this.options.fileName),
220217
path.join('.serverless', f.module),
221-
this.serverless
218+
this.serverless,
219+
this.servicePath,
220+
this.options
222221
);
222+
if (f.vendor) {
223+
// copy vendor libraries to requirements folder
224+
copyVendors(
225+
f.vendor,
226+
path.join('.serverless', f.module),
227+
this.serverless
228+
);
229+
}
230+
doneModules.push(f.module);
223231
}
224-
doneModules.push(f.module);
225-
}
226-
});
232+
});
227233
} else {
228234
installRequirements(
229235
this.options.fileName,

test.bats

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ teardown() {
197197
cd tests/individually
198198
npm i $(npm pack ../..)
199199
sls package
200-
unzip .serverless/module1.zip -d puck
201-
unzip .serverless/module2.zip -d puck2
200+
unzip .serverless/module1-sls-py-req-test-indiv-dev-hello1.zip -d puck
201+
unzip .serverless/module2-sls-py-req-test-indiv-dev-hello2.zip -d puck2
202202
ls puck/handler1.py
203203
ls puck2/handler2.py
204204
ls puck/pyaml

tests/base/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"author": "",
1010
"license": "ISC",
1111
"dependencies": {
12-
"serverless-python-requirements": "file:serverless-python-requirements-4.0.0.tgz"
12+
"serverless-python-requirements": "file:serverless-python-requirements-4.0.2.tgz"
1313
}
1414
}

0 commit comments

Comments
 (0)