<commit_before>var fs = require( "fs" )
  , exec = require( "child_process" ).exec
  , Localhoses = function(){}
  
if ( process.platform != "darwin" ) throw "Sorry, only tested on OS X."
  
Localhoses.prototype = {
  hostsPath: "/private/etc/hosts",
  backupSuffix: ".localhoseBackup",

  domains: function(){ return Object.keys( this ) },

  set: function() {
    for ( var i = arguments.length; i--; ) this[ arguments[ i ] ] = true
    
    return this.save()
  },

  unset: function() {
    var domains = 0 in arguments ? arguments : this.domains()

    for ( var i = domains.length; i--; ) delete this[ domains[ i ] ]
    
    return this.save()
  },
  
  teardown: function() {
    var hostsPath = this.hostsPath
      , backupPath = hostsPath + this.backupSuffix
      , backup
      
    try {
      backup = fs.readFileSync( backupPath )
      fs.unlinkSync( backupPath )
      fs.writeFileSync( hostsPath, backup )
    }
    
    catch( e ){}
    
    return this
  },

  toString: function() {
    return [ "\n\n# The following were added by 'localhose':" ]
      .concat( this.domains() )
      .join( "\n127.0.0.1\t" )
  },

  save: function() {
    exec( "dscacheutil -flushcache" )

    if ( !this.domains().length ) return this.teardown()

    var hostsPath = this.hostsPath
      , backupPath = hostsPath + this.backupSuffix
      , hosts, backup
      
    try { hosts = fs.readFileSync( hostsPath ) }
    catch ( e ) { throw "/etc/hosts does not exist or cannot be read." }

    try { backup = fs.readFileSync( backupPath ) }
    catch ( e ) {
      try { fs.writeFileSync( backupPath, backup = hosts ) }
      catch ( e ){ throw backupPath + " is not writeable." }
    }
    
    fs.writeFileSync( hostsPath, backup + this.toString() )
    
    return this
  }
}

module.exports = new Localhoses<commit_msg>replaced backup file with in-file markup<commit_after>var fs = require( "fs" )
  , exec = require( "child_process" ).exec
  , Localhoses = function(){}
  
if ( process.platform != "darwin" ) throw "Sorry, only tested on OS X."
  
Localhoses.prototype = {
  hostsPath: "/private/etc/hosts",

  header:
    "# <localhose>",

  description:
    "# The following have been added temporarily by Localhose\n" +
    "# For more information, see https://github.com/jed/localhose",

  footer:
    "# </localhose>",

  domains: function(){ return Object.keys( this ) },

  set: function() {
    for ( var i = arguments.length; i--; ) this[ arguments[ i ] ] = true
    
    return this.save()
  },

  unset: function() {
    var domains = 0 in arguments ? arguments : this.domains()

    for ( var i = domains.length; i--; ) delete this[ domains[ i ] ]
    
    return this.save()
  },

  toString: function() {
    return this.domains()
      .map( function( x ){ return "127.0.0.1\t" + x } )
      .join( "\n" )
  },

  save: function() {
    var hostsPath = this.hostsPath
      , hosts, start, end

    try {
      hosts = fs.readFileSync( hostsPath, "utf8" ).split( "\n" )
      start = hosts.indexOf( this.header )
      
      if ( ~start ) {
        end = hosts.indexOf( this.footer ) || hosts.length
        hosts.splice( start, 1 + end - start )
      }

      this.domains().length && hosts.push(
        this.header, this.description, this.toString(), this.footer
      )

      try { fs.writeFileSync( hostsPath, hosts.join( "\n" ) ) }
      catch ( e ){ throw hostsPath + " is not writeable." }

      exec( "dscacheutil -flushcache" )
    }

    catch ( e ) { throw hostsPath + " does not exist or cannot be read." }
    
    return this
  }
}

module.exports = new Localhoses
{'fraction_non_alphanumeric': 0.14166897698918768, 'fraction_numerical': 0.004435819240365955, 'mean_word_length': 2.368814192343604, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2079898', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1154, 'n_words': 321}