Class: After

Inherits:
Object
  • Object
show all
Defined in:
lib/after.rb

Class Method Summary collapse

Class Method Details

.find_and_wait_for(args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/after.rb', line 36

def self.find_and_wait_for(args)
  pids = find_pids args
  if pids.length > 1
    puts "found more than one -- waiting for all -- #{pids.map{|pid, name| name}.inspect}" if $VERBOSE
  end
  pids.each{|pid, name|
    puts "waiting for #{pid} (#{name.strip})" if $VERBOSE
    WaitPid.wait_nonchild_pid pid
  }
end

.find_pids(many_args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/after.rb', line 10

def self.find_pids(many_args)

  procs_by_pid = {}
  if OS.windows?
    procs = WMI::Win32_Process.find(:all)
    for proc in procs
      procs_by_pid[proc.ProcessId] = ("% 20s" % proc.Name.to_s) + ' ' + proc.CommandLine.to_s
    end
  else
    a = `ps -ef` # my linux support isn't very good yet...
    a.lines.to_a[1..-1].each{|l| pid = l.split(/\s+/)[1]; procs_by_pid[pid.to_i] = l}
  end

  good_pids = []
  for pid, description in procs_by_pid
    if description.contain?(many_args)
      next if pid == Process.pid
      good_pids << [pid, description]
      if $DISPLAY_ALL
       pps 'found', "% 5d" % pid, description
      end
    end
  end
  good_pids
end

.goObject

main, really



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/after.rb', line 53

def self.go
  if ARGV.delete('-q')
    $VERBOSE = false
  else
    $VERBOSE = true
  end

  $DISPLAY_ALL = false
  # TODO real command line parsing, yikes for this stuff!
  if ARGV[0].in? ['-l', '--list', '-ls']
    ARGV.shift
    $DISPLAY_ALL = true
    query = ARGV.shift || ''
    got = After.find_pids(query)
    if got.empty?
      puts "none found #{query}"
    end
    exit # early exit
  elsif ARGV[0] == '-p'
    ARGV.shift
    pids = ARGV.shift
    pids = pids.split(',')      
    puts "waiting for pids #{pids.join(',')}" if $VERBOSE and pids.length > 1
    for pid in pids
      puts "waiting for pid #{pid}" if $VERBOSE
      begin
        After.wait_pid pid.to_i
      rescue Errno::EPERM
        p 'pid does not exist maybe it already has exited ' + pid if $VERBOSE
      end
    end
  else
    After.find_and_wait_for(ARGV.shift)
  end

  puts 'after: now running:', ARGV if $VERBOSE
  system(*ARGV) if ARGV.length > 0
end

.wait_pid(pid) ⇒ Object



47
48
49
# File 'lib/after.rb', line 47

def self.wait_pid pid
  WaitPid.wait_nonchild_pid pid
end