mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
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>
This commit is contained in:
parent
58f8ebf491
commit
4d7351ef07
@ -30,6 +30,7 @@ import fcntl
|
|||||||
import termios
|
import termios
|
||||||
import struct
|
import struct
|
||||||
import base64
|
import base64
|
||||||
|
import traceback
|
||||||
try:
|
try:
|
||||||
#pylint: disable=F0401
|
#pylint: disable=F0401
|
||||||
import default_encoding_utf8
|
import default_encoding_utf8
|
||||||
@ -875,15 +876,33 @@ class show_mappings(frontend.Command):
|
|||||||
|
|
||||||
|
|
||||||
class console(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()
|
has_output = tuple()
|
||||||
|
|
||||||
def run(self, **options):
|
def run(self, filename=None, **options):
|
||||||
code.interact(
|
local = dict(api=self.api)
|
||||||
'(Custom IPA interactive Python console)',
|
if filename:
|
||||||
local=dict(api=self.api)
|
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):
|
class show_api(frontend.Command):
|
||||||
|
Loading…
Reference in New Issue
Block a user