Consider the simplest possible python setup.py cmd
:
from distutils.core import Command, setup
class Foo(Command):
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
pass
setup(
cmdclass={'cmd': Foo}
)
It does precisely nothing.
But suppose something goes wrong while we're doing nothing, how should the command return a non-zero exit code to the user?
Return values seem to be ignored; the documentation says only:
All terminal output and filesystem interaction should be done by run().
which seems relevant without being particularly helpful.
Perhaps we should infer to exit
:
class Foo(Command):
# ...
def run(self):
exit(1)
This works, of course.
But it's not clear to me that it's the right thing to do: if the command is run as a part of a longer process, or it's overriding a built-in command, presumably nothing further will execute.
We could raise a relevant exception directly instead, assuming it might be more likely to be well-handled, but then there's a nasty traceback when we exit - what if we've already logged something nicer ourselves?
class Foo(Command):
# ...
def run(self):
print('Oh noes!', file=sys.stderr)
exit(1)
Is this safe to do; is there a better alternative?