diff --git a/lib/manager/pip_setup/extract.js b/lib/manager/pip_setup/extract.js index cc205f241b871dcc9957ea36b10e03dd71443695..93ce1aa903b0f0cf2dbfcb5aa96957697b021a44 100644 --- a/lib/manager/pip_setup/extract.js +++ b/lib/manager/pip_setup/extract.js @@ -4,11 +4,41 @@ const { join } = require('upath'); const { isSkipComment } = require('../../util/ignore'); const { dependencyPattern } = require('../pip_requirements/extract'); +const pythonVersions = ['python', 'python3', 'python3.7']; +let pythonAlias = null; module.exports = { extractPackageFile, extractSetupFile, + parsePythonVersion, + getPythonAlias, + pythonVersions, }; +function parsePythonVersion(str) { + const arr = str.split(' ')[1].split('.'); + return [parseInt(arr[0], 10), parseInt(arr[1], 10)]; +} + +async function getPythonAlias() { + if (pythonAlias) { + return pythonAlias; + } + pythonAlias = pythonVersions[0]; // fallback to 'python' + for (const pythonVersion of pythonVersions) { + try { + const { stdout, stderr } = await exec(`${pythonVersion} --version`); + const version = parsePythonVersion(stdout || stderr); + // istanbul ignore if + if (version[0] >= 3 && version[1] >= 7) { + pythonAlias = pythonVersion; + } + } catch (err) { + logger.debug(`${pythonVersion} alias not found`); + } + } + return pythonAlias; +} + async function extractSetupFile(content, packageFile, config) { const cwd = config.localDir; // extract.py needs setup.py to be written to disk @@ -40,7 +70,7 @@ async function extractSetupFile(content, packageFile, config) { ); } else { logger.info('Running python via global command'); - cmd = 'python'; + cmd = await getPythonAlias(); } logger.debug({ cmd, args }, 'python command'); let stdout; diff --git a/test/manager/pip_setup/extract.spec.js b/test/manager/pip_setup/extract.spec.js index 82e7593f5ca16fe31d7cb9fda325479b332bc010..4e8b8945f4d849d191d7406345eed75b1990c96f 100644 --- a/test/manager/pip_setup/extract.spec.js +++ b/test/manager/pip_setup/extract.spec.js @@ -3,6 +3,9 @@ const tmp = require('tmp-promise'); const { relative } = require('path'); const { extractPackageFile, + parsePythonVersion, + getPythonAlias, + pythonVersions, // extractSetupFile, } = require('../../../lib/manager/pip_setup/extract'); @@ -39,6 +42,17 @@ describe('lib/manager/pip_setup/extract', () => { ).toBe(null); }); }); + + describe('parsePythonVersion', () => { + it('returns major and minor version numbers', () => { + expect(parsePythonVersion('Python 2.7.15rc1')).toEqual([2, 7]); + }); + }); + describe('getPythonAlias', () => { + it('returns the python alias to use', async () => { + expect(pythonVersions.includes(await getPythonAlias())).toBeTruthy(); + }); + }); /* describe('extractSetupFile()', () => { it('should return parsed setup() call', async () => {