initial commit

This commit is contained in:
Rob Watson 2010-11-10 15:08:57 +00:00
parent f15865c5a2
commit de686f0c79
4 changed files with 147 additions and 2 deletions

View File

@ -5,8 +5,8 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "noprocast"
gem.summary = %Q{TODO: one-line summary of your gem}
gem.description = %Q{TODO: longer description of your gem}
gem.summary = %Q{Give procastination a swift kick in the balls.}
gem.description = %Q{Block access to addictive websites in one command-line swoop.}
gem.email = "rfwatson@gmail.com"
gem.homepage = "http://github.com/rfwatson/noprocast"
gem.authors = ["Rob Watson"]

24
bin/noprocast Normal file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env ruby
require File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'noprocast')
begin
case ARGV.first
when 'on'
Noprocast.activate!
when 'off'
Noprocast.deactivate!
when 'status'
puts Noprocast.status_message
when 'edit'
if Process.uid != File.stat(Noprocast.deny_file_path).uid
puts "Can't edit this file as it belongs to another user (hint: don't use sudo)"
exit
end
Noprocast.edit!
when 'help', '-h', '--help', nil
puts "Usage: [sudo] noprocast [on|off|status|edit]"
end
rescue Errno::EACCES
puts "Permission denied: try sudo noprocast"
end

View File

@ -0,0 +1,65 @@
require 'fileutils'
class Noprocast
class << self
def default_hosts
['news.ycombinator.com', 'twitter.com', 'facebook.com', 'reddit.com']
end
def deny_file_path
File.expand_path("~/.noprocast")
end
def current_hosts
setup_deny_file_if_required!
File.read(deny_file_path).split(/\n/).map(&:strip)
end
def hosts_file_content
File.read("/etc/hosts")
end
def activate!
backup_hosts_file_if_required!
deactivate! # ensure that /etc/hosts is clean
File.open("/etc/hosts", 'a') do |file|
file << "\n\n# noprocast start\n#{current_hosts.map { |host| "127.0.0.1 #{host}" }.join("\n")}\n# noprocast end"
end
end
def deactivate!
clean_hosts = hosts_file_content.gsub(/(\n\n)?\# noprocast start.*\# noprocast end/m, '')
File.open("/etc/hosts", 'w') do |file|
file << clean_hosts
end
end
def active?
hosts_file_content.match(/\# noprocast start/)
end
def status_message
active? ? "noprocast enabled for #{current_hosts.size} hosts" : "noprocast disabled"
end
def backup_hosts_file_if_required!
unless File.exists?("/etc/.hosts.noprocastbackup")
FileUtils.cp("/etc/hosts", "/etc/.hosts.noprocastbackup")
end
end
def setup_deny_file_if_required!
unless File.exists?(deny_file_path)
File.open(deny_file_path, 'w') do |file|
file << default_hosts.join("\n")
end
end
end
def edit!
editor = ENV['EDITOR'] || 'vi'
system "#{editor} #{deny_file_path}"
end
end
end

56
noprocast.gemspec Normal file
View File

@ -0,0 +1,56 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = %q{noprocast}
s.version = "0.1.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Rob Watson"]
s.date = %q{2010-11-10}
s.default_executable = %q{noprocast}
s.description = %q{Block access to addictive websites in one command-line swoop.}
s.email = %q{rfwatson@gmail.com}
s.executables = ["noprocast"]
s.extra_rdoc_files = [
"LICENSE",
"README.rdoc"
]
s.files = [
".document",
".gitignore",
"LICENSE",
"README.rdoc",
"Rakefile",
"VERSION",
"lib/noprocast.rb",
"spec/noprocast_spec.rb",
"spec/spec.opts",
"spec/spec_helper.rb"
]
s.homepage = %q{http://github.com/rfwatson/noprocast}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
s.summary = %q{Give procastination a swift kick in the balls.}
s.test_files = [
"spec/noprocast_spec.rb",
"spec/spec_helper.rb"
]
if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
else
s.add_dependency(%q<rspec>, [">= 1.2.9"])
end
else
s.add_dependency(%q<rspec>, [">= 1.2.9"])
end
end