sys-host 0.5.2-mswin32 → 0.6.0-mswin32

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,13 @@
1
+ == 0.6.0 - 8-Jun-2007
2
+ * Now supports OS X!
3
+ * Added the Host.host_id class method.
4
+ * Bug/warning fix for OS X.
5
+ * HostError is now Host::Error.
6
+ * Added a Rakefile, including tasks for installation and testing.
7
+ * Fixed the gemspec (I hope).
8
+ * Lots of internal reorganization, including the removal of the install.rb
9
+ file. That's handled by the Rakefile.
10
+
1
11
  == 0.5.2 - 27-Jun-2006
2
12
  * Added the Host.info method for Linux.
3
13
 
data/MANIFEST CHANGED
@@ -1,19 +1,15 @@
1
- CHANGES
2
- MANIFEST
3
- INSTALL
4
- extconf.rb
5
- install.rb
6
-
7
- doc/host.txt
8
-
9
- doc/examples/test.rb
10
-
11
- ext/constants.h
12
- ext/freebsd.c
13
- ext/generic.c
14
- ext/linux.c
15
- ext/sunos.c
16
-
17
- lib/sys/windows.rb
18
-
19
- test/tc_host.rb
1
+ * CHANGES
2
+ * MANIFEST
3
+ * README
4
+ * Rakefile
5
+ * sys-host.gemspec
6
+ * doc/host.txt
7
+ * examples/host_test.rb
8
+ * ext/constants.h
9
+ * ext/extconf.rb
10
+ * ext/bsd/bsd.c
11
+ * ext/generic/generic.c
12
+ * ext/linux/linux.c
13
+ * ext/sunos/sunos.c
14
+ * lib/sys/windows.rb
15
+ * test/tc_host.rb
data/README CHANGED
@@ -1,14 +1,26 @@
1
1
  = Prerequisites
2
- Ruby 1.8.0 or later.
3
- Ruby 1.8.2 or later preferred on Windows.
2
+ Ruby 1.8.0 or later.
3
+ Ruby 1.8.2 or later preferred on Windows.
4
+
5
+ = Synopsis
6
+ require 'sys/host'
7
+ include Sys
8
+
9
+ p Host.hostname
10
+ p Host.ip_addr
11
+
12
+ Host.info{ |h|
13
+ p h
14
+ }
4
15
 
5
16
  = Installation
