procps-rb 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9ea0f9d5b89c78686d361fa00b86aa3cb7b13c2e
4
- data.tar.gz: 9a589588aacb19ea074f429d9410d52acbd1f24f
3
+ metadata.gz: c976117d306543a0b1edc526541e00095305a4be
4
+ data.tar.gz: 3374e2058c9a1b07c867945bef45daaeb02d4173
5
5
  SHA512:
6
- metadata.gz: 8fdde5f8c2d50c6a8b719f16197c941a2fa9fbd851124008ee228bc8bb29f8d1def36c4ed6db1ca5287638415cc585eb55b3453db5db24143f7b9b9e741b3285
7
- data.tar.gz: bb29ed35cfc8e7133f75e6845e2978c5dbed4b7c75a021fce8206cf6b502596081086445541a654cc11a75b49c62565822f75137608cb95e7971358e5f1fed11
6
+ metadata.gz: d7845dbf401dd46514182ecf18e3b7b7b5ff76ad32a2db33456a8cff96d60b5012bd29532c1179cdc9a49b2344894b36aafa8eb631ae0b16d7b381c9725f66b7
7
+ data.tar.gz: 310656840e0238caf054e823959f5249b242ff1f406581b663a9ca97411bc384c1d3fb00da939c24dad9e242ca664285ae37e23f1e2498e54686fb829ddb0530
data/lib/procps/column.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  module Procps
2
+ # Creates a column object with a typecasting for ps results.
2
3
  class Column
3
4
  attr_reader :header
4
5
  alias :to_s :header
@@ -10,6 +11,7 @@ module Procps
10
11
  freeze
11
12
  end
12
13
 
14
+ # Typecasts a raw value
13
15
  def call(value)
14
16
  @cast.nil? ? value : @cast.call(value)
15
17
  rescue => e
@@ -17,6 +19,7 @@ module Procps
17
19
  value
18
20
  end
19
21
 
22
+ # The abstract class for complex column objects.
20
23
  class Type
21
24
  attr_reader :original
22
25
  alias :to_s :original
@@ -1,6 +1,7 @@
1
1
  require 'procps/column'
2
2
 
3
3
  module Procps
4
+ # The column type for a +command+ column.
4
5
  class Command < Column::Type
5
6
  attr_reader :name, :arguments, :title
6
7
  alias :to_s :name
@@ -1,6 +1,7 @@
1
1
  require 'procps/column'
2
2
 
3
3
  module Procps
4
+ # The column type for a +memsize+ column.
4
5
  class Memsize < DelegateClass(Integer)
5
6
  def self.call(value)
6
7
  new(value)
@@ -1,6 +1,7 @@
1
1
  require 'procps/column'
2
2
 
3
3
  module Procps
4
+ # The column type for a +policy+ column.
4
5
  class SchedulingPolicy < Column::Type
5
6
  attr_reader :value
6
7
  alias :to_sym :value
data/lib/procps/ps.rb CHANGED
@@ -8,22 +8,37 @@ module Procps
8
8
 
9
9
  attr_accessor :bin_path, :options, :modifiers
10
10
 
11
+ # Creates a Procps::PS object. Takes an argument with a bin path to ps command.
11
12
  def initialize(bin_path = nil)
12
13
  @bin_path = bin_path || DEFAULT_BIN_PATH
13
14
  @options = { o: [] }
14
15
  @modifiers = Set.new
15
16
  end
16
17
 
17
- def sum
18
- @modifiers << "S"
19
- self
20
- end
21
-
18
+ # Select columns to list with ps command
22
19
  def select(*columns)
23
- @options[:o].concat(columns)
20
+ columns.each do |col|
21
+ unless @@columns.include?(col)
22
+ raise ArgumentError, "unknown column :#{col}, please add it manually to Procps::PS.columns."
23
+ end
24
+
25
+ @options[:o] << col
26
+ end
24
27
  self
25
28
  end
26
29
 
30
+ # Filter processes list with conditions
31
+ #
32
+ # Available options:
33
+ # * <tt>:command</tt>
34
+ # * <tt>:group</tt>
35
+ # * <tt>:user</tt>
36
+ # * <tt>:pid</tt>
37
+ # * <tt>:ppid</tt>
38
+ # * <tt>:sid</tt>
39
+ # * <tt>:tty</tt>
40
+ # * <tt>:real_group</tt>
41
+ # * <tt>:real_user</tt>
27
42
  def where(
28
43
  command: nil,
29
44
  group: nil,
@@ -47,36 +62,56 @@ module Procps
47
62
  self
48
63
  end
49
64
 
65
+ # Sum a CPU time for parent proccesses
66
+ def sum
67
+ @modifiers << "S"
68
+ self
69
+ end
70
+
71
+ # Limit processes list size
50
72
  def limit(n)
51
73
  @limit = n
52
74
  self
53
75
  end
54
76
 
77
+ # Limit processes list size & get result
55
78
  def take(n = 1)
56
79
  limit(n).to_a
57
80
  end
58
81
 
82
+ # Takes a hash of options to set custom ps arguments.
83
+ #
84
+ # Example:
85
+ # Procps::PS.new.select(:pid, :rss).with_args(m: true).to_a
59
86
  def with_args(**args)
60
87
  @options.merge!(args)
61
88
  self
62
89
  end
63
90
 
91
+ # Set sorting option. Doesn't supported by an original OSX ps (use with_args() method instead).
92
+ #
93
+ # Example:
94
+ # Procps::PS.new.select(:pid, :rss).sort("ppid", "-rss").to_a
64
95
  def sort(*orders)
65
96
  (@options[:sort] ||= []).concat(orders)
66
97
  self
67
98
  end
68
99
 
100
+ # Reset a result
69
101
  def reset
70
102
  @result = nil
71
103
  self
72
104
  end
73
105
 
74
- def load
106
+ # Executes a ps command & sets a result.
107
+ def load(force = false)
108
+ reset if force
75
109
  @result ||= exec_command
76
110
  end
77
111
 
78
112
  alias :to_a :load
79
113
 
114
+ # List requested column objects with a typecast.
80
115
  def columns
81
116
  @columns ||= @options[:o].map(&@@columns)
82
117
  end
@@ -8,7 +8,6 @@ module Procps
8
8
  Address = -> (base = 10, null: "-".freeze) { -> (v) { v.is_a?(String) ? v == null ? nil : v.to_i(base) : v } }
9
9
  Address_10 = Address[]
10
10
  Address_16 = Address[16]
11
- # KiB = -> (v) { v.to_i * 1024 }
12
11
 
13
12
  @@columns = {
14
13
  :blocked => Column.new("BLOCKED"),
@@ -2,6 +2,7 @@ require 'shellwords'
2
2
 
3
3
  module Procps
4
4
  class PS
5
+ # Builds complete +ps+ shell command before execution.
5
6
  class CommandBuilder
6
7
  def initialize(ps)
7
8
  @ps = ps
@@ -1,3 +1,3 @@
1
1
  module Procps
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: procps-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Semenov