Created
January 28, 2012 14:22
-
-
Save fatbox/1694496 to your computer and use it in GitHub Desktop.
Load Django modules from within a Salt module
This file contains 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
""" | |
This allows you to import Django modules into a Salt module | |
""" | |
import logging | |
import sys | |
import os | |
log = logging.getLogger(__name__) | |
# point this at the virtualenv dir that your django deployment runs out of | |
# you are using virtualenv. right? | |
DJANGO_ENV = '/home/core/python-envs/production' | |
# where your code lives (ie where settings.py is) | |
DJANGO_DIR = '/home/core/code/production' | |
log.debug("Preparing Django modules...") | |
# add our site packages | |
import site | |
site_packages = os.path.join(DJANGO_ENV, 'lib', 'python%s' % sys.version[:3], 'site-packages') | |
site.addsitedir(site_packages) | |
# put the main Django directory on first | |
sys.path.insert(0, DJANGO_DIR) | |
# setup the Django environment pointing to our settings | |
import settings | |
import django.core.management | |
django.core.management.setup_environ(settings) | |
# finally, import our objects | |
from yourapp.models import YourModel |
Yeah, this could definitely use a refactor. Most of it was based on another snippet I found.
Done. Thanks for the motivation :)
s/Preapring/Preparing/ :)
Now you see why Tom loves me. This is really cool stuff man! It would be fantastic if you could do a writeup about it all somewhere public and maybe post about it on the salt-users mailinglist.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about
sys.path.insert(0, "/path/...")
instead of append and then creating a new list?