linux_stat 1.1.0 → 1.2.3

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.
@@ -6,10 +6,17 @@ module LinuxStat
6
6
  #
7
7
  # The return type is an Array of Integers.
8
8
  def list
9
- Dir['/proc/*'].select { |x|
10
- pid = File.split(x)[1]
11
- pid.to_i.to_s == pid
12
- }.map! { |x| File.split(x)[-1].to_i }
9
+ d = Dir['/proc/*'.freeze]
10
+ ret, i = [], -1
11
+ count = d.length
12
+
13
+ while(i += 1) < count
14
+ pid = File.split(d[i])[1]
15
+ pid_i = pid.to_i
16
+ ret << pid_i if pid_i.to_s == pid
17
+ end
18
+
19
+ ret
13
20
  end
14
21
 
15
22
  ##
@@ -17,28 +24,42 @@ module LinuxStat
17
24
  #
18
25
  # The return type is Integer.
19
26
  def count
20
- list.count
27
+ list.length
21
28
  end
22
29
 
23
30
  ##
24
31
  # Returns all the id of processes mapped with their names as a Hash.
25
32
  def names
26
- list.reduce({}) { |h, x|
33
+ h, i = {}, -1
34
+
35
+ l = list
36
+ count = l.length
37
+
38
+ while(i += 1) < count
39
+ x = l[i]
40
+
27
41
  begin
28
- h.merge!( x => IO.foreach(File.join('/proc', x.to_s, 'status')).first.split[1] )
29
- rescue Exception
30
- h
42
+ h.merge!( x => IO.foreach("/proc/#{x}/status").first.split[1])
43
+ rescue StandardError
31
44
  end
32
- }
45
+ end
46
+ h
33
47
  end
34
48
 
35
49
  ##
36
50
  # Returns all the id of processes mapped with their status as a Hash.
37
51
  def types
38
- list.reduce({}) { |h, x|
52
+ h, i = {}, -1
53
+
54
+ l = list
55
+ count = l.length
56
+
57
+ while(i += 1) < count
58
+ x = l[i]
59
+
39
60
  begin
40
61
  h.merge!(x =>
41
- case IO.read(File.join('/proc', x.to_s, 'stat')).split[2]
62
+ case IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0]
42
63
  when ?S.freeze then :sleeping
43
64
  when ?I.freeze then :idle
44
65
  when ?Z.freeze then :zombie
@@ -46,10 +67,11 @@ module LinuxStat
46
67
  else :unknown
47
68
  end
48
69
  )
49
- rescue Exception
50
- h
70
+ rescue StandardError
51
71
  end
52
- }
72
+ end
73
+
74
+ h
53
75
  end
54
76
 
55
77
  ##
@@ -58,8 +80,8 @@ module LinuxStat
58
80
  def sleeping
59
81
  list.select { |x|
60
82
  begin
61
- IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?S
62
- rescue Exception
83
+ IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0] == ?S.freeze
84
+ rescue StandardError
63
85
  nil
64
86
  end
65
87
  }
@@ -71,8 +93,8 @@ module LinuxStat
71
93
  def idle
72
94
  list.select { |x|
73
95
  begin
74
- IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?I
75
- rescue Exception
96
+ IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0] == ?I.freeze
97
+ rescue StandardError
76
98
  nil
77
99
  end
78
100
  }
@@ -84,8 +106,8 @@ module LinuxStat
84
106
  def zombie
85
107
  list.select { |x|
86
108
  begin
87
- IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?Z
88
- rescue Exception
109
+ IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0] == ?Z.freeze
110
+ rescue StandardError
89
111
  nil
90
112
  end
91
113
  }
@@ -97,8 +119,8 @@ module LinuxStat
97
119
  def running
98
120
  list.select { |x|
99
121
  begin
100
- IO.read(File.join('/proc', x.to_s, 'stat')).split[2] == ?R
101
- rescue Exception
122
+ IO.foreach("/proc/#{x}/stat", ' '.freeze).first(3)[-1][0] == ?R.freeze
123
+ rescue StandardError
102
124
  nil
103
125
  end
104
126
  }
@@ -8,7 +8,7 @@ module LinuxStat
8
8
  def list
9
9
  return {} unless swaps_readable?
10
10
 
