Automatically activates the virtualenv when running manage.py command.
Just create requirements.pip file inside the root of django project and run ./manage.py update_ve in first time.
Code checks requirements.pip and virtual env timestamps and require to recreate outdated environment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #!/usr/bin/env python2.6
# -*- coding: utf-8 -*-
from os import path
import shutil, sys, virtualenv, subprocess
PROJECT_ROOT = path.abspath(path.dirname(__file__))
REQUIREMENTS = path.join(PROJECT_ROOT, 'requirements.pip')
VE_ROOT = path.join(PROJECT_ROOT, '.ve')
VE_TIMESTAMP = path.join(VE_ROOT, 'timestamp')
envtime = path.exists(VE_ROOT) and path.getmtime(VE_ROOT) or 0
envreqs = path.exists(VE_TIMESTAMP) and path.getmtime(VE_TIMESTAMP) or 0
envspec = path.getmtime(REQUIREMENTS)
def go_to_ve():
# going into ve
if not sys.prefix == VE_ROOT:
if sys.platform == 'win32':
python = path.join(VE_ROOT, 'Scripts', 'python.exe')
else:
python = path.join(VE_ROOT, 'bin', 'python')
retcode = subprocess.call([python, __file__] + sys.argv[1:])
sys.exit(retcode)
update_ve = 'update_ve' in sys.argv
if update_ve or envtime < envspec or envreqs < envspec:
if update_ve:
# install ve
if envtime < envspec:
if path.exists(VE_ROOT):
shutil.rmtree(VE_ROOT)
virtualenv.logger = virtualenv.Logger(consumers=[])
virtualenv.create_environment(VE_ROOT, site_packages=True)
go_to_ve()
# check requirements
if update_ve or envreqs < envspec:
import pip
pip.main(initial_args=['install', '-r', REQUIREMENTS])
file(VE_TIMESTAMP, 'w').close()
sys.exit(0)
else:
print "VirtualEnv need to be updated"
print "Run ./manage.py update_ve"
sys.exit(1)
go_to_ve()
# run django
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory")
sys.exit(1)
if __name__ == "__main__":
execute_manager(settings)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Just wondering - Why did you not use the VE_ACTIVATE in your script?
#
@hans just forget to remove.
It just another way to go to ve without spawning new python process, but i finish with using the same apprache as in pip
#
Error on Mac
#
.ev/bin/python
#
Please login first before commenting.