From d50282fdcb6783e7a9d40c76f3efc47c2d2be4be Mon Sep 17 00:00:00 2001 From: Ask Solem Date: Mon, 26 Sep 2011 16:22:17 +0100 Subject: [PATCH] Parse metadata from dist module instead of importing it --- kombu/__init__.py | 88 ++++++++++++++++++++++++----------------------- setup.py | 51 ++++++++++++++++++++++----- 2 files changed, 87 insertions(+), 52 deletions(-) diff --git a/kombu/__init__.py b/kombu/__init__.py index 825c326f..19f44c7c 100644 --- a/kombu/__init__.py +++ b/kombu/__init__.py @@ -6,58 +6,60 @@ __contact__ = "ask@celeryproject.org" __homepage__ = "http://github.com/ask/kombu/" __docformat__ = "restructuredtext en" +# -eof meta- + import os import sys -if not os.environ.get("KOMBU_NO_EVAL", False): - # Lazy loading. - # - See werkzeug/__init__.py for the rationale behind this. - from types import ModuleType - all_by_module = { - "kombu.connection": ["BrokerConnection", "Connection"], - "kombu.entity": ["Exchange", "Queue"], - "kombu.messaging": ["Consumer", "Producer"], - "kombu.pools": ["connections", "producers"], - } +# Lazy loading. +# - See werkzeug/__init__.py for the rationale behind this. +from types import ModuleType - object_origins = {} - for module, items in all_by_module.iteritems(): - for item in items: - object_origins[item] = module +all_by_module = { + "kombu.connection": ["BrokerConnection", "Connection"], + "kombu.entity": ["Exchange", "Queue"], + "kombu.messaging": ["Consumer", "Producer"], + "kombu.pools": ["connections", "producers"], +} - class module(ModuleType): +object_origins = {} +for module, items in all_by_module.iteritems(): + for item in items: + object_origins[item] = module - def __getattr__(self, name): - if name in object_origins: - module = __import__(object_origins[name], None, None, [name]) - for extra_name in all_by_module[module.__name__]: - setattr(self, extra_name, getattr(module, extra_name)) - return getattr(module, name) - return ModuleType.__getattribute__(self, name) +class module(ModuleType): - def __dir__(self): - result = list(new_module.__all__) - result.extend(("__file__", "__path__", "__doc__", "__all__", - "__docformat__", "__name__", "__path__", "VERSION", - "__package__", "__version__", "__author__", - "__contact__", "__homepage__", "__docformat__")) - return result + def __getattr__(self, name): + if name in object_origins: + module = __import__(object_origins[name], None, None, [name]) + for extra_name in all_by_module[module.__name__]: + setattr(self, extra_name, getattr(module, extra_name)) + return getattr(module, name) + return ModuleType.__getattribute__(self, name) - # keep a reference to this module so that it's not garbage collected - old_module = sys.modules[__name__] + def __dir__(self): + result = list(new_module.__all__) + result.extend(("__file__", "__path__", "__doc__", "__all__", + "__docformat__", "__name__", "__path__", "VERSION", + "__package__", "__version__", "__author__", + "__contact__", "__homepage__", "__docformat__")) + return result - new_module = sys.modules[__name__] = module(__name__) - new_module.__dict__.update({ - "__file__": __file__, - "__path__": __path__, - "__doc__": __doc__, - "__all__": tuple(object_origins), - "__version__": __version__, - "__author__": __author__, - "__contact__": __contact__, - "__homepage__": __homepage__, - "__docformat__": __docformat__, - "VERSION": VERSION}) +# keep a reference to this module so that it's not garbage collected +old_module = sys.modules[__name__] + +new_module = sys.modules[__name__] = module(__name__) +new_module.__dict__.update({ + "__file__": __file__, + "__path__": __path__, + "__doc__": __doc__, + "__all__": tuple(object_origins), + "__version__": __version__, + "__author__": __author__, + "__contact__": __contact__, + "__homepage__": __homepage__, + "__docformat__": __docformat__, + "VERSION": VERSION}) if os.environ.get("KOMBU_LOG_DEBUG"): os.environ.update(KOMBU_LOG_CHANNEL="1", KOMBU_LOG_CONNECTION="1") diff --git a/setup.py b/setup.py index 43900746..7d6c27a0 100644 --- a/setup.py +++ b/setup.py @@ -24,10 +24,43 @@ except ImportError: from distutils.command.install import INSTALL_SCHEMES -os.environ["KOMBU_NO_EVAL"] = "yes" -import kombu -os.environ.pop("KOMBU_NO_EVAL", None) -sys.modules.pop("kombu", None) +# -- Parse meta +import re +re_meta = re.compile(r'__(\w+?)__\s*=\s*(.*)') +re_vers = re.compile(r'VERSION\s*=\s*\((.*?)\)') +re_doc = re.compile(r'^"""(.+?)"""') +here = os.path.abspath(os.path.dirname(__file__)) +meta_fh = open(os.path.join(here, "kombu/__init__.py")) +rq = lambda s: s.strip("\"'") + +def add_default(m): + attr_name, attr_value = m.groups() + return ((attr_name, rq(attr_value)), ) + + +def add_version(m): + v = list(map(rq, m.groups()[0].split(", "))) + return (("VERSION", ".".join(v[0:3]) + "".join(v[3:])), ) + + +def add_doc(m): + return (("doc", m.groups()[0]), ) + +pats = {re_meta: add_default, + re_vers: add_version, + re_doc: add_doc} +try: + meta = {} + for line in meta_fh: + if line.strip() == '# -eof meta-': + break + for pattern, handler in pats.items(): + m = pattern.match(line.strip()) + if m: + meta.update(handler(m)) +finally: + meta_fh.close() +# -- packages, data_files = [], [] root_dir = os.path.dirname(__file__) @@ -69,11 +102,11 @@ else: setup( name='kombu', - version=kombu.__version__, - description=kombu.__doc__, - author=kombu.__author__, - author_email=kombu.__contact__, - url=kombu.__homepage__, + version=meta["VERSION"], + description=meta["doc"], + author=meta["author"], + author_email=meta["contact"], + url=meta["homepage"], platforms=["any"], packages=packages, data_files=data_files,