hosum 0.0.1a

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hosum.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rb-fsevent'
8
+ gem 'growl_notify'
9
+ end
@@ -0,0 +1,11 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { "spec" } #{ |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ watch(%r{^spec/(.+).(dsl|hosts|dnsmasq)$}) { "spec" }
10
+ end
11
+
data/README ADDED
File without changes
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1a
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
+
5
+ require "hosum"
6
+ require 'optparse'
7
+
8
+
9
+ options = {
10
+ :output_type => :hosts,
11
+ :context => []
12
+ }
13
+ OptionParser.new do |opts|
14
+ opts.banner = "Usage: hosum [options] hosum_config_path"
15
+
16
+ opts.on("--dnsmasq", "Output dnsmasq config") do |v|
17
+ options[:output_type] = :dnsmasq
18
+ end
19
+
20
+ opts.on("--context CONTEXT", "comma separated, ex: --context adsl,bstar") do |values|
21
+ options[:context] = values.split(",")
22
+ end
23
+ end.parse!(ARGV)
24
+
25
+ p options
26
+ p ARGV
27
+
28
+
29
+ rs = Hosum.dsl_by_file(ARGV.first)
30
+ if options[:output_type] == :dnsmasq
31
+ puts rs.to_dnsmasq
32
+ else
33
+ puts rs.to_hosts
34
+ end
@@ -0,0 +1,70 @@
1
+ group "google" do
2
+ ip "203.208.46.163"
3
+
4
+ subject "core" do
5
+ hosts "accounts.google.com"
6
+ hosts "www.google.com"
7
+ hosts "ssl.gstatic.com"
8
+ hosts "lh[1-5].googleusercontent.com"
9
+ hosts "ssl.google-analytics.com"
10
+ hosts "gg.google.com"
11
+ end
12
+
13
+ subject "plus" do
14
+ hosts "plus.google.com"
15
+ end
16
+
17
+ subject "search" do
18
+ hosts "encrypted.google.com"
19
+ hosts "encrypted-tbn[0-3].google.com"
20
+ hosts "clients1.google.com"
21
+ hosts "news.google.com"
22
+ hosts "webcache.googleusercontent.com"
23
+ hosts "www.google.com.hk"
24
+ end
25
+
26
+ subject "map" do
27
+ hosts "maps.google.com"
28
+ hosts "maps.gstatic.com"
29
+ hosts "mts[0-1].google.com"
30
+ end
31
+
32
+ subject "reader" do
33
+ hosts "s2.googleusercontent.com"
34
+ hosts "reader.googleusercontent.com"
35
+ hosts "mw2.google.com"
36
+ hosts "t0.gstatic.com"
37
+ end
38
+
39
+ subject "mail" do
40
+ hosts "www.gmail.com"
41
+ hosts "mail.google.com"
42
+ hosts "chatenabled.mail.google.com"
43
+ hosts "mail-attachment.googleusercontent.com"
44
+ end
45
+
46
+ subject "docs" do
47
+ hosts "docs.google.com"
48
+ hosts "[1-99].docs.google.com", :dnsmasq => ".docs.google.com"
49
+ hosts "themes.googleusercontent.com"
50
+ end
51
+
52
+ subject "calendar" do
53
+ hosts "calendar.google.com"
54
+ end
55
+
56
+ subject "video" do
57
+ hosts "video.google.com"
58
+ end
59
+
60
+ end
61
+
62
+ group "itunes" do
63
+ hosts "a[1-2000].phobos.apple.com", :dnsmasq => ".phobos.apple.com"
64
+
65
+ ip "203.69.138.18"
66
+ end
67
+
68
+
69
+
70
+
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "hosum"
5
+ s.version = File.read(File.expand_path("../VERSION", __FILE__)).chomp
6
+ s.authors = ["Sunteya"]
7
+ s.email = ["Sunteya@gmail.com"]
8
+ s.homepage = ""
9
+ s.summary = %q{host file generator}
10
+ s.description = %q{host file generator}
11
+
12
+ s.rubyforge_project = "hosum"
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_runtime_dependency "activesupport"
20
+ s.add_runtime_dependency "i18n"
21
+
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "guard-rspec"
24
+ s.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,24 @@
1
+ require "active_support/all"
2
+ require "hosum/dsl"
3
+
4
+ module Hosum
5
+ VERSION = File.read(File.expand_path("../../VERSION", __FILE__)).chomp
6
+
7
+ autoload :Container, "hosum/container"
8
+ autoload :Subject, "hosum/subject"
9
+ autoload :Hosts, "hosum/hosts"
10
+ autoload :Dsl, "hosum/dsl"
11
+ autoload :Group, "hosum/group"
12
+ autoload :Ips, "hosum/ips"
13
+
14
+ def self.dsl(options = {}, &block)
15
+ Dsl.new(options = {}, &block)
16
+ end
17
+
18
+ def self.dsl_by_file(path)
19
+ self.dsl do
20
+ instance_eval(File.read(path), path, 1)
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,5 @@
1
+ require "hosum//version"
2
+
3
+ module Hosum/
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,24 @@
1
+ module Hosum
2
+ class Container
3
+ attr_accessor :parent
4
+ attr_accessor :children
5
+ attr_accessor :options
6
+
7
+ attr_accessor :ips_list
8
+
9
+ def initialize
10
+ self.children = []
11
+ self.ips_list = []
12
+ self.options = {}
13
+ end
14
+
15
+ def find_first_ip
16
+ if self.ips_list.empty?
17
+ self.parent.find_first_ip if self.parent
18
+ else
19
+ self.ips_list.first.addresses.first
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,93 @@
1
+ require "ostruct"
2
+
3
+ module Hosum
4
+ class Dsl
5
+ attr_accessor :containers, :ips_list_scopes
6
+ attr_accessor :groups
7
+
8
+ def initialize(options = {}, &block)
9
+ self.groups = []
10
+ self.containers = []
11
+ self.ips_list_scopes = []
12
+
13
+ instance_eval(&block) if block
14
+ end
15
+
16
+ def containers_eval(object, &block)
17
+ self.containers.push(object)
18
+ instance_eval(&block)
19
+ self.containers.pop
20
+ end
21
+
22
+ def group(name, &block)
23
+ group = Group.new
24
+ group.name = name
25
+ self.groups << group
26
+
27
+ containers_eval(group, &block) if block
28
+ end
29
+
30
+ def ip(*args, &block)
31
+ options = args.extract_options!
32
+
33
+ ips = Ips.new
34
+ ips.options = options
35
+ ips.addresses = [ args ].flatten
36
+
37
+ if block
38
+ self.ips_list_scopes << [ ips ]
39
+ instance_eval(&block)
40
+ self.ips_list_scopes.pop
41
+ else
42
+ self.containers.last.ips_list << ips
43
+ end
44
+ end
45
+
46
+ def subject(name, &block)
47
+ subject = Subject.new
48
+ subject.name = name
49
+
50
+ parent = self.containers.last
51
+ parent.children << subject
52
+ subject.parent = parent
53
+
54
+ containers_eval(subject, &block) if block
55
+ end
56
+
57
+ def hosts(*args, &block)
58
+ options = args.extract_options!
59
+
60
+ container = self.containers.last
61
+ hosts = Hosts.new
62
+ hosts.options = options
63
+ hosts.domains = [ args ].flatten
64
+ hosts.parent = container
65
+ container.children << hosts
66
+
67
+ if !self.ips_list_scopes.empty?
68
+ ips_list = self.ips_list_scopes.last
69
+ hosts.ips_list = ips_list
70
+ end
71
+
72
+ containers_eval(hosts, &block) if block
73
+ end
74
+
75
+ def to_hosts(options = {})
76
+ self.groups.map do |group|
77
+ group.to_hosts(options)
78
+ end.join("\n\n")
79
+ end
80
+
81
+ def to_dnsmasq(options = {})
82
+ self.groups.map do |group|
83
+ group.to_dnsmasq(options)
84
+ end.join("\n\n")
85
+ end
86
+
87
+ end
88
+
89
+ class DslException < Exception
90
+
91
+ end
92
+
93
+ end
@@ -0,0 +1,15 @@
1
+ module Hosum
2
+
3
+ class Group < Container
4
+ attr_accessor :name
5
+
6
+ def to_hosts(options = {})
7
+ "# #{self.name}\n" + children.map{ |c| c.to_hosts(options) }.join
8
+ end
9
+
10
+ def to_dnsmasq(options = {})
11
+ "# #{self.name}\n" + children.map{ |c| c.to_dnsmasq(options) }.join
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,59 @@
1
+ module Hosum
2
+ class Hosts < Container
3
+ attr_accessor :domains
4
+
5
+ def initialize
6
+ super
7
+ self.domains = []
8
+ end
9
+
10
+ def expand_domains
11
+ self.domains.map do |domain|
12
+ holders = []
13
+
14
+ regex = /\[([^\[\]]+)-([^\[\]]+)\]/
15
+ domain = domain.gsub(regex) do |str|
16
+ m = str.match(regex)
17
+ holders << Range.new(m[1], m[2]).to_a
18
+ "%s"
19
+ end
20
+
21
+ expand_list = []
22
+ holders.each do |holder|
23
+ if expand_list.empty?
24
+ expand_list = holder.map {|v| [v] }
25
+ next
26
+ end
27
+
28
+ tmp = []
29
+ expand_list.each do |expand|
30
+ holder.each do |h|
31
+ tmp << expand + [ h ]
32
+ end
33
+ end
34
+ expand_list = tmp
35
+ end
36
+
37
+ if holders.empty?
38
+ domain
39
+ else
40
+ expand_list.map { |e| domain % e}
41
+ end
42
+
43
+ end.flatten
44
+ end
45
+
46
+ def to_hosts(options = {})
47
+ self.expand_domains.map{ |domain| "#{find_first_ip} #{domain}\n" }.join
48
+ end
49
+
50
+ def to_dnsmasq(options = {})
51
+ domains = self.expand_domains
52
+ if self.options[:dnsmasq]
53
+ domains = [ self.options[:dnsmasq] ].flatten
54
+ end
55
+
56
+ domains.map{ |domain| "address=/#{domain}/#{find_first_ip}\n" }.join
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,6 @@
1
+ module Hosum
2
+ class Ips
3
+ attr_accessor :options
4
+ attr_accessor :addresses
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ module Hosum
2
+
3
+ class Subject < Container
4
+ attr_accessor :name
5
+
6
+ def to_hosts(options = {})
7
+ "\n# #{self.parent.name}.#{self.name}\n" + children.map{ |c| c.to_hosts(options) }.join
8
+ end
9
+
10
+ def to_dnsmasq(options = {})
11
+ "\n# #{self.parent.name}.#{self.name}\n" + children.map{ |c| c.to_dnsmasq(options) }.join
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,2 @@
1
+ # itunes
2
+ address=/.phobos.apple.com/203.69.138.18
@@ -0,0 +1,5 @@
1
+ group "itunes" do
2
+ hosts "a[1-2000].phobos.apple.com", :dnsmasq => ".phobos.apple.com"
3
+
4
+ ip "203.69.138.18", :any => :bstar
5
+ end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ describe Hosum::Hosts do
4
+
5
+ it "expand hosts" do
6
+ hosts = Hosum::Hosts.new
7
+ hosts.domains << "a[7-10].apple.com"
8
+ hosts.expand_domains.should == [ "a7.apple.com", "a8.apple.com", "a9.apple.com", "a10.apple.com" ]
9
+ end
10
+
11
+ it "don't expand hosts" do
12
+ hosts = Hosum::Hosts.new
13
+ hosts.domains << "www.apple.com"
14
+ hosts.expand_domains.should == [ "www.apple.com" ]
15
+ end
16
+
17
+ end
@@ -0,0 +1,19 @@
1
+ require "spec_helper"
2
+
3
+ describe Hosum do
4
+
5
+ it "dsl" do
6
+ paths = File.expand_path("../*.dsl", __FILE__)
7
+ Dir.glob(paths).each do |dsl_path|
8
+ rs = Hosum.dsl_by_file(dsl_path)
9
+
10
+ hosts_file = dsl_path.sub(".dsl", ".hosts")
11
+ rs.to_hosts.should == File.read(hosts_file) if File.exist?(hosts_file)
12
+
13
+ dnsmasq_file = dsl_path.sub(".dsl", ".dnsmasq")
14
+ rs.to_dnsmasq.should == File.read(dnsmasq_file) if File.exist?(dnsmasq_file)
15
+
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,12 @@
1
+ group "itunes" do
2
+ ip "1.1.1.1"
3
+ hosts "www1.apple.com"
4
+
5
+ hosts "www2.apple.com" do
6
+ ip "2.2.2.2"
7
+ end
8
+
9
+ ip "3.3.3.3" do
10
+ hosts "www3.apple.com"
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ # itunes
2
+ 1.1.1.1 www1.apple.com
3
+ 2.2.2.2 www2.apple.com
4
+ 3.3.3.3 www3.apple.com
@@ -0,0 +1,14 @@
1
+ group "google" do
2
+
3
+ ip "1.1.1.1"
4
+
5
+ subject "core" do
6
+ hosts "www.google.com"
7
+ end
8
+
9
+ subject "search" do
10
+ ip "2.2.2.2"
11
+ hosts "encrypted.google.com"
12
+ end
13
+
14
+ end
@@ -0,0 +1,7 @@
1
+ # google
2
+
3
+ # google.core
4
+ 1.1.1.1 www.google.com
5
+
6
+ # google.search
7
+ 2.2.2.2 encrypted.google.com
@@ -0,0 +1,7 @@
1
+ require 'rspec'
2
+ Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
3
+
4
+ require "hosum"
5
+
6
+ RSpec.configure do |c|
7
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hosum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1a
5
+ prerelease: 5
6
+ platform: ruby
7
+ authors:
8
+ - Sunteya
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-21 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70341398285800 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70341398285800
25
+ - !ruby/object:Gem::Dependency
26
+ name: i18n
27
+ requirement: &70341398285320 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70341398285320
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70341398284780 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70341398284780
47
+ - !ruby/object:Gem::Dependency
48
+ name: guard-rspec
49
+ requirement: &70341398284060 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70341398284060
58
+ - !ruby/object:Gem::Dependency
59
+ name: rake
60
+ requirement: &70341398274020 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70341398274020
69
+ description: host file generator
70
+ email:
71
+ - Sunteya@gmail.com
72
+ executables:
73
+ - hosum
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - Guardfile
80
+ - README
81
+ - Rakefile
82
+ - VERSION
83
+ - bin/hosum
84
+ - examples/my.hosum
85
+ - hosum.gemspec
86
+ - lib/hosum.rb
87
+ - lib/hosum/.rb
88
+ - lib/hosum/container.rb
89
+ - lib/hosum/dsl.rb
90
+ - lib/hosum/group.rb
91
+ - lib/hosum/hosts.rb
92
+ - lib/hosum/ips.rb
93
+ - lib/hosum/subject.rb
94
+ - spec/lib/dnsmasq.dnsmasq
95
+ - spec/lib/dnsmasq.dsl
96
+ - spec/lib/hosum/hosts_spec.rb
97
+ - spec/lib/hosum_spec.rb
98
+ - spec/lib/simple.dsl
99
+ - spec/lib/simple.hosts
100
+ - spec/lib/subject.dsl
101
+ - spec/lib/subject.hosts
102
+ - spec/spec_helper.rb
103
+ homepage: ''
104
+ licenses: []
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ segments:
116
+ - 0
117
+ hash: -3945610133677893382
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>'
122
+ - !ruby/object:Gem::Version
123
+ version: 1.3.1
124
+ requirements: []
125
+ rubyforge_project: hosum
126
+ rubygems_version: 1.8.7
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: host file generator
130
+ test_files:
131
+ - spec/lib/dnsmasq.dnsmasq
132
+ - spec/lib/dnsmasq.dsl
133
+ - spec/lib/hosum/hosts_spec.rb
134
+ - spec/lib/hosum_spec.rb
135
+ - spec/lib/simple.dsl
136
+ - spec/lib/simple.hosts
137
+ - spec/lib/subject.dsl
138
+ - spec/lib/subject.hosts
139
+ - spec/spec_helper.rb