FindPython.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. var nThen = require('nthen');
  16. var Spawn = require('child_process').spawn;
  17. var Fs = require('fs');
  18. // We're going to prefer python2.7 or python2 because we know they work
  19. // and our test code is very strict.
  20. // If neither of those work, we'll try python3.7 or python3 which we
  21. // de-prioritize because the testing script accepts ANY python3 version
  22. // (as of this writing, we don't know what python3 versions actually work)
  23. // whereas we know that python2.7 is the only working python2 version.
  24. var PYTHONS = ["python3.7", "python3", "python2.7", "python2", "python"];
  25. var SCRIPT = [
  26. 'import sys',
  27. 'print(sys.version_info)',
  28. // we know <= 2.6 is no good
  29. 'if sys.version_info[0] == 2 and sys.version_info[1] >= 7: exit(0)',
  30. // Lets hope that all python3 versions are ok...
  31. 'if sys.version_info[0] == 3: exit(0)',
  32. ].join('\n');
  33. var find = module.exports.find = function (tempFile, callback) {
  34. var nt = nThen(function (waitFor) {
  35. Fs.writeFile(tempFile, SCRIPT, waitFor(function (err) { if (err) { throw err; } }));
  36. }).nThen;
  37. PYTHONS.forEach(function (python) {
  38. nt = nt(function (waitFor) {
  39. console.log("testing python " + python);
  40. var py = Spawn(python, [tempFile]);
  41. var cont = waitFor();
  42. py.stdout.on('data', function (dat) { console.log(dat.toString('utf8')); });
  43. py.on('close', function(ret) {
  44. if (ret === 0) {
  45. Fs.exists(tempFile, waitFor(function (exists) {
  46. if (!exists) { return; }
  47. Fs.unlink(tempFile, waitFor(function (err) {
  48. if (err) { throw err; }
  49. }));
  50. }));
  51. callback(undefined, python);
  52. waitFor.abort();
  53. } else {
  54. cont();
  55. }
  56. });
  57. py.on('error', function (err) {
  58. if (err !== 'ENOENT') { console.log('error starting python ' + err); }
  59. });
  60. // Don't worry about errors, try the next.
  61. }).nThen;
  62. });
  63. nt(function (waitFor) {
  64. callback(new Error("no sutible python2 or python3 executable found ( < 2.7 unsupported)"));
  65. });
  66. };