Skip to content

Commit 5dcf654

Browse files
msabramoTooTallNate
authored andcommitted
Check for Python 3 by spawning it and checking the output of '--version'
Closes #76.
1 parent e3d0a0f commit 5dcf654

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

lib/configure.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var fs = require('graceful-fs')
1212
, which = require('which')
1313
, semver = require('semver')
1414
, mkdirp = require('mkdirp')
15+
, exec = require('child_process').exec
1516
, win = process.platform == 'win32'
1617

1718
exports.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' for the current module'
@@ -38,8 +39,7 @@ function configure (gyp, argv, callback) {
3839
return
3940
}
4041
log.verbose('`which` succeeded for `' + python + '`', execPath)
41-
// TODO: ensure compatible Python version
42-
getNodeDir()
42+
checkPythonVersion()
4343
})
4444
}
4545

@@ -58,20 +58,40 @@ function configure (gyp, argv, callback) {
5858
if (err.code == 'ENOENT') {
5959
failNoPython()
6060
} else {
61-
callbackk(err)
61+
callback(err)
6262
}
6363
return
6464
}
6565
python = pythonPath
66-
getNodeDir()
66+
checkPythonVersion()
6767
})
6868
}
6969

70+
function checkPythonVersion () {
71+
exec(python + ' --version', function (err, stdout, stderr) {
72+
if (err) {
73+
return callback(err)
74+
}
75+
var version = stderr.trim().replace(/^python /i, '')
76+
if (semver.lt(version, '3.0.0')) {
77+
getNodeDir()
78+
} else {
79+
failPython3()
80+
}
81+
});
82+
}
83+
7084
function failNoPython () {
7185
callback(new Error('Can\'t find Python executable "' + python
7286
+ '", you can set the PYTHON env variable.'))
7387
}
7488

89+
function failPython3 () {
90+
callback(new Error('Python executable "' + python
91+
+ '" is Python 3, which is not supported.\n'
92+
+ 'You can set the PYTHON env variable to point to a Python 2 interpreter.'))
93+
}
94+
7595
function getNodeDir () {
7696

7797
// 'python' should be set by now

0 commit comments

Comments
 (0)