ps 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.irbrc ADDED
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
3
+ require 'ps'
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Rakefile CHANGED
@@ -1 +1,2 @@
1
1
  require "bundler/gem_tasks"
2
+
data/lib/ps.rb CHANGED
@@ -1,5 +1,69 @@
1
1
  require "ps/version"
2
2
 
3
- module Ps
4
- # Your code goes here...
3
+ module PS
4
+ extend self
5
+
6
+ ALL_OPTS = `ps -L`.chomp.split(/[\s\n]/).freeze
7
+ FORMAT_ALIASES = {
8
+ 'pcpu' => '%cpu',
9
+ 'pmem' => '%mem',
10
+ 'acflg' => 'acflag',
11
+ 'f' => 'flags',
12
+ 'group' => 'gid',
13
+ 'inblock' => 'inblk',
14
+ 'ni' => 'nice',
15
+ 'nsignals' => 'nsigs',
16
+ 'oublock' => 'oublk',
17
+ 'pending' => 'sig',
18
+ 'blocked' => 'sigmask',
19
+ 'stat' => 'stage',
20
+ 'cputime' => 'time',
21
+ 'usrpri' => 'upr',
22
+ 'putime' => 'utime',
23
+ 'vsize' => 'vsz'
24
+ }
25
+
26
+ def default_formatting
27
+ @default_formatting ||= ALL_OPTS
28
+ end
29
+ attr_writer :default_formatting
30
+
31
+ DEFAULT_FORMATTING = %w{pid ppid pgid rss vsz %mem %cpu ruser
32
+ user uid gid lstart state command}
33
+
34
+ def all opts={}
35
+ opts ||= {}
36
+ opts[:flag] ||= %w{A}
37
+ opts[:format] ||= DEFAULT_FORMATTING
38
+
39
+ c = Command.new(opts)
40
+ c.to_processes
41
+ end
42
+
43
+ def pid *pids
44
+ opts = pids.pop if pids.last.is_a?(Hash)
45
+ opts ||= {}
46
+ opts[:flag] ||= %w{A}
47
+ opts[:pid] = pids
48
+ opts[:format] ||= DEFAULT_FORMATTING
49
+
50
+ c = Command.new(opts)
51
+ c.to_processes
52
+ end
53
+ end
54
+
55
+ require 'ps/command'
56
+ require 'ps/process'
57
+ require 'ps/process_list'
58
+
59
+ def PS *args
60
+ case args[0]
61
+ when Regexp
62
+ procs = PS.all(args[1])
63
+ procs.select {|proc| proc.command =~ args[0]}
64
+ when Integer
65
+ PS.pid(args[0],args[1]).first
66
+ when Hash
67
+ PS.all(args[0])
68
+ end
5
69
  end
