diff --git a/molecule.py b/molecule.py index be42a39..78b47da 100644 --- a/molecule.py +++ b/molecule.py @@ -25,29 +25,20 @@ sys.path.insert(0,'molecule/') sys.path.insert(0,'.') import molecule.cmdline from molecule.handlers import Runner -from molecule.utils import RUNNING_PIDS, is_super_user -from molecule.i18n import _ +from molecule.utils import RUNNING_PIDS def kill_pids(): for pid in RUNNING_PIDS: os.kill(pid, signal.SIGTERM) -molecule_data, molecule_data_order = molecule.cmdline.parse() +parse_data = molecule.cmdline.parse() +if parse_data is None: + raise SystemExit(1) +molecule_data, molecule_data_order = parse_data if not molecule_data_order: molecule.cmdline.print_help() raise SystemExit(1) -super_user = is_super_user() -for el in molecule_data_order: - spec_data = molecule_data.get(el) - if spec_data is None: - # wtf - continue - super_user_required = spec_data['__plugin__'].require_super_user() - if super_user_required and (not super_user): - sys.stderr.write("%s: %s\n" % (el, _("required super user access"),)) - raise SystemExit(1) - for el in molecule_data_order: my = Runner(el, molecule_data.get(el)) try: diff --git a/molecule/cmdline.py b/molecule/cmdline.py index 1dabac4..ecdc861 100644 --- a/molecule/cmdline.py +++ b/molecule/cmdline.py @@ -25,6 +25,12 @@ from molecule.settings import SpecParser, Configuration def parse(): + """ + Parse .spec files passed in sys.argv and returns a tuple composed by + a dict (key=spec file, value=metadata) and a list (spec file order). + Can return None if an error occurs. + """ + args_to_remove = ["--nocolor"] data = {} @@ -39,6 +45,16 @@ def parse(): if arg in myargs: myargs.remove(arg) + def check_super_user(el_data): + # check is super user is required + su_required = el_data['__plugin__'].require_super_user() + if su_required and (not super_user): + molecule.output.print_error("%s: %s" % (el, + _("required super user access"),)) + return False + return True + + super_user = molecule.utils.is_super_user() data_order = [] for el in myargs: if os.path.isfile(el) and os.access(el, os.R_OK): @@ -46,6 +62,9 @@ def parse(): el_data = obj.parse() del obj if el_data: + good = check_super_user(el_data) + if not good: + return None data_order.append(el) data[el] = el_data return data, data_order