6
- == Windows
7
- ruby test\tc_host.rb (optional)
8
- ruby install.rb
9
-
10
- == Unix
11
- ruby extconf.rb
12
- make
13
- ruby test/tc_host.rb (optional)
14
- make install
17
+ == Standard (non-gem) installation
18
+ rake test (optional)
19
+ rake install
20
+
21
+ == Gem Installation
22
+ rake test (optional)
23
+ rake install_gem
24
+
25
+ = Documentation
26
+ See the doc/host.txt file for more information.
@@ -0,0 +1,79 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+ require 'rbconfig'
5
+ include Config
6
+
7
+ desc "Clean the build files for the sys-host source for UNIX systems"
8
+ task :clean do
9
+ Dir.chdir('ext') do
10
+ unless RUBY_PLATFORM.match('mswin')
11
+ sh 'make distclean' if File.exists?('host.o')
12
+ FileUtils.rm_rf('host.c') if File.exists?('host.c')
13
+ FileUtils.rm_rf('sys') if File.exists?('sys')
14
+ end
15
+ end
16
+ FileUtils.rm_rf('sys') if File.exists?('sys')
17
+ end
18
+
19
+ desc "Build the sys-host package on UNIX systems (but don't install it)"
20
+ task :build => [:clean] do
21
+ Dir.chdir('ext') do
22
+ ruby 'extconf.rb'
23
+ sh 'make'
24
+ build_file = 'host.' + Config::CONFIG['DLEXT']
25
+ Dir.mkdir('sys') unless File.exists?('sys')
26
+ FileUtils.cp(build_file, 'sys')
27
+ end
28
+ end
29
+
30
+ desc "Run the example program"
31
+ task :example => [:build] do
32
+ ruby 'host_test.rb'
33
+ end
34
+
35
+ if RUBY_PLATFORM.match('mswin')
36
+ desc "Install the sys-host package (non-gem)"
37
+ task :install do
38
+ install_dir = File.join(CONFIG['sitelibdir'], 'sys')
39
+ Dir.mkdir(install_dir) unless File.exists?(install_dir)
40
+ FileUtils.cp('lib/sys/windows.rb', install_dir, :verbose => true)
41
+ end
42
+ else
43
+ desc "Install the sys-host package (non-gem)"
44
+ task :install => [:build] do
45
+ Dir.chdir('ext') do
46
+ sh 'make install'
47
+ end
48
+ end
49
+ end
50
+
51
+ desc "Install the sys-host package as a gem"
52
+ task :install_gem do
53
+ ruby 'sys-host.gemspec'
54
+ file = Dir["sys-host*.gem"].last
55
+ sh "gem install #{file}"
56
+ end
57
+
58
+ desc "Run the example sys-host program"
59
+ task :example => [:build] do
60
+ Dir.mkdir('sys') unless File.exists?('sys')
61
+ if RUBY_PLATFORM.match('mswin')
62
+ FileUtils.cp('lib/sys/windows.rb', 'lib/sys/host.rb')
63
+ ruby '-Ilib examples/host_test.rb'
64
+ else
65
+ ruby '-Iext examples/host_test.rb'
66
+ end
67
+ end
68
+
69
+ Rake::TestTask.new do |t|
70
+ if RUBY_PLATFORM.match('mswin')
71
+ FileUtils.cp('lib/sys/windows.rb', 'lib/sys/host.rb')
72
+ t.libs << 'lib'
73
+ else
74
+ task :test => :build
75
+ t.libs << 'ext'
76
+ t.libs.delete('lib')
77
+ end
78
+ t.test_files = FileList['test/tc_host.rb']
79
+ end
@@ -0,0 +1,67 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+ require 'rbconfig'
5
+ include Config
6
+
7
+ desc "Clean the build files for the sys-host source for UNIX systems"
8
+ task :clean do
9
+ Dir.chdir('ext') do
10
+ unless RUBY_PLATFORM.match('mswin')
11
+ sh 'make distclean' if File.exists?('host.o')
12
+ FileUtils.rm_rf('host.c') if File.exists?('host.c')
13
+ FileUtils.rm_rf('sys') if File.exists?('sys')
14
+ end
15
+ end
16
+ FileUtils.rm_rf('sys') if File.exists?('sys')
17
+ end
18
+
19
+ desc "Build the sys-host package on UNIX systems (but don't install it)"
20
+ task :build => [:clean] do
21
+ Dir.chdir('ext') do
22
+ ruby 'extconf.rb'
23
+ sh 'make'
24
+ build_file = 'host.' + Config::CONFIG['DLEXT']
25
+ Dir.mkdir('sys') unless File.exists?('sys')
26
+ FileUtils.cp(build_file, 'sys')
27
+ end
28
+ end
29
+
30
+ desc "Run the example program"
31
+ task :example => [:build] do
32
+ ruby 'host_test.rb'
33
+ end
34
+
35
+ if RUBY_PLATFORM.match('mswin')
36
+ desc "Install the sys-host package (non-gem)"
37
+ task :install do
38
+ install_dir = File.join(CONFIG['sitelibdir'], 'sys')
39
+ Dir.mkdir(install_dir) unless File.exists?(install_dir)
40
+ FileUtils.cp('lib/sys/windows.rb', install_dir, :verbose => true)
41
+ end
42
+ else
43
+ desc "Install the sys-host package (non-gem)"
44
+ task :install => [:build] do
45
+ Dir.chdir('ext') do
46
+ sh 'make install'
47
+ end
48
+ end
49
+ end
50
+
51
+ desc "Install the sys-host package as a gem"
52
+ task :install_gem do
53
+ ruby 'sys-host.gemspec'
54
+ file = Dir["sys-host*.gem"].last
55
+ sh "gem install #{file}"
56
+ end
57
+
58
+ Rake::TestTask.new do |t|
59
+ if RUBY_PLATFORM.match('mswin')
60
+ t.libs << 'lib'
61
+ else
62
+ task :test => :build
63
+ t.libs << 'ext'
64
+ t.libs.delete('lib')
65
+ end
66
+ t.test_files = FileList['test/tc_host.rb']
67
+ end
@@ -33,7 +33,7 @@ Host.info(host=localhost){ |h| ... }
33
33
  vary depending on your platform.
34
34
 
35
35
  == Exception Classes
36
- HostError < StandardError
36
+ Host::Error < StandardError
37
37
  Raised in the event of a failure for any of the class methods provided
38
38
  with this package. Generally speaking, it means there was a failure in
39
39
  the underlying gethostname() or gethostbyname() calls.
@@ -68,7 +68,7 @@ HostError < StandardError
68
68
  Ruby's
69
69
 
70
70
  == Copyright
71
- Copyright 2002-2006, Daniel J. Berger, djberg96 at gmail dot com
71
+ Copyright 2002-2007, Daniel J. Berger, djberg96 at gmail dot com
72
72
 
73
73
  All Rights Reserved. This module is free software. It may be used,
74
74
  redistributed and/or modified under the same terms as Ruby itself.
@@ -1,11 +1,13 @@
1
- require "win32ole"
2
- require "socket"
3
- require "parsedate"
1
+ require 'win32ole'
2
+ require 'socket'
3
+ require 'parsedate'
4
4
 
5
5
  module Sys
6
- class HostError < StandardError; end
7
6
  class Host
8
- VERSION = "0.5.2"
7
+ class Error < StandardError; end
8
+
9
+ VERSION = '0.6.0'
10
+
9
11
  fields = %w/
10
12
  arp_always_source_route?
11
13
  arp_use_ether_snap?
@@ -69,14 +71,20 @@ module Sys
69
71
  wins_secondary_server
70
72
  /
71
73
 
72
- HostInfo = Struct.new("HostInfo",*fields)
74
+ HostInfo = Struct.new("HostInfo", *fields)
73
75
 
76
+ # Yields a HostInfo struct for each network adapter on 'host', or an array
77
+ # of HostInfo struct's in non-block form. The exact members of this struct
78
+ # vary depending on your platform.
79
+ #
74
80
  def self.info(host=Socket.gethostname)
81
+ array = [] unless block_given?
75
82
  cs = "winmgmts://#{host}/root/cimv2"
83
+
76
84
  begin
77
85
  wmi = WIN32OLE.connect(cs)
78
- rescue WIN32OLERuntimeError => e
79
- raise HostError, e
86
+ rescue WIN32OLERuntimeError => err
87
+ raise Error, err
80
88
  else
81
89
  query = "
82
90
  select * from Win32_NetworkAdapterConfiguration
@@ -86,14 +94,18 @@ module Sys
86
94
  dhcp_expires = nic.DHCPLeaseExpires
87
95
  dhcp_lease_obtained = nic.DHCPLeaseObtained
88
96
 
97
+ # It appears that the DHCP Lease Expired Date and DHCP Lease
98
+ # Obtained date can be so large as to cause an error. So, we
99
+ # simply return nil in these cases.
100
+ #
89
101
  if dhcp_expires
90
102
  parsed = ParseDate.parsedate(dhcp_expires)
91
- dhcp_expires = Time.mktime(*parsed)
103
+ dhcp_expires = Time.mktime(*parsed) rescue nil
92
104
  end
93
105
 
94
106
  if dhcp_lease_obtained
95
107
  parsed = ParseDate.parsedate(dhcp_lease_obtained)
96
- dhcp_lease_obtained = Time.mktime(*parsed)
108
+ dhcp_lease_obtained = Time.mktime(*parsed) rescue nil
97
109
  end
