Skip to content

Commit 22a1f83

Browse files
authored
fix: Add legacy pipenv backward compatability (#742)
1 parent 1436c17 commit 22a1f83

File tree

5 files changed

+88
-28
lines changed

5 files changed

+88
-28
lines changed

.github/workflows/integrate.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
strategy:
1717
matrix:
1818
sls-version: [2, 3]
19+
pipenv-version: ['2022.8.5', '2022.8.13']
1920
steps:
2021
- name: Checkout repository
2122
uses: actions/checkout@v2
@@ -48,7 +49,7 @@ jobs:
4849
run: python -m pip install --force setuptools wheel
4950

5051
- name: Install pipenv / poetry
51-
run: python -m pip install pipenv poetry
52+
run: python -m pip install pipenv==${{ matrix.pipenv-version }} poetry
5253

5354
- name: Install serverless
5455
run: npm install -g serverless@${{ matrix.sls-version }}
@@ -67,6 +68,7 @@ jobs:
6768
strategy:
6869
matrix:
6970
sls-version: [2, 3]
71+
pipenv-version: ['2022.8.5', '2022.8.13']
7072
steps:
7173
- name: Checkout repository
7274
uses: actions/checkout@v2
@@ -99,7 +101,7 @@ jobs:
99101
run: python -m pip install --force setuptools wheel
100102

101103
- name: Install pipenv / poetry
102-
run: python -m pip install pipenv poetry
104+
run: python -m pip install pipenv==${{ matrix.pipenv-version }} poetry
103105

104106
- name: Install serverless
105107
run: npm install -g serverless@${{ matrix.sls-version }}
@@ -147,7 +149,7 @@ jobs:
147149
run: python -m pip install --force setuptools wheel
148150

149151
- name: Install pipenv / poetry
150-
run: python -m pip install pipenv poetry
152+
run: python -m pip install pipenv==${{ matrix.pipenv-version }} poetry
151153

152154
- name: Install serverless
153155
run: npm install -g serverless@${{ matrix.sls-version }}
@@ -166,6 +168,7 @@ jobs:
166168
strategy:
167169
matrix:
168170
sls-version: [2, 3]
171+
pipenv-version: ['2022.8.5', '2022.8.13']
169172
needs: [windowsNode14, linuxNode14, linuxNode12]
170173
steps:
171174
- name: Checkout repository

.github/workflows/validate.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
strategy:
1717
matrix:
1818
sls-version: [2, 3]
19+
pipenv-version: ['2022.8.5', '2022.8.13']
1920
steps:
2021
- name: Checkout repository
2122
uses: actions/checkout@v2
@@ -61,7 +62,7 @@ jobs:
6162
run: python -m pip install --force setuptools wheel
6263

6364
- name: Install pipenv / poetry
64-
run: python -m pip install pipenv poetry
65+
run: python -m pip install pipenv==${{ matrix.pipenv-version }} poetry
6566

6667
- name: Install serverless
6768
run: npm install -g serverless@${{ matrix.sls-version }}
@@ -94,6 +95,7 @@ jobs:
9495
strategy:
9596
matrix:
9697
sls-version: [2, 3]
98+
pipenv-version: ['2022.8.5', '2022.8.13']
9799
steps:
98100
- name: Checkout repository
99101
uses: actions/checkout@v2
@@ -128,7 +130,7 @@ jobs:
128130
run: python -m pip install --force setuptools wheel
129131

130132
- name: Install pipenv / poetry
131-
run: python -m pip install pipenv poetry
133+
run: python -m pip install pipenv==${{ matrix.pipenv-version }} poetry
132134

133135
- name: Install serverless
134136
run: npm install -g serverless@${{ matrix.sls-version }}
@@ -147,6 +149,7 @@ jobs:
147149
strategy:
148150
matrix:
149151
sls-version: [2, 3]
152+
pipenv-version: ['2022.8.5', '2022.8.13']
150153
steps:
151154
- name: Checkout repository
152155
uses: actions/checkout@v2
@@ -181,7 +184,7 @@ jobs:
181184
run: python -m pip install --force setuptools wheel
182185

183186
- name: Install pipenv / poetry
184-
run: python -m pip install pipenv poetry
187+
run: python -m pip install pipenv==${{ matrix.pipenv-version }} poetry
185188

186189
- name: Install serverless
187190
run: npm install -g serverless@${{ matrix.sls-version }}

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ custom:
110110

111111
Requires `pipenv` in version `2022-04-08` or higher.
112112

113-
If you include a `Pipfile` and have `pipenv` installed instead of a `requirements.txt` this will use
114-
`pipenv lock -r` to generate them. It is fully compatible with all options such as `zip` and
113+
If you include a `Pipfile` and have `pipenv` installed, this will use `pipenv` to generate requirements instead of a `requirements.txt`. It is fully compatible with all options such as `zip` and
115114
`dockerizePip`. If you don't want this plugin to generate it for you, set the following option:
116115

117116
```yaml

lib/pipenv.js

+74-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,43 @@ const fse = require('fs-extra');
22
const path = require('path');
33
const spawn = require('child-process-ext/spawn');
44
const { EOL } = require('os');
5+
const semver = require('semver');
6+
7+
const LEGACY_PIPENV_VERSION = '2022.8.5';
8+
9+
async function getPipenvVersion() {
10+
try {
11+
const res = await spawn('pipenv', ['--version'], {
12+
cwd: this.servicePath,
13+
});
14+
15+
const stdoutBuffer =
16+
(res.stdoutBuffer && res.stdoutBuffer.toString().trim()) || '';
17+
18+
const version = stdoutBuffer.split(' ')[2];
19+
20+
if (semver.valid(version)) {
21+
return version;
22+
} else {
23+
throw new this.serverless.classes.Error(
24+
`Unable to parse pipenv version!`,
25+
'PYTHON_REQUIREMENTS_PIPENV_VERSION_ERROR'
26+
);
27+
}
28+
} catch (e) {
29+
const stderrBufferContent =
30+
(e.stderrBuffer && e.stderrBuffer.toString()) || '';
31+
32+
if (stderrBufferContent.includes('command not found')) {
33+
throw new this.serverless.classes.Error(
34+
`pipenv not found! Install it according to the pipenv docs.`,
35+
'PYTHON_REQUIREMENTS_PIPENV_NOT_FOUND'
36+
);
37+
} else {
38+
throw e;
39+
}
40+
}
41+
}
542

643
/**
744
* pipenv install
@@ -28,31 +65,48 @@ async function pipfileToRequirements() {
2865
}
2966

3067
try {
31-
try {
32-
await spawn('pipenv', ['lock', '--keep-outdated'], {
33-
cwd: this.servicePath,
34-
});
35-
} catch (e) {
36-
const stderrBufferContent =
37-
(e.stderrBuffer && e.stderrBuffer.toString()) || '';
68+
// Get and validate pipenv version
69+
if (this.log) {
70+
this.log.info('Getting pipenv version');
71+
} else {
72+
this.serverless.cli.log('Getting pipenv version');
73+
}
74+
75+
const pipenvVersion = await getPipenvVersion();
76+
let res;
3877

39-
if (stderrBufferContent.includes('must exist to use')) {
40-
// No previous Pipfile.lock, we will try to generate it here
41-
await spawn('pipenv', ['lock'], {
78+
if (semver.gt(pipenvVersion, LEGACY_PIPENV_VERSION)) {
79+
// Using new pipenv syntax ( >= 2022.8.13)
80+
try {
81+
await spawn('pipenv', ['lock', '--keep-outdated'], {
4282
cwd: this.servicePath,
4383
});
44-
} else if (stderrBufferContent.includes('command not found')) {
45-
throw new this.serverless.classes.Error(
46-
`pipenv not found! Install it according to the poetry docs.`,
47-
'PYTHON_REQUIREMENTS_PIPENV_NOT_FOUND'
48-
);
49-
} else {
50-
throw e;
84+
} catch (e) {
85+
const stderrBufferContent =
86+
(e.stderrBuffer && e.stderrBuffer.toString()) || '';
87+
if (stderrBufferContent.includes('must exist to use')) {
88+
// No previous Pipfile.lock, we will try to generate it here
89+
await spawn('pipenv', ['lock'], {
90+
cwd: this.servicePath,
91+
});
92+
} else {
93+
throw e;
94+
}
5195
}
96+
97+
res = await spawn('pipenv', ['requirements'], {
98+
cwd: this.servicePath,
99+
});
100+
} else {
101+
// Falling back to legacy pipenv syntax
102+
res = await spawn(
103+
'pipenv',
104+
['lock', '--requirements', '--keep-outdated'],
105+
{
106+
cwd: this.servicePath,
107+
}
108+
);
52109
}
53-
const res = await spawn('pipenv', ['requirements'], {
54-
cwd: this.servicePath,
55-
});
56110

57111
fse.ensureDirSync(path.join(this.servicePath, '.serverless'));
58112
fse.writeFileSync(

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"lodash.uniqby": "^4.7.0",
7373
"lodash.values": "^4.3.0",
7474
"rimraf": "^3.0.2",
75+
"semver": "^7.3.8",
7576
"set-value": "^4.1.0",
7677
"sha256-file": "1.0.0",
7778
"shell-quote": "^1.7.4"

0 commit comments

Comments
 (0)