Skip to content

Instantly share code, notes, and snippets.

@zodman
Created July 6, 2012 18:20
Show Gist options
  • Save zodman/3061793 to your computer and use it in GitHub Desktop.
Save zodman/3061793 to your computer and use it in GitHub Desktop.
"""
code: zodman
Fabric es una herramienta que uso desde mis proyectos para evitar realizar
tareas repetidas ( si lo hago mas de 2 veces hago un script).
Tiene una funcion especial que llama do_rebuild() esta funcion se encarga
de traers el ultimo log de svn y revisar palabras claves en el commit.
Dependiendo a esta palabra clave realiza una accion.
Esto es util ponerlo en un apphook.sh para realizar distintos comandos en soberbia
si necesidad de entrar.
requires:
fabric
lxml
"""
from fabric.api import local, settings, show,env, get, run, prompt, cd, lcd
from fabric.colors import blue,green
from lxml import etree
import datetime
import os
import imp
env.shell = "/bin/bash -l -c"
# django suppport
imp.find_module("settings")
import settings
VIRTUAL_ENV = os.environ["VIRTUAL_ENV"]
# custom functions
def commit_translate():
params = "-m 'update translate' --username andres.vargas --password &gHx-1aL --non-interactive "
local("svn commit conf/locale/es %s" % params)
local("svn commit conf/locale/en %s" % params)
def __remote_virtualenv(command):
source = '. %s/bin/activate && ' % VIRTUAL_ENV
run(source + command)
def __virtualenv(command):
source = '. %s/bin/activate && ' % VIRTUAL_ENV
local(source + command)
def mysqldump(directory= '.', name = None):
""" use: fab mysqldump:directory='/tmp',name='dump.sql' """
dir_destino = directory
db = settings.DATABASES["default"]
db["fecha"] ="%s" % datetime.datetime.now().strftime("%d%m%Y")
dir_destino = dir_destino if dir_destino.endswith("/") else dir_destino + "/"
if db["HOST"] == "":
db["HOST"] ="localhost"
db["dest"] = dir_destino + "%(HOST)s_%(NAME)s_%(fecha)s.sql" % db
if not name is None:
db["dest"] = dir_destino + name
command_str = "mysqldump -u %(USER)s -p%(PASSWORD)s -h %(HOST)s %(NAME)s > %(dest)s"
command = command_str % db
local(command)
print blue( "db in: %s" % db["dest"])
return db["dest"]
def get_mysqldump_from_gula(database='w00t'):
""" use: fab get_mysqldump_from_gula:database='db_name' """
user ="desarrolloweb"
pass_ = "123456"
local("mysqldump -u%(user)s -p%(password)s -h %(host)s %(db_name)s > /tmp/dump.sql"% \
dict(user=user, password=pass_, host="10.0.0.2", db_name=database)
)
__virtualenv("python manage.py dbshell < /tmp/dump.sql")
def get_stg_dump():
__virtualenv("fab mysqldump:directory=/tmp,name='dump.sql'")
get("/tmp/dump.sql", "/tmp/dump.sql")
__virtualenv("python manage.py dbshell < /tmp/dump.sql")
def touch():
local("touch django.wsgi")
def update_subversion():
local("svn up")
def collectstatic():
__virtualenv("python manage.py collectstatic --noinput")
def generatemedia():
__virtualenv("python manage.py generatemedia ")
def makemessages():
__virtualenv("python manage.py makemessages -d django --all")
__virtualenv("python manage.py makemessages -d djangojs --all")
def compilemessages():
__virtualenv("python manage.py compilemessages")
def rebuild_index():
__virtualenv("python manage.py rebuild_index")
def syncdb():
__virtualenv("python manage.py syncdb")
def update_requires():
__virtualenv("pip install -r requires.txt")
def __getting_log_comment():
output = local("svn log -l 1 --xml", capture = True)
root = etree.fromstring(output)
msg = root.xpath("/log/logentry/msg")[0].text
rev = root.xpath("/log/logentry")[0].get("revision")
return rev,msg or ""
def gendoc():
with lcd("doc"):
output = local("make html", capture = True)
f = open("doc/build/html/error.txt","w")
f.write(output)
f.close()
def genepydoc():
local(" DJANGO_SETTINGS_MODULE=settings epydoc --html --parse-only --docformat plaintext . -o epydoc --exclude=_generated_media_* ")
def do_rebuild():
rev,comment = __getting_log_comment()
if "doc" in comment:
gendoc()
elif "media" in comment:
print green("build media at rev %s " % rev)
do_mediastuff()
elif "compilemessages" in comment:
print blue(" do compile rev %s" % rev)
compilemessages()
touch()
elif "nothing" in comment:
print blue(" do nothing rev %s" % rev)
touch()
elif "syncdb" in comment:
print blue(" syncdb rev %s" % rev)
syncdb()
touch()
elif "action:translate" in comment:
commit_translate()
elif "pip" in comment and "update requires" in comment:
print blue("pip update requires " )
update_requires()
elif "all" in comment or "build" in comment:
print green("build all at rev %s" % rev)
do_all()
else:
touch()
def __play():
if os.path.exists("/usr/bin/play"):
local("play -D -q http://simplythebest.net/sounds/WAV/WAV_files/movie_WAV_files/erased.wav")
def do_mediastuff():
collectstatic()
generatemedia()
touch()
__play()
def do_all():
do_mediastuff()
makemessages()
compilemessages()
touch()
def get_pofiles():
get(
"/export/web/volaris.mx/volaris/conf/locale/es/LC_MESSAGES/*.po",
"conf/locale/es/LC_MESSAGES/"
)
get(
"/export/web/volaris.mx/volaris/conf/locale/en/LC_MESSAGES/*.po",
"conf/locale/en/LC_MESSAGES/"
)
def update_cloud():
with cd("volaris.interalia/volaris"):
run("svn up")
__remote_virtualenv(" fab do_mediastuff")
__remote_virtualenv(" python manage.py clear_cache")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment