Mikhail Korobov committed c8ef214

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

  • Participants
  • Parent commits fba3730

Comments (0)

Files changed (5)


 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


-#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
 def restart_apache():
     run('/etc/init.d/apache2 restart')
 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')
 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))
 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:
 def mysqldump(dir='hosting/backups'):
     now ="%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))
 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")


 #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
-def manage(command, on_server=True):
-    if on_server:
-        with cd(env.conf['SRC_DIR']):
-            run('python ./ '+ command)
-    else:
-        local('./ ' + command)
+def manage(command):
+    run('python ./ '+ command)
 def migrate(params='', do_backup=True):
     if do_backup:
     with settings(warn_only=True):
         manage('synccompress %s' % params)
 def test(what=''):
     with settings(warn_only=True):
-        with cd(env.conf['SRC_DIR']):
-            run('./ ' + what)
+        run('./ ' + what)
 def coverage():


 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