data/lib/ps/command.rb ADDED
@@ -0,0 +1,99 @@
1
+ module PS
2
+ class Command
3
+ ARR_ARGS = {
4
+ :format => '-o ',
5
+ :gid => '-G ',
6
+ :group => '-g ',
7
+ :uid => '-u ',
8
+ :pid => '-p ',
9
+ :tty => '-t ',
10
+ :user => '-U ',
11
+ :flag => '-'
12
+ }
13
+
14
+ attr_reader *ARR_ARGS.keys.collect {|key| key.to_s+'s'}
15
+ FLAG_LIST = 'AaCcEefhjlMmrSTvwXx'.split('').freeze
16
+
17
+ def initialize opts={}
18
+ ARR_ARGS.keys.each do |arg|
19
+ arr = opts[arg] || []
20
+ instance_variable_set "@#{arg}s", arr
21
+ end
22
+ end
23
+
24
+ def to_s
25
+ cmd = []
26
+
27
+ parse_formats!
28
+
29
+ ARR_ARGS.each do |var,trigger|
30
+ sep = var == :flags ? nil : ','
31
+ vals = instance_variable_get("@#{var}s").uniq
32
+ cmd << trigger+vals.join(sep) unless vals.empty?
33
+ end
34
+
35
+ "ps "<<cmd.join(' ')
36
+ end
37
+
38
+ def run!
39
+ `#{to_s}`.chomp
40
+ end
41
+
42
+ def regex
43
+ r = @last_ran_formats.inject("") do |reg,fmt|
44
+ reg << '\s*' << case fmt
45
+ when 'command='
46
+ '(.+)$'
47
+ when 'lstart='
48
+ '(\w{3}\s\w{3}\s\d{2}\s[\d\:]{8}\s\d{4})'
49
+ else
50
+ '([\w\,_\-\.]+)'
51
+ end
52
+ end
53
+
54
+ Regexp.new(r)
55
+ end
56
+
57
+ def to_hashes
58
+ reg = nil
59
+ run!.split("\n").collect do |line|
60
+ reg ||= regex
61
+ m = line.match(reg)
62
+ hsh = {}
63
+ @last_ran_formats.each_with_index do |val,i|
64
+ hsh[val.sub('=','')] = m[i+1].rstrip
65
+ end
66
+
67
+ hsh
68
+ end
69
+ end
70
+
71
+ def to_processes
72
+ to_hashes.inject(ProcessList.new) {|plist,hsh| plist << Process.new(hsh)}
73
+ end
74
+
75
+ private
76
+
77
+ def parse_formats!
78
+ @formats.each do |format|
79
+ unless DEFAULT_FORMATTING.include?(format)
80
+ warn("Untested formatting option: #{format}")
81
+ end
82
+ end
83
+
84
+ @formats = @formats.collect do |format|
85
+ format = FORMAT_ALIASES[format.sub('=','')] || format
86
+ format.sub(/(=)?$/,'=')
87
+ end.uniq
88
+
89
+ # ensure command is always at the end of the formats array
90
+ if @formats.delete('command=')
91
+ @formats.push('command=')
92
+ end
93
+
94
+ # prevent race conditions
95
+ @last_ran_formats = @formats
96
+ end
97
+
98
+ end
99
+ end
data/lib/ps/process.rb ADDED
@@ -0,0 +1,44 @@
1
+ module PS
2
+ class Process
3
+
4
+ ALL_OPTS.each do |opt|
5
+ define_method opt do
6
+ @data[opt]
7
+ end
8
+ end
9
+
10
+ FORMAT_ALIASES.each do |al,orig|
11
+ define_method al do
12
+ @data[orig]
13
+ end
14
+ end
15
+
16
+ def initialize data
17
+ @data = {}
18
+
19
+ data.each do |key,value|
20
+ @data[key] = case value
21
+ when /^\-?\d+$/
22
+ value.to_i
23
+ when /^\-?\d+\.\d+$/
24
+ value.to_f
25
+ else
26
+ value.strip
27
+ end
28
+ end
29
+ end
30
+
31
+ def include? value
32
+ @data.has_key?(value.to_s)
33
+ end
34
+
35
+ def has
36
+ @data.keys
37
+ end
38
+
39
+ def mem
40
+ rss/1024.0 if rss
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ module PS
2
+ class ProcessList < Array
3
+
4
+ end
5
+ end
data/lib/ps/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- module Ps
2
- VERSION = "0.0.1"
1
+ module PS
2
+ VERSION = "0.0.2"
3
3
  end
data/ps.gemspec CHANGED
@@ -4,7 +4,7 @@ require "ps/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "ps"
7
- s.version = Ps::VERSION
7
+ s.version = PS::VERSION
8
8
  s.authors = ["Tal Atlas"]
9
9
  s.email = ["me@tal.by"]
