PEP8: fix E302: expected 2 blank lines, found 1
[garming/samba-autobuild/.git] / python / samba / netcmd / processes.py
1 #   Unix SMB/CIFS implementation.
2 #   List processes (to aid debugging on systems without setproctitle)
3 #   Copyright (C) 2010-2011 Jelmer Vernooij <jelmer@samba.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Testbed for loadparm.c/params.c
19 #
20 # This module simply loads a specified configuration file and
21 # if successful, dumps it's contents to stdout. Note that the
22 # operation is performed with DEBUGLEVEL at 3.
23 #
24 # Useful for a quick 'syntax check' of a configuration file.
25 #
26
27 import os
28 import sys
29
30 import samba
31 import samba.getopt as options
32 from samba.netcmd import Command, CommandError, Option
33 from samba.messaging import Messaging
34
35
36 class cmd_processes(Command):
37     """List processes (to aid debugging on systems without setproctitle)."""
38
39     synopsis = "%prog [options]"
40
41     takes_optiongroups = {
42         "sambaopts": options.SambaOptions,
43         "versionopts": options.VersionOptions
44     }
45
46     takes_options = [
47         Option("--name", type=str,
48                help="Return only processes associated with one particular name"),
49         Option("--pid", type=int,
50                help="Return only names assoicated with one particular PID"),
51     ]
52
53     takes_args = []
54
55     def run(self, sambaopts, versionopts, section_name=None,
56             name=None, pid=None):
57
58         lp = sambaopts.get_loadparm()
59         logger = self.get_logger("processes")
60
61         msg_ctx = Messaging()
62
63         if name is not None:
64             try:
65                 ids = msg_ctx.irpc_servers_byname(name)
66             except KeyError:
67                 ids = []
68
69             for server_id in ids:
70                 self.outf.write("%d\n" % server_id.pid)
71         elif pid is not None:
72             names = msg_ctx.irpc_all_servers()
73             for name in names:
74                 for server_id in name.ids:
75                     if server_id.pid == int(pid):
76                         self.outf.write("%s\n" % name.name)
77         else:
78             names = msg_ctx.irpc_all_servers()
79             self.outf.write(" Service:                PID \n")
80             self.outf.write("-----------------------------\n")
81             for name in names:
82                 for server_id in name.ids:
83                     self.outf.write("%-16s      %6d\n" % (name.name, server_id.pid))