11
- file = IO.readlines('/proc/swaps').drop(1)
11
+ file = IO.readlines('/proc/swaps'.freeze).drop(1)
12
12
  file.reduce({}) do |h, x|
13
13
  name, *stats = x.strip.split
14
14
  h.merge!(name => stats.map! { |v| v.to_i.to_s == v ? v.to_i : v.to_sym })
@@ -20,7 +20,7 @@ module LinuxStat
20
20
  #
21
21
  # If the info isn't available, it will return an empty Hash.
22
22
  def any?
23
- !!IO.foreach('/proc/swaps').drop(1).first
23
+ !!IO.foreach('/proc/swaps'.freeze).first(2)[1]
24
24
  end
25
25
 
26
26
  # Show aggregated used and available swap.
@@ -114,7 +114,7 @@ module LinuxStat
114
114
  def read_usage
115
115
  return [[], []] unless swaps_readable?
116
116
 
117
- val = IO.readlines('/proc/swaps').drop(1)
117
+ val = IO.readlines('/proc/swaps'.freeze).drop(1)
118
118
  return [[], []] if val.empty?
119
119
 
120
120
  val.map! { |x|
@@ -123,7 +123,7 @@ module LinuxStat
123
123
  end
124
124
 
125
125
  def swaps_readable?
126
- @@swaps_readable ||= File.readable?('/proc/swaps')
126
+ @@swaps_readable ||= File.readable?('/proc/swaps'.freeze)
127
127
  end
128
128
  end
129
129
  end
@@ -49,73 +49,75 @@ module LinuxStat
49
49
  return [] unless @@sys_usb_readable
50
50
 
51
51
  Dir['/sys/bus/usb/devices/*/'.freeze].sort!.map! { |x|
52
- id_vendor_file = File.join(x, 'idVendor'.freeze)
53
- next unless File.readable?(id_vendor_file)
54
- id_vendor = IO.read(id_vendor_file).strip
52
+ begin
53
+ id_vendor_file = File.join(x, 'idVendor'.freeze)
54
+ next unless File.readable?(id_vendor_file)
55
+ id_vendor = IO.read(id_vendor_file).strip
55
56
 
56
- id_product_file = File.join(x, 'idProduct'.freeze)
57
- next unless File.readable?(id_vendor_file)
58
- id_product = IO.read(id_product_file).strip
57
+ id_product_file = File.join(x, 'idProduct'.freeze)
58
+ next unless File.readable?(id_vendor_file)
59
+ id_product = IO.read(id_product_file).strip
59
60
 
60
- bus_num_file = File.join(x, 'busnum'.freeze)
61
- bus_num = File.readable?(bus_num_file) ? IO.read(bus_num_file).strip : ''.freeze
61
+ bus_num_file = File.join(x, 'busnum'.freeze)
62
+ bus_num = File.readable?(bus_num_file) ? IO.read(bus_num_file).strip : ''.freeze
62
63
 
63
- dev_num_file = File.join(x, 'devnum'.freeze)
64
- dev_num = File.readable?(dev_num_file) ? IO.read(dev_num_file).strip : ''.freeze
64
+ dev_num_file = File.join(x, 'devnum'.freeze)
65
+ dev_num = File.readable?(dev_num_file) ? IO.read(dev_num_file).strip : ''.freeze
65
66
 
66
- serial_file = File.join(x, 'serial'.freeze)
67
- serial = File.readable?(serial_file) ? IO.read(serial_file).strip : ''.freeze
67
+ serial_file = File.join(x, 'serial'.freeze)
68
+ serial = File.readable?(serial_file) ? IO.read(serial_file).strip : ''.freeze
68
69
 
69
- product_file = File.join(x, 'product'.freeze)
70
- product = File.readable?(product_file) ? IO.read(product_file).strip : ''.freeze
70
+ product_file = File.join(x, 'product'.freeze)
71
+ product = File.readable?(product_file) ? IO.read(product_file).strip : ''.freeze
71
72
 
72
- manufacturer_file = File.join(x, 'manufacturer'.freeze)
73
- manufacturer = File.readable?(manufacturer_file) ? IO.read(manufacturer_file).strip : ''.freeze
73
+ manufacturer_file = File.join(x, 'manufacturer'.freeze)
74
+ manufacturer = File.readable?(manufacturer_file) ? IO.read(manufacturer_file).strip : ''.freeze
74
75
 
75
- removable_file = File.join(x, 'removable'.freeze)
76
- removable = File.readable?(removable_file) ? IO.read(removable_file).strip : ''.freeze
76
+ removable_file = File.join(x, 'removable'.freeze)
77
+ removable = File.readable?(removable_file) ? IO.read(removable_file).strip.downcase : ''.freeze
77
78
 
78
- authorized_file = File.join(x, 'authorized'.freeze)
79
- authorized = File.readable?(authorized_file) ? IO.read(authorized_file).to_i : ''.freeze
79
+ authorized_file = File.join(x, 'authorized'.freeze)
80
+ authorized = File.readable?(authorized_file) ? IO.read(authorized_file).to_i : ''.freeze
80
81
 
81
- b_max_power_file = File.join(x, 'bMaxPower'.freeze)
82
- b_max_power = File.readable?(b_max_power_file) ? IO.read(b_max_power_file).strip : ''.freeze
82
+ b_max_power_file = File.join(x, 'bMaxPower'.freeze)
83
+ b_max_power = File.readable?(b_max_power_file) ? IO.read(b_max_power_file).strip : ''.freeze
83
84
 
84
- b_max_packet_size0_file = File.join(x, 'bMaxPacketSize0'.freeze)
85
- b_max_packet_size0 = File.readable?(b_max_packet_size0_file) ? IO.read(b_max_packet_size0_file).to_i : ''.freeze
85
+ b_max_packet_size0_file = File.join(x, 'bMaxPacketSize0'.freeze)
86
+ b_max_packet_size0 = File.readable?(b_max_packet_size0_file) ? IO.read(b_max_packet_size0_file).to_i : ''.freeze
86
87
 
87
- query = hwdata ? query_hwdata(id_vendor, id_product) : {}
88
+ query = hwdata ? query_hwdata(id_vendor, id_product) : {}
88
89
 
89
- removable.downcase!
90
- is_removable = if removable == 'removable'.freeze
91
- true
92
- elsif removable == 'unknown'.freeze
93
- nil
94
- else
95
- false
96
- end
90
+ is_removable = if removable == 'removable'.freeze
91
+ true
92
+ elsif removable == 'unknown'.freeze
93
+ nil
94
+ else
95
+ false
96
+ end
97
97
 
98
- ret = {
99
- path: x, id: "#{id_vendor}:#{id_product}",
100
- vendor_id: id_vendor, product_id: id_product
101
- }
98
+ ret = {
99
+ path: x, id: "#{id_vendor}:#{id_product}",
100
+ vendor_id: id_vendor, product_id: id_product
101
+ }
102
102
 
103
- ret.merge!(bus_num: bus_num.to_i) unless bus_num.empty?
104
- ret.merge!(dev_num: dev_num.to_i) unless dev_num.empty?
103
+ ret.merge!(bus_num: bus_num.to_i) unless bus_num.empty?
104
+ ret.merge!(dev_num: dev_num.to_i) unless dev_num.empty?
105
105
 
106
- ret.merge!(serial: serial) unless serial.empty?
106
+ ret.merge!(serial: serial) unless serial.empty?
107
107
 
108
- ret.merge!(hwdata: query) unless query.empty?
109
- ret.merge!(product: product) unless product.empty?
110
- ret.merge!(manufacturer: manufacturer) unless manufacturer.empty?
108
+ ret.merge!(hwdata: query) unless query.empty?
109
+ ret.merge!(product: product) unless product.empty?
110
+ ret.merge!(manufacturer: manufacturer) unless manufacturer.empty?
111
111
 
112
- ret.merge!(removable: is_removable) unless is_removable.nil?
113
- ret.merge!(authorized: authorized == 1)
112
+ ret.merge!(removable: is_removable) unless is_removable.nil?
113
+ ret.merge!(authorized: authorized == 1)
114
114
 
115
- ret.merge!(b_max_power: b_max_power) unless b_max_power.empty?
116
- ret.merge!(b_max_packet_size0: b_max_packet_size0) if b_max_packet_size0
115
+ ret.merge!(b_max_power: b_max_power) unless b_max_power.empty?
116
+ ret.merge!(b_max_packet_size0: b_max_packet_size0) if b_max_packet_size0
117
117
 
118
- ret
118
+ ret
119
+ rescue StandardError
120
+ end
119
121
  }.tap(&:compact!)
120
122
  end
121
123
 
@@ -143,11 +145,63 @@ module LinuxStat
143
145
  }