10
10
  s.homepage = "https://github.com/Talby/ps"
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe PS::Command do
4
+
5
+ it 'should remove header from format' do
6
+ c = PS::Command.new
7
+ c.formats << '%cpu'
8
+ c.formats << '%mem'
9
+ c.formats << 'command'
10
+ c.to_s.should include('-o %cpu=,%mem=')
11
+ c.to_s.should include('-o %cpu=,%mem=')
12
+ end
13
+ end
data/spec/ps_spec.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ describe PS do
4
+ # it "should get from regex" do
5
+ # PS(/core/)
6
+ # end
7
+
8
+ # it "should find based on port" do
9
+ # PS(':80')
10
+ # end
11
+
12
+ # it "should find based on pid" do
13
+ # PS(1234)
14
+ # end
15
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'ps'
4
+ require 'rspec'
data/test ADDED
@@ -0,0 +1,14 @@
1
+ %CPU %MEM ACFLG ARGS BLOCKED COMM CPU TIME ELAPSED F GID INBLK JOBC KTRACE KTRACEP LIM LOGIN LOGIN STARTED MAJFLT MINFLT MSGRCV MSGSND NI NIVCSW NSIGS NSWAP NVCSW WCHAN OUBLK P_RU PADDR PAGEIN PENDING PGID PID PPID PRI STIME UTIME RE RGID RGID RSS RUID RUSER SESS SL STARTED STAT STIME SVGID SVUID TDEV TPGID TSESS TSIZ TT TTY UCOMM UID UPR USER VSZ WCHAN WQ WQB WQL WQR XSTAT COMMAND
2
+ 0.0 0.0 2 -zsh 0 -zsh 0 0:00.10 05:11:55 4006 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Sun Nov 27 17:36:42 2011 - - - - 0 - - - - 0 - 0 ffffff8021c19200 0 0 40394 40394 40393 31 0:00.04 0:00.06 0 1052059314 1052059314 4100 1607571338 tatlas ffffff801464e080 0 5:36PM S 0:00.04 1052059314 1607571338 16/0 40486 ffffff801464e080 0 s000 ttys000 zsh 1607571338 0 tatlas 2438344 - - - - - 0 -zsh
3
+ 0.0 0.0 0 ssh pair@verdic.co 0 ssh 0 0:00.12 05:11:32 4006 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Sun Nov 27 17:37:05 2011 - - - - 0 - - - - 0 - 0 ffffff8023f24a80 0 0 40486 40486 40394 31 0:00.08 0:00.03 0 1052059314 1052059314 2336 1607571338 tatlas ffffff801464e080 0 5:37PM S+ 0:00.08 1052059314 1607571338 16/0 40486 ffffff801464e080 0 s000 ttys000 ssh 1607571338 0 tatlas 2435320 - - - - - 0 ssh pair@verdic.co
4
+ 0.0 0.0 2 -zsh 0 -zsh 0 0:01.81 04-08:04:58 4006 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Wed Nov 23 14:43:39 2011 - - - - 0 - - - - 0 - 0 ffffff8018b88100 0 0 16441 16441 16440 31 0:01.15 0:00.66 0 1052059314 1052059314 3872 1607571338 tatlas ffffff80172d3440 0 Wed02PM S+ 0:01.15 1052059314 1607571338 16/1 16441 ffffff80172d3440 0 s001 ttys001 zsh 1607571338 0 tatlas 2446596 - - - - - 0 -zsh
5
+ 0.0 0.1 2 -zsh 0 -zsh 0 0:01.00 04:04:48 4006 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Sun Nov 27 18:43:49 2011 - - - - 0 - - - - 0 - 0 ffffff80157b3cc0 0 0 43882 43882 43881 31 0:00.59 0:00.41 0 1052059314 1052059314 4680 1607571338 tatlas ffffff80172d3860 0 6:43PM S 0:00.59 1052059314 1607571338 16/2 54650 ffffff80172d3860 0 s002 ttys002 zsh 1607571338 0 tatlas 2447620 - - - - - 0 -zsh
6
+ 0.0 0.0 0 man ps 0 man 0 0:00.01 03:55 4006 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Sun Nov 27 22:44:42 2011 - - - - 0 - - - - 0 - 0 ffffff80199a0980 0 0 54650 54650 43882 31 0:00.01 0:00.00 0 1052059314 1052059314 600 1607571338 tatlas ffffff80172d3860 0 10:44PM S+ 0:00.01 1052059314 1607571338 16/2 54650 ffffff80172d3860 0 s002 ttys002 man 1607571338 0 tatlas 2434848 - - - - - 0 man ps
7
+ 0.0 0.0 0 sh -c (cd '/usr/share/man' && (echo ".ll 16.4i"; echo ".nr LL 16 0 sh 0 0:00.00 03:55 4006 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Sun Nov 27 22:44:42 2011 - - - - 0 - - - - 0 - 0 ffffff801560ab80 0 0 54650 54651 54650 31 0:00.00 0:00.00 0 1052059314 1052059314 840 1607571338 tatlas ffffff80172d3860 0 10:44PM S+ 0:00.00 1052059314 1607571338 16/2 54650 ffffff80172d3860 0 s002 ttys002 sh 1607571338 0 tatlas 2435488 - - - - - 0 sh -c (cd '/usr/share/man' && (echo ".ll 16.4i"; echo ".nr LL 16.4i"; /bin/cat '/usr/share/man/man1/ps.1') | /usr/bin/tbl | /usr/bin/groff -Wall -mtty-char -Tascii -mandoc -c | (less || true))
8
+ 0.0 0.0 1 sh -c (cd '/usr/share/man' && (echo ".ll 16.4i"; echo ".nr LL 16 0 sh 0 0:00.00 03:55 6 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Sun Nov 27 22:44:42 2011 - - - - 0 - - - - 0 - 0 ffffff8023f22000 0 0 54650 54652 54651 31 0:00.00 0:00.00 0 1052059314 1052059314 468 1607571338 tatlas ffffff80172d3860 0 10:44PM S+ 0:00.00 1052059314 1607571338 16/2 54650 ffffff80172d3860 0 s002 ttys002 sh 1607571338 0 tatlas 2435488 - - - - - 0 sh -c (cd '/usr/share/man' && (echo ".ll 16.4i"; echo ".nr LL 16.4i"; /bin/cat '/usr/share/man/man1/ps.1') | /usr/bin/tbl | /usr/bin/groff -Wall -mtty-char -Tascii -mandoc -c | (less || true))
9
+ 0.0 0.0 1 sh -c (cd '/usr/share/man' && (echo ".ll 16.4i"; echo ".nr LL 16 0 sh 0 0:00.00 03:55 6 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Sun Nov 27 22:44:42 2011 - - - - 0 - - - - 0 - 0 ffffff80199a1a80 0 0 54650 54656 54652 31 0:00.00 0:00.00 0 1052059314 1052059314 416 1607571338 tatlas ffffff80172d3860 0 10:44PM S+ 0:00.00 1052059314 1607571338 16/2 54650 ffffff80172d3860 0 s002 ttys002 sh 1607571338 0 tatlas 2435488 - - - - - 0 sh -c (cd '/usr/share/man' && (echo ".ll 16.4i"; echo ".nr LL 16.4i"; /bin/cat '/usr/share/man/man1/ps.1') | /usr/bin/tbl | /usr/bin/groff -Wall -mtty-char -Tascii -mandoc -c | (less || true))
10
+ 0.0 0.0 0 less 0 less 0 0:00.01 03:55 4006 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Sun Nov 27 22:44:42 2011 - - - - 0 - - - - 0 - 0 ffffff8019ce7980 0 0 54650 54658 54656 31 0:00.00 0:00.01 0 1052059314 1052059314 628 1607571338 tatlas ffffff80172d3860 0 10:44PM S+ 0:00.00 1052059314 1607571338 16/2 54650 ffffff80172d3860 0 s002 ttys002 less 1607571338 0 tatlas 2434920 - - - - - 0 less
11
+ 0.0 0.0 2 -zsh 0 -zsh 0 0:00.38 01:18:38 4006 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Sun Nov 27 21:29:59 2011 - - - - 0 - - - - 0 - 0 ffffff8023f22cc0 0 0 50492 50492 50491 31 0:00.22 0:00.15 0 1052059314 1052059314 4124 1607571338 tatlas ffffff801464d160 0 9:29PM S 0:00.22 1052059314 1607571338 16/3 54632 ffffff801464d160 0 s003 ttys003 zsh 1607571338 0 tatlas 2446536 - - - - - 0 -zsh
12
+ 0.0 0.3 0 irb 0 irb 0 0:00.33 06:11 4006 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Sun Nov 27 22:42:26 2011 - - - - 0 - - - - 0 - 0 ffffff80199a1640 0 0 54632 54632 50492 31 0:00.04 0:00.29 0 1052059314 1052059314 27652 1607571338 tatlas ffffff801464d160 0 10:42PM S+ 0:00.04 1052059314 1607571338 16/3 54632 ffffff801464d160 0 s003 ttys003 ruby 1607571338 0 tatlas 2454452 - - - - - 0 irb
13
+ 0.0 0.0 0 sh -c ps -o %cpu,%mem,acflag,args,sigmask,caught,comm,cpu,time,e 0 sh 0 0:00.00 00:00 4006 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Sun Nov 27 22:48:37 2011 - - - - 0 - - - - 0 - 0 ffffff801ca87a80 0 0 54632 54795 54632 31 0:00.00 0:00.00 0 1052059314 1052059314 872 1607571338 tatlas ffffff801464d160 0 10:48PM S+ 0:00.00 1052059314 1607571338 16/3 54632 ffffff801464d160 0 s003 ttys003 sh 1607571338 0 tatlas 2435488 - - - - - 0 sh -c ps -o %cpu,%mem,acflag,args,sigmask,caught,comm,cpu,time,etime,flags,gid,ignored,inblk,jobc,ktrace,ktracep,lim,login,logname,lstart,majflt,minflt,msgrcv,msgsnd,nice,nivcsw,nsigs,nswap,nvcsw,nwchan,oublk,p_ru,paddr,pagein,sig,pgid,pid,ppid,pri,pstime,utime,re,rgid,rgroup,rss,ruid,ruser,sess,sl,start,stage,state,stime,svgid,svuid,tdev,tpgid,tsess,tsiz,tt,tty,ucomm,uid,upr,user,vsz,wchan,wq,wqb,wql,wqr,xstat,command > test
14
+ 0.0 0.0 2 -zsh 0 -zsh 0 0:00.10 03:15 4006 1052059314 - 1 0 0 - UNKNOWN UNKNOWN Sun Nov 27 22:45:22 2011 - - - - 0 - - - - 0 - 0 ffffff8017c37300 0 0 54668 54668 54667 31 0:00.04 0:00.06 0 1052059314 1052059314 4068 1607571338 tatlas ffffff8014651d00 0 10:45PM S+ 0:00.04 1052059314 1607571338 16/4 54668 ffffff8014651d00 0 s004 ttys004 zsh 1607571338 0 tatlas 2438344 - - - - - 0 -zsh
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ps
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tal Atlas
@@ -42,11 +42,20 @@ extra_rdoc_files: []
42
42
 
43
43
  files:
44
44
  - .gitignore
45
+ - .irbrc
46
+ - .rspec
45
47
  - Gemfile
46
48
  - Rakefile
47
49
  - lib/ps.rb
50
+ - lib/ps/command.rb
51
+ - lib/ps/process.rb
52
+ - lib/ps/process_list.rb
48
53
  - lib/ps/version.rb
49
54
  - ps.gemspec
55
+ - spec/command_spec.rb
56
+ - spec/ps_spec.rb
57
+ - spec/spec_helper.rb
58
+ - test
50
59
  homepage: https://github.com/Talby/ps
51
60
  licenses: []
52
61
 
@@ -80,6 +89,8 @@ rubygems_version: 1.8.8
80
89
  signing_key:
81
90
  specification_version: 3
82
91
  summary: A ruby wrapper for the unix tool 'ps'
83
- test_files: []
84
-
92
+ test_files:
93
+ - spec/command_spec.rb
94
+ - spec/ps_spec.rb
95
+ - spec/spec_helper.rb
85
96
  has_rdoc: