Skip to content
This repository has been archived by the owner on Feb 17, 2018. It is now read-only.

Commit

Permalink
Fix #1: fabric isn't necessary to import the module, but the commands…
Browse files Browse the repository at this point in the history
… now raise exceptions if fabric is not available
  • Loading branch information
muhuk committed Nov 11, 2012
1 parent cb2cc7b commit 34f15ab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/
*.egg-info/
MANIFEST
*.py[oc]
43 changes: 39 additions & 4 deletions cuisine_postgresql.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
from fabric.context_managers import cd, hide, settings
from fabric.operations import sudo
from fabric.utils import puts
try:
from fabric.context_managers import cd, hide, settings
from fabric.operations import sudo
from fabric.utils import puts
__fabric_available = True
except ImportError:
__fabric_available = False


__version__ = '0.1.0'
__version__ = '0.1.1'
__maintainer__ = u'Atamert \xd6l\xe7gen'
__email__ = 'muhuk@muhuk.com'
__all__ = [
'postgresql_database_check',
'postgresql_database_create',
'postgresql_database_ensure',
'postgresql_role_check',
'postgresql_role_create',
'postgresql_role_ensure',
]


def require_fabric(f):
"""
Raises ``RuntimeError`` if the wrapped method is called but ``fabric``
cannot be imported for some reason.
"""
global __fabric_available
if __fabric_available:
return f
else:
def _f(*args, **kwargs):
error_message = 'To use function "{0}", you must have ' \
'fabric in the import path'.format(f.func_name)
raise RuntimeError(error_message)
return _f


@require_fabric
def postgresql_database_check(database_name):
cmd = 'psql -U postgres -l | grep \'{0} *|\''.format(database_name)
with settings(hide('everything'), warn_only=True):
return bool(run_as_postgres(cmd))


@require_fabric
def postgresql_database_create(database_name,
tablespace=None,
locale=None,
Expand All @@ -34,6 +64,7 @@ def postgresql_database_create(database_name,
run_as_postgres(cmd)


@require_fabric
def postgresql_database_ensure(database_name,
tablespace=None,
locale=None,
Expand All @@ -52,12 +83,14 @@ def postgresql_database_ensure(database_name,
template)


@require_fabric
def postgresql_role_check(username):
cmd = 'psql -U postgres -c \'\\du\' | grep \'^ *{0} *|\''.format(username)
with settings(hide('everything'), warn_only=True):
return bool(run_as_postgres(cmd))


@require_fabric
def postgresql_role_create(username,
password,
superuser=False,
Expand All @@ -78,6 +111,7 @@ def postgresql_role_create(username,
run_as_postgres(cmd)


@require_fabric
def postgresql_role_ensure(username,
password,
superuser=False,
Expand All @@ -98,6 +132,7 @@ def postgresql_role_ensure(username,
login)


@require_fabric
def run_as_postgres(cmd):
"""
Run given command as postgres user.
Expand Down

0 comments on commit 34f15ab

Please sign in to comment.