sys-uname 0.8.6-universal-mingw32 → 0.9.2-universal-mingw32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,469 @@
1
+ require 'socket'
2
+ require 'time'
3
+
4
+ # See Ruby bugs #2618 and #7681. This is a workaround.
5
+ BEGIN{
6
+ require 'win32ole'
7
+ if RUBY_VERSION.to_f < 2.0
8
+ WIN32OLE.ole_initialize
9
+ at_exit { WIN32OLE.ole_uninitialize }
10
+ end
11
+ }
12
+
13
+ # The Sys module provides a namespace only.
14
+ module Sys
15
+
16
+ # The Uname class encapsulates uname (platform) information.
17
+ class Uname
18
+
19
+ # This is the error raised if any of the Sys::Uname methods should fail.
20
+ class Error < StandardError; end
21
+
22
+ # The version of the sys-uname library.
23
+ VERSION = '0.9.2'
24
+
25
+ fields = %w[
26
+ boot_device
27
+ build_number
28
+ build_type
29
+ caption
30
+ code_set
31
+ country_code
32
+ creation_class_name
33
+ cscreation_class_name
34
+ csd_version
35
+ cs_name
36
+ current_time_zone
37
+ debug
38
+ description
39
+ distributed
40
+ foreground_application_boost
41
+ free_physical_memory
42
+ free_space_in_paging_files
43
+ free_virtual_memory
44
+ install_date
45
+ last_bootup_time
46
+ local_date_time
47
+ locale
48
+ manufacturer
49
+ max_number_of_processes
50
+ max_process_memory_size
51
+ name
52
+ number_of_licensed_users
53
+ number_of_processes
54
+ number_of_users
55
+ organization
56
+ os_language
57
+ os_product_suite
58
+ os_type
59
+ other_type_description
60
+ plus_product_id
61
+ plus_version_number
62
+ primary
63
+ quantum_length
64
+ quantum_type
65
+ registered_user
66
+ serial_number
67
+ service_pack_major_version
68
+ service_pack_minor_version
69
+ size_stored_in_paging_files
70
+ status
71
+ system_device
72
+ system_directory
73
+ total_swap_space_size
74
+ total_virtual_memory_size
75
+ total_visible_memory_size
76
+ version
77
+ windows_directory
78
+ ]
79
+
80
+ # The UnameStruct is used to store platform information for some methods.
81
+ UnameStruct = Struct.new("UnameStruct", *fields)
82
+
83
+ # Returns the version plus patch information of the operating system,
84
+ # separated by a hyphen, e.g. "2915-Service Pack 2".
85
+ #--
86
+ # The instance name is unpredictable, so we have to resort to using
87
+ # the 'InstancesOf' method to get the data we need, rather than
88
+ # including it as part of the connection.
89
+ #
90
+ def self.version(host=Socket.gethostname)
91
+ cs = "winmgmts://#{host}/root/cimv2"
92
+ begin
93
+ wmi = WIN32OLE.connect(cs)
94
+ rescue WIN32OLERuntimeError => e
95
+ raise Error, e
96
+ else
97
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |ole|
98
+ str = "#{ole.Version} #{ole.BuildNumber}-"
99
+ str << "#{ole.ServicePackMajorVersion}"
100
+ return str
101
+ }
102
+ end
103
+ end
104
+
105
+ # Returns the operating system name, e.g. "Microsoft Windows XP Home"
106
+ #
107
+ def self.sysname(host=Socket.gethostname)
108
+ cs = "winmgmts:{impersonationLevel=impersonate,(security)}"
109
+ cs << "//#{host}/root/cimv2"
110
+ begin
111
+ wmi = WIN32OLE.connect(cs)
112
+ rescue WIN32OLERuntimeError => e
113
+ raise Error, e
114
+ else
115
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |ole|
116
+ return ole.Caption
117
+ }
118
+ end
119
+ end
120
+
121
+ # Returns the nodename. This is usually, but not necessarily, the
122
+ # same as the system's hostname.
123
+ #
124
+ def self.nodename(host=Socket.gethostname)
125
+ cs = "winmgmts:{impersonationLevel=impersonate,(security)}"
126
+ cs << "//#{host}/root/cimv2"
127
+ begin
128
+ wmi = WIN32OLE.connect(cs)
129
+ rescue WIN32OLERuntimeError => e
130
+ raise Error, e
131
+ else
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 => e
150
+ raise Error, e
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
+ 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
+ wmi.InstancesOf("Win32_OperatingSystem").each{ |os|
393
+ return UnameStruct.new(
394
+ os.BootDevice,
395
+ os.BuildNumber,
396
+ os.BuildType,
397
+ os.Caption,
398
+ os.CodeSet,
399
+ os.CountryCode,
400
+ os.CreationClassName,
401
+ os.CSCreationClassName,
402
+ os.CSDVersion,
403
+ os.CSName,
404
+ os.CurrentTimeZone,
405
+ os.Debug,
406
+ os.Description,
407
+ os.Distributed,
408
+ os.ForegroundApplicationBoost,
409
+ self.convert(os.FreePhysicalMemory),
410
+ self.convert(os.FreeSpaceInPagingFiles),
411
+ self.convert(os.FreeVirtualMemory),
412
+ self.parse_ms_date(os.InstallDate),
413
+ self.parse_ms_date(os.LastBootUpTime),
414
+ self.parse_ms_date(os.LocalDateTime),
415
+ os.Locale,
416
+ os.Manufacturer,
417
+ os.MaxNumberOfProcesses,
418
+ self.convert(os.MaxProcessMemorySize),
419
+ os.Name,
420
+ os.NumberOfLicensedUsers,
421
+ os.NumberOfProcesses,
422
+ os.NumberOfUsers,
423
+ os.Organization,
424
+ os.OSLanguage,
425
+ os.OSProductSuite,
426
+ os.OSType,
427
+ os.OtherTypeDescription,
428
+ os.PlusProductID,
429
+ os.PlusVersionNumber,
430
+ os.Primary,
431
+ os.respond_to?(:QuantumLength) ? os.QuantumLength : nil,
432
+ os.respond_to?(:QuantumType) ? os.QuantumType : nil,
433
+ os.RegisteredUser,
434
+ os.SerialNumber,
435
+ os.ServicePackMajorVersion,
436
+ os.ServicePackMinorVersion,
437
+ self.convert(os.SizeStoredInPagingFiles),
438
+ os.Status,
439
+ os.SystemDevice,
440
+ os.SystemDirectory,
441
+ self.convert(os.TotalSwapSpaceSize),
442
+ self.convert(os.TotalVirtualMemorySize),
443
+ self.convert(os.TotalVisibleMemorySize),
444
+ os.Version,
445
+ os.WindowsDirectory
446
+ )
447
+ }
448
+ end
449
+ end
450
+
451
+ private
452
+
453
+ # Converts a string in the format '20040703074625.015625-360' into a
454
+ # Ruby Time object.
455
+ #
456
+ def self.parse_ms_date(str)
457
+ return if str.nil?
458
+ return Time.parse(str.split('.')[0])
459
+ end
460
+
461
+ # There is a bug in win32ole where uint64 types are returned as a
462
+ # String rather than a Fixnum/Bignum. This deals with that for now.
463
+ #
464
+ def self.convert(str)
465
+ return nil if str.nil? # Don't turn nil into 0
466
+ return str.to_i
467
+ end
468
+ end
469
+ end