1
0

FindPython2.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 find = module.exports.find = function (tempFile, callback) {
  20. var nt = nThen(function (waitFor) {
  21. Fs.writeFile(tempFile,
  22. "import sys; exit(sys.version_info[0] != 2);",
  23. waitFor(function (err) { if (err) { throw err; } }));
  24. }).nThen;
  25. PYTHONS.forEach(function (python) {
  26. nt = nt(function (waitFor) {
  27. console.log("testing python " + python);
  28. var py = Spawn(python, [tempFile]);
  29. var cont = waitFor();
  30. py.on('close', function(ret) {
  31. if (ret === 0) {
  32. callback(undefined, python);
  33. waitFor.abort();
  34. } else {
  35. cont();
  36. }
  37. });
  38. py.on('error', function (err) {
  39. if (err !== 'ENOENT') { console.log('error starting python ' + err); }
  40. });
  41. // Don't worry about errors, try the next.
  42. }).nThen;
  43. });
  44. nt(function (waitFor) {
  45. callback(new Error("no sutible python2 executable found"));
  46. });
  47. };