annotate python/process.py @ 322:19b5bf66ba4e

example
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 08 Jun 2013 08:28:13 -0700
parents 748c232c0af3
children 581436742074
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
179
a5061b41a781 make flag optional
Jeff Hammel <jhammel@mozilla.com>
parents: 178
diff changeset
6 def ps(arg='axwww'):
180
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
7 """
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
8 python front-end to `ps`
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
9 http://en.wikipedia.org/wiki/Ps_%28Unix%29
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
10 returns a list of process dicts based on the `ps` header
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
11 """
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 retval = []
179
a5061b41a781 make flag optional
Jeff Hammel <jhammel@mozilla.com>
parents: 178
diff changeset
13 process = subprocess.Popen(['ps', arg], stdout=subprocess.PIPE)
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 stdout, _ = process.communicate()
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 header = None
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 for line in stdout.splitlines():
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 line = line.strip()
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 if header is None:
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 # first line is the header
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 header = line.split()
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 continue
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 split = line.split(None, len(header)-1)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 process_dict = dict(zip(header, split))
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 retval.append(process_dict)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 return retval
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26
183
748c232c0af3 update to process examiner
Jeff Hammel <jhammel@mozilla.com>
parents: 182
diff changeset
27 def running_processes(name, psarg='axwww', defunct=True):
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 """
182
f9863f733344 fix up a bit
Jeff Hammel <jhammel@mozilla.com>
parents: 180
diff changeset
29 returns a list of 2-tuples of running processes:
f9863f733344 fix up a bit
Jeff Hammel <jhammel@mozilla.com>
parents: 180
diff changeset
30 (pid, ['path/to/executable', 'args', '...'])
180
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
31 with the executable named `name`.
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
32 - defunct: whether to return defunct processes
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 """
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 retval = []
183
748c232c0af3 update to process examiner
Jeff Hammel <jhammel@mozilla.com>
parents: 182
diff changeset
35 for process in ps(psarg):
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 command = process['COMMAND']
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 command = shlex.split(command)
180
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
38 if command[-1] == '<defunct>':
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
39 command = command[:-1]
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
40 if not command or not defunct:
f52486ceadee handle defunct processes
Jeff Hammel <jhammel@mozilla.com>
parents: 179
diff changeset
41 continue
178
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 prog = command[0]
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 basename = os.path.basename(prog)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 if basename == name:
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 retval.append((int(process['PID']), command))
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 return retval
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 if __name__ == '__main__':
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 for arg in sys.argv[1:]:
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 processes = running_processes(arg)
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 for pid, command in processes:
fa94f0b4459d add python ps
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 print '%s %s : %s' % (pid, arg, command)