annotate python/process.py @ 178:fa94f0b4459d

add python ps
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 08 Nov 2011 12:08:11 -0800
parents
children a5061b41a781
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 import os
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 import shlex
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 import subprocess
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 import sys
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 def ps():
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 retval = []
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 process = subprocess.Popen(['ps', 'axwww'], stdout=subprocess.PIPE)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 stdout, _ = process.communicate()
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 header = None
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 for line in stdout.splitlines():
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 line = line.strip()
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 if header is None:
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 # first line is the header
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 header = line.split()
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 continue
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 split = line.split(None, len(header)-1)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 process_dict = dict(zip(header, split))
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 retval.append(process_dict)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 return retval
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 def running_processes(name):
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 """
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 returns a list of
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 {'PID': PID of process (int)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 'command': command line of process (list)}
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 with the executable named `name`
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 """
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 retval = []
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 for process in ps():
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 command = process['COMMAND']
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 command = shlex.split(command)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 prog = command[0]
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 basename = os.path.basename(prog)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 if basename == name:
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 retval.append((int(process['PID']), command))
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 return retval
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 if __name__ == '__main__':
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 for arg in sys.argv[1:]:
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 processes = running_processes(arg)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 for pid, command in processes:
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 print '%s %s : %s' % (pid, arg, command)