Login

chdjango

Author:
jbronn
Posted:
March 11, 2008
Language:
Python
Version:
.96
Score:
4 (after 4 ratings)

This is a convenient script for those working with different branches of Django. Place all of your branches in ~/django (e.g., ~/django/newforms-admin, ~/django/trunk) and you're ready to quickly change between them. For example: chdjango.py trunk.

 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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 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.

#

Please login first before commenting.