find-python.js 11 KB

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