tryouts 0.8.8 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sysinfo.rb ADDED
@@ -0,0 +1,278 @@
1
+ require 'socket'
2
+ require 'time'
3
+
4
+ # = SysInfo
5
+ #
6
+ # A container for the platform specific system information.
7
+ # Portions of this code were originally from Amazon's EC2 AMI tools,
8
+ # specifically lib/platform.rb.
9
+ class SysInfo
10
+ unless defined?(IMPLEMENTATIONS)
11
+ VERSION = "0.7.3".freeze
12
+ IMPLEMENTATIONS = [
13
+
14
+ # These are for JRuby, System.getproperty('os.name').
15
+ # For a list of all values, see: http://lopica.sourceforge.net/os.html
16
+
17
+ #regexp matcher os implementation
18
+ [/mac\s*os\s*x/i, :unix, :osx ],
19
+ [/sunos/i, :unix, :solaris ],
20
+ [/windows\s*ce/i, :windows, :wince ],
21
+ [/windows/i, :windows, :windows ],
22
+ [/osx/i, :unix, :osx ],
23
+
24
+ # These are for RUBY_PLATFORM and JRuby
25
+ [/java/i, :java, :java ],
26
+ [/darwin/i, :unix, :osx ],
27
+ [/linux/i, :unix, :linux ],
28
+ [/freebsd/i, :unix, :freebsd ],
29
+ [/netbsd/i, :unix, :netbsd ],
30
+ [/solaris/i, :unix, :solaris ],
31
+ [/irix/i, :unix, :irix ],
32
+ [/cygwin/i, :unix, :cygwin ],
33
+ [/mswin/i, :windows, :windows ],
34
+ [/djgpp/i, :windows, :djgpp ],
35
+ [/mingw/i, :windows, :mingw ],
36
+ [/bccwin/i, :windows, :bccwin ],
37
+ [/wince/i, :windows, :wince ],
38
+ [/vms/i, :vms, :vms ],
39
+ [/os2/i, :os2, :os2 ],
40
+ [nil, :unknown, :unknown ],
41
+ ].freeze
42
+
43
+ ARCHITECTURES = [
44
+ [/(i\d86)/i, :x86 ],
45
+ [/x86_64/i, :x86_64 ],
46
+ [/x86/i, :x86 ], # JRuby
47
+ [/ia64/i, :ia64 ],
48
+ [/alpha/i, :alpha ],
49
+ [/sparc/i, :sparc ],
50
+ [/mips/i, :mips ],
51
+ [/powerpc/i, :powerpc ],
52
+ [/universal/i,:x86_64 ],
53
+ [nil, :unknown ],
54
+ ].freeze
55
+ end
56
+
57
+ attr_reader :vm
58
+ attr_reader :os
59
+ attr_reader :impl
60
+ attr_reader :arch
61
+ attr_reader :hostname
62
+ attr_reader :ipaddress_internal
63
+ attr_reader :uptime
64
+
65
+ attr_reader :paths
66
+ attr_reader :tmpdir
67
+ attr_reader :home
68
+ attr_reader :shell
69
+ attr_reader :user
70
+ attr_reader :ruby
71
+
72
+ attr_reader :attr_names
73
+ alias :implementation :impl
74
+ alias :architecture :arch
75
+
76
+ def initialize
77
+ @vm, @os, @impl, @arch = find_platform_info
78
+ @hostname, @ipaddress_internal, @uptime = find_network_info
79
+ @ruby = RUBY_VERSION.split('.').collect { |v| v.to_i }
80
+ @user = ENV['USER']
81
+ require 'Win32API' if @os == :windows && @vm == :ruby
82
+ end
83
+
84
+ # Returns [vm, os, impl, arch]
85
+ def find_platform_info
86
+ vm, os, impl, arch = :ruby, :unknown, :unknown, :unknow
87
+ IMPLEMENTATIONS.each do |r, o, i|
88
+ next unless RUBY_PLATFORM =~ r
89
+ os, impl = [o, i]
90
+ break
91
+ end
92
+ ARCHITECTURES.each do |r, a|
93
+ next unless RUBY_PLATFORM =~ r
94
+ arch = a
95
+ break
96
+ end
97
+ os == :java ? guess_java : [vm, os, impl, arch]
98
+ end
99
+
100
+ # Returns [hostname, ipaddr (internal), uptime]
101
+ def find_network_info
102
+ hostname, ipaddr, uptime = :unknown, :unknown, :unknown
103
+ begin
104
+ hostname = find_hostname
105
+ ipaddr = find_ipaddress_internal
106
+ uptime = find_uptime
107
+ rescue => ex # Be silent!
108
+ end
109
+ [hostname, ipaddr, uptime]
110
+ end
111
+
112
+ # Return the hostname for the local machine
113
+ def find_hostname; Socket.gethostname; end
114
+
115
+ # Returns the local uptime in hours. Use Win32API in Windows,
116
+ # 'sysctl -b kern.boottime' os osx, and 'who -b' on unix.
117
+ # Based on Ruby Quiz solutions by: Matthias Reitinger
118
+ # On Windows, see also: net statistics server
119
+ def find_uptime
120
+ hours = 0
121
+ begin
122
+ seconds = execute_platform_specific("find_uptime") || 0
123
+ hours = seconds / 3600 # seconds to hours
124
+ rescue => ex
125
+ #puts ex.message # TODO: implement debug?
126
+ end
127
+ hours
128
+ end
129
+
130
+
131
+ # Return the local IP address which receives external traffic
132
+ # from: http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
133
+ # NOTE: This <em>does not</em> open a connection to the IP address.
134
+ def find_ipaddress_internal
135
+ # turn off reverse DNS resolution temporarily
136
+ orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
137
+ UDPSocket.open {|s| s.connect('65.74.177.129', 1); s.addr.last } # GitHub IP
138
+ ensure
139
+ Socket.do_not_reverse_lookup = orig
140
+ end
141
+
142
+ # Returns a Symbol of the short platform descriptor in the format: VM-OS
143
+ # e.g. <tt>:java-unix</tt>
144
+ def platform
145
+ "#{@vm}-#{@os}".to_sym
146
+ end
147
+
148
+ # Returns a String of the full platform descriptor in the format: VM-OS-IMPL-ARCH
149
+ # e.g. <tt>java-unix-osx-x86_64</tt>
150
+ def to_s(*args)
151
+ "#{@vm}-#{@os}-#{@impl}-#{@arch}".to_sym
152
+ end
153
+
154
+ # Returns the environment paths as an Array
155
+ def paths; execute_platform_specific(:paths); end
156
+ # Returns the path to the current user's home directory
157
+ def home; execute_platform_specific(:home); end
158
+ # Returns the name of the current shell
159
+ def shell; execute_platform_specific(:shell); end
160
+ # Returns the path to the current temp directory
161
+ def tmpdir; execute_platform_specific(:tmpdir); end
162
+
163
+ private
164
+
165
+ # Look for and execute a platform specific method.
166
+ # The name of the method will be in the format: +dtype-VM-OS-IMPL+.
167
+ # e.g. find_uptime_ruby_unix_osx
168
+ #
169
+ def execute_platform_specific(dtype)
170
+ criteria = [@vm, @os, @impl]
171
+ while !criteria.empty?
172
+ meth = [dtype, criteria].join('_').to_sym
173
+ return self.send(meth) if SysInfo.private_method_defined?(meth)
174
+ criteria.pop
175
+ end
176
+ raise "#{dtype}_#{@vm}_#{@os}_#{@impl} not implemented"
177
+ end
178
+
179
+ def paths_ruby_unix; (ENV['PATH'] || '').split(':'); end
180
+ def paths_ruby_windows; (ENV['PATH'] || '').split(';'); end # Not tested!
181
+ def paths_java
182
+ delim = @impl == :windows ? ';' : ':'
183
+ (ENV['PATH'] || '').split(delim)
184
+ end
185
+
186
+ def tmpdir_ruby_unix; (ENV['TMPDIR'] || '/tmp'); end
187
+ def tmpdir_ruby_windows; (ENV['TMPDIR'] || 'C:\\temp'); end
188
+ def tmpdir_java
189
+ default = @impl == :windows ? 'C:\\temp' : '/tmp'
190
+ (ENV['TMPDIR'] || default)
191
+ end
192
+
193
+ def shell_ruby_unix; (ENV['SHELL'] || 'bash').to_sym; end
194
+ def shell_ruby_windows; :dos; end
195
+ alias_method :shell_java_unix, :shell_ruby_unix
196
+ alias_method :shell_java_windows, :shell_ruby_windows
197
+
198
+ def home_ruby_unix; File.expand_path(ENV['HOME']); end
199
+ def home_ruby_windows; File.expand_path(ENV['USERPROFILE']); end
200
+ def home_java
201
+ if @impl == :windows
202
+ File.expand_path(ENV['USERPROFILE'])
203
+ else
204
+ File.expand_path(ENV['HOME'])
205
+ end
206
+ end
207
+
208
+ # Ya, this is kinda wack. Ruby -> Java -> Kernel32. See:
209
+ # http://www.oreillynet.com/ruby/blog/2008/01/jruby_meets_the_windows_api_1.html
210
+ # http://msdn.microsoft.com/en-us/library/ms724408(VS.85).aspx
211
+ # Ruby 1.9.1: Win32API is now deprecated in favor of using the DL library.
212
+ def find_uptime_java_windows_windows
213
+ kernel32 = com.sun.jna.NativeLibrary.getInstance('kernel32')
214
+ buf = java.nio.ByteBuffer.allocate(256)
215
+ (kernel32.getFunction('GetTickCount').invokeInt([256, buf].to_java).to_f / 1000).to_f
216
+ end
217
+ def find_uptime_ruby_windows_windows
218
+ # Win32API is required in self.guess
219
+ getTickCount = Win32API.new("kernel32", "GetTickCount", nil, 'L')
220
+ ((getTickCount.call()).to_f / 1000).to_f
221
+ end
222
+ def find_uptime_ruby_unix_osx
223
+ # This is faster than "who" and could work on BSD also.
224
+ (Time.now.to_f - Time.at(`sysctl -b kern.boottime 2>/dev/null`.unpack('L').first).to_f).to_f
225
+ end
226
+
227
+ # This should work for most unix flavours.
228
+ def find_uptime_ruby_unix
229
+ # who is sloooooow. Use File.read('/proc/uptime')
230
+ (Time.now.to_i - Time.parse(`who -b 2>/dev/null`).to_f)
231
+ end
232
+ alias_method :find_uptime_java_unix_osx, :find_uptime_ruby_unix
233
+
234
+ # Determine the values for vm, os, impl, and arch when running on Java.
235
+ def guess_java
236
+ vm, os, impl, arch = :java, :unknown, :unknown, :unknown
237
+ require 'java'
238
+ include_class java.lang.System unless defined?(System)
239
+
240
+ osname = System.getProperty("os.name")
241
+ IMPLEMENTATIONS.each do |r, o, i|
242
+ next unless osname =~ r
243
+ os, impl = [o, i]
244
+ break
245
+ end
246
+
247
+ osarch = System.getProperty("os.arch")
248
+ ARCHITECTURES.each do |r, a|
249
+ next unless osarch =~ r
250
+ arch = a
251
+ break
252
+ end
253
+ [vm, os, impl, arch]
254
+ end
255
+
256
+ # Returns the local IP address based on the hostname.
257
+ # According to coderrr (see comments on blog link above), this implementation
258
+ # doesn't guarantee that it will return the address for the interface external
259
+ # traffic goes through. It's also possible the hostname isn't resolvable to the
260
+ # local IP.
261
+ #
262
+ # NOTE: This code predates the current ip_address_internal. It was just as well
263
+ # but the other code is cleaner. I'm keeping this old version here for now.
264
+ def ip_address_internal_alt
265
+ ipaddr = :unknown
266
+ begin
267
+ saddr = Socket.getaddrinfo( Socket.gethostname, nil, Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME)
268
+ ipaddr = saddr.select{|type| type[0] == 'AF_INET' }[0][3]
269
+ rescue => ex
270
+ end
271
+ ipaddr
272
+ end
273
+ end
274
+
275
+
276
+ if $0 == __FILE__
277
+ puts SysInfo.new.to_yaml
278
+ end