find-python.js 12 KB

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