1
0

FindPython2.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. var nThen = require('nthen');
  16. var Spawn = require('child_process').spawn;
  17. var Fs = require('fs');
  18. var PYTHONS = ["python", "python2", "python2.7", "python2.6", "python2.5"];
  19. var SCRIPT = [
  20. 'import sys;',
  21. 'print(sys.version_info);',
  22. 'exit(sys.version_info[0] != 2 or sys.version_info[1] < 7);'
  23. ].join('\n');
  24. var find = module.exports.find = function (tempFile, callback) {
  25. var nt = nThen(function (waitFor) {
  26. Fs.writeFile(tempFile, SCRIPT, waitFor(function (err) { if (err) { throw err; } }));
  27. }).nThen;
  28. PYTHONS.forEach(function (python) {
  29. nt = nt(function (waitFor) {
  30. console.log("testing python " + python);
  31. var py = Spawn(python, [tempFile]);
  32. var cont = waitFor();
  33. py.stdout.on('data', function (dat) { console.log(dat.toString('utf8')); });
  34. py.on('close', function(ret) {
  35. if (ret === 0) {
  36. callback(undefined, python);
  37. waitFor.abort();
  38. } else {
  39. cont();
  40. }
  41. });
  42. py.on('error', function (err) {
  43. if (err !== 'ENOENT') { console.log('error starting python ' + err); }
  44. });
  45. // Don't worry about errors, try the next.
  46. }).nThen;
  47. });
  48. nt(function (waitFor) {
  49. callback(new Error("no sutible python2 executable found ( < 2.7 unsupported)"));
  50. });
  51. };