chdjango

 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
#!/usr/bin/env python
import getopt, os, sys

# The site-packages directory.
site_packages = os.path.join(os.path.dirname(os.__file__), 'site-packages')

# The path where your Django branches are.
django_path = os.path.join(os.environ['HOME'], 'django')

def usage():
    print """Usage: chdjango.py [-p <django_path>] <django_name>
  
  Where <django_name> is the directory of the Django distribution located
  within <django_path> (defaults to ~/django, but may be customized with
  the `-p` option).

  For example, if you nave `queryset-refactor` and `newforms-admin` in
  in `/opt/django`, then `chdjango.py -p /opt/django newforms-admin` will
  set newforms-admin to be the Django distribution used in Python.
"""

if __name__=='__main__':
    try:
        opt, args = getopt.getopt(sys.argv[1:], 'p:', ['path'])
    except Exception, msg:
        sys.stderr.write('%s\n' % msg)
        usage()
        sys.exit(1)

    if len(args) != 1:
        usage()
        sys.exit(2)

    # Seeing if the path option was specified.
    for o, a in opt:
        if o in ('-p', '--path'):
            django_path = a

    # Getting the new Django path, and making sure it exists.
    dj_pth = os.path.join(django_path, args[0])
    if not os.path.isdir(dj_pth):
        sys.stderr.write('ERROR: The Django path ("%s") does not exist.\n' % dj_pth)
        sys.exit(3)

    # Writing `django.pth` in the site-packages directory.
    fh = open(os.path.join(site_packages, 'django.pth'), 'w')
    fh.write('%s\n' % dj_pth)
    fh.close()

More like this

  1. Breadcrumbs for flatpages by jca 6 years, 4 months ago
  2. Dynamic Models Revisited by Ben 6 years, 6 months ago
  3. {% if %} tag with {% elif %} support by amagee 3 years, 5 months ago
  4. SelectDateWidget with format: day, month, year by zenx 4 years, 4 months ago
  5. update-django: Update your django git branches. by telenieko 5 years, 5 months ago

Comments

theju (on March 11, 2008):

Thanks for the snippet. It makes life super simple.

#

ihome (on March 17, 2008):

very useful.

#

andrews (on April 2, 2008):

Very good snippet.

#

(Forgotten your password?)