From 23c5ffa5885db6413b1bf0d2516c9bd3c4d30a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Luttringer?= Date: Thu, 13 Jun 2013 14:29:11 +0200 Subject: [PATCH] Handle exit() in scripts Allow scripts to call exit() or exit(0) to interupt their execution properly. Can save a useless brace adn tab time. Exiting another value cause script to fail. --- installsystems/image.py | 8 +++++++- installsystems/template.py | 4 ++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/installsystems/image.py b/installsystems/image.py index b3bbdf6..580cc3c 100644 --- a/installsystems/image.py +++ b/installsystems/image.py @@ -223,8 +223,14 @@ class Image(object): # we need installsystems.printer to conserve arrow level sysmodules["installsystems.printer"] = installsystems.printer exec bytecode in global_dict + except SystemExit as e: + # skip a script which call exit(0) or exit() + if e.code is None or e.code == 0: + return + else: + raise ISError(u"Script %s exits with status" % path, e) except Exception as e: - raise ISError(u"Unable to execute script %s" % path, e) + raise ISError(u"Fail to execute script %s" % path, e) finally: sysmodules.clear() sysmodules.update(sysmodules_backup) diff --git a/installsystems/template.py b/installsystems/template.py index b9a3fe7..77bf6fd 100644 --- a/installsystems/template.py +++ b/installsystems/template.py @@ -49,6 +49,8 @@ parser = """# -*- python -*- # global image object is a reference to current image # global parser object is your installsystems subparser (argparse) +# you can use exit() to break the execution of the script + import os import argparse from installsystems.printer import arrow @@ -72,6 +74,8 @@ setup = u"""# -*- python -*- # global image object is a reference to current image # namespace object is the persistant, it can be used to store data accross scripts +# you can use exit() to break the execution of the script + from installsystems.printer import arrow arrow(u"hostname: %s" % namespace.hostname) -- GitLab