UNPKG

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