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