hosts 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/.gitignore +12 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/.yardopts +5 -0
- data/Gemfile +23 -0
- data/HISTORY.md +6 -0
- data/LICENSE.md +15 -0
- data/README.md +347 -0
- data/Rakefile +48 -0
- data/hosts.gemspec +57 -0
- data/lib/aef/hosts.rb +51 -0
- data/lib/aef/hosts/comment.rb +73 -0
- data/lib/aef/hosts/element.rb +108 -0
- data/lib/aef/hosts/empty_element.rb +50 -0
- data/lib/aef/hosts/entry.rb +123 -0
- data/lib/aef/hosts/file.rb +252 -0
- data/lib/aef/hosts/helpers.rb +121 -0
- data/lib/aef/hosts/section.rb +141 -0
- data/lib/aef/hosts/version.rb +29 -0
- data/lib/hosts.rb +25 -0
- data/lib/hosts/bare.rb +23 -0
- data/spec/aef/hosts/comment_spec.rb +136 -0
- data/spec/aef/hosts/element_spec.rb +12 -0
- data/spec/aef/hosts/empty_element_spec.rb +96 -0
- data/spec/aef/hosts/entry_spec.rb +252 -0
- data/spec/aef/hosts/file_spec.rb +290 -0
- data/spec/aef/hosts/helpers_spec.rb +103 -0
- data/spec/aef/hosts/section_spec.rb +299 -0
- data/spec/fixtures/linux_hosts +21 -0
- data/spec/fixtures/windows_hosts +14 -0
- data/spec/integration/integration_spec.rb +384 -0
- data/spec/spec_helper.rb +47 -0
- metadata +202 -0
- metadata.gz.sig +4 -0
data.tar.gz.sig
ADDED
Binary file
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
=begin
|
3
|
+
Copyright Alexander E. Fischer <aef@raxys.net>, 2012
|
4
|
+
|
5
|
+
This file is part of Hosts.
|
6
|
+
|
7
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
8
|
+
purpose with or without fee is hereby granted, provided that the above
|
9
|
+
copyright notice and this permission notice appear in all copies.
|
10
|
+
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
12
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
13
|
+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
14
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
15
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
16
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
17
|
+
PERFORMANCE OF THIS SOFTWARE.
|
18
|
+
=end
|
19
|
+
|
20
|
+
source 'http://rubygems.org'
|
21
|
+
|
22
|
+
# Specify your gem's dependencies in weekling.gemspec
|
23
|
+
gemspec
|
data/HISTORY.md
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Copyright Alexander E. Fischer <aef@raxys.net>, 2012
|
2
|
+
|
3
|
+
Hosts is licensed under the following ISC-style license:
|
4
|
+
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
10
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
11
|
+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
12
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
13
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
14
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
15
|
+
PERFORMANCE OF THIS SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,347 @@
|
|
1
|
+
Hosts
|
2
|
+
=====
|
3
|
+
|
4
|
+
[![Build Status](https://secure.travis-ci.org/aef/hosts.png)](
|
5
|
+
https://secure.travis-ci.org/aef/hosts)
|
6
|
+
|
7
|
+
* [Documentation][docs]
|
8
|
+
* [Project][project]
|
9
|
+
|
10
|
+
[docs]: http://rdoc.info/github/aef/hosts/
|
11
|
+
[project]: https://github.com/aef/hosts/
|
12
|
+
|
13
|
+
Description
|
14
|
+
-----------
|
15
|
+
|
16
|
+
Hosts is a Ruby library able to read or manipulate the operating system's host
|
17
|
+
files. When manipulating it tries to preserve their original formatting.
|
18
|
+
|
19
|
+
Features / Problems
|
20
|
+
-------------------
|
21
|
+
|
22
|
+
This project tries to conform to:
|
23
|
+
|
24
|
+
* [Semantic Versioning (2.0.0-rc.1)][semver]
|
25
|
+
* [Ruby Packaging Standard (0.5-draft)][rps]
|
26
|
+
* [Ruby Style Guide][style]
|
27
|
+
* [Gem Packaging: Best Practices][gem]
|
28
|
+
|
29
|
+
[semver]: http://semver.org/
|
30
|
+
[rps]: http://chneukirchen.github.com/rps/
|
31
|
+
[style]: https://github.com/bbatsov/ruby-style-guide
|
32
|
+
[gem]: http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-practices
|
33
|
+
|
34
|
+
Additional facts:
|
35
|
+
|
36
|
+
* Written purely in Ruby.
|
37
|
+
* Documented with YARD.
|
38
|
+
* Intended to be used with Ruby 1.8.7 or higher.
|
39
|
+
* Cryptographically signed gem and git tags.
|
40
|
+
|
41
|
+
Synopsis
|
42
|
+
--------
|
43
|
+
|
44
|
+
This documentation defines the public interface of the software. Don't rely
|
45
|
+
on elements marked as private. Those should be hidden in the documentation
|
46
|
+
by default.
|
47
|
+
|
48
|
+
This is still experimental software, even the public interface may change
|
49
|
+
substantially in future releases.
|
50
|
+
|
51
|
+
### Loading
|
52
|
+
|
53
|
+
In most cases you want to load the library by the following command:
|
54
|
+
|
55
|
+
~~~~~ ruby
|
56
|
+
require 'hosts'
|
57
|
+
~~~~~
|
58
|
+
|
59
|
+
In a bundler Gemfile you should use the following:
|
60
|
+
|
61
|
+
~~~~~ ruby
|
62
|
+
gem 'hosts'
|
63
|
+
~~~~~
|
64
|
+
|
65
|
+
### Reading a hosts file
|
66
|
+
|
67
|
+
You can either read a hosts file from the file system:
|
68
|
+
|
69
|
+
~~~~~ ruby
|
70
|
+
hosts = Hosts::File.read('/etc/hosts')
|
71
|
+
~~~~~
|
72
|
+
|
73
|
+
Or you can parse a String containing the content of a hosts file:
|
74
|
+
|
75
|
+
~~~~~ ruby
|
76
|
+
hosts = Hosts::File.parse(hosts_content)
|
77
|
+
~~~~~
|
78
|
+
|
79
|
+
### Elements
|
80
|
+
|
81
|
+
Afterwards the hosts file's elements are accessible through the elements attribute:
|
82
|
+
|
83
|
+
~~~~~ ruby
|
84
|
+
hosts.elements
|
85
|
+
# => [ #<Aef::Hosts::Entry: address="127.0.0.1" name="localhost" aliases=[] comment=nil cached!>,
|
86
|
+
# #<Aef::Hosts::EmptyElement: cached!>,
|
87
|
+
# #<Aef::Hosts::Entry: address="192.168.1.1" name="myhost" aliases=["myhost.mydomain"] comment=" Some comment" cached!>
|
88
|
+
# ]
|
89
|
+
~~~~~
|
90
|
+
|
91
|
+
Elements can simply be appended to the existing elements array:
|
92
|
+
|
93
|
+
~~~~~ ruby
|
94
|
+
hosts.elements << new_element
|
95
|
+
hosts.elements += new_elements
|
96
|
+
~~~~~
|
97
|
+
|
98
|
+
Or you can insert elements at specific positions:
|
99
|
+
|
100
|
+
~~~~~ ruby
|
101
|
+
hosts.elements.insert(0, new_element)
|
102
|
+
~~~~~
|
103
|
+
|
104
|
+
There are four different types of elements for a hosts file:
|
105
|
+
|
106
|
+
#### Entry
|
107
|
+
|
108
|
+
The Entry is the most common element of a hosts file. A single entry has
|
109
|
+
an address, a name and optionally an unlimited amount of alias names and a
|
110
|
+
comment.
|
111
|
+
|
112
|
+
~~~~~ ruby
|
113
|
+
Hosts::Entry.new('10.23.5.1', 'otherhost',
|
114
|
+
:aliases => ['otherhost.mydomain'],
|
115
|
+
:comment => ' A new host')
|
116
|
+
~~~~~
|
117
|
+
|
118
|
+
#### Comment
|
119
|
+
|
120
|
+
A Comment represents a line containing only a comment.
|
121
|
+
|
122
|
+
~~~~~ ruby
|
123
|
+
Hosts::Comment.new(' Nothing special')
|
124
|
+
~~~~~
|
125
|
+
|
126
|
+
#### Section
|
127
|
+
|
128
|
+
A section has a name and optionally an unlimited amount of inner elements. In
|
129
|
+
String representation a section is enclosed by easily distinguishable header
|
130
|
+
and footer
|
131
|
+
|
132
|
+
# ----- BEGIN SECTION somename -----
|
133
|
+
# Elements here
|
134
|
+
# ----- END SECTION somename -----
|
135
|
+
|
136
|
+
A section is created by the following:
|
137
|
+
|
138
|
+
~~~~~ ruby
|
139
|
+
elements
|
140
|
+
# => [#<Aef::Hosts::Comment comment=" Elements here">]
|
141
|
+
|
142
|
+
section = Hosts::Section.new('somename', :elements => elements)
|
143
|
+
~~~~~
|
144
|
+
|
145
|
+
On an existing Section, elements can be modified in the same way as on a File:
|
146
|
+
|
147
|
+
~~~~~ ruby
|
148
|
+
section.elements
|
149
|
+
# => [#<Aef::Hosts::Comment comment=" Elements here">]
|
150
|
+
~~~~~
|
151
|
+
|
152
|
+
#### Empty element
|
153
|
+
|
154
|
+
Also, to represent completly empty lines without abandoning their whitespace
|
155
|
+
contents there is an EmptyElement.
|
156
|
+
|
157
|
+
~~~~~ ruby
|
158
|
+
Hosts::EmptyElement.new
|
159
|
+
~~~~~
|
160
|
+
|
161
|
+
#### Cache
|
162
|
+
|
163
|
+
When creating an element you can also specify it's String cache. This is done
|
164
|
+
automatically when reading in an existing hosts file. Should you for any reason
|
165
|
+
want to do this manually, do it like the following:
|
166
|
+
|
167
|
+
~~~~~ ruby
|
168
|
+
Hosts::EmptyElement.new(:cache => " \t ")
|
169
|
+
~~~~~
|
170
|
+
|
171
|
+
Note that the semantics for cache of a Section differ. See the class
|
172
|
+
documentation for this.
|
173
|
+
|
174
|
+
### Generating a String representation
|
175
|
+
|
176
|
+
To render the hosts file back into a String simply call the #to_s method:
|
177
|
+
|
178
|
+
~~~~~ ruby
|
179
|
+
hosts.to_s
|
180
|
+
# => " 127.0.0.1\tlocalhost\n \n 192.168.1.1\tmyhost\tmyhost.mydomain\t# Some comment\n"
|
181
|
+
~~~~~
|
182
|
+
|
183
|
+
If you have read the hosts file from a file system path you can simply save it
|
184
|
+
back to this path:
|
185
|
+
|
186
|
+
~~~~~ ruby
|
187
|
+
hosts.write
|
188
|
+
~~~~~
|
189
|
+
|
190
|
+
Otherwise, if there is no known path for the file already you can specify one
|
191
|
+
for each write operation:
|
192
|
+
|
193
|
+
~~~~~ ruby
|
194
|
+
hosts.write(:path => '/tmp/hosts')
|
195
|
+
~~~~~
|
196
|
+
|
197
|
+
Or set the path for all future write operations by setting the path attribute:
|
198
|
+
|
199
|
+
~~~~~ ruby
|
200
|
+
hosts.path = '/tmp/hosts'
|
201
|
+
|
202
|
+
hosts.write
|
203
|
+
~~~~~
|
204
|
+
|
205
|
+
### String cache
|
206
|
+
|
207
|
+
Normally, if a cached String representation of an element is available, it will
|
208
|
+
be used instead of rendering a new one to preserve the overall layout of the
|
209
|
+
hosts file. If you which to generate the whole file from scratch, simply supply
|
210
|
+
the :force_generation option:
|
211
|
+
|
212
|
+
~~~~~ ruby
|
213
|
+
hosts.to_s(:force_generation => true)
|
214
|
+
# => "127.0.0.1 localhost\n\n192.168.1.1 myhost myhost.mydomain # Some comment\n"
|
215
|
+
~~~~~
|
216
|
+
|
217
|
+
Instead of temporarily ignoring the cached String representation you could also
|
218
|
+
invalidate the cache completely:
|
219
|
+
|
220
|
+
~~~~~ ruby
|
221
|
+
hosts.invalidate_cache!
|
222
|
+
~~~~~
|
223
|
+
|
224
|
+
This can also be done on single elements of the hosts file:
|
225
|
+
|
226
|
+
~~~~~ ruby
|
227
|
+
hosts.elements[1].invalidate_cache!
|
228
|
+
~~~~~
|
229
|
+
|
230
|
+
If you change attributes of an element, the cache will be cleaned
|
231
|
+
automatically:
|
232
|
+
|
233
|
+
~~~~~ ruby
|
234
|
+
hosts.elements[0].address = '127.0.1.1'
|
235
|
+
~~~~~
|
236
|
+
|
237
|
+
Requirements
|
238
|
+
------------
|
239
|
+
|
240
|
+
* Ruby 1.8.7 or higher
|
241
|
+
|
242
|
+
Installation
|
243
|
+
------------
|
244
|
+
|
245
|
+
On *nix systems you may need to prefix the command with sudo to get root
|
246
|
+
privileges.
|
247
|
+
|
248
|
+
### High security (recommended)
|
249
|
+
|
250
|
+
There is a high security installation option available through rubygems. It is
|
251
|
+
highly recommended over the normal installation, although it may be a bit less
|
252
|
+
comfortable. To use the installation method, you will need my [gem signing
|
253
|
+
public key][gemkey], which I use for cryptographic signatures on all my gems.
|
254
|
+
|
255
|
+
Add the key to your rubygems' trusted certificates by the following command:
|
256
|
+
|
257
|
+
gem cert --add aef-gem.pem
|
258
|
+
|
259
|
+
Now you can install the gem while automatically verifying it's signature by the
|
260
|
+
following command:
|
261
|
+
|
262
|
+
gem install hosts -P HighSecurity
|
263
|
+
|
264
|
+
Please notice that you may need other keys for dependent libraries, so you may
|
265
|
+
have to install dependencies manually.
|
266
|
+
|
267
|
+
[gemkey]: http://aef.name/crypto/aef-gem.pem
|
268
|
+
|
269
|
+
### Normal
|
270
|
+
|
271
|
+
gem install hosts
|
272
|
+
|
273
|
+
### Automated testing
|
274
|
+
|
275
|
+
Go into the root directory of the installed gem and run the following command
|
276
|
+
to fetch all development dependencies:
|
277
|
+
|
278
|
+
bundle
|
279
|
+
|
280
|
+
Afterwards start the test runner:
|
281
|
+
|
282
|
+
rake spec
|
283
|
+
|
284
|
+
If something goes wrong you should be noticed through failing examples.
|
285
|
+
|
286
|
+
Development
|
287
|
+
-----------
|
288
|
+
|
289
|
+
### Bugs Reports and Feature Requests
|
290
|
+
|
291
|
+
Please use the [issue tracker][issues] on github.com to let me know about errors
|
292
|
+
or ideas for improvement of this software.
|
293
|
+
|
294
|
+
[issues]: https://github.com/aef/hosts/issues/
|
295
|
+
|
296
|
+
### Source code
|
297
|
+
|
298
|
+
This software is developed in the source code management system git hosted
|
299
|
+
at github.com. You can download the most recent sourcecode through the
|
300
|
+
following command:
|
301
|
+
|
302
|
+
git clone https://github.com/aef/hosts.git
|
303
|
+
|
304
|
+
The final commit before each released gem version will be marked by a tag
|
305
|
+
named like the version with a prefixed lower-case "v", as required by Semantic
|
306
|
+
Versioning. Every tag will be signed by my [OpenPGP public key][openpgp] which
|
307
|
+
enables you to verify your copy of the code cryptographically.
|
308
|
+
|
309
|
+
[openpgp]: http://aef.name/crypto/aef-openpgp.asc
|
310
|
+
|
311
|
+
Add the key to your GnuPG keyring by the following command:
|
312
|
+
|
313
|
+
gpg --import aef-openpgp.asc
|
314
|
+
|
315
|
+
This command will tell you if your code is of integrity and authentic:
|
316
|
+
|
317
|
+
git tag -v [TAG NAME]
|
318
|
+
|
319
|
+
### Contribution
|
320
|
+
|
321
|
+
Help on making this software better is always very appreciated. If you want
|
322
|
+
your changes to be included in the official release, please clone my project
|
323
|
+
on github.com, create a named branch to commit and push your changes into and
|
324
|
+
send me a pull request afterwards.
|
325
|
+
|
326
|
+
Please make sure to write tests for your changes so that I won't break them
|
327
|
+
when changing other things on the library. Also notice that I can't promise
|
328
|
+
to include your changes before reviewing them.
|
329
|
+
|
330
|
+
License
|
331
|
+
-------
|
332
|
+
|
333
|
+
Copyright Alexander E. Fischer <aef@raxys.net>, 2012
|
334
|
+
|
335
|
+
This file is part of Hosts.
|
336
|
+
|
337
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
338
|
+
purpose with or without fee is hereby granted, provided that the above
|
339
|
+
copyright notice and this permission notice appear in all copies.
|
340
|
+
|
341
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
342
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
343
|
+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
344
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
345
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
346
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
347
|
+
PERFORMANCE OF THIS SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
=begin
|
3
|
+
Copyright Alexander E. Fischer <aef@raxys.net>, 2012
|
4
|
+
|
5
|
+
This file is part of Hosts.
|
6
|
+
|
7
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
8
|
+
purpose with or without fee is hereby granted, provided that the above
|
9
|
+
copyright notice and this permission notice appear in all copies.
|
10
|
+
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
12
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
13
|
+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
14
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
15
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
16
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
17
|
+
PERFORMANCE OF THIS SOFTWARE.
|
18
|
+
=end
|
19
|
+
|
20
|
+
require 'bundler/gem_tasks'
|
21
|
+
require 'rake'
|
22
|
+
require 'pathname'
|
23
|
+
require 'yard'
|
24
|
+
require 'rspec/core/rake_task'
|
25
|
+
|
26
|
+
RSpec::Core::RakeTask.new
|
27
|
+
|
28
|
+
YARD::Rake::YardocTask.new('doc')
|
29
|
+
|
30
|
+
desc "Removes temporary project files"
|
31
|
+
task :clean do
|
32
|
+
%w{doc coverage pkg .yardoc .rbx Gemfile.lock}.map{|name| Pathname.new(name) }.each do |path|
|
33
|
+
path.rmtree if path.exist?
|
34
|
+
end
|
35
|
+
|
36
|
+
Pathname.glob('*.gem').each &:delete
|
37
|
+
Pathname.glob('**/*.rbc').each &:delete
|
38
|
+
end
|
39
|
+
|
40
|
+
desc "Opens an interactive console with the library loaded"
|
41
|
+
task :console do
|
42
|
+
Bundler.setup
|
43
|
+
require 'pry'
|
44
|
+
require 'hosts'
|
45
|
+
Pry.start
|
46
|
+
end
|
47
|
+
|
48
|
+
task :default => :spec
|