sys-uname 1.2.1 → 1.2.3

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