ctdb-scripts: Improve update and listing code
[samba4-gss.git] / python / samba / netcmd / shell.py
blob54c4019df365c44634da2d2c9558f4b69d10a57c
1 # Unix SMB/CIFS implementation.
3 # Interactive Python shell for SAMBA
5 # Copyright (C) Catalyst.Net Ltd. 2023
7 # Written by Rob van der Linde <rob@catalyst.net.nz>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 import code
24 import readline
25 import rlcompleter
27 import ldb
29 import samba.getopt as options
30 from samba import version
31 from samba.domain.models import MODELS
32 from samba.netcmd import Command
35 class cmd_shell(Command):
36 """Open a SAMBA Python shell."""
38 synopsis = "%prog -H [options]"
40 takes_optiongroups = {
41 "sambaopts": options.SambaOptions,
42 "credopts": options.CredentialsOptions,
43 "hostopts": options.HostOptions,
46 def run(self, sambaopts=None, credopts=None, hostopts=None):
47 samdb = self.ldb_connect(hostopts, sambaopts, credopts)
49 context = globals()
50 context.update({
51 "samdb": samdb,
52 "ldb": ldb,
54 context.update({model.__name__: model for model in MODELS.values()})
56 banner = rf"""
57 _____ __ __ ____
58 / ____| /\ | \/ | _ \ /\
59 | (___ / \ | \ / | |_) | / \
60 \___ \ / /\ \ | |\/| | _ < / /\ \
61 ____) / ____ \| | | | |_) / ____ \
62 |_____/_/ \_\_| |_|____/_/ \_\
63 v{version}
65 Variables:
67 samdb = {samdb}
69 Models:
71 """
72 for name, model in MODELS.items():
73 banner += f"{model.__name__}: {name}\n"
75 readline.parse_and_bind("tab: complete")
76 readline.set_completer(rlcompleter.Completer(context).complete)
77 code.InteractiveConsole(locals=context).interact(banner=banner)