144
146
  end
145
147
 
148
+ ##
149
+ # hwdata_file = file
150
+ #
151
+ # Lets you set the hwdata_file about usb.ids.
152
+ #
153
+ # The hwdata file about usb.ids contains vendor name and product name information about
154
+ # devices. This is then mapped by the other methods that utilizes hwdata/usb.ids.
155
+ #
156
+ # Do note that this method is intended to run only once, at the beginning.
157
+ # If you use any other method that utilizes hwdata/usb.ids, before
158
+ # calling this method, this method will not work.
159
+ def hwdata_file=(file)
160
+ @@hwdata_file ||= file.freeze
161
+ end
162
+
163
+ ##
164
+ # Checks if hwdata_file is already initialized or not.
165
+ # Once it's initialized, calling hwdata_file = 'something/usb.ids' is futile.
166
+ def hwdata_file_set?
167
+ @@hwdata_file ||= nil
168
+ !!@@hwdata_file
169
+ end
170
+
171
+ ##
172
+ # Returns the hwdata_file as string.
173
+ #
174
+ # If hwdata_file isn't set, it will return an empty frozen string.
175
+ #
176
+ # Once it's set, it can't be changed.
177
+ def hwdata_file
178
+ @@hwdata_file ||= nil
179
+ @@hwdata_file ? @@hwdata_file : ''.freeze
180
+ end
181
+
182
+ ##
183
+ # Initializes hwdata
184
+ #
185
+ # hwdata can take upto 0.1 to 0.2 seconds to get initialized.
186
+ #
187
+ # Calling this method will load hwdata for future use.
188
+ #
189
+ # Once it's initialized, hwdata_file can't be changed.
190
+ #
191
+ # If this method initializes hwdata, it will return true
192
+ # Othewise this method will return false.
193
+ def initialize_hwdata
194
+ @@hwdata ||= nil
195
+ init = !@@hwdata
196
+ hwdata
197
+ init
198
+ end
199
+
146
200
  alias count_devices count