98
110
 
99
111
  igmp_level = nil
@@ -142,7 +154,7 @@ module Sys
142
154
  tcpip_netbios_options = "DisableNetbios"
143
155
  end
144
156
 
145
- yield HostInfo.new(
157
+ struct = HostInfo.new(
146
158
  nic.ArpAlwaysSourceRoute,
147
159
  nic.ArpUseEtherSNAP,
148
160
  nic.Caption,
@@ -204,41 +216,56 @@ module Sys
204
216
  nic.WINSScopeID,
205
217
  nic.WINSSecondaryServer
206
218
  )
219
+
220
+ if block_given?
221
+ yield struct
222
+ else
223
+ array << struct
224
+ end
207
225
  }
208
226
  end
227
+
228
+ block_given? ? nil : array
209
229
  end
210
230
 
231
+ # Returns the hostname of the current host. This may or not return
232
+ # the FQDN, depending on your system.
233
+ #
211
234
  def self.hostname
235
+ host = nil
212
236
  begin
213
237
  wmi = WIN32OLE.connect("winmgmts://")
214
238
  rescue WIN32OLERuntimeError => e
215
- raise HostError, e
239
+ raise Error, e
216
240
  else
217
241
  query = "
218
242
  select * from Win32_NetworkAdapterConfiguration
219
243
  where IPEnabled = True
220
244
  "
221
245
  wmi.ExecQuery(query).each{ |nic|
222
- return nic.DNSHostName
246
+ host = nic.DNSHostName
247
+ break
223
248
  }
224
249
  end
250
+ host
225
251
  end
226
252
 
253
+ # Returns a list of unique IP addresses for the current host.
254
+ #
227
255
  def self.ip_addr
256
+ ip_addrs = []
228
257
  begin
229
258
  wmi = WIN32OLE.connect("winmgmts://")
230
259
  rescue WIN32OLERuntimeError => e
231
- raise HostError, e
260
+ raise Error, e
232
261
  else
233
262
  query = "
234
263
  select * from Win32_NetworkAdapterConfiguration
235
264
  where IPEnabled = True
236
265
  "
237
- wmi.ExecQuery(query).each{ |nic|
238
- return nic.IPAddress
239
- }
266
+ wmi.ExecQuery(query).each{ |nic| ip_addrs = nic.IPAddress }
240
267
  end
241
- end
242
-
268
+ ip_addrs.uniq
269
+ end
243
270
  end
244
- end
271
+ end
@@ -2,7 +2,7 @@ require "rubygems"
2
2
 
3
3
  spec = Gem::Specification.new do |gem|
4
4
  gem.name = "sys-host"
5
- gem.version = "0.5.2"
5
+ gem.version = "0.6.0"
6
6
  gem.author = "Daniel J. Berger"
7
7
  gem.email = "djberg96@gmail.com"
8
8
  gem.homepage = "http://www.rubyforge.org/projects/sysutils"
@@ -16,32 +16,32 @@ spec = Gem::Specification.new do |gem|
16
16
  files = Dir["doc/*"] + Dir["test/*"] + Dir["[A-Z]*"]
17
17
  files.delete_if{ |item| item.include?("CVS") }
18
18
  gem.files = files
19
- end
20
-
21
- if $0 == __FILE__
19
+
22
20
  if RUBY_PLATFORM.match("mswin")
23
21
  File.rename("lib/sys/windows.rb", "lib/sys/host.rb")
24
- spec.required_ruby_version = '>= 1.8.2'
25
- spec.files += ["lib/sys/host.rb"]
26
- spec.platform = Gem::Platform::WIN32
22
+ gem.required_ruby_version = '>= 1.8.2'
23
+ gem.files += ["lib/sys/host.rb"]
24
+ gem.platform = Gem::Platform::WIN32
27
25
  else
