<commit_before>#!/usr/bin/env ruby-1.9.2-p0@global
# encoding: utf-8

gem 'activesupport',  '~> 2.3.8'
gem 'actionpack',     '~> 2.3.8'
gem 'i18n',           '~> 0.3.5'

$LOAD_PATH.unshift File.expand_path("~/Code/tap")

if ARGV.include? '-f'
  go_foreground = true
  ARGV.shift
end
PORT = 1111
ROOT = "/Users/elia"

tap_app = proc {
  Signal.trap("INT")  {exit}
  Signal.trap("TERM") {exit}
  require 'active_support'
  require 'tap_projects'
  
  Thread.abort_on_exception = true
  @server = Thread.new {
    
    Signal.trap("INT")  {exit}
    Signal.trap("TERM") {exit}
    
    puts "loading sinatra..."
    require 'sinatra/base'
    require 'haml'
    require 'action_view'
    require 'tap_server'
    
    Signal.trap("INT")  {exit}
    Signal.trap("TERM") {exit}
    
    TapServer.run! :host => 'localhost', :port => PORT
    exit
  }
  
  class MateError < StandardError
  end
  
  require 'appscript'
  include Appscript
  last = nil
  File.open(File.expand_path("#{ROOT}/.tap_history"), 'a') do |history|
    loop do
      exit if $stop
      begin
        raise(MateError) if mate_isnt_running = `ps -ax -o comm|grep TextMate`.chomp.strip.empty?
        mate = app('TextMate')
        document = mate.document.get
        raise(MateError) if document.blank?
        path = document.first.path.get rescue nil
        raise(MateError) if path.blank?
        mtime = File.stat(path).mtime
        current = [path, mtime]
        
        # Versione shell:
        # 
        #   `echo \`date +%s\`: \`pwd\``
        # 
        history << "#{mtime.to_i}: #{path}\n" unless current == last
        history.flush
        last = [path, mtime]
      rescue MateError
        # do nothing
      rescue
        puts Time.now.to_s
        puts $!.to_s
        puts $!.backtrace.join("\n")
        
        File.open(File.expand_path("#{ROOT}/.tap_errors"), "w") do |file|
          file.puts Time.now.to_s
          file.puts $!.to_s
          file.puts $!.backtrace.join("\n")
        end
        
        raise if $!.kind_of?(SignalException)
      end
      sleep 30
    end
  end
}


unless go_foreground
  pid = fork {
    Process.daemon(true)
    tap_app.call
  }
  # Process.detach pid
else
  puts "going foreground"
  tap_app.call
end
<commit_msg>Try to replace "ruby" with "TimeTap" in the command string (for "ps -A" & co.).<commit_after>#!/usr/bin/env ruby-1.9.2-p0@global
# encoding: utf-8

gem 'activesupport',  '~> 2.3.8'
gem 'actionpack',     '~> 2.3.8'
gem 'i18n',           '~> 0.3.5'

$LOAD_PATH.unshift File.expand_path("~/Code/tap")

if ARGV.include? '-f'
  go_foreground = true
  ARGV.shift
end
PORT = 1111
ROOT = "/Users/elia"

tap_app = proc {
  Signal.trap("INT")  {exit}
  Signal.trap("TERM") {exit}
  require 'active_support'
  require 'tap_projects'
  
  Thread.abort_on_exception = true
  @server = Thread.new {
    
    Signal.trap("INT")  {exit}
    Signal.trap("TERM") {exit}
    
    puts "loading sinatra..."
    require 'sinatra/base'
    require 'haml'
    require 'action_view'
    require 'tap_server'
    
    Signal.trap("INT")  {exit}
    Signal.trap("TERM") {exit}
    
    TapServer.run! :host => 'localhost', :port => PORT
    exit
  }
  
  class MateError < StandardError
  end
  
  require 'appscript'
  include Appscript
  last = nil
  File.open(File.expand_path("#{ROOT}/.tap_history"), 'a') do |history|
    loop do
      exit if $stop
      begin
        raise(MateError) if mate_isnt_running = `ps -ax -o comm|grep TextMate`.chomp.strip.empty?
        mate = app('TextMate')
        document = mate.document.get
        raise(MateError) if document.blank?
        path = document.first.path.get rescue nil
        raise(MateError) if path.blank?
        mtime = File.stat(path).mtime
        current = [path, mtime]
        
        # Versione shell:
        # 
        #   `echo \`date +%s\`: \`pwd\``
        # 
        history << "#{mtime.to_i}: #{path}\n" unless current == last
        history.flush
        last = [path, mtime]
      rescue MateError
        # do nothing
      rescue
        puts Time.now.to_s
        puts $!.to_s
        puts $!.backtrace.join("\n")
        
        File.open(File.expand_path("#{ROOT}/.tap_errors"), "w") do |file|
          file.puts Time.now.to_s
          file.puts $!.to_s
          file.puts $!.backtrace.join("\n")
        end
        
        raise if $!.kind_of?(SignalException)
      end
      sleep 30
    end
  end
}


# Try to replace "ruby" with "TimeTap" in the command string (for "ps -A" & co.)
$0 = 'TimeTap'

unless go_foreground
  pid = fork {
    Process.daemon(true)
    tap_app.call
  }
  # Process.detach pid
else
  puts "going foreground"
  tap_app.call
end

{'fraction_non_alphanumeric': 0.141422772911771, 'fraction_numerical': 0.009613330484939116, 'mean_word_length': 2.1592442645074224, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1279006', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1695, 'n_words': 373}