Commits

Mikhail Korobov committed c8ef214

inside_project decorator. It simplifies the most common task: run something with virtualenv activated inside project dir.

Comments (0)

Files changed (5)

fab_deploy/__init__.py

 from fab_deploy.commands import touch, mysqldump, delete_pyc, pip_install, pip_update, restart_apache
 from fab_deploy.django_commands import migrate, manage, syncdb, compress, test, coverage
 from fab_deploy.crontab import crontab_set, crontab_add, crontab_show, crontab_remove, crontab_update
-from fab_deploy.utils import run_as, update_env
+from fab_deploy.utils import run_as, update_env, inside_project, inside_virtualenv

fab_deploy/commands.py

-#coding: utf-8
 from datetime import datetime
-from fabric.api import run, env, cd
-from fab_deploy.utils import run_as, inside_virtualenv
+from fabric.api import run, env
+from fab_deploy.utils import run_as, inside_project
 
 @run_as('root')
 def restart_apache():
     run('/etc/init.d/apache2 restart')
 
+@inside_project
 def touch():
     """ Reloads source code by touching the wsgi file """
-    run('touch %s/hosting/generated/django.wsgi' % env.conf['SRC_DIR'])
+    run('touch hosting/generated/django.wsgi')
 
-@inside_virtualenv
+@inside_project
 def pip_install(what='active', options=''):
     """ Installs pip requirements listed in reqs/<file>.txt file. """
-    run('pip install %s -r %s/reqs/%s.txt' % (options, env.conf['SRC_DIR'], what))
+    run('pip install %s -r reqs/%s.txt' % (options, what))
     touch()
 
-@inside_virtualenv
+@inside_project
 def pip_update(what='active', options='', restart=True):
     """ Updates pip requirements listed in reqs/<file>.txt file. """
-    run('pip install %s -U -r %s/reqs/%s.txt' % (options, env.conf['SRC_DIR'], what))
+    run('pip install %s -U -r reqs/%s.txt' % (options, what))
     if restart:
         touch()
 
+@inside_project
 def mysqldump(dir='hosting/backups'):
     now = datetime.now().strftime("%Y.%m.%d-%H.%M")
     db = env.conf['DB_NAME']
     password = env.conf['DB_PASSWORD']
-    with cd(env.conf['SRC_DIR']):
-        run('mysqldump -uroot -p%s %s > %s/%s%s.sql' % (password, db, dir, db, now))
+    run('mysqldump -uroot -p%s %s > %s/%s%s.sql' % (password, db, dir, db, now))
 
+@inside_project
 def delete_pyc():
     """ Deletes *.pyc files from project source dir """
-    with cd(env.conf['SRC_DIR']):
-        run("find . -name '*.pyc' -delete")
+    run("find . -name '*.pyc' -delete")

fab_deploy/django_commands.py

 #coding: utf-8
 from __future__ import with_statement
-from fabric.api import env, cd, local, run, settings
+from fabric.api import run, settings
 from fab_deploy.commands import mysqldump
-from fab_deploy.utils import inside_virtualenv
+from fab_deploy.utils import inside_project
 
-@inside_virtualenv
-def manage(command, on_server=True):
-    if on_server:
-        with cd(env.conf['SRC_DIR']):
-            run('python ./manage.py '+ command)
-    else:
-        local('./manage.py ' + command)
+@inside_project
+def manage(command):
+    run('python ./manage.py '+ command)
 
 def migrate(params='', do_backup=True):
     if do_backup:
     with settings(warn_only=True):
         manage('synccompress %s' % params)
 
-@inside_virtualenv
+@inside_project
 def test(what=''):
     with settings(warn_only=True):
-        with cd(env.conf['SRC_DIR']):
-            run('./runtests.sh ' + what)
+        run('./runtests.sh ' + what)
 
 def coverage():
     pass

fab_deploy/utils.py

 from functools import wraps
-from fabric.context_managers import prefix
+from fabric.context_managers import prefix, cd
 from fabric.contrib.files import upload_template
 from fabric.api import env
 
         with virtualenv():
             return func(*args, **kwargs)
     return inner
+
+def inside_project(func):
+    """
+    Decorator. Use it to perform actions inside project dir with
+    virtualenv activated:
+
+        @inside_project
+        def my_command():
+            # the current dir is a project source dir and
+            # virtualenv is activated
+
+    """
+    @wraps(func)
+    def inner(*args, **kwargs):
+        with cd(env.conf['SRC_DIR']):
+            with virtualenv():
+                return func(*args, **kwargs)
+    return inner
 #!/usr/bin/env python
 from distutils.core import setup
 
-version='0.0.10'
+version='0.0.11'
 
 setup(
     name='django-fab-deploy',