hostor 1.0.0-mswin32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/hostor +241 -0
- data/bin/hostor.png +0 -0
- data/bin/hostor_gui +111 -0
- data/bin/hostor_gui.bat +2 -0
- metadata +67 -0
data/bin/hostor
ADDED
@@ -0,0 +1,241 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'optparse'
|
4
|
+
require 'etc'
|
5
|
+
require 'ftools'
|
6
|
+
require 'tempfile'
|
7
|
+
require 'rbconfig'
|
8
|
+
|
9
|
+
class File
|
10
|
+
def self.prepend(path, string)
|
11
|
+
Tempfile.open File.basename(path) do |tempfile|
|
12
|
+
# prepend data to tempfile
|
13
|
+
tempfile << string
|
14
|
+
|
15
|
+
File.open(path, 'r+') do |file|
|
16
|
+
# append original data to tempfile
|
17
|
+
tempfile << file.read
|
18
|
+
# reset file positions
|
19
|
+
file.pos = tempfile.pos = 0
|
20
|
+
# copy all data back to original file
|
21
|
+
file << tempfile.read
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
module Hostor
|
29
|
+
def os
|
30
|
+
host_os = RbConfig::CONFIG['host_os']
|
31
|
+
case host_os
|
32
|
+
when /mswin|msys|mingw32/
|
33
|
+
:windows
|
34
|
+
when /darwin|mac os/
|
35
|
+
:macosx
|
36
|
+
when /linux/
|
37
|
+
:linux
|
38
|
+
when /solaris|bsd/
|
39
|
+
:unix
|
40
|
+
else
|
41
|
+
raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def is_windows
|
46
|
+
os.eql? :windows
|
47
|
+
end
|
48
|
+
|
49
|
+
def is_mac
|
50
|
+
os.eql? :macosx
|
51
|
+
end
|
52
|
+
|
53
|
+
def is_linux
|
54
|
+
os.eql? :linux
|
55
|
+
end
|
56
|
+
|
57
|
+
#todo: 根据不同平台获取当前目录
|
58
|
+
def hostor_dir
|
59
|
+
user = Etc.getlogin
|
60
|
+
is_windows ? "#{ENV['USERPROFILE']}\\hostor" : "#{ENV['HOME']}/.hostor"
|
61
|
+
end
|
62
|
+
|
63
|
+
def default_host
|
64
|
+
"#{hostor_dir}/default.host"
|
65
|
+
end
|
66
|
+
|
67
|
+
def sys_host_file
|
68
|
+
is_windows ? "#{ENV['SystemDrive']}/windows/system32/drivers/etc/hosts" : '/etc/hosts'
|
69
|
+
end
|
70
|
+
|
71
|
+
def get_host_name(path)
|
72
|
+
unknown = "未定名称的HOST文件"
|
73
|
+
ln = nil
|
74
|
+
File.open(path, 'r').each_line do |line|
|
75
|
+
ln = line
|
76
|
+
break
|
77
|
+
end
|
78
|
+
return unknown if ln.nil?
|
79
|
+
|
80
|
+
if (ln =~ /#==\[.*?\]==/).eql? 0
|
81
|
+
m = ln.match /#==\[(.*)?\.host\]==\B/
|
82
|
+
m[1] || unknown
|
83
|
+
else
|
84
|
+
unknown
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def init
|
89
|
+
if File.exists?(hostor_dir)
|
90
|
+
puts "已经初始化过了!"
|
91
|
+
else
|
92
|
+
Dir.mkdir(hostor_dir)
|
93
|
+
puts "创建hostor配置目录#{hostor_dir}"
|
94
|
+
|
95
|
+
File.copy(sys_host_file, default_host)
|
96
|
+
puts "复制#{sys_host_file}到#{default_host}"
|
97
|
+
|
98
|
+
File.prepend(default_host, "#==[default.host]==\n")
|
99
|
+
|
100
|
+
puts "初始化完成"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def get_path_by_name(name)
|
105
|
+
p1 = "#{hostor_dir}/#{name}"
|
106
|
+
return p1 if File.exists? p1
|
107
|
+
|
108
|
+
p2 = "#{hostor_dir}/#{name}.host"
|
109
|
+
return p2 if File.exists? p2
|
110
|
+
|
111
|
+
raise "名为#{name}.host的文件不存在"
|
112
|
+
end
|
113
|
+
|
114
|
+
def switch(file)
|
115
|
+
File.copy(get_path_by_name(file), sys_host_file)
|
116
|
+
end
|
117
|
+
|
118
|
+
def current
|
119
|
+
get_host_name(sys_host_file)
|
120
|
+
end
|
121
|
+
|
122
|
+
def view(file)
|
123
|
+
dest = file ? get_path_by_name(file) : sys_host_file
|
124
|
+
File.open(dest, 'r').each_line{|ln| puts ln }
|
125
|
+
end
|
126
|
+
|
127
|
+
def remove(file)
|
128
|
+
File.delete(get_path_by_name(file))
|
129
|
+
end
|
130
|
+
|
131
|
+
def delete(keyword)
|
132
|
+
end
|
133
|
+
|
134
|
+
def list
|
135
|
+
files = []
|
136
|
+
Dir.foreach(hostor_dir) do |file|
|
137
|
+
if not File.directory? file and File.extname(file).eql? '.host'
|
138
|
+
files << file
|
139
|
+
end
|
140
|
+
end
|
141
|
+
files
|
142
|
+
end
|
143
|
+
|
144
|
+
def add(file)
|
145
|
+
if File.exists? file
|
146
|
+
file_bak = file.to_s + "" #for_win
|
147
|
+
basename = File.basename(file)
|
148
|
+
ext = File.extname(file)
|
149
|
+
if ext.empty?
|
150
|
+
basename += '.host'
|
151
|
+
elsif not ext.eql? '.host'
|
152
|
+
basename.gsub!(/\.[^.]*?$/, '.host')
|
153
|
+
end
|
154
|
+
dest = hostor_dir + "/" + basename
|
155
|
+
if File.exists? dest
|
156
|
+
raise "已经添加了名为#{basename}文件,请重命名后添加或者使用#$0 --remove #{basename} 删除已有文件"
|
157
|
+
else
|
158
|
+
File.copy(file_bak, dest)
|
159
|
+
File.prepend(dest, "#==[#{basename}]==\n")
|
160
|
+
end
|
161
|
+
else
|
162
|
+
raise "文件#{file}不存在"
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def main(args)
|
167
|
+
opts = OptionParser.new do |opts|
|
168
|
+
opts.banner = <<-EOF
|
169
|
+
Usage:
|
170
|
+
#$0 查看系统当前host文件
|
171
|
+
#$0 <hostname> 设定指定的文件为系统host文件
|
172
|
+
EOF
|
173
|
+
|
174
|
+
opts.on('--init', "初始化hostor") do
|
175
|
+
init()
|
176
|
+
end
|
177
|
+
|
178
|
+
opts.on('-a', '--add <file>', "将指定文件加入hostor管理") do |file|
|
179
|
+
add(file)
|
180
|
+
end
|
181
|
+
|
182
|
+
opts.on('-e', '--edit [file]', "编辑指定的HOST文件") do |file|
|
183
|
+
f = file || current
|
184
|
+
dest = get_path_by_name(f)
|
185
|
+
if is_mac
|
186
|
+
`open -a macvim #{dest}`
|
187
|
+
elsif is_linux
|
188
|
+
`xterm -e vim #{dest}`
|
189
|
+
elsif is_windows
|
190
|
+
`notepad #{dest}`
|
191
|
+
end
|
192
|
+
if f.gsub(/\..*/, '') == current
|
193
|
+
switch( f )
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
opts.on('-r', '--remove <file>', "从hostor文件夹移除指定的HOST文件") do |file|
|
198
|
+
remove(file)
|
199
|
+
end
|
200
|
+
|
201
|
+
#适用于 -a -d
|
202
|
+
#opts.on('-t', '--target <file>', "指定操作的HOST文件") do |name|
|
203
|
+
#end
|
204
|
+
|
205
|
+
opts.on('-l', '--list', "列举当前用户的所有HOST文件") do
|
206
|
+
list().each{|l| puts l}
|
207
|
+
end
|
208
|
+
|
209
|
+
opts.on('-v', '--view [file]', "查看指定的HOST文件") do |name|
|
210
|
+
view(name)
|
211
|
+
end
|
212
|
+
|
213
|
+
opts.on('-c', '--curr', "列举当前使用HOST文件") do
|
214
|
+
puts current()
|
215
|
+
end
|
216
|
+
|
217
|
+
opts.on('-s', '--switch <file>', "将指定HOST文件设为系统当前HOST文件") do |file|
|
218
|
+
switch(file)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
begin
|
223
|
+
if args.length.eql? 0
|
224
|
+
view(nil)
|
225
|
+
elsif args.length.eql? 1 and args[0] =~ /^[^-]/
|
226
|
+
switch(args[0])
|
227
|
+
else
|
228
|
+
opts.parse!(args)
|
229
|
+
end
|
230
|
+
rescue OptionParser::InvalidOption => e
|
231
|
+
puts "参数非法, 查看帮助请使用: #$0 --help "
|
232
|
+
rescue Exception => e
|
233
|
+
puts e.to_s
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
include Hostor
|
239
|
+
main ARGV
|
240
|
+
|
241
|
+
|
data/bin/hostor.png
ADDED
Binary file
|
data/bin/hostor_gui
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
#!/usr/bin/env jruby
|
2
|
+
require 'java'
|
3
|
+
module Hostor
|
4
|
+
module SysPopupMenu
|
5
|
+
def self.run
|
6
|
+
if (Java::JavaAwt::SystemTray.isSupported())
|
7
|
+
tray = Java::JavaAwt::SystemTray.getSystemTray();
|
8
|
+
image = Java::JavaAwt::Toolkit.getDefaultToolkit().getImage(File.expand_path(File.dirname(__FILE__))+"/hostor.png")
|
9
|
+
|
10
|
+
popup = Java::JavaAwt::PopupMenu.new
|
11
|
+
self.refresh(popup)
|
12
|
+
|
13
|
+
icon = Java::JavaAwt::TrayIcon.new(image, "hostor", popup)
|
14
|
+
icon.setImageAutoSize true
|
15
|
+
|
16
|
+
begin
|
17
|
+
tray.add(icon)
|
18
|
+
rescue Java::JavaAwt::AWTException => e
|
19
|
+
java::lang::System.err.println("TrayIcon could not be added.")
|
20
|
+
end
|
21
|
+
else
|
22
|
+
java::lang::System.err.println("System tray is currently not supported.")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.refresh(popup)
|
27
|
+
popup.removeAll()
|
28
|
+
|
29
|
+
curr = get_curr_host()
|
30
|
+
get_host_list.each do |name|
|
31
|
+
popup.add(make_host_item(name, curr))
|
32
|
+
end
|
33
|
+
|
34
|
+
popup.add(Java::JavaAwt::MenuItem.new("--------"))
|
35
|
+
popup.add(make_refresh_item())
|
36
|
+
popup.add(make_exit_item())
|
37
|
+
end
|
38
|
+
|
39
|
+
class << self
|
40
|
+
private
|
41
|
+
def get_host_list()
|
42
|
+
return (`hostor -l`).split("\n").collect {|x| x.sub /.host/, ''}
|
43
|
+
end
|
44
|
+
|
45
|
+
def get_curr_host()
|
46
|
+
return (`hostor -c`).delete("\n")
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_prefix(name, curr)
|
50
|
+
name.eql?(curr) ? "*" : " "
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_host_item_label(name, curr)
|
54
|
+
get_prefix(name, curr) + name
|
55
|
+
end
|
56
|
+
|
57
|
+
def make_host_item(name, curr)
|
58
|
+
item = Java::JavaAwt::MenuItem.new(get_host_item_label(name, curr))
|
59
|
+
item.setActionCommand(name)
|
60
|
+
listener = java::awt::event::ActionListener.new
|
61
|
+
def listener.actionPerformed(e)
|
62
|
+
item = e.getSource()
|
63
|
+
curr_host = item.getActionCommand()
|
64
|
+
|
65
|
+
old_host = (`hostor -c`).delete("\n")
|
66
|
+
system("hostor #{curr_host}")
|
67
|
+
|
68
|
+
if curr_host != old_host
|
69
|
+
item.setLabel("*"+curr_host)
|
70
|
+
popup = item.getParent()
|
71
|
+
|
72
|
+
n = popup.getItemCount()
|
73
|
+
n.times do |i|
|
74
|
+
o = popup.getItem(i)
|
75
|
+
name = o.getActionCommand()
|
76
|
+
if(name == old_host)
|
77
|
+
o.setLabel(' '+ name)
|
78
|
+
break
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
item.addActionListener(listener)
|
85
|
+
return item
|
86
|
+
end
|
87
|
+
|
88
|
+
def make_exit_item()
|
89
|
+
item = Java::JavaAwt::MenuItem.new("exit")
|
90
|
+
listener = java::awt::event::ActionListener.new
|
91
|
+
def listener.actionPerformed(e)
|
92
|
+
java::lang::System.exit(0)
|
93
|
+
end
|
94
|
+
item.addActionListener(listener)
|
95
|
+
return item
|
96
|
+
end
|
97
|
+
|
98
|
+
def make_refresh_item()
|
99
|
+
item = Java::JavaAwt::MenuItem.new("reload")
|
100
|
+
listener = java::awt::event::ActionListener.new
|
101
|
+
def listener.actionPerformed(e)
|
102
|
+
Hostor::SysPopupMenu.refresh(e.getSource().getParent())
|
103
|
+
end
|
104
|
+
item.addActionListener(listener)
|
105
|
+
return item
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
Hostor::SysPopupMenu.run
|
data/bin/hostor_gui.bat
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hostor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: mswin32
|
11
|
+
authors:
|
12
|
+
- Zhong Xingdou
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-24 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: " system host file manager\n"
|
22
|
+
email: zhongxingdou@gmail.com
|
23
|
+
executables:
|
24
|
+
- hostor
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- bin/hostor_gui.bat
|
31
|
+
- bin/hostor
|
32
|
+
- bin/hostor_gui
|
33
|
+
- bin/hostor.png
|
34
|
+
has_rdoc: true
|
35
|
+
homepage:
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.7
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: system host file manager
|
66
|
+
test_files: []
|
67
|
+
|