find-python.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.6.0 || >=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:', 'Python27', 'python.exe'),
  26. path.join(process.env.SystemDrive || 'C:', 'Python37', '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 "python" can be used') },
  84. check: this.checkCommand,
  85. arg: 'python'
  86. },
  87. {
  88. before: () => { this.addLog('checking if "python2" can be used') },
  89. check: this.checkCommand,
  90. arg: 'python2'
  91. },
  92. {
  93. before: () => { this.addLog('checking if "python3" can be used') },
  94. check: this.checkCommand,
  95. arg: 'python3'
  96. }
  97. ]
  98. if (this.win) {
  99. checks.push({
  100. before: () => {
  101. this.addLog(
  102. 'checking if the py launcher can be used to find Python 2')
  103. },
  104. check: this.checkPyLauncher
  105. })
  106. for (var i = 0; i < this.winDefaultLocations.length; ++i) {
  107. const location = this.winDefaultLocations[i]
  108. checks.push({
  109. before: () => {
  110. this.addLog('checking if Python is ' +
  111. `${location}`)
  112. },
  113. check: this.checkExecPath,
  114. arg: location
  115. })
  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. // Because the Python launcher supports all versions of Python, we have to
  174. // explicitly request a Python 2 version. This is done by supplying "-2" as
  175. // the first command line argument. Since "py.exe -2" would be an invalid
  176. // executable for "execFile", we have to use the launcher to figure out
  177. // where the actual "python.exe" executable is located.
  178. checkPyLauncher: function checkPyLauncher (errorCallback) {
  179. this.log.verbose(
  180. `- executing "${this.pyLauncher}" to get Python 2 executable path`)
  181. this.run(this.pyLauncher, ['-2', ...this.argsExecutable], false,
  182. function (err, execPath) {
  183. // Possible outcomes: same as checkCommand
  184. if (err) {
  185. this.addLog(
  186. `- "${this.pyLauncher}" is not in PATH or produced an error`)
  187. return errorCallback(err)
  188. }
  189. this.addLog(`- executable path is "${execPath}"`)
  190. this.checkExecPath(execPath, errorCallback)
  191. }.bind(this))
  192. },
  193. // Check if a Python executable is the correct version to use.
  194. // Will exit the Python finder on success.
  195. checkExecPath: function checkExecPath (execPath, errorCallback) {
  196. this.log.verbose(`- executing "${execPath}" to get version`)
  197. this.run(execPath, this.argsVersion, false, function (err, version) {
  198. // Possible outcomes:
  199. // - Error: executable can not be run (likely meaning the command wasn't
  200. // a Python executable and the previous command produced gibberish)
  201. // - Gibberish: somehow the last command produced an executable path,
  202. // this will fail when verifying the version
  203. // - Version of the Python executable
  204. if (err) {
  205. this.addLog(`- "${execPath}" could not be run`)
  206. return errorCallback(err)
  207. }
  208. this.addLog(`- version is "${version}"`)
  209. const range = new semver.Range(this.semverRange)
  210. var valid = false
  211. try {
  212. valid = range.test(version)
  213. } catch (err) {
  214. this.log.silly('range.test() threw:\n%s', err.stack)
  215. this.addLog(`- "${execPath}" does not have a valid version`)
  216. this.addLog('- is it a Python executable?')
  217. return errorCallback(err)
  218. }
  219. if (!valid) {
  220. this.addLog(`- version is ${version} - should be ${this.semverRange}`)
  221. this.addLog('- THIS VERSION OF PYTHON IS NOT SUPPORTED')
  222. return errorCallback(new Error(
  223. `Found unsupported Python version ${version}`))
  224. }
  225. this.succeed(execPath, version)
  226. }.bind(this))
  227. },
  228. // Run an executable or shell command, trimming the output.
  229. run: function run (exec, args, shell, callback) {
  230. var env = extend({}, this.env)
  231. env.TERM = 'dumb'
  232. const opts = { env: env, shell: shell }
  233. this.log.silly('execFile: exec = %j', exec)
  234. this.log.silly('execFile: args = %j', args)
  235. this.log.silly('execFile: opts = %j', opts)
  236. try {
  237. this.execFile(exec, args, opts, execFileCallback.bind(this))
  238. } catch (err) {
  239. this.log.silly('execFile: threw:\n%s', err.stack)
  240. return callback(err)
  241. }
  242. function execFileCallback (err, stdout, stderr) {
  243. this.log.silly('execFile result: err = %j', (err && err.stack) || err)
  244. this.log.silly('execFile result: stdout = %j', stdout)
  245. this.log.silly('execFile result: stderr = %j', stderr)
  246. if (err) {
  247. return callback(err)
  248. }
  249. const execPath = stdout.trim()
  250. callback(null, execPath)
  251. }
  252. },
  253. succeed: function succeed (execPath, version) {
  254. this.log.info(`using Python version ${version} found at "${execPath}"`)
  255. process.nextTick(this.callback.bind(null, null, execPath))
  256. },
  257. fail: function fail () {
  258. const errorLog = this.errorLog.join('\n')
  259. const pathExample = this.win ? 'C:\\Path\\To\\python.exe'
  260. : '/path/to/pythonexecutable'
  261. // For Windows 80 col console, use up to the column before the one marked
  262. // with X (total 79 chars including logger prefix, 58 chars usable here):
  263. // X
  264. const info = [
  265. '**********************************************************',
  266. 'You need to install the latest version of Python.',
  267. 'Node-gyp should be able to find and use Python. If not,',
  268. 'you can try one of the following options:',
  269. `- Use the switch --python="${pathExample}"`,
  270. ' (accepted by both node-gyp and npm)',
  271. '- Set the environment variable PYTHON',
  272. '- Set the npm configuration variable python:',
  273. ` npm config set python "${pathExample}"`,
  274. 'For more information consult the documentation at:',
  275. 'https://github.com/nodejs/node-gyp#installation',
  276. '**********************************************************'
  277. ].join('\n')
  278. this.log.error(`\n${errorLog}\n\n${info}\n`)
  279. process.nextTick(this.callback.bind(null, new Error(
  280. 'Could not find any Python installation to use')))
  281. }
  282. }
  283. function findPython (configPython, callback) {
  284. var finder = new PythonFinder(configPython, callback)
  285. finder.findPython()
  286. }
  287. module.exports = findPython
  288. module.exports.test = {
  289. PythonFinder: PythonFinder,
  290. findPython: findPython
  291. }