28
- spec.required_ruby_version = '>= 1.8.0'
29
- spec.extensions = ["extconf.rb"]
26
+ gem.required_ruby_version = '>= 1.8.0'
27
+ gem.extensions = ["ext/extconf.rb"]
30
28
 
31
29
  case RUBY_PLATFORM
32
30
  when /linux/i
33
- file = 'ext/linux.c'
31
+ file = 'ext/linux/linux.c'
34
32
  when /sunos|solaris/i
35
- file = 'ext/sunos.c'
36
- when /freebsd/i
37
- file = 'ext/freebsd.c'
33
+ file = 'ext/sunos/sunos.c'
34
+ when /bsd|darwin|powerpc|mach/i
35
+ file = 'ext/bsd/bsd.c'
38
36
  else
39
- file = 'ext/generic.c'
37
+ file = 'ext/generic/generic.c'
40
38
  end
41
39
 
42
- spec.files += ['ext/constants.h', file]
40
+ gem.files += ['ext/constants.h', file]
43
41
  end
44
-
42
+ end
43
+
44
+ if $0 == __FILE__
45
45
  Gem.manage_gems
46
46
  Gem::Builder.new(spec).build
47
47
  end
@@ -1,38 +1,15 @@
1
- #########################################
1
+ #################################################
2
2
  # tc_host.rb
3
3
  #
4
- # Test suite for sys-host, all platforms
5
- #########################################
6
- base = File.basename(Dir.pwd)
7
- if base == "test" || base =~ /host.*/
8
- require "ftools"
9
- Dir.chdir("..") if base == "test"
10
- Dir.mkdir("sys") unless File.exists?("sys")
11
-
12
- if RUBY_PLATFORM.match("mswin")
13
- File.copy("lib/windows.rb", "sys/host.rb")
14
- else
15
- case RUBY_PLATFORM
16
- when /hpux/i
17
- File.copy("host.sl","sys")
18
- when /osx|darwin|mac/i
19
- File.copy("host.bundle","sys")
20
- else
21
- File.copy("host.so","sys")
22
- end
23
- end
24
-
25
- $LOAD_PATH.unshift Dir.pwd
26
- Dir.chdir("test") rescue nil
27
- end
28
-
29
- require "sys/host"
30
- require "test/unit"
4
+ # Test suite for sys-host, all platforms.
5
+ #################################################
6
+ require 'sys/host'
7
+ require 'test/unit'
31
8
  include Sys
32
9
 
33
10
  class TC_Host < Test::Unit::TestCase
34
11
  def test_version
35
- assert_equal("0.5.2",Host::VERSION)
12
+ assert_equal('0.6.0', Host::VERSION)
36
13
  end
37
14
 
38
15
  def test_hostname
@@ -48,14 +25,20 @@ class TC_Host < Test::Unit::TestCase
48
25
  assert_kind_of(Array, Host.ip_addr)
49
26
  end
50
27
 
28
+ def test_host_id
29
+ assert_respond_to(Host, :host_id)
30
+ assert_nothing_raised{ Host.host_id }
31
+ assert_kind_of(Integer, Host.host_id)
32
+ end
33
+
51
34
  def test_info
52
- if RUBY_PLATFORM =~ /mswin|solaris|linux/i
35
+ if RUBY_PLATFORM =~ /mswin|solaris|linux|bsd|mach|darwin|osx/i
53
36
  assert_respond_to(Host, :info)
54
37
  assert_nothing_raised{ Host.info }
55
38
  assert_kind_of(Array, Host.info)
56
39
  assert_kind_of(Struct::HostInfo, Host.info.first)
57
40
  assert_nil(Host.info{ })
58
- assert_nothing_raised{ 1000.times{ Host.info } }
41
+ assert_nothing_raised{ 100.times{ Host.info } }
59
42
  else
60
43
  puts "The 'test_info' test was skipped on this platform"
61
44
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: sys-host
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.2
7
- date: 2006-11-18 00:00:00 -07:00
6
+ version: 0.6.0
7
+ date: 2007-06-09 00:00:00 -06:00
8
8
  summary: Provides hostname and ip address info for a given host