147
201
 
148
202
  private
149
203
  def hwdata
150
- @@hwdata_file ||= "/usr/share/hwdata/usb.ids"
204
+ @@hwdata_file ||= "/usr/share/hwdata/usb.ids".freeze
151
205
 
152
206
  @@hwdata ||= if File.readable?(@@hwdata_file)
153
207
  file_data = IO.readlines(@@hwdata_file, encoding: 'ASCII-8BIT')
@@ -55,6 +55,7 @@ module LinuxStat
55
55
  # But if the status isn't available it will return an empty Hash.
56
56
  def gids
57
57
  return {} unless passwd_readable?
58
+
58
59
  passwd_splitted.reduce({}) { |h, x|
59
60
  h.merge!(x[0].to_sym => x[3].to_i)
60
61
  }
@@ -103,7 +104,7 @@ module LinuxStat
103
104
 
104
105
  uid, gid = LinuxStat::Sysconf.get_uid, LinuxStat::Sysconf.get_gid
105
106
 
106
- username = ''
107
+ username = ''.freeze
107
108
  passwd.each { |x|
108
109
  splitted = x.split(?:).freeze
109
110
  if splitted[2].to_i == uid && splitted[3].to_i == gid
@@ -187,7 +188,7 @@ module LinuxStat
187
188
  def username_by_gid(gid = get_gid)
188
189
  return ''.freeze unless passwd_readable?
189
190
 
190
- username = ''
191
+ username = ''.freeze
191
192
  passwd.each do |x|
192
193
  splitted = x.split(?:.freeze)
193
194
  if splitted[2].to_i == gid
@@ -314,7 +315,7 @@ module LinuxStat
314
315
  def home_by_gid(id = get_gid)
315
316
  return ''.freeze unless passwd_readable?
316
317
 
317
- home = ''
318
+ home = ''.freeze
318
319
  passwd.each do |x|
319
320
  splitted = x.split(?:.freeze)
320
321
 
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION ||= "1.1.0"
2
+ VERSION ||= "1.2.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-26 00:00:00.000000000 Z
11
+ date: 2020-12-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem