ruby-wmi 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,5 @@
1
+ == 1.0.0 / 2007-07-19
2
+
3
+ * 1 major enhancement
4
+ * Birthday!
5
+
data/Manifest.txt ADDED
@@ -0,0 +1,15 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/ruby-wmi.rb
6
+ lib/ruby-wmi/base.rb
7
+ lib/ruby-wmi/cim.rb
8
+ lib/ruby-wmi/core_ext.rb
9
+ lib/ruby-wmi/core_ext/time_ext.rb
10
+ lib/ruby-wmi/core_ext/win32ole_ext.rb
11
+ lib/ruby-wmi/win32.rb
12
+ samples/disk.rb
13
+ samples/logoff.rb
14
+ samples/memory.rb
15
+ test/test_ruby-wmi.rb
data/README.txt ADDED
@@ -0,0 +1,50 @@
1
+ ruby-wmi
2
+ by Gordon Thiesfeld
3
+ http://ruby-wmi.rubyforge.org/
4
+
5
+ == DESCRIPTION:
6
+
7
+ ruby-wmi is an ActiveRecord style interface for Microsoft's Windows Management Instrumentation provider.
8
+
9
+ == SYNOPSIS:
10
+
11
+ # The following code sample kills all processes of a given name, except the oldest (in this case, Notepad)
12
+
13
+ require 'ruby-wmi'
14
+
15
+ processes = Win32::Process.find(:all, :conditions => { :name => 'Notepad.exe' })
16
+ morituri = processes.sort_by{|p| p.CreationDate } #those who are about to die
17
+ morituri.shift
18
+ morituri.each{|p| p.terminate }
19
+
20
+ == REQUIREMENTS:
21
+
22
+
23
+ == INSTALL:
24
+
25
+ gem install ruby-wmi
26
+
27
+ == LICENSE:
28
+
29
+ (The MIT License)
30
+
31
+ Copyright (c) 2007 Gordon Thiesfeld
32
+
33
+ Permission is hereby granted, free of charge, to any person obtaining
34
+ a copy of this software and associated documentation files (the
35
+ 'Software'), to deal in the Software without restriction, including
36
+ without limitation the rights to use, copy, modify, merge, publish,
37
+ distribute, sublicense, and/or sell copies of the Software, and to
38
+ permit persons to whom the Software is furnished to do so, subject to
39
+ the following conditions:
40
+
41
+ The above copyright notice and this permission notice shall be
42
+ included in all copies or substantial portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
45
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
46
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
47
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
48
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
49
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
50
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/ruby-wmi.rb'
6
+
7
+ Hoe.new('ruby-wmi', Ruby_wmi::VERSION) do |p|
8
+ p.rubyforge_name = 'ruby-wmi'
9
+ p.author = 'Gordon Thiesfeld'
10
+ p.email = 'gthiesfeld@gmail.com'
11
+ p.summary = "ruby-wmi is an ActiveRecord style interface for Microsoft\'s Windows Management Instrumentation provider."
12
+ # p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
13
+ # p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
14
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
15
+ p.need_tar = false
16
+ end
17
+
18
+ # vim: syntax=Ruby
data/lib/ruby-wmi.rb ADDED
@@ -0,0 +1,11 @@
1
+ class Ruby_wmi
2
+ VERSION = '0.2.0'
3
+ end
4
+
5
+ $:.unshift(File.dirname(__FILE__)) unless
6
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
7
+
8
+ require 'ruby-wmi/core_ext'
9
+ require 'ruby-wmi/base'
10
+ require 'ruby-wmi/cim'
11
+ require 'ruby-wmi/win32'
@@ -0,0 +1,114 @@
1
+ require 'win32ole'
2
+
3
+ module WMI
4
+
5
+ def subclasses(options ={})
6
+ Base.set_connection(options)
7
+ b = Base.connection
8
+ b.SubclassesOf.map { |subclass| class_name = subclass.Path_.Class }
9
+ end
10
+
11
+ alias :subclasses_of :subclasses
12
+
13
+ def const_missing(name)
14
+ self.const_set(name, Class.new(self::Base))
15
+ end
16
+ extend self
17
+
18
+ class Base
19
+
20
+ class << self
21
+ def subclass_name
22
+ self.name.split('::').last
23
+ end
24
+
25
+ def connection
26
+ c = WIN32OLE.new("WbemScripting.SWbemLocator")
27
+ c.ConnectServer(@host,@klass,@credentials[:user],@credentials[:passwd])
28
+ end
29
+
30
+ def set_connection(options)
31
+ @host = options[:host] || nil
32
+ @klass = options[:class] || 'root/cimv2'
33
+ @credentials = options[:credentials] || {:user => nil,:passwd => nil}
34
+ end
35
+
36
+ def find_by_wql(query)
37
+ d = connection.ExecQuery(query)
38
+ d.count # needed to check for errors. Weird, but it works.
39
+ d.to_a
40
+ end
41
+
42
+ def find(arg=:all, options={})
43
+ set_connection options
44
+ case arg
45
+ when :all; find_all(options)
46
+ when :first; find_first(options)
47
+ end
48
+ end
49
+
50
+ def find_first(options={})
51
+ find_all(options).first
52
+ end
53
+
54
+ def find_all(options={})
55
+ find_by_wql(construct_finder_sql(options))
56
+ end
57
+
58
+ def construct_finder_sql(options)
59
+ #~ scope = scope(:find)
60
+ sql = "SELECT #{options[:select] || '*'} "
61
+ sql << "FROM #{options[:from] || subclass_name} "
62
+
63
+ #~ add_joins!(sql, options, scope)
64
+ add_conditions!(sql, options[:conditions], nil)
65
+
66
+ sql << " GROUP BY #{options[:group]} " if options[:group]
67
+
68
+ #~ add_order!(sql, options[:order], scope)
69
+ #~ add_limit!(sql, options, scope)
70
+ #~ add_lock!(sql, options, scope)
71
+
72
+ sql
73
+ end
74
+
75
+ def add_conditions!(sql, conditions, scope = :auto)
76
+ #~ scope = scope(:find) if :auto == scope
77
+ segments = []
78
+ segments << sanitize_sql(conditions) unless conditions.nil?
79
+ #~ segments << conditions unless conditions.nil?
80
+ #~ segments << type_condition unless descends_from_active_record?
81
+ segments.compact!
82
+ sql << "WHERE (#{segments.join(") AND (")}) " unless segments.empty?
83
+ sql.gsub!("\\","\\\\\\")
84
+ end
85
+
86
+ # Accepts an array, hash, or string of sql conditions and sanitizes
87
+ # them into a valid SQL fragment.
88
+ # ["name='%s' and group_id='%s'", "foo'bar", 4] returns "name='foo''bar' and group_id='4'"
89
+ # { :name => "foo'bar", :group_id => 4 } returns "name='foo''bar' and group_id='4'"
90
+ # "name='foo''bar' and group_id='4'" returns "name='foo''bar' and group_id='4'"
91
+ def sanitize_sql(condition)
92
+ case condition
93
+ when Array; sanitize_sql_array(condition)
94
+ when Hash; sanitize_sql_hash(condition)
95
+ else condition
96
+ end
97
+ end
98
+
99
+ # Sanitizes a hash of attribute/value pairs into SQL conditions.
100
+ # { :name => "foo'bar", :group_id => 4 }
101
+ # # => "name='foo''bar' and group_id= 4"
102
+ # { :status => nil, :group_id => [1,2,3] }
103
+ # # => "status IS NULL and group_id IN (1,2,3)"
104
+ def sanitize_sql_hash(attrs)
105
+ conditions = attrs.map do |attr, value|
106
+ #~ "#{table_name}.#{connection.quote_column_name(attr)} #{attribute_condition(value)}"
107
+ "#{attr} = '#{value}'"
108
+ end.join(' AND ')
109
+
110
+ #~ replace_bind_variables(conditions, attrs.values)
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,12 @@
1
+ module CIM
2
+ extend WMI
3
+
4
+ class Base < WMI::Base
5
+ class << self
6
+ def subclass_name
7
+ self.name.gsub('::', '_')
8
+ end
9
+ end
10
+ end
11
+
12
+ end
@@ -0,0 +1 @@
1
+ Dir[File.dirname(__FILE__) + "/core_ext/*.rb"].each { |file| require(file) }
@@ -0,0 +1,9 @@
1
+ class Time
2
+
3
+ def to_swebmDateTime
4
+ t = strftime("%Y%m%d%H%M%S")
5
+ o = gmt_offset/60
6
+ "#{t}.000000#{o}"
7
+ end
8
+
9
+ end
@@ -0,0 +1,5 @@
1
+ require 'win32ole'
2
+
3
+ class WIN32OLE
4
+ include Enumerable
5
+ end
@@ -0,0 +1,11 @@
1
+ module Win32
2
+ extend WMI
3
+
4
+ class Base < WMI::Base
5
+ class << self
6
+ def subclass_name
7
+ self.name.gsub('::', '_')
8
+ end
9
+ end
10
+ end
11
+ end
data/samples/disk.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'wmi'
2
+
3
+ disks = WMI::Win32_LogicalDisk.find(:all)
4
+
5
+ disks.each do |disk|
6
+ disk.properties_.each do |p|
7
+ puts "#{p.name}: #{disk[p.name]}"
8
+ end
9
+ end
data/samples/logoff.rb ADDED
@@ -0,0 +1,14 @@
1
+ #~ 'active_wmi'
2
+ #~ 'wmi_rb'
3
+ #~ 'ruby-wmi'
4
+ #~ 'rWMI'
5
+ #~ 'wmi4r'
6
+
7
+ require 'wmi'
8
+
9
+ LOGOFF = 0
10
+
11
+ host = 'computername'
12
+ host = WMI::Win32_OperatingSystem.find(:first, :conditions => {:primary => true}, :host => host)
13
+ host.win32shutdown(LOGOFF)
14
+
data/samples/memory.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'wmi'
2
+
3
+ properties = ['Description','MaxCapacity','MemoryDevices','MemoryErrorCorrection']
4
+
5
+
6
+ mem = WMI::Win32_PhysicalMemoryArray.find(:all, :host => 'server1')
7
+ mem.each{|i| puts properties.map{|p| "#{p}: #{i[p]}"}}
8
+
9
+ mem2 = WMI::Win32_PhysicalMemoryArray.find(:all, {:host => 'server2', :credentials =>
10
+ {:user => 'domain\\gordon', :passwd => 'password'}
11
+ }
12
+ )
13
+ mem2.each{|i| puts properties.map{|p| "#{p}: #{i[p]}"}}
14
+
@@ -0,0 +1,2 @@
1
+ # stub file, not sure how to test this stuff yet.
2
+ # Mocks, maybe?
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-wmi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Gordon Thiesfeld
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-01-10 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.4.0
23
+ version:
24
+ description: The author was too lazy to write a description
25
+ email: gthiesfeld@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - History.txt
32
+ - Manifest.txt
33
+ - README.txt
34
+ files:
35
+ - History.txt
36
+ - Manifest.txt
37
+ - README.txt
38
+ - Rakefile
39
+ - lib/ruby-wmi.rb
40
+ - lib/ruby-wmi/base.rb
41
+ - lib/ruby-wmi/cim.rb
42
+ - lib/ruby-wmi/core_ext.rb
43
+ - lib/ruby-wmi/core_ext/time_ext.rb
44
+ - lib/ruby-wmi/core_ext/win32ole_ext.rb
45
+ - lib/ruby-wmi/win32.rb
46
+ - samples/disk.rb
47
+ - samples/logoff.rb
48
+ - samples/memory.rb
49
+ - test/test_ruby-wmi.rb
50
+ has_rdoc: true
51
+ homepage: http://www.zenspider.com/ZSS/Products/ruby-wmi/
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --main
55
+ - README.txt
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project: ruby-wmi
73
+ rubygems_version: 1.0.1
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: ruby-wmi is an ActiveRecord style interface for Microsoft's Windows Management Instrumentation provider.
77
+ test_files:
78
+ - test/test_ruby-wmi.rb