-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathinject.js
180 lines (170 loc) · 5.28 KB
/
inject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const BbPromise = require('bluebird');
const fse = require('fs-extra');
const glob = require('glob-all');
const get = require('lodash.get');
const set = require('set-value');
const path = require('path');
const JSZip = require('jszip');
const { writeZip, zipFile } = require('./zipTree');
BbPromise.promisifyAll(fse);
/**
* Inject requirements into packaged application.
* @param {string} requirementsPath requirements folder path
* @param {string} packagePath target package path
* @param {string} injectionRelativePath installation directory in target package
* @param {Object} options our options object
* @return {Promise} the JSZip object constructed.
*/
function injectRequirements(
requirementsPath,
packagePath,
injectionRelativePath,
options
) {
const noDeploy = new Set(options.noDeploy || []);
return fse
.readFileAsync(packagePath)
.then((buffer) => JSZip.loadAsync(buffer))
.then((zip) =>
BbPromise.resolve(
glob.sync([path.join(requirementsPath, '**')], {
mark: true,
dot: true,
})
)
.map((file) => [
file,
path.join(
injectionRelativePath,
path.relative(requirementsPath, file)
),
])
.filter(
([file, relativeFile]) =>
!file.endsWith('/') &&
!relativeFile.match(/^__pycache__[\\/]/) &&
!noDeploy.has(relativeFile.split(/([-\\/]|\.py$|\.pyc$)/, 1)[0])
)
.map(([file, relativeFile]) =>
Promise.all([file, relativeFile, fse.statAsync(file)])
)
.mapSeries(([file, relativeFile, fileStat]) =>
zipFile(zip, relativeFile, fse.readFileAsync(file), {
unixPermissions: fileStat.mode,
createFolders: false,
})
)
.then(() => writeZip(zip, packagePath))
);
}
/**
* Remove all modules but the selected module from a package.
* @param {string} source path to original package
* @param {string} target path to result package
* @param {string} module module to keep
* @return {Promise} the JSZip object written out.
*/
function moveModuleUp(source, target, module) {
const targetZip = new JSZip();
return fse
.readFileAsync(source)
.then((buffer) => JSZip.loadAsync(buffer))
.then((sourceZip) =>
sourceZip.filter(
(file) =>
file.startsWith(module + '/') ||
file.startsWith('serverless_sdk/') ||
file.match(/^s_.*\.py/) !== null
)
)
.map((srcZipObj) =>
zipFile(
targetZip,
srcZipObj.name.startsWith(module + '/')
? srcZipObj.name.replace(module + '/', '')
: srcZipObj.name,
srcZipObj.async('nodebuffer')
)
)
.then(() => writeZip(targetZip, target));
}
/**
* Inject requirements into packaged application.
* @return {Promise} the combined promise for requirements injection.
*/
async function injectAllRequirements(funcArtifact) {
if (this.options.layer) {
// The requirements will be placed in a Layer, so just resolve
return BbPromise.resolve();
}
let injectProgress;
if (this.progress && this.log) {
injectProgress = this.progress.get('python-inject-requirements');
injectProgress.update('Injecting required Python packages to package');
this.log.info('Injecting required Python packages to package');
} else {
this.serverless.cli.log('Injecting required Python packages to package...');
}
let injectionRelativePath = '.';
if (this.serverless.service.provider.name == 'scaleway') {
injectionRelativePath = 'package';
}
try {
if (this.serverless.service.package.individually) {
await BbPromise.resolve(this.targetFuncs)
.filter((func) =>
(func.runtime || this.serverless.service.provider.runtime).match(
/^python.*/
)
)
.map((func) => {
if (!get(func, 'module')) {
set(func, ['module'], '.');
}
return func;
})
.map((func) => {
if (func.module !== '.') {
const artifact = func.package
? func.package.artifact
: funcArtifact;
const newArtifact = path.join(
'.serverless',
`${func.module}-${func.name}.zip`
);
func.package.artifact = newArtifact;
return moveModuleUp(artifact, newArtifact, func.module).then(
() => func
);
} else {
return func;
}
})
.map((func) => {
return this.options.zip
? func
: injectRequirements(
path.join(
this.serverless.serviceDir,
'.serverless',
func.module,
'requirements'
),
func.package.artifact,
injectionRelativePath,
this.options
);
});
} else if (!this.options.zip) {
await injectRequirements(
path.join(this.serverless.serviceDir, '.serverless', 'requirements'),
this.serverless.service.package.artifact || funcArtifact,
injectionRelativePath,
this.options
);
}
} finally {
injectProgress && injectProgress.remove();
}
}
module.exports = { injectAllRequirements };