Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add find python test (v2) #757

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 88 additions & 94 deletions lib/configure.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = exports = configure
module.exports.test = { findPython: findPython }

/**
* Module dependencies.
Expand All @@ -24,109 +25,25 @@ exports.usage = 'Generates ' + (win ? 'MSVC project files' : 'a Makefile') + ' f

function configure (gyp, argv, callback) {

var python = gyp.opts.python || process.env.PYTHON || 'python2'
, buildDir = path.resolve('build')
var buildDir = path.resolve('build')
, configNames = [ 'config.gypi', 'common.gypi' ]
, configs = []
, nodeDir
, release = processRelease(argv, gyp, process.version, process.release)

checkPython()

// Check if Python is in the $PATH
function checkPython () {
log.verbose('check python', 'checking for Python executable "%s" in the PATH', python)
which(python, function (err, execPath) {
if (err) {
log.verbose('`which` failed', python, err)
if (python === 'python2') {
python = 'python'
return checkPython()
}
if (win) {
guessPython()
} else {
failNoPython()
}
} else {
log.verbose('`which` succeeded', python, execPath)
checkPythonVersion()
}
})
}

// Called on Windows when "python" isn't available in the current $PATH.
// We're gonna check if "%SystemDrive%\python27\python.exe" exists.
function guessPython () {
log.verbose('could not find "' + python + '". guessing location')
var rootDir = process.env.SystemDrive || 'C:\\'
if (rootDir[rootDir.length - 1] !== '\\') {
rootDir += '\\'
findPython(gyp.opts.python, function (err, found) {
if (err) {
callback(err)
} else {
process.env.PYTHON = found
getNodeDir()
}
var pythonPath = path.resolve(rootDir, 'Python27', 'python.exe')
log.verbose('ensuring that file exists:', pythonPath)
fs.stat(pythonPath, function (err, stat) {
if (err) {
if (err.code == 'ENOENT') {
failNoPython()
} else {
callback(err)
}
return
}
python = pythonPath
checkPythonVersion()
})
}

function checkPythonVersion () {
var env = extend({}, process.env)
env.TERM = 'dumb'

execFile(python, ['-c', 'import platform; print(platform.python_version());'], { env: env }, function (err, stdout) {
if (err) {
return callback(err)
}
log.verbose('check python version', '`%s -c "import platform; print(platform.python_version());"` returned: %j', python, stdout)
var version = stdout.trim()
if (~version.indexOf('+')) {
log.silly('stripping "+" sign(s) from version')
version = version.replace(/\+/g, '')
}
if (~version.indexOf('rc')) {
log.silly('stripping "rc" identifier from version')
version = version.replace(/rc(.*)$/ig, '')
}
var range = semver.Range('>=2.5.0 <3.0.0')
var valid = false
try {
valid = range.test(version)
} catch (e) {
log.silly('range.test() error', e)
}
if (valid) {
getNodeDir()
} else {
failPythonVersion(version)
}
})
}

function failNoPython () {
callback(new Error('Can\'t find Python executable "' + python +
'", you can set the PYTHON env variable.'))
}

function failPythonVersion (badVersion) {
callback(new Error('Python executable "' + python +
'" is v' + badVersion + ', which is not supported by gyp.\n' +
'You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.'))
}
})

function getNodeDir () {

// 'python' should be set by now
process.env.PYTHON = python


if (gyp.opts.nodedir) {
// --nodedir was specified. use that for the dev files
Expand Down Expand Up @@ -341,7 +258,7 @@ function configure (gyp, argv, callback) {
var pypath = new PathArray(process.env, 'PYTHONPATH')
pypath.unshift(path.join(__dirname, '..', 'gyp', 'pylib'))

var cp = gyp.spawn(python, argv)
var cp = gyp.spawn(process.env.PYTHON, argv)
cp.on('exit', onCpExit)
})
}
Expand All @@ -360,3 +277,80 @@ function configure (gyp, argv, callback) {
}

}

function findPython (opt, callback) {
findPythonRecursive(opt || process.env.PYTHON || 'python2')

// Check if Python is in the $PATH
function findPythonRecursive (pythonGuess) {
log.verbose('find python', 'looking for Python executable "%s" in the PATH', pythonGuess)
which(pythonGuess, function (err, execPath) {
if (err) {
log.verbose('`which` failed', pythonGuess, err)
if (pythonGuess === 'python2') {
return findPythonRecursive('python')
}
if (win) {
log.verbose('guessing location on windows')
var winGuess = windowsPythonGuess()
if (winGuess !== pythonGuess) {
return findPythonRecursive(winGuess)
}
}
return callback(
new Error('Can\'t find Python executable, you can set the PYTHON env variable or pass a --python switch.')
)
}
log.verbose('`which` succeeded', pythonGuess, execPath)
// Found the `python` executable, and from now on we use it explicitly
// This solves #667 and #750 (`execFile` won't run batch files (*.cmd, and *.bat))
checkPythonVersion(execPath)
})
}

// Called on Windows when "python" isn't available in the current $PATH.
// We're gonna check if "%SystemDrive%\python27\python.exe" exists.
function windowsPythonGuess () {
var rootDir = process.env.SystemDrive || 'C:\\'
if (rootDir[rootDir.length - 1] !== '\\') {
rootDir += '\\'
}
var pythonPath = path.resolve(rootDir, 'Python27', 'python.exe')
return pythonPath
}

function checkPythonVersion (execPath) {
var env = extend({}, process.env)
env.TERM = 'dumb'

execFile(execPath, ['-c', 'import platform; print(platform.python_version());'], { env: env }, function (err, stdout) {
if (err) {
return callback(err)
}
log.verbose('check python version', '`%s -c "import platform; print(platform.python_version());"` returned: %j', execPath, stdout)
var version = stdout.trim()
if (~version.indexOf('+')) {
log.silly('stripping "+" sign(s) from version')
version = version.replace(/\+/g, '')
}
if (~version.indexOf('rc')) {
log.silly('stripping "rc" identifier from version')
version = version.replace(/rc(.*)$/ig, '')
}
var range = semver.Range('>=2.5.0 <3.0.0')
var valid = false
try {
valid = range.test(version)
} catch (e) {
log.silly('range.test() error', e)
}
if (valid) {
callback(null, execPath)
} else {
callback(new Error('Python executable "' + execPath +
'" is v' + version + ', which is not supported by gyp.\n' +
'You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.'))
}
})
}
}
1 change: 1 addition & 0 deletions test/python2.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@echo 2.7.9
39 changes: 39 additions & 0 deletions test/test-find-python.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

var test = require('tape')
var configure = require('../lib/configure')
var execFile = require('child_process').execFile
var fs = require('graceful-fs')

test('find python executable', function (t) {
t.plan(4)

configure.test.findPython(null, function (err, found) {
t.equal(err, null)
var proc = execFile(found, ['-V'], function (err, stdout, stderr) {
t.equal(err, null)
t.equal(stdout, '')
t.ok(/Python 2/.test(stderr), 'should be python 2.x.x')
})
proc.stdout.setEncoding('utf-8')
proc.stderr.setEncoding('utf-8')
})
})


test('find python batch files on windows', function (t) {
if (process.platform !== 'win32') {
t.end()
}
var mockFileName = __dirname + '\\python2.bat'
var oldPath = process.env.PATH
process.env.PATH = __dirname
t.plan(2)

configure.test.findPython(null, function (err, found) {
process.env.PATH = oldPath

t.equal(err, null)
t.equal(found.toLowerCase(), mockFileName.toLowerCase())
})
})