9
9
  require_paths:
10
10
  - lib
@@ -35,10 +35,10 @@ files:
35
35
  - doc
36
36
  - examples
37
37
  - ext
38
- - extconf.rb
39
- - install.rb
40
38
  - lib
41
39
  - MANIFEST
40
+ - Rakefile
41
+ - Rakefile~
42
42
  - README
43
43
  - sys-host.gemspec
44
44
  - test
data/extconf.rb DELETED
@@ -1,48 +0,0 @@
1
- require "mkmf"
2
- require "ftools"
3
-
4
- # Remove any symlinks that may already exist
5
- File.delete('host.c') if File.exists?('host.c')
6
- File.delete('constants.h') if File.exists?('constants.h')
7
-
8
- have_func("gethostid")
9
-
10
- case RUBY_PLATFORM
11
- when /freebsd/i
12
- have_func("getipnodebyname")
13
- File.symlink("ext/freebsd.c", "host.c")
14
- File.symlink("ext/constants.h", "constants.h")
15
- when /sunos|solaris/i
16
- have_library("nsl")
17
- have_library("socket")
18
- have_func("gethostbyname_r", "netdb.h")
19
- have_func("gethostent_r", "netdb.h")
20
- File.symlink("ext/sunos.c", "host.c")
21
- File.symlink("ext/constants.h", "constants.h")
22
- when /linux/i
23
- have_func("gethostbyname_r", "netdb.h")
24
- have_func("gethostent_r", "netdb.h")
25
- File.symlink("ext/linux.c", "host.c")
26
- File.symlink("ext/constants.h", "constants.h")
27
- when /win32|windows|dos|mingw|cygwin/i
28
- STDERR.puts "Run the 'install.rb' script to install on Windows systems"
29
- else
30
- File.symlink("ext/generic.c", "host.c")
31
- File.symlink("ext/constants.h", "constants.h")
32
- end
33
-
34
- # Rename the windows.rb file to prevent 'make install' from installing it
35
- unless RUBY_PLATFORM.match("mswin")
36
- if File.exists?('lib/sys/windows.rb')
37
- File.rename('lib/sys/windows.rb', 'lib/sys/windows.orig')
38
- end
39
- end
40
-
41
- have_func("inet_ntop")
42
-
43
- # Already grabbed this from netdb.h on Solaris
44
- unless RUBY_PLATFORM =~ /sunos|solaris/i
45
- have_func("gethostent_r")
46
- end
47
-
48
- create_makefile("sys/host")
data/install.rb DELETED
@@ -1,40 +0,0 @@
1
- ################################################
2
- # install.rb - for sys-host
3
- #
4
- # For pure Ruby versions only
5
- ################################################
6
- require 'rbconfig'
7
- require 'ftools'
8
- include Config
9
- install_dir = CONFIG['sitelibdir'] + '/sys'
10
-
11
- file = ""
12
-
13
- case RUBY_PLATFORM
14
- when /windows|win32|mingw|cygwin|dos/i
15
- file = "lib/os/windows.rb"
16
- else
17
- STDERR.puts "Use 'extconf.rb/make/make site-install' for this platform"
18
- exit
19
- end
20
-
21
- # Create the 'sys' toplevel directory if it doesn't already exist
22
- begin
23
- unless File.exist?(install_dir)
24
- Dir.mkdir(install_dir)
25
- end
26
- rescue Errno::EACCES => e
27
- puts "Unable to create #{install_dir}: #{e}"
28
- exit
29
- end
30
-
31
- # Finally, copy the file to the appropriate directory
32
- begin
33
- File.copy(file,"#{install_dir}/host.rb")
34
- puts "cp #{file} #{install_dir}/host.rb"
35
- rescue Errno::EACCES => e
36
- puts "Unable to install cpu.rb to #{install_dir}: #{e}"
37
- exit
38
- end
39
-
40
- puts "Installation successful"