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 <nils@redhat.com>
This commit is contained in:
Nils Philippsen 2019-11-21 10:55:28 +01:00
parent bce634b199
commit 8cfa616073
2 changed files with 24 additions and 9 deletions

View file

@ -401,6 +401,13 @@ def _is_retired(product, project):
return True return True
class ScriptExecError(RuntimeError):
def __init__(self, *args, **kwargs):
self.errorcode = kwargs.pop('errorcode', 1)
super().__init__(*args, **kwargs)
class DistgitBugzillaSync: class DistgitBugzillaSync:
def notify_users(self, errors): def notify_users(self, errors):
@ -587,8 +594,20 @@ class DistgitBugzillaSync:
return override_yaml.get('bugzilla_contact', {}) return override_yaml.get('bugzilla_contact', {})
return {} return {}
def main(self): @classmethod
"""The entrypoint to the script.""" 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 global envname, env, projects_dict
times = { times = {
"start": time.time(), "start": time.time(),
@ -604,8 +623,7 @@ class DistgitBugzillaSync:
if self.args.env in self.config['environments']: if self.args.env in self.config['environments']:
envname = self.args.env envname = self.args.env
else: else:
print(f"Invalid environment specified: {self.args.env}") raise ScriptExecError(f"Invalid environment specified: {self.args.env}")
return 1
self.env = self.config['environments'][envname] self.env = self.config['environments'][envname]
@ -736,9 +754,6 @@ class DistgitBugzillaSync:
delta = times["end"] - times["start"] delta = times["end"] - times["start"]
print(f" Ran on {delta:.2f} seconds -- ie: {delta/60:.2f} minutes") print(f" Ran on {delta:.2f} seconds -- ie: {delta/60:.2f} minutes")
return 0
if __name__ == '__main__': if __name__ == '__main__':
dbs = DistgitBugzillaSync() DistgitBugzillaSync.main()
sys.exit(dbs.main())

View file

@ -40,7 +40,7 @@ setup(
tests_require=TESTS_REQUIRE, tests_require=TESTS_REQUIRE,
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'distgit-bugzilla-sync = distgit_bugzilla_sync.script:main', 'distgit-bugzilla-sync = distgit_bugzilla_sync.script:DistgitBugzillaSync.main',
], ],
}, },
) )