after 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/after.rb +22 -10
  3. data/spec/spec.after.rb +33 -29
  4. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.7.0
data/lib/after.rb CHANGED
@@ -1,24 +1,36 @@
1
- require 'ruby-wmi' # wow not even pretended linux compat. yet
1
+ require 'os'
2
+ if OS.windows?
3
+ require 'ruby-wmi' # wow not even pretended linux compat. yet
4
+ end
2
5
  require 'sane'
3
6
  require 'wait_pid'
4
7
 
5
8
  class After
6
9
 
7
10
  def self.find_pids(many_args)
8
- procs = WMI::Win32_Process.find(:all)
9
- #_dbg
10
- pids = []
11
- for proc in procs # TODO respect proc.Name!
12
- if (proc.CommandLine && proc.CommandLine.contain?(many_args)) || proc.Name.include?(many_args)
13
- pid = proc.ProcessId.to_i
11
+
12
+ procs_by_pid = {}
13
+ if OS.windows?
14
+ procs = WMI::Win32_Process.find(:all)
15
+ for proc in procs
16
+ procs_by_pid[proc.ProcessId] = proc.CommandLine.to_s + proc.Name.to_s
17
+ end
18
+ else
19
+ a = `ps -ef`
20
+ a.lines.to_a[1..-1].each{|l| pid = l.split(/\s+/)[1]; procs_by_pid[pid.to_i] = l}
21
+ end
22
+
23
+ good_pids = []
24
+ for pid, description in procs_by_pid
25
+ if description.contain?(many_args)
14
26
  next if pid == Process.pid
15
- pids << [pid, proc.CommandLine]
27
+ good_pids << [pid, description]
16
28
  if $VERBOSE
17
- print 'adding ', proc.ProcessId, ' ', proc.Name, ' ', proc.CommandLine, "\n"
29
+ pps 'adding', pid, description
18
30
  end
19
31
  end
20
32
  end
21
- pids
33
+ good_pids
22
34
  end
23
35
 
24
36
  def self.find_and_wait_for(args)
data/spec/spec.after.rb CHANGED
@@ -1,19 +1,21 @@
1
+ require 'rubygems'
1
2
  require 'spec/autorun'
2
3
  require 'sane'
3
4
  require_relative '../lib/after'
5
+ require 'backports' if RUBY_VERSION < '1.9'
4
6
 
5
7
  describe After do
6
8
 
7
9
  def go how_many = 1
8
- pid = Process.spawn "ruby ./sleep.rb #{how_many}"
9
- Thread.new { Process.wait pid } # wait for it, so we can collect child processes, too
10
- pid
10
+ pid = Process.spawn "ruby ./sleep.rb #{how_many}"
11
+ Thread.new { Process.wait pid } # wait for it, so we can collect child processes, too
12
+ pid
11
13
  end
12
14
 
13
15
  it "should be able to grab the right pid" do
14
- pid = go
15
- a = After.find_pids('sleep')
16
- a[0][0].should == pid
16
+ pid = go
17
+ a = After.find_pids('sleep')
18
+ a[0][0].should == pid
17
19
  end
18
20
 
19
21
  it "should immediately return if the other process doesn't exist" do
@@ -25,7 +27,7 @@ describe After do
25
27
  go
26
28
  start = Time.now
27
29
  After.find_and_wait_for('sleep')
28
- assert (Time.now - start) > 0.5
30
+ assert (Time.now - start) > 0.5
29
31
  end
30
32
 
31
33
  it "should work if there are several available" do
@@ -34,7 +36,7 @@ describe After do
34
36
  go 3
35
37
  start = Time.now
36
38
  After.find_and_wait_for('sleep')
37
- assert (Time.now - start) > 2
39
+ assert (Time.now - start) > 2
38
40
  end
39
41
 
40
42
  it "should not return the PID of this process" do
@@ -43,37 +45,39 @@ describe After do
43
45
  end
44
46
 
45
47
  it "should allow for passing in a pid" do
46
- pid = go 1
47
- After.wait_pid pid
48
+ pid = go 1
49
+ After.wait_pid pid
48
50
  end
49
51
 
50
- it "should find .bat filenames" do
51
- # unfortunately I don't know how to query for exact parameters, though...
52
- pid = Process.spawn "cmd /c sleep.bat 1"
53
- Thread.new { Process.wait pid } # wait for it, so we can collect child processes, too
54
- a = After.find_pids('sleep.bat')
55
- assert a[0][0] == pid && a.length == 1
56
- end
57
-
58
-
52
+ if OS.windows?
53
+ it "should find .bat filenames" do
54
+ # unfortunately I don't know how to query for exact parameters, though...
55
+ pid = Process.spawn "cmd /c sleep.bat 1"
56
+ Thread.new { Process.wait pid } # wait for it, so we can collect child processes, too
57
+ a = After.find_pids('sleep.bat')
58
+ assert a[0][0] == pid && a.length == 1
59
+ end
60
+ end
61
+
62
+
59
63
  it "should find .bat filenames when run by selves" do
60
- pending "knowing how to do this in windows" do
61
-
64
+ pending "knowing how to do this in windows" do
65
+
62
66
  # unfortunately I don't know how to query for exact parameters, though...
63
67
  pid = Process.spawn "sleep.bat 1"
64
68
  Thread.new { Process.wait pid } # wait for it, so we can collect child processes, too
65
69
  a = After.find_pids('sleep.bat')
66
70
  assert a[0][0] == pid && a.length == 1
67
71
  end
68
- end
69
-
70
- it "should find exe names too" do
71
- a = After.find_pids('csrss') # only searchable via name of csrss.exe
72
- assert a.length == 1
73
72
  end
74
73
 
74
+ if OS.windows?
75
+ it "should find exe names too" do
76
+ a = After.find_pids('csrss') # only searchable via name of csrss.exe
77
+ assert a.length == 1
78
+ end
79
+ end
80
+
75
81
  it "should split the commands up right and across name, too"
76
-
77
-
78
82
 
79
- end
83
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: after
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - rogerdpack
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-08 00:00:00 -07:00
12
+ date: 2010-01-12 00:00:00 -07:00
13
13
  default_executable: after
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency