Seamless Django and VirtualEnv integration

 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

  1. sqlallall.py by jkl 6 years, 9 months ago
  2. script for run a django task by tailorcai 6 years, 10 months ago
  3. Exclude Apps When Testing by cronosa 3 years, 6 months ago
  4. Table Creation Using ORM Standalone by EliAndrewC 6 years, 2 months ago
  5. showing environment variables in the django admin by tonemcd 4 years, 2 months ago

Comments

hans (on September 28, 2010):

Just wondering - Why did you not use the VE_ACTIVATE in your script?

#

kmmbvnr (on October 11, 2010):

@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

#

ka2n (on July 23, 2011):

Error on Mac

Traceback (most recent call last): File "./manage.py", line 37, in [HTML_REMOVED] go_to_ve()
File "./manage.py", line 24, in go_to_ve retcode = subprocess.call([python, file] + sys.argv[1:]) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 486, in call return Popen(popenargs, *kwargs).wait() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 672, in init errread, errwrite) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1112, in _execute_child self.pid = os.fork() OSError: [Errno 35] Resource temporarily unavailable

#

ka2n (on July 23, 2011):

.ev/bin/python

import sys

sys.prefix

'workspaceDIR/.ve/bin/./..'

#

(Forgotten your password?)