Skip to content

Commit 21b7bd1

Browse files
bsamuel-uiAubtin
authored andcommitted
Control the python versions fetched more carefully.
Check that the mapping isn't empty and the python command didn't return null stdout.
1 parent a46d90d commit 21b7bd1

File tree

7 files changed

+53
-41
lines changed

7 files changed

+53
-41
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@ jobs:
2828
uses: actions/setup-node@v1
2929
with:
3030
version: ${{ matrix.node-version }}
31-
- name: Install pipenv
32-
uses: dschep/install-pipenv-action@v1
33-
- name: Install poetry
34-
uses: dschep/[email protected]
35-
with:
36-
preview: false
31+
- name: Install pipenv, poetry
32+
run: python${{ matrix.python-version}} -m pip install pipenv poetry setuptools
3733

3834
- name: Install serverless
3935
run: npm install -g serverless
@@ -47,10 +43,8 @@ jobs:
4743
if: matrix.python-version == 3.7 && matrix.node-version == 12 && matrix.os == 'ubuntu-latest'
4844

4945
- name: Test
50-
run: |
51-
PATH=$HOME/.poetry/bin:$PATH \
52-
LC_ALL=C.UTF-8 \
53-
LANG=C.UTF-8 \
54-
npm run test
46+
run: npm run test
5547
env:
56-
RUNTIME: python${{ matrix.python-version }}
48+
USE_PYTHON: ${{ matrix.python-version }}
49+
LC_ALL: C.UTF-8
50+
LANG: C.UTF-8

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"prettier": "^1",
4949
"cross-spawn": "*",
5050
"deasync-promise": "*",
51-
"tape": "*"
51+
"tape": "*",
52+
"lodash": "^4.16.15"
5253
},
5354
"dependencies": {
5455
"@iarna/toml": "^2.2.3",

test.js

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
} = require('fs-extra');
1616
const { quote } = require('shell-quote');
1717
const { sep, delimiter } = require('path');
18+
const { _ } = require('lodash');
1819

1920
const { getUserCachePath, sha256Path } = require('./lib/shared');
2021

@@ -94,39 +95,55 @@ const test = (desc, func, opts = {}) =>
9495
}
9596
});
9697

97-
const executableExtension = process.platform === 'win32' ? '.exe' : '';
98-
const executableSearch = process.env.PATH.split(delimiter);
99-
const whichCache = {};
100-
101-
const which = (name, extra) => {
102-
const found = whichCache[name];
103-
if (found !== undefined) {
104-
return found;
98+
const availablePythons = (() => {
99+
const versions = [];
100+
const mapping = {};
101+
if (process.env.USE_PYTHON) {
102+
versions.push(
103+
...process.env.USE_PYTHON.split(',').map(v => v.toString().trim())
104+
);
105+
} else {
106+
versions.push('3.8', '3.7', '3.6', '2.7');
105107
}
106-
const fullName = `${name}${executableExtension}`;
107-
for (const path of (extra || []).concat(executableSearch)) {
108-
const fullPath = `${path}${sep}${fullName}`;
109-
if (pathExistsSync(fullPath)) {
110-
whichCache[name] = fullPath;
111-
return fullPath;
108+
const exe = process.platform === 'win32' ? '.exe' : '';
109+
for (const ver of _.uniq(
110+
_.concat(
111+
versions,
112+
versions.map(v => v[0]),
113+
['']
114+
)
115+
)) {
116+
const python = `python${ver}${exe}`;
117+
const { stdout, stderr, status } = crossSpawn.sync(python, [
118+
'-c',
119+
'import sys; sys.stdout.write(".".join(map(str, sys.version_info[:2])))'
120+
]);
121+
const realVer = stdout && stdout.toString().trim();
122+
if (!status && realVer && _.includes(versions, realVer)) {
123+
for (const recommend of [realVer, realVer[0]]) {
124+
if (!mapping[recommend]) {
125+
mapping[recommend] = python;
126+
}
127+
}
112128
}
113129
}
114-
whichCache[name] = null;
115-
return null;
116-
};
130+
if (_.isEmpty(mapping)) {
131+
throw new Error(`No pythons available meeting ${versions}`);
132+
}
133+
return mapping;
134+
})();
117135

118136
const getPythonBin = (version = 3) => {
119-
if (![2, 3].includes(version)) throw new Error('version must be 2 or 3');
120-
const extra = [];
121-
/* if (process.platform === 'win32')
122-
* extra.push(...glob.sync(`c:/python${version}*`)); */
123-
const bin = which(`python${version}`, extra);
124-
if (bin === null) throw new Error(`Can't find python${version} on PATH`);
137+
const bin = availablePythons[String(version)];
138+
if (!bin)
139+
throw new Error(
140+
`No python version ${version} available, only ${availablePythons}`
141+
);
125142
return bin;
126143
};
127144

128145
const hasPython = version => {
129-
return getPythonBin(version) !== null;
146+
return Boolean(availablePythons[String(version)]);
130147
};
131148

132149
const listZipFiles = filename =>

tests/individually/package.json

Lines changed: 1 addition & 1 deletion
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-5.0.0.tgz"
12+
"serverless-python-requirements": "file:serverless-python-requirements-5.1.0.tgz"
1313
}
1414
}

tests/non_build_pyproject/package.json

Lines changed: 1 addition & 1 deletion
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-5.0.0.tgz"
12+
"serverless-python-requirements": "file:serverless-python-requirements-5.1.0.tgz"
1313
}
1414
}

tests/pipenv/package.json

Lines changed: 1 addition & 1 deletion
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-5.0.0.tgz"
12+
"serverless-python-requirements": "file:serverless-python-requirements-5.1.0.tgz"
1313
}
1414
}

tests/poetry/package.json

Lines changed: 1 addition & 1 deletion
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-5.0.0.tgz"
12+
"serverless-python-requirements": "file:serverless-python-requirements-5.1.0.tgz"
1313
}
1414
}

0 commit comments

Comments
 (0)