Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'test,': 0.04; 'value,': 0.04; '"""': 0.07; 'importerror:': 0.07; 'type,': 0.07; 'python': 0.08; 'disable': 0.09; 'imported.': 0.09; 'setup.py': 0.09; 'def': 0.13; 'projects,': 0.15; 'tries': 0.15; '"""this': 0.16; 'egg': 0.16; 'loaded.': 0.16; 'runner': 0.16; 'setuptools': 0.16; 'subject:auto': 0.16; 'subject:import': 0.16; 'integrate': 0.18; 'tests.': 0.23; 'tests': 0.25; 'somewhere': 0.25; 'module': 0.26; 'import': 0.27; "i'm": 0.27; 'raise': 0.28; 'filter': 0.28; 'problem': 0.29; 'skip:p 30': 0.29; 'class': 0.29; 'environment.': 0.30; 'error': 0.30; 'anyone': 0.31; 'modules': 0.32; 'thanks': 0.32; 'message-id:@gmail.com': 0.33; 'header:User- Agent:1': 0.33; 'to:addr:python-list': 0.33; 'received:209.85.212': 0.34; 'flag': 0.34; 'try:': 0.34; 'something': 0.35; 'test': 0.35; 'project': 0.35; 'run': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'skip:_ 10': 0.37; 'received:209.85': 0.38; 'created': 0.38; 'should': 0.38; 'first.': 0.38; 'except': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.40; 'enable': 0.62; 'auto': 0.63; 'harder': 0.64; 'here': 0.64; 'hook': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=MhQvyXHzOyK8khn7kjkN6ZUTtS2vrCnmzo+CY0TwEK0=; b=QljRV/EB0XxR9x8Cuz0Btb0YpByH3ojAFvZZVTgNG5iyUOab0aHt42KooM112YNyjt WI2BgrQr7yQ/tHGcYQGHDcCQFRZPeORr5gJHNS3InKHL9FNm8MWJGLp8SLtrkHwMFV2j XOuqJtkGla1b5FX/aWrR4vQipyE/wR/mKu5Ss= Date: Thu, 19 Jan 2012 13:12:22 +0000 From: Andrea Crotti User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:9.0) Gecko/20111224 Thunderbird/9.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: multiversion flag and auto requiring import hook Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1326978756 news.xs4all.nl 6841 [2001:888:2000:d::a6]:60073 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19127 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---