1
0

FindPython2.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. py.on('close', waitFor(function(ret) {
  30. if (ret === 0) {
  31. callback(undefined, python);
  32. waitFor.abort();
  33. }
  34. }));
  35. py.on('error', waitFor(function (err) {
  36. if (err !== 'ENOENT') { console.log('error starting python ' + err); }
  37. }));
  38. // Don't worry about errors, try the next.
  39. }).nThen;
  40. });
  41. nt(function (waitFor) {
  42. callback(new Error("no sutible python2 executable found"));
  43. });
  44. };