Saturday, July 9, 2011

Django terminal

I’m developing web-applications on Django with IDE but running Django-server and working in Django-shell and with a database I prefer in the terminal. To speed up the start of it, I wrote a script that launches gnome-terminal with four tabs that runs ./manage.py runserver, ./manage.py shell, ./manage.py dbshell и bash.

#!/bin/bash

###
#
# Run a django manage.py subcommands in a gnome-terminal tabs.
# To switch to a tab after terminal launch install xdotool.
#
###

BASH=/bin/bash
PYTHON=/usr/bin/python
GNOME_TERMINAL=/usr/bin/gnome-terminal
XDOTOOL=/usr/bin/xdotool

# config
WORK_DIR=/path/to/your/project/on/django
MANAGE_SUBCOMMANDS="runserver shell dbshell"
SWITCH_TO_TAB=2

${PYTHON} -c """
import subprocess

terminal = ['${GNOME_TERMINAL}']
for cmd in '${MANAGE_SUBCOMMANDS}'.split():
  terminal.extend(['--tab', '-e', '''
      ${BASH} -c '
          cd ${WORK_DIR}
          ./manage.py {cmd}
          read
      '
      '''.format(cmd=cmd)])

terminal.extend(['--tab', '-e', '''
  ${BASH} -c '
      cd ${WORK_DIR}
      [ -x ${XDOTOOL} ] && ${XDOTOOL} key alt+${SWITCH_TO_TAB}
      ${BASH}
      read
  '
  '''])

subprocess.call(terminal)
"""
To configure this script set the variables:
WORK_DIR: the path to your Django-project;
MANAGE_SUBCOMMANDS: manage.py commands to run in the tabs;
SWITCH_TO_TAB: switch to the tab after running the terminal (you need to install xdotool).