multiversion flag and auto requiring import hook

A

Andrea Crotti

I'm using the multiversion flag in setuptools (-m) to be able to run
many different projects, without modifying the global environment.

Then thanks to pkg_resources magic and setuptools I can get
automatically everything loaded.

Now the problem is that we want to be able to run tests. The best way
would be to use python setup.py test, which would make everything needed
for the tests available.

The problem is that it's very slow to run and harder to integrate in
Eclipse, so I created an import hook, that tries to do a
pkg_resources.require first.

The test runner should do something like

with AutoRequire():
# run tests

But now I'm fighting with nose and plugins, and I'm not sure this
approach is really correct...

Anyone has a better idea??

Here is the import hook for the auto_import...

--8<---------------cut here---------------start------------->8---
class AutoRequireImport(object):
"""This require import automatically requires with pkg_resources
all the modules that need to be imported.
"""

def find_module(self, module_name, package=None):
#TODO: try to filter to only the important modules
mod_parent = module_name.split('.')[0]
try:
find_module(mod_parent)
except ImportError:
try:
pkg_resources.require(module_name)
except pkg_resources.DistributionNotFound:
logger.error("not able to require the module %s, you
need to run a related project or develop the egg with dev_main" %
mod_parent)
#TODO: raise the right error to be catched from
somewhere else?
else:
logger.info("%s required correctly" % mod_parent)


class AutoRequire(object):
"""Context manager to enable and disable the auto-require import hook
"""

def __init__(self):
self.ar = AutoRequireImport()

def __enter__(self):
logger.debug("enable the auto require import hook")
sys.meta_path.append(self.ar)
return self

def __exit__(self, type, value, traceback):
logger.debug("disable the auto require import hook")
sys.meta_path.remove(self.ar)
--8<---------------cut here---------------end--------------->8---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top