sys-uname 0.8.4-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sys/uname.rb ADDED
@@ -0,0 +1,470 @@
1
+ require 'win32ole'
2
+ require 'socket'
3
+ require 'time'
4
+
5
+ # The Sys module provides a namespace only.
6
+ module Sys
7
+
8
+ # The Uname class encapsulates uname (platform) information.
9
+ class Uname
10
+
11
+ # This is the error raised if any of the Sys::Uname methods should fail.
12
+ class Error < StandardError; end
13
+
14
+ # The version of the sys-uname library
15
+ VERSION = '0.8.4'
16
+
17
+ # :stopdoc:
18
+
19
+ fields = %w/
20
+ boot_device
21
+ build_number
22
+ build_type
23
+ caption
24
+ code_set
25
+ country_code
26
+ creation_class_name
27
+ cscreation_class_name
28
+ csd_version
29
+ cs_name
30
+ current_time_zone
31
+ debug
32
+ description
33
+ distributed
34
+ foreground_application_boost
35
+ free_physical_memory
36
+ free_space_in_paging_files
37
+ free_virtual_memory
38
+ install_date
39
+ last_bootup_time
40
+ local_date_time
41
+ locale
42
+ manufacturer
43
+ max_number_of_processes
44
+ max_process_memory_size
45
+ name
46
+ number_of_licensed_users
47
+ number_of_processes
48
+ number_of_users
49
+ organization
50
+ os_language
51
+ os_product_suite
52
+ os_type
53
+ other_type_description
54
+ plus_product_id
55
+ plus_version_number
56
+ primary
57
+ quantum_length
58
+ quantum_type
59
+ registered_user
60
+ serial_number
61
+ service_pack_major_version
62
+ service_pack_minor_version
63
+ size_stored_in_paging_files
64
+ status
65
+ system_device
66
+ system_directory
67
+ total_swap_space_size
68
+ total_virtual_memory_size
69
+ total_visible_memory_size
70
+ version
71
+ windows_directory
72
+ /
73
+
74
+ # The UnameStruct is used to store platform information for some methods.
75
+ UnameStruct = Struct.new("UnameStruct", *fields)
76
+
77
+ # :startdoc:
78
+
79
+ # Returns the version plus patch information of the operating system,
80
+ # separated by a hyphen, e.g. "2915-Service Pack 2".
81
+ #--
82
+ # The instance name is unpredictable, so we have to resort to using
83
+ # the 'InstancesOf' method to get the data we need, rather than
84
+ # including it as part of the connection.
85
+ #
86
+ def self.version(host=Socket.gethostname)
87
+ cs = "winmgmts://#{host}/root/cimv2"
88
+ begin
89
+ wmi = WIN32OLE.connect(cs)
90
+ rescue WIN32OLERuntimeError => err
91
+ raise Error, err
92
+ else
93
+ query = "select * from Win32_OperatingSystem"
94
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |ole|
95
+ str = "#{ole.Version} #{ole.BuildNumber}-"
96
+ str << "#{ole.ServicePackMajorVersion}"
97
+ return str
98
+ }
99
+ end
100
+ end
101
+
102
+ # Returns the operating system name, e.g. "Microsoft Windows XP Home"
103
+ #
104
+ def self.sysname(host=Socket.gethostname)
105
+ cs = "winmgmts:{impersonationLevel=impersonate,(security)}"
106
+ cs << "//#{host}/root/cimv2"
107
+ begin
108
+ wmi = WIN32OLE.connect(cs)
109
+ rescue WIN32OLERuntimeError => err
110
+ raise Error, err
111
+ else
112
+ query = "select * from Win32_OperatingSystem"
113
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |ole|
114
+ return ole.Caption
115
+ }
116
+ end
117
+ end
118
+
119
+ # Returns the nodename. This is usually, but not necessarily, the
120
+ # same as the system's hostname.
121
+ #
122
+ def self.nodename(host=Socket.gethostname)
123
+ cs = "winmgmts:{impersonationLevel=impersonate,(security)}"
124
+ cs << "//#{host}/root/cimv2"
125
+ begin
126
+ wmi = WIN32OLE.connect(cs)
127
+ rescue WIN32OLERuntimeError => err
128
+ raise Error, err
129
+ else
130
+ query = "select * from Win32_OperatingSystem"
131
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |ole|
132
+ return ole.CSName
133
+ }
134
+ end
135
+ end
136
+
137
+ # Returns the machine hardware type. e.g. "i686".
138
+ #--
139
+ # This may or may not return the expected value because some CPU types
140
+ # were unknown to the OS when the OS was originally released. It
141
+ # appears that MS doesn't necessarily patch this, either.
142
+ #
143
+ def self.machine(cpu_num=0, host=Socket.gethostname)
144
+ cs = "winmgmts:{impersonationLevel=impersonate,(security)}"
145
+ cs << "//#{host}/root/cimv2:Win32_Processor='cpu#{cpu_num}'"
146
+ begin
147
+ wmi = WIN32OLE.connect(cs)
148
+ rescue WIN32OLERuntimeError => err
149
+ raise Error, err
150
+ else
151
+ # Convert a family number into the equivalent string
152
+ case wmi.Family
153
+ when 1
154
+ return "Other"
155
+ when 2
156
+ return "Unknown"
157
+ when 3
158
+ return "8086"
159
+ when 4
160
+ return "80286"
161
+ when 5
162
+ return "80386"
163
+ when 6
164
+ return "80486"
165
+ when 7
166
+ return "8087"
167
+ when 8
168
+ return "80287"
169
+ when 9
170
+ return "80387"
171
+ when 10
172
+ return "80487"
173
+ when 11
174
+ return "Pentium brand"
175
+ when 12
176
+ return "Pentium Pro"
177
+ when 13
178
+ return "Pentium II"
179
+ when 14
180
+ return "Pentium processor with MMX technology"
181
+ when 15
182
+ return "Celeron"
183
+ when 16
184
+ return "Pentium II Xeon"
185
+ when 17
186
+ return "Pentium III"
187
+ when 18
188
+ return "M1 Family"
189
+ when 19
190
+ return "M2 Family"
191
+ when 24
192
+ return "K5 Family"
193
+ when 25
194
+ return "K6 Family"
195
+ when 26
196
+ return "K6-2"
197
+ when 27
198
+ return "K6-3"
199
+ when 28
200
+ return "AMD Athlon Processor Family"
201
+ when 29
202
+ return "AMD Duron Processor"
203
+ when 30
204
+ return "AMD2900 Family"
205
+ when 31
206
+ return "K6-2+"
207
+ when 32
208
+ return "Power PC Family"
209
+ when 33
210
+ return "Power PC 601"
211
+ when 34
212
+ return "Power PC 603"
213
+ when 35
214
+ return "Power PC 603+"
215
+ when 36
216
+ return "Power PC 604"
217
+ when 37
218
+ return "Power PC 620"
219
+ when 38
220
+ return "Power PC X704"
221
+ when 39
222
+ return "Power PC 750"
223
+ when 48
224
+ return "Alpha Family"
225
+ when 49
226
+ return "Alpha 21064"
227
+ when 50
228
+ return "Alpha 21066"
229
+ when 51
230
+ return "Alpha 21164"
231
+ when 52
232
+ return "Alpha 21164PC"
233
+ when 53
234
+ return "Alpha 21164a"
235
+ when 54
236
+ return "Alpha 21264"
237
+ when 55
238
+ return "Alpha 21364"
239
+ when 64
240
+ return "MIPS Family"
241
+ when 65
242
+ return "MIPS R4000"
243
+ when 66
244
+ return "MIPS R4200"
245
+ when 67
246
+ return "MIPS R4400"
247
+ when 68
248
+ return "MIPS R4600"
249
+ when 69
250
+ return "MIPS R10000"
251
+ when 80
252
+ return "SPARC Family"
253
+ when 81
254
+ return "SuperSPARC"
255
+ when 82
256
+ return "microSPARC II"
257
+ when 83
258
+ return "microSPARC IIep"
259
+ when 84
260
+ return "UltraSPARC"
261
+ when 85
262
+ return "UltraSPARC II"
263
+ when 86
264
+ return "UltraSPARC IIi"
265
+ when 87
266
+ return "UltraSPARC III"
267
+ when 88
268
+ return "UltraSPARC IIIi"
269
+ when 96
270
+ return "68040"
271
+ when 97
272
+ return "68xxx Family"
273
+ when 98
274
+ return "68000"
275
+ when 99
276
+ return "68010"
277
+ when 100
278
+ return "68020"
279
+ when 101
280
+ return "68030"
281
+ when 112
282
+ return "Hobbit Family"
283
+ when 120
284
+ return "Crusoe TM5000 Family"
285
+ when 121
286
+ return "Crusoe TM3000 Family"
287
+ when 128
288
+ return "Weitek"
289
+ when 130
290
+ return "Itanium Processor"
291
+ when 144
292
+ return "PA-RISC Family"
293
+ when 145
294
+ return "PA-RISC 8500"
295
+ when 146
296
+ return "PA-RISC 8000"
297
+ when 147
298
+ return "PA-RISC 7300LC"
299
+ when 148
300
+ return "PA-RISC 7200"
301
+ when 149
302
+ return "PA-RISC 7100LC"
303
+ when 150
304
+ return "PA-RISC 7100"
305
+ when 160
306
+ return "V30 Family"
307
+ when 176
308
+ return "Pentium III Xeon�"
309
+ when 177
310
+ return "Pentium III Processor with Intel SpeedStep Technology"
311
+ when 178
312
+ return "Pentium 4"
313
+ when 179
314
+ return "Intel Xeon�"
315
+ when 180
316
+ return "AS400 Family"
317
+ when 181
318
+ return "Intel Xeon processor MP"
319
+ when 182
320
+ return "AMD AthlonXP Family"
321
+ when 183
322
+ return "AMD AthlonMP Family"
323
+ when 184
324
+ return "Intel Itanium 2"
325
+ when 185
326
+ return "AMD Opteron� Family"
327
+ when 190
328
+ return "K7"
329
+ when 200
330
+ return "IBM390 Family"
331
+ when 201
332
+ return "G4"
333
+ when 202
334
+ return "G5"
335
+ when 250
336
+ return "i860"
337
+ when 251
338
+ return "i960"
339
+ when 260
340
+ return "SH-3"
341
+ when 261
342
+ return "SH-4"
343
+ when 280
344
+ return "ARM"
345
+ when 281
346
+ return "StrongARM"
347
+ when 300
348
+ return "6x86"
349
+ when 301
350
+ return "MediaGX"
351
+ when 302
352
+ return "MII"
353
+ when 320
354
+ return "WinChip"
355
+ when 350
356
+ return "DSP"
357
+ when 500
358
+ return "Video Processor"
359
+ else
360
+ return "Unknown"
361
+ end
362
+ end
363
+ end
364
+
365
+ # Returns the release number, e.g. 5.1.2600.
366
+ #
367
+ def self.release(host=Socket.gethostname)
368
+ cs = "winmgmts://#{host}/root/cimv2"
369
+ begin
370
+ wmi = WIN32OLE.connect(cs)
371
+ rescue WIN32OLERuntimeError => e
372
+ raise Error, e
373
+ else
374
+ query = "select * from Win32_OperatingSystem"
375
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |ole|
376
+ return ole.Version
377
+ }
378
+ end
379
+ end
380
+
381
+ # Returns a struct of type UnameStruct that contains sysname, nodename,
382
+ # machine, version, and release, as well as a plethora of other fields.
383
+ # Please see the MSDN documentation for what each of these fields mean.
384
+ #
385
+ def self.uname(host=Socket.gethostname)
386
+ cs = "winmgmts://#{host}/root/cimv2"
387
+ begin
388
+ wmi = WIN32OLE.connect(cs)
389
+ rescue WIN32OLERuntimeError => e
390
+ raise Error, e
391
+ else
392
+ query = "select * from Win32_OperatingSystem"
393
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |os|
394
+ return UnameStruct.new(
395
+ os.BootDevice,
396
+ os.BuildNumber,
397
+ os.BuildType,
398
+ os.Caption,
399
+ os.CodeSet,
400
+ os.CountryCode,
401
+ os.CreationClassName,
402
+ os.CSCreationClassName,
403
+ os.CSDVersion,
404
+ os.CSName,
405
+ os.CurrentTimeZone,
406
+ os.Debug,
407
+ os.Description,
408
+ os.Distributed,
409
+ os.ForegroundApplicationBoost,
410
+ self.convert(os.FreePhysicalMemory),
411
+ self.convert(os.FreeSpaceInPagingFiles),
412
+ self.convert(os.FreeVirtualMemory),
413
+ self.parse_ms_date(os.InstallDate),
414
+ self.parse_ms_date(os.LastBootUpTime),
415
+ self.parse_ms_date(os.LocalDateTime),
416
+ os.Locale,
417
+ os.Manufacturer,
418
+ os.MaxNumberOfProcesses,
419
+ self.convert(os.MaxProcessMemorySize),
420
+ os.Name,
421
+ os.NumberOfLicensedUsers,
422
+ os.NumberOfProcesses,
423
+ os.NumberOfUsers,
424
+ os.Organization,
425
+ os.OSLanguage,
426
+ os.OSProductSuite,
427
+ os.OSType,
428
+ os.OtherTypeDescription,
429
+ os.PlusProductID,
430
+ os.PlusVersionNumber,
431
+ os.Primary,
432
+ os.respond_to?(:QuantumLength) ? os.QuantumLength : nil,
433
+ os.respond_to?(:QuantumType) ? os.QuantumType : nil,
434
+ os.RegisteredUser,
435
+ os.SerialNumber,
436
+ os.ServicePackMajorVersion,
437
+ os.ServicePackMinorVersion,
438
+ self.convert(os.SizeStoredInPagingFiles),
439
+ os.Status,
440
+ os.SystemDevice,
441
+ os.SystemDirectory,
442
+ self.convert(os.TotalSwapSpaceSize),
443
+ self.convert(os.TotalVirtualMemorySize),
444
+ self.convert(os.TotalVisibleMemorySize),
445
+ os.Version,
446
+ os.WindowsDirectory
447
+ )
448
+ }
449
+ end
450
+ end
451
+
452
+ private
453
+
454
+ # Converts a string in the format '20040703074625.015625-360' into a
455
+ # Ruby Time object.
456
+ #
457
+ def self.parse_ms_date(str)
458
+ return if str.nil?
459
+ return Time.parse(str.split('.')[0])
460
+ end
461
+
462
+ # There is a bug in win32ole where uint64 types are returned as a
463
+ # String rather than a Fixnum/Bignum. This deals with that for now.
464
+ #
465
+ def self.convert(str)
466
+ return nil if str.nil? # Don't turn nil into 0
467
+ return str.to_i
468
+ end
469
+ end
470
+ end
data/sys-uname.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'sys-uname'
5
+ spec.version = '0.8.4'
6
+ spec.license = 'Artistic 2.0'
7
+ spec.author = 'Daniel J. Berger'
8
+ spec.email = 'djberg96@gmail.com'
9
+ spec.homepage = 'http://www.rubyforge.org/projects/sysutils'
10
+ spec.platform = Gem::Platform::RUBY
11
+ spec.summary = 'An interface for returning system platform information'
12
+ spec.test_file = 'test/test_sys_uname.rb'
13
+ spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
14
+
15
+ spec.extra_rdoc_files = ['CHANGES', 'README', 'MANIFEST', 'doc/uname.txt']
16
+ spec.rubyforge_project = 'sysutils'
17
+
18
+ spec.add_development_dependency('test-unit', '>= 2.0.6')
19
+
20
+ spec.description = <<-EOF
21
+ The sys-uname library provides an interface for gathering information
22
+ about your current platform. The library is named after the Unix 'uname'
23
+ command but also works on MS Windows. Available information includes
24
+ OS name, OS version, system name and so on. Additional information is
25
+ available for certain platforms.
26
+ EOF
27
+ end