summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetr Viktorin <pviktori@redhat.com>2014-05-15 15:42:48 +0200
committerPetr Viktorin <pviktori@redhat.com>2014-05-22 18:22:57 +0200
commit4d7351ef070176283d7626ae2501a4ad7acc093e (patch)
tree304ba8989719cd69c3a386f1149cbd7183d8124e
parent58f8ebf49148172c6f3b1d22bcd7ea0fb3fb21c7 (diff)
downloadfreeipa-4d7351ef070176283d7626ae2501a4ad7acc093e.tar.gz
freeipa-4d7351ef070176283d7626ae2501a4ad7acc093e.tar.xz
freeipa-4d7351ef070176283d7626ae2501a4ad7acc093e.zip
ipalib.cli: Add filename argument to ipa console
This allows writing simple IPA scripts using the shebang #! /usr/bin/ipa console https://fedorahosted.org/freeipa/ticket/4351 Reviewed-By: Nathaniel McCallum <npmccallum@redhat.com>
-rw-r--r--ipalib/cli.py31
1 files changed, 25 insertions, 6 deletions
diff --git a/ipalib/cli.py b/ipalib/cli.py
index f03db9c61..ea47c7bb0 100644
--- a/ipalib/cli.py
+++ b/ipalib/cli.py
@@ -30,6 +30,7 @@ import fcntl
import termios
import struct
import base64
+import traceback
try:
#pylint: disable=F0401
import default_encoding_utf8
@@ -875,15 +876,33 @@ class show_mappings(frontend.Command):
class console(frontend.Command):
- """Start the IPA interactive Python console."""
+ """Start the IPA interactive Python console, or run a script.
+ An IPA API object is initialized and made available
+ in the `api` global variable.
+ """
+
+ takes_args = ('filename?',)
has_output = tuple()
- def run(self, **options):
- code.interact(
- '(Custom IPA interactive Python console)',
- local=dict(api=self.api)
- )
+ def run(self, filename=None, **options):
+ local = dict(api=self.api)
+ if filename:
+ try:
+ script = open(filename)
+ except IOError as e:
+ exit("%s: %s" % (e.filename, e.strerror))
+ try:
+ with script:
+ exec(script, globals(), local)
+ except:
+ traceback.print_exc()
+ exit(1)
+ else:
+ code.interact(
+ '(Custom IPA interactive Python console)',
+ local=local
+ )
class show_api(frontend.Command):