Created
September 14, 2009 11:07
-
-
Save ask/186603 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Publish and detect reusable app requirements. | |
Problem | |
------- | |
Django projects use a lot of external apps, some of these has dependencies | |
to other apps, the usual way of disclosing these dependencies is by writing | |
a note in the distributions README:: | |
This application depends on django-tagging. | |
To use x from your project add the following to settings.py:: | |
INSTALLED_APPS = ("tagging", | |
"x") | |
Regular python modules can list their dependencies in setup.py to have | |
them automatically resolved, wouldn't it be nice if we could do that with | |
INSTALLED_APPS requirements as well? | |
Proposed solution | |
----------------- | |
Every reusable app has a .app module, for application metadata. | |
This module lists the INSTALLED_APPS requirements in a variable | |
called "requirements": | |
>>> requirements = ["django.contrib.auth", "registration", "tagging"] | |
Then in your project settings you use :func`get_requirements` to | |
resolve all dependencies: | |
>>> INSTALLED_APPS = ("django.contrib.auth", | |
... "durian", | |
... "myapp1", | |
... "myapp2", | |
... ) | |
>>> from djangoapp.appmeta import get_requirements | |
>>> INSTALLED_APPS += get_requirements(INSTALLED_APPS) | |
""" | |
import sys | |
from collections import deque | |
def get_requirements(installed_apps): | |
requirements = set() | |
stream = deque(installed_apps) | |
while True: | |
if not stream: | |
break | |
next_app = stream.pop() | |
subreqs = get_app_requirements(next_app) | |
requirements |= subreqs | |
stream.extend(subreqs & requirements) | |
return tuple(requirements) | |
def get_app_requirements(app_name): | |
try: | |
__import__("%s.app" % app_name, {}, {}, ['']) | |
except ImportError: | |
return set() | |
app_mod = sys.modules["%s.app" % app_name] | |
return set(getattr(app_mod, "requirements", [])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment