Reputation: 5732
I would like to know how to make some code in setup.py conditional on which command (e.g. install
or upload
) was run.
Specifically, I'd like to have:
install
, but no other commands.I have tried reading the distutils documentation, but it's pretty sparse on details – the distutils.command[.foo] modules are completely undocumented.
For the first point I can check sys.argv
like mentioned in this question, but that doesn't work when multiple commands are run, like:
python setup.py sdist bdist upload
so it isn't applicable in general.
Upvotes: 3
Views: 552
Reputation: 10727
You can override the command instead:
from distutils.command.install import install
from distutils.core import setup
def run_file(path):
with open(path, 'r') as f:
exec(f.read())
class myinstall(install): # subclass distutils's install command
def finalize_options(self): # called after option parsing
# call base class function
install.finalize_options(self)
# super won't work because distutils under Python 2 uses old-style classes
# ignore a module
self.distribution.py_modules.remove('mymodule')
def run(self): # called to run a command
# run tests first
run_file('path/to/test.py')
# ^ remember to make sure the module is in sys.path
# run the real commands
install.run(self)
setup(
name='abc',
py_modules=['mymodule'],
cmdclass={'install': myinstall}
# ^ override the install command
)
Upvotes: 2