find-python.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. 'use strict'
  2. const path = require('path')
  3. const log = require('npmlog')
  4. const semver = require('semver')
  5. const cp = require('child_process')
  6. const extend = require('util')._extend // eslint-disable-line
  7. const win = process.platform === 'win32'
  8. const logWithPrefix = require('./util').logWithPrefix
  9. function PythonFinder (configPython, callback) {
  10. this.callback = callback
  11. this.configPython = configPython
  12. this.errorLog = []
  13. }
  14. PythonFinder.prototype = {
  15. log: logWithPrefix(log, 'find Python'),
  16. argsExecutable: ['-c', 'import sys; print(sys.executable);'],
  17. argsVersion: ['-c', 'import sys; print("%s.%s.%s" % sys.version_info[:3]);'],
  18. semverRange: '2.7.x || >=3.5.0',
  19. // These can be overridden for testing:
  20. execFile: cp.execFile,
  21. env: process.env,
  22. win: win,
  23. pyLauncher: 'py.exe',
  24. winDefaultLocations: [
  25. path.join(process.env.SystemDrive || 'C:', 'Python37', 'python.exe'),
  26. path.join(process.env.SystemDrive || 'C:', 'Python27', 'python.exe')
  27. ],
  28. // Logs a message at verbose level, but also saves it to be displayed later
  29. // at error level if an error occurs. This should help diagnose the problem.
  30. addLog: function addLog (message) {
  31. this.log.verbose(message)
  32. this.errorLog.push(message)
  33. },
  34. // Find Python by trying a sequence of possibilities.
  35. // Ignore errors, keep trying until Python is found.
  36. findPython: function findPython () {
  37. const SKIP = 0; const FAIL = 1
  38. var toCheck = getChecks.apply(this)
  39. function getChecks () {
  40. if (this.env.NODE_GYP_FORCE_PYTHON) {
  41. return [{
  42. before: () => {
  43. this.addLog(
  44. 'checking Python explicitly set from NODE_GYP_FORCE_PYTHON')
  45. this.addLog('- process.env.NODE_GYP_FORCE_PYTHON is ' +
  46. `"${this.env.NODE_GYP_FORCE_PYTHON}"`)
  47. },
  48. check: this.checkCommand,
  49. arg: this.env.NODE_GYP_FORCE_PYTHON
  50. }]
  51. }
  52. var checks = [
  53. {
  54. before: () => {
  55. if (!this.configPython) {
  56. this.addLog(
  57. 'Python is not set from command line or npm configuration')
  58. return SKIP
  59. }
  60. this.addLog('checking Python explicitly set from command line or ' +
  61. 'npm configuration')
  62. this.addLog('- "--python=" or "npm config get python" is ' +
  63. `"${this.configPython}"`)
  64. },
  65. check: this.checkCommand,
  66. arg: this.configPython
  67. },
  68. {
  69. before: () => {
  70. if (!this.env.PYTHON) {
  71. this.addLog('Python is not set from environment variable ' +
  72. 'PYTHON')
  73. return SKIP
  74. }
  75. this.addLog('checking Python explicitly set from environment ' +
  76. 'variable PYTHON')
  77. this.addLog(`- process.env.PYTHON is "${this.env.PYTHON}"`)
  78. },
  79. check: this.checkCommand,
  80. arg: this.env.PYTHON
  81. },
  82. {
  83. before: () => { this.addLog('checking if "python3" can be used') },
  84. check: this.checkCommand,
  85. arg: 'python3'
  86. },
  87. {
  88. before: () => { this.addLog('checking if "python" can be used') },
  89. check: this.checkCommand,
  90. arg: 'python'
  91. },
  92. {
  93. before: () => { this.addLog('checking if "python2" can be used') },
  94. check: this.checkCommand,
  95. arg: 'python2'
  96. }
  97. ]
  98. if (this.win) {
  99. for (var i = 0; i < this.winDefaultLocations.length; ++i) {
  100. const location = this.winDefaultLocations[i]
  101. checks.push({
  102. before: () => {
  103. this.addLog('checking if Python is ' +
  104. `${location}`)
  105. },
  106. check: this.checkExecPath,
  107. arg: location
  108. })
  109. }
  110. checks.push({
  111. before: () => {
  112. this.addLog(
  113. 'checking if the py launcher can be used to find Python')
  114. },
  115. check: this.checkPyLauncher
  116. })
  117. }
  118. return checks
  119. }
  120. function runChecks (err) {
  121. this.log.silly('runChecks: err = %j', (err && err.stack) || err)
  122. const check = toCheck.shift()
  123. if (!check) {
  124. return this.fail()
  125. }
  126. const before = check.before.apply(this)
  127. if (before === SKIP) {
  128. return runChecks.apply(this)
  129. }
  130. if (before === FAIL) {
  131. return this.fail()
  132. }
  133. const args = [runChecks.bind(this)]
  134. if (check.arg) {
  135. args.unshift(check.arg)
  136. }
  137. check.check.apply(this, args)
  138. }
  139. runChecks.apply(this)
  140. },
  141. // Check if command is a valid Python to use.
  142. // Will exit the Python finder on success.
  143. // If on Windows, run in a CMD shell to support BAT/CMD launchers.
  144. checkCommand: function checkCommand (command, errorCallback) {
  145. var exec = command
  146. var args = this.argsExecutable
  147. var shell = false
  148. if (this.win) {
  149. // Arguments have to be manually quoted
  150. exec = `"${exec}"`
  151. args = args.map(a => `"${a}"`)
  152. shell = true
  153. }
  154. this.log.verbose(`- executing "${command}" to get executable path`)
  155. this.run(exec, args, shell, function (err, execPath) {
  156. // Possible outcomes:
  157. // - Error: not in PATH, not executable or execution fails
  158. // - Gibberish: the next command to check version will fail
  159. // - Absolute path to executable
  160. if (err) {
  161. this.addLog(`- "${command}" is not in PATH or produced an error`)
  162. return errorCallback(err)
  163. }
  164. this.addLog(`- executable path is "${execPath}"`)
  165. this.checkExecPath(execPath, errorCallback)
  166. }.bind(this))
  167. },
  168. // Check if the py launcher can find a valid Python to use.
  169. // Will exit the Python finder on success.
  170. // Distributions of Python on Windows by default install with the "py.exe"
  171. // Python launcher which is more likely to exist than the Python executable
  172. // being in the $PATH.
  173. checkPyLauncher: function checkPyLauncher (errorCallback) {
  174. this.log.verbose(
  175. `- executing "${this.pyLauncher}" to get Python executable path`)
  176. this.run(this.pyLauncher, this.argsExecutable, false,
  177. function (err, execPath) {
  178. // Possible outcomes: same as checkCommand
  179. if (err) {
  180. this.addLog(
  181. `- "${this.pyLauncher}" is not in PATH or produced an error`)
  182. return errorCallback(err)
  183. }
  184. this.addLog(`- executable path is "${execPath}"`)
  185. this.checkExecPath(execPath, errorCallback)
  186. }.bind(this))
  187. },
  188. // Check if a Python executable is the correct version to use.
  189. // Will exit the Python finder on success.
  190. checkExecPath: function checkExecPath (execPath, errorCallback) {
  191. this.log.verbose(`- executing "${execPath}" to get version`)
  192. this.run(execPath, this.argsVersion, false, function (err, version) {
  193. // Possible outcomes:
  194. // - Error: executable can not be run (likely meaning the command wasn't
  195. // a Python executable and the previous command produced gibberish)
  196. // - Gibberish: somehow the last command produced an executable path,
  197. // this will fail when verifying the version
  198. // - Version of the Python executable
  199. if (err) {
  200. this.addLog(`- "${execPath}" could not be run`)
  201. return errorCallback(err)
  202. }
  203. this.addLog(`- version is "${version}"`)
  204. const range = new semver.Range(this.semverRange)
  205. var valid = false
  206. try {
  207. valid = range.test(version)
  208. } catch (err) {
  209. this.log.silly('range.test() threw:\n%s', err.stack)
  210. this.addLog(`- "${execPath}" does not have a valid version`)
  211. this.addLog('- is it a Python executable?')
  212. return errorCallback(err)
  213. }
  214. if (!valid) {
  215. this.addLog(`- version is ${version} - should be ${this.semverRange}`)
  216. this.addLog('- THIS VERSION OF PYTHON IS NOT SUPPORTED')
  217. return errorCallback(new Error(
  218. `Found unsupported Python version ${version}`))
  219. }
  220. this.succeed(execPath, version)
  221. }.bind(this))
  222. },
  223. // Run an executable or shell command, trimming the output.
  224. run: function run (exec, args, shell, callback) {
  225. var env = extend({}, this.env)
  226. env.TERM = 'dumb'
  227. const opts = { env: env, shell: shell }
  228. this.log.silly('execFile: exec = %j', exec)
  229. this.log.silly('execFile: args = %j', args)
  230. this.log.silly('execFile: opts = %j', opts)
  231. try {
  232. this.execFile(exec, args, opts, execFileCallback.bind(this))
  233. } catch (err) {
  234. this.log.silly('execFile: threw:\n%s', err.stack)
  235. return callback(err)
  236. }
  237. function execFileCallback (err, stdout, stderr) {
  238. this.log.silly('execFile result: err = %j', (err && err.stack) || err)
  239. this.log.silly('execFile result: stdout = %j', stdout)
  240. this.log.silly('execFile result: stderr = %j', stderr)
  241. if (err) {
  242. return callback(err)
  243. }
  244. const execPath = stdout.trim()
  245. callback(null, execPath)
  246. }
  247. },
  248. succeed: function succeed (execPath, version) {
  249. this.log.info(`using Python version ${version} found at "${execPath}"`)
  250. process.nextTick(this.callback.bind(null, null, execPath))
  251. },
  252. fail: function fail () {
  253. const errorLog = this.errorLog.join('\n')
  254. const pathExample = this.win ? 'C:\\Path\\To\\python.exe'
  255. : '/path/to/pythonexecutable'
  256. // For Windows 80 col console, use up to the column before the one marked
  257. // with X (total 79 chars including logger prefix, 58 chars usable here):
  258. // X
  259. const info = [
  260. '**********************************************************',
  261. 'You need to install the latest version of Python.',
  262. 'Node-gyp should be able to find and use Python. If not,',
  263. 'you can try one of the following options:',
  264. `- Use the switch --python="${pathExample}"`,
  265. ' (accepted by both node-gyp and npm)',
  266. '- Set the environment variable PYTHON',
  267. '- Set the npm configuration variable python:',
  268. ` npm config set python "${pathExample}"`,
  269. 'For more information consult the documentation at:',
  270. 'https://github.com/nodejs/node-gyp#installation',
  271. '**********************************************************'
  272. ].join('\n')
  273. this.log.error(`\n${errorLog}\n\n${info}\n`)
  274. process.nextTick(this.callback.bind(null, new Error(
  275. 'Could not find any Python installation to use')))
  276. }
  277. }
  278. function findPython (configPython, callback) {
  279. var finder = new PythonFinder(configPython, callback)
  280. finder.findPython()
  281. }
  282. module.exports = findPython
  283. module.exports.test = {
  284. PythonFinder: PythonFinder,
  285. findPython: findPython
  286. }