<commit_before>"use strict";
var path = require('path')
  , fs = require('fs')
  , env = process.env
  , exec = require('child_process').exec
  , common = require("./common.js")
  , async = require('async');

function IOSChecker(log) {
  this.log = log;
  this.osVersion = null;
}
exports.IOSChecker = IOSChecker;

IOSChecker.prototype.runAllChecks = function(cb) {
  async.series([
    this.getMacOSXVersion.bind(this),
    this.checkForXcode.bind(this),
    this.checkForXcodeCommandLineTools.bind(this),
    this.checkDevToolsSecurity.bind(this),
    this.checkAuthorizationDB.bind(this),
    this.checkForNodeBinary.bind(this)
  ], cb);
};

IOSChecker.prototype.getMacOSXVersion = function(cb) {
  exec("sw_vers -productVersion", function(err, stdout) {
    if (err === null) {
      if (stdout.match('10.8') !== null) {
        this.osVersion = '10.8';
        cb(null, "Mac OS X 10.8 is installed.");
      } else if (stdout.match('10.9') !== null) {
        this.osVersion = '10.9';
        cb(null, "Mac OS X 10.9 is installed.");
      } else {
        this.log.fail("Could not detect Mac OS X Version", cb);
      }
    } else {
      this.log.fail("Unknown SW Version Command: " + err, cb);
    }
  }.bind(this));
};

IOSChecker.prototype.checkForXcode = function(cb) {
  var msg;
  exec("xcode-select --print-path", { maxBuffer: 524288}, function(err, stdout) {
    if (err === null) {
      var xcodePath = stdout.replace("\n","");
      if(fs.existsSync(xcodePath)) {
        this.log.pass("Xcode is installed at " + xcodePath, cb);
      } else {
        msg = "Xcode is not installed.";
        this.log.fail(msg);
        this.log.promptToFix("Xcode is not installed.", function() {
          this.installXcode(cb);
        }.bind(this), function() {
          cb(msg, msg);
        });
      }
    } else {
      msg = "Xcode is not installed: " + err;
      this.log.fail(msg);
      this.log.promptToFix("Xcode is not installed.", function() {
        this.installXcode(cb);
      }.bind(this), function() {
        cb(msg, msg);
      });
    }
  }.bind(this));
};

IOSChecker.prototype.checkForXcodeCommandLineTools = function(cb) {
  var msg;
  var pkgName = this.osVersion === '10.8' ? "com.apple.pkg.DeveloperToolsCLI" : "com.apple.pkg.CLTools_Executables";
  exec("pkgutil --pkg-info=" + pkgName, { maxBuffer: 524288}, function(err, stdout) {
    if (err === null) {
      var match = stdout.match(/install-time/);
      if (match !== null) {
        this.log.pass("Xcode Command Line Tools are installed.", cb);
      } else {
        msg = "Xcode Command Line Tools are NOT installed.";
        this.log.fail(msg);
        this.log.promptToFix("Xcode's command line tools are NOT installed.", function() {
          this.installXcodeCommandLineTools(cb);
        }.bind(this), function() {
          cb(msg, msg);
        });
      }
    } else {
      msg = "Xcode Command Line Tools are NOT installed: " + err;
      this.log.fail(msg);
      this.log.promptToFix("Xcode's command line tools are NOT installed.", function() {
        this.installXcodeCommandLineTools(cb);
      }.bind(this), function() {
        cb(msg, msg);
      });
    }
  }.bind(this));
};

IOSChecker.prototype.checkDevToolsSecurity = function(cb) {
  var msg;
  exec("DevToolsSecurity", { maxBuffer: 524288}, function(err, stdout) {
    if (err === null && stdout.match(/enabled/) !== null) {
      this.log.pass("DevToolsSecurity is enabled.", cb);
    } else {
      msg = 'DevToolsSecurity is not enabled.';
      this.log.fail(msg);
      this.log.promptToFix(msg, function() {
        this.authorizeIOS(this.checkDevToolsSecurity.bind(this), cb);
      }.bind(this), function() {
        cb(msg, msg);
      });
    }
  }.bind(this));
};

IOSChecker.prototype.checkAuthorizationDB = function(cb) {
  var msg;
  if (this.osVersion === '10.8') {
    fs.readFile('/etc/authorization', 'utf8', function (err,data) {
      if (err) {
        this.log.fail('The Authorization DB is NOT set up properly.', cb);
      } else {
        var match = data.match(/<key>system.privilege.taskport<\/key>\s*\n\s*<dict>\n\s*<key>allow-root<\/key>\n\s*(<true\/>)/);
        if (match !== null) {
          this.log.pass("The Authorization DB is set up properly.", cb);
        } else {
          this.log.fail('The Authorization DB is NOT set up properly.', cb);
        }
      }
    }.bind(this));
  } else {
    exec("security authorizationdb read system.privilege.taskport", { maxBuffer: 524288}, function(err, stdout) {
      if (err === null && stdout.match(/is-developer/) !== null) {
        this.log.pass("The Authorization DB is set up properly.", cb);
      } else {
        msg = 'The Authorization DB is NOT set up properly.';
        this.log.fail(msg);
        this.log.promptToFix(msg, function() {
          this.authorizeIOS(this.checkAuthorizationDB.bind(this), cb);
        }.bind(this), function() {
          cb(msg, msg);
        });
      }
    }.bind(this));
  }
};

IOSChecker.prototype.checkForNodeBinary = function(cb) {
  this.checkForNodeBinaryInCommonPlaces(function(err, msg) {
    if (!err) {<commit_msg>update appium doctor to respect new auth db value<commit_after>"use strict";
var path = require('path')
  , fs = require('fs')
  , env = process.env
  , exec = require('child_process').exec
  , common = require("./common.js")
  , async = require('async');

function IOSChecker(log) {
  this.log = log;
  this.osVersion = null;
}
exports.IOSChecker = IOSChecker;

IOSChecker.prototype.runAllChecks = function(cb) {
  async.series([
    this.getMacOSXVersion.bind(this),
    this.checkForXcode.bind(this),
    this.checkForXcodeCommandLineTools.bind(this),
    this.checkDevToolsSecurity.bind(this),
    this.checkAuthorizationDB.bind(this),
    this.checkForNodeBinary.bind(this)
  ], cb);
};

IOSChecker.prototype.getMacOSXVersion = function(cb) {
  exec("sw_vers -productVersion", function(err, stdout) {
    if (err === null) {
      if (stdout.match('10.8') !== null) {
        this.osVersion = '10.8';
        cb(null, "Mac OS X 10.8 is installed.");
      } else if (stdout.match('10.9') !== null) {
        this.osVersion = '10.9';
        cb(null, "Mac OS X 10.9 is installed.");
      } else {
        this.log.fail("Could not detect Mac OS X Version", cb);
      }
    } else {
      this.log.fail("Unknown SW Version Command: " + err, cb);
    }
  }.bind(this));
};

IOSChecker.prototype.checkForXcode = function(cb) {
  var msg;
  exec("xcode-select --print-path", { maxBuffer: 524288}, function(err, stdout) {
    if (err === null) {
      var xcodePath = stdout.replace("\n","");
      if(fs.existsSync(xcodePath)) {
        this.log.pass("Xcode is installed at " + xcodePath, cb);
      } else {
        msg = "Xcode is not installed.";
        this.log.fail(msg);
        this.log.promptToFix("Xcode is not installed.", function() {
          this.installXcode(cb);
        }.bind(this), function() {
          cb(msg, msg);
        });
      }
    } else {
      msg = "Xcode is not installed: " + err;
      this.log.fail(msg);
      this.log.promptToFix("Xcode is not installed.", function() {
        this.installXcode(cb);
      }.bind(this), function() {
        cb(msg, msg);
      });
    }
  }.bind(this));
};

IOSChecker.prototype.checkForXcodeCommandLineTools = function(cb) {
  var msg;
  var pkgName = this.osVersion === '10.8' ? "com.apple.pkg.DeveloperToolsCLI" : "com.apple.pkg.CLTools_Executables";
  exec("pkgutil --pkg-info=" + pkgName, { maxBuffer: 524288}, function(err, stdout) {
    if (err === null) {
      var match = stdout.match(/install-time/);
      if (match !== null) {
        this.log.pass("Xcode Command Line Tools are installed.", cb);
      } else {
        msg = "Xcode Command Line Tools are NOT installed.";
        this.log.fail(msg);
        this.log.promptToFix("Xcode's command line tools are NOT installed.", function() {
          this.installXcodeCommandLineTools(cb);
        }.bind(this), function() {
          cb(msg, msg);
        });
      }
    } else {
      msg = "Xcode Command Line Tools are NOT installed: " + err;
      this.log.fail(msg);
      this.log.promptToFix("Xcode's command line tools are NOT installed.", function() {
        this.installXcodeCommandLineTools(cb);
      }.bind(this), function() {
        cb(msg, msg);
      });
    }
  }.bind(this));
};

IOSChecker.prototype.checkDevToolsSecurity = function(cb) {
  var msg;
  exec("DevToolsSecurity", { maxBuffer: 524288}, function(err, stdout) {
    if (err === null && stdout.match(/enabled/) !== null) {
      this.log.pass("DevToolsSecurity is enabled.", cb);
    } else {
      msg = 'DevToolsSecurity is not enabled.';
      this.log.fail(msg);
      this.log.promptToFix(msg, function() {
        this.authorizeIOS(this.checkDevToolsSecurity.bind(this), cb);
      }.bind(this), function() {
        cb(msg, msg);
      });
    }
  }.bind(this));
};

IOSChecker.prototype.checkAuthorizationDB = function(cb) {
  var msg;
  exec("security authorizationdb read system.privilege.taskport", { maxBuffer: 524288}, function(err, stdout) {
    if (err === null && (stdout.match(/is-developer/) !== null || stdout.match(/allow/) !== null)) {
      this.log.pass("The Authorization DB is set up properly.", cb);
    } else if (this.osVersion === '10.8') {
      fs.readFile('/etc/authorization', 'utf8', function (err,data) {
        if (err) {
          this.log.fail('The Authorization DB is NOT set up properly.', cb);
        } else {
          var match = data.match(/<key>system.privilege.taskport<\/key>\s*\n\s*<dict>\n\s*<key>allow-root<\/key>\n\s*(<true\/>)/);
          if (match !== null) {
            this.log.pass("The Authorization DB is set up properly.", cb);
          } else {
            this.log.fail('The Authorization DB is NOT set up properly.', cb);
          }
        }
      }.bind(this));
    } else {
      msg = 'The Authorization DB is NOT set up properly.';
      this.log.fail(msg);
      this.log.promptToFix(msg, function() {
        this.authorizeIOS(this.checkAuthorizationDB.bind(this), cb);
      }.bind(this), function() {
        cb(msg, msg);
      });
    }
  }.bind(this));
};

IOSChecker.prototype.checkForNodeBinary = function(cb) {
  this.checkForNodeBinaryInCommonPlaces(function(err, msg) {
    if (!err) {
{'fraction_non_alphanumeric': 0.16563347539713288, 'fraction_numerical': 0.009492444788841535, 'mean_word_length': 2.6743772241992882, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7084137', 'n_tokens_mistral': 3468, 'n_tokens_neox': 3363, 'n_words': 718}