Skip to content

Instantly share code, notes, and snippets.

@wraithan
Created July 30, 2011 21:34
Show Gist options
  • Save wraithan/1116026 to your computer and use it in GitHub Desktop.
Save wraithan/1116026 to your computer and use it in GitHub Desktop.
from fabric.api import local, prefix, settings
from contextlib import contextmanager
remote_servers_tmux = 'aquameta'
class TmuxException(Exception):
pass
class Tmux(object):
def __init__(self, session_name, *args, **kwargs):
super(Tmux, self).__init__(*args, **kwargs)
self.session_name = session_name
with settings(warn_only=True):
test = local('tmux has-session -t %s' % self.session_name)
if test.failed:
local('tmux new-session -d -s %s' % self.session_name)
def new_window(self, num='', name='', command=''):
if num:
if self.has_window_numbered(num):
return False
num = ':%s' % num
if name:
if self.has_window_named(name):
return False
name = '-n %s' % name
if command:
command = '"%s"' % command
local('tmux new-window -t %s%s %s %s' % (self.session_name, num, name, command))
return True
def rename_window(self, num, name):
local('tmux rename-window -t %s:%s "%s"' % (self.session_name, num, name))
def send_keys(self, num, command):
local('tmux send-keys -t %s:%s "%s" ^M' % (self.session_name, num, command))
def has_window_named(self, name):
with settings(warn_only=True):
return local('tmux list-windows -t %s | grep -Z %s' % (self.session_name, name)).succeeded
def has_window_numbered(self, number):
with settings(warn_only=True):
return local('tmux list-windows -t %s | grep -Z ^%s:' % (self.session_name, number)).succeeded
def beehive():
beehive_tmux = Tmux('beehive')
if not beehive_tmux.has_window_named('bh_dev'):
if beehive_tmux.has_window_numbered(1):
beehive_tmux.rename_window(1, 'bh_dev')
else:
beehive_tmux.new_window(1, 'bh_dev')
beehive_tmux.send_keys(1, 've beehive; while true; do ./manage.py runserver 0.0.0.0:8000 --settings=env.bh_dev; done')
if beehive_tmux.new_window(2, 'w_dev'):
beehive_tmux.send_keys(2, 've beehive; while true; do ./manage.py runserver 0.0.0.0:8001 --settings=env.w_dev; done')
if beehive_tmux.new_window(3, 'bh_web'):
beehive_tmux.send_keys(3, 've beehive; while true; do ./manage.py runserver 0.0.0.0:8002 --settings=env.bh_web; done')
if beehive_tmux.new_window(4, 'shell plus'):
beehive_tmux.send_keys(4, 've beehive;./manage.py shell_plus')
if beehive_tmux.new_window(5):
beehive_tmux.send_keys(5, 've beehive'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment