forked from serverless/serverless-python-requirements
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipenv.js
83 lines (76 loc) · 2.35 KB
/
pipenv.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
const fse = require('fs-extra');
const path = require('path');
const spawn = require('child-process-ext/spawn');
const { EOL } = require('os');
/**
* pipenv install
*/
async function pipfileToRequirements() {
if (
!this.options.usePipenv ||
!fse.existsSync(path.join(this.servicePath, 'Pipfile'))
) {
return;
}
let generateRequirementsProgress;
if (this.progress && this.log) {
generateRequirementsProgress = this.progress.get(
'python-generate-requirements-pipfile'
);
generateRequirementsProgress.update(
'Generating requirements.txt from Pipfile'
);
this.log.info('Generating requirements.txt from Pipfile');
} else {
this.serverless.cli.log('Generating requirements.txt from Pipfile...');
}
try {
try {
await spawn('pipenv', ['lock', '--keep-outdated'], {
cwd: this.servicePath,
});
} catch (e) {
const stderrBufferContent =
(e.stderrBuffer && e.stderrBuffer.toString()) || '';
if (stderrBufferContent.includes('must exist to use')) {
// No previous Pipfile.lock, we will try to generate it here
await spawn('pipenv', ['lock'], {
cwd: this.servicePath,
});
} else if (stderrBufferContent.includes('command not found')) {
throw new this.serverless.classes.Error(
`pipenv not found! Install it according to the poetry docs.`,
'PYTHON_REQUIREMENTS_PIPENV_NOT_FOUND'
);
} else {
throw e;
}
}
const res = await spawn('pipenv', ['requirements'], {
cwd: this.servicePath,
});
fse.ensureDirSync(path.join(this.servicePath, '.serverless'));
fse.writeFileSync(
path.join(this.servicePath, '.serverless/requirements.txt'),
removeEditableFlagFromRequirementsString(res.stdoutBuffer)
);
} finally {
generateRequirementsProgress && generateRequirementsProgress.remove();
}
}
/**
*
* @param requirementBuffer
* @returns Buffer with editable flags remove
*/
function removeEditableFlagFromRequirementsString(requirementBuffer) {
const flagStr = '-e ';
const lines = requirementBuffer.toString('utf8').split(EOL);
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith(flagStr)) {
lines[i] = lines[i].substring(flagStr.length);
}
}
return Buffer.from(lines.join(EOL));
}
module.exports = { pipfileToRequirements };