summaryrefslogtreecommitdiff
path: root/lib/configure.js
blob: 9a5696aa0c1b14a5fb1016b1183c4d498252fab4 (plain)
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
/*
 * mongo-edu
 *
 * Copyright (c) 2014 Przemyslaw Pluta
 * Licensed under the MIT license.
 * https://github.com/przemyslawpluta/mongo-edu/blob/master/LICENSE
 */

var fs = require('fs'),
    path = require('path'),
    which = require('which');

module.exports = function configure(argv, callback) {

    'use strict';

    var python = argv.py || process.env.PYTHON || 'python',
        isWin = /^win/.test(process.platform);

    function checkPython() {

        which(python, function whichTest(err) {
            if (err !== null) {
                //console.log('`which` failed', python, err);
                if (isWin) { guessPython(); } else { failNoPython(); }
            } else {
                //console.log('`which` succeeded', python, execPath);
                process.env.PYTHON = python;
                callback(null);
            }
        });
    }

    function guessPython() {

        console.log('Could not find "' + python + '". guessing location');

        var rootDir = process.env.SystemDrive || 'C:\\', pythonPath;

        if (rootDir[rootDir.length - 1] !== '\\') { rootDir += '\\'; }

        pythonPath = path.resolve(rootDir, 'Python27', 'python.exe');
        console.log('ensuring that file exists:', pythonPath);

        fs.stat(pythonPath, function stat(err) {
            if (err !== null) {
                if (err.code === 'ENOENT') { failNoPython(); } else { callback(err); }
                return;
            }
            python = pythonPath;
            process.env.PYTHON = pythonPath;
            callback(null);
        });
    }

    function failNoPython() {
        callback(new Error('Can\'t find Python executable "' + python + '", you can set the PYTHON env variable.'));
    }

    checkPython();
};