forked from TurboWarp/scratch-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
universal-python.js
82 lines (76 loc) · 2.96 KB
/
universal-python.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* eslint-env node */
// There are a lot of different ways that people can install python, and there is no
// universal name that they use for the actual executable. We can already assume
// that there is a working Node.js environment, so this script can figure it out.
var child_process = require('child_process');
var _which = require('which');
function which(command) {
return _which.sync(command, {
nothrow: true
});
}
function run(command, args) {
console.log('universal-python: Running ' + command + ' with arguments ' + args.join(', '));
var subprocess = child_process.spawn(command, args, {
windowsHide: true,
shell: false,
stdio: 'inherit'
});
subprocess.on('exit', function(code) {
// Microsoft Store install shim exits with code 9009
if (process.platform === 'win32' && code === 9009) {
// eslint-disable-next-line max-len
console.log('universal-python: Attempted to start python, but got the Microsoft Store installation shim. Install python from the Microsoft Store or python.org. (code 9009)');
} else if (code === 0) {
console.log('universal-python: Success');
} else {
console.log('universal-python: Failed with code ' + code);
}
process.exit(code);
});
subprocess.on('error', function(error) {
console.log('universal-python: Error starting python; please ensure you have python installed: ' + error);
process.exit(1);
});
process.on('SIGINT', function() {
subprocess.kill('SIGINT');
});
}
var argv = process.argv.slice(2);
if (process.platform === 'win32') {
// Python in Windows is weird. Most commonly it will be installed using the python.org installers or
// from the Microsoft Store. Interestingly the Microsoft Store shims always exist on PATH and are
// also weird fake files so Node.js' fs does not work very well with them.
// Python.org installers will usually install the py launcher, so we'll try that first so that we
// don't open up the Microsoft Store for people who installed Python from there.
var py = which('py.exe');
if (py) {
run(py, ['-3'].concat(argv));
} else {
// This will either run the python installation from PATH or the Microsoft Store install shim.
// Don't know what version we'll get, but 2 and 3 both work so it doesn't matter.
run('python.exe', argv);
}
} else {
// Python 3 is usually installed as python3
var python3 = which('python3');
if (python3) {
run(python3, argv);
} else {
// Sometimes it might only be installed as python
// Don't know what version we'll get, but 2 and 3 both work so it doesn't matter
var python = which('python');
if (python) {
run(python, argv);
} else {
// Some old systems might only have python 2
var python2 = which('python2');
if (python2) {
run(python2, argv);
} else {
console.log("universal-python: Could not find python on your PATH; please ensure you have python installed.");
process.exit(1);
}
}
}
}