From 8cfa61607336ddb154933c11a591ae65fb910b4b Mon Sep 17 00:00:00 2001 From: Nils Philippsen Date: Nov 21 2019 09:55:28 +0000 Subject: fix console script entry point In the course, clean up how execution errors are processed, i.e. use exceptions rather than integer return codes to indicate errors. Signed-off-by: Nils Philippsen --- diff --git a/distgit_bugzilla_sync/script.py b/distgit_bugzilla_sync/script.py index 093d99b..ea53c2c 100644 --- a/distgit_bugzilla_sync/script.py +++ b/distgit_bugzilla_sync/script.py @@ -401,6 +401,13 @@ def _is_retired(product, project): return True +class ScriptExecError(RuntimeError): + + def __init__(self, *args, **kwargs): + self.errorcode = kwargs.pop('errorcode', 1) + super().__init__(*args, **kwargs) + + class DistgitBugzillaSync: def notify_users(self, errors): @@ -587,8 +594,20 @@ class DistgitBugzillaSync: return override_yaml.get('bugzilla_contact', {}) return {} - def main(self): - """The entrypoint to the script.""" + @classmethod + def main(cls): + """The entrypoint for running the script.""" + dbs = cls() + try: + dbs.run() + except ScriptExecError as e: + print(str(e), file=sys.stderr) + sys.exit(e.errorcode) + else: + sys.exit(0) + + def run(self): + """Run the script.""" global envname, env, projects_dict times = { "start": time.time(), @@ -604,8 +623,7 @@ class DistgitBugzillaSync: if self.args.env in self.config['environments']: envname = self.args.env else: - print(f"Invalid environment specified: {self.args.env}") - return 1 + raise ScriptExecError(f"Invalid environment specified: {self.args.env}") self.env = self.config['environments'][envname] @@ -736,9 +754,6 @@ class DistgitBugzillaSync: delta = times["end"] - times["start"] print(f" Ran on {delta:.2f} seconds -- ie: {delta/60:.2f} minutes") - return 0 - if __name__ == '__main__': - dbs = DistgitBugzillaSync() - sys.exit(dbs.main()) + DistgitBugzillaSync.main() diff --git a/setup.py b/setup.py index b1fd094..8c14918 100644 --- a/setup.py +++ b/setup.py @@ -40,7 +40,7 @@ setup( tests_require=TESTS_REQUIRE, entry_points={ 'console_scripts': [ - 'distgit-bugzilla-sync = distgit_bugzilla_sync.script:main', + 'distgit-bugzilla-sync = distgit_bugzilla_sync.script:DistgitBugzillaSync.main', ], }, )