import urlparse
import tempfile
import pkg_resources
+import traceback
import wizard
-from wizard import resolve, shell, sql, util
+from wizard import plugin, resolve, shell, sql, util
_applications = None
def applications():
try:
return applications()[appname]
except KeyError:
- raise UnknownApplicationError(appname)
+ raise NoSuchApplication(appname)
class Application(object):
"""
"""
#: String name of the application
name = None
+ #: Human-readable name of the application
+ fullname = None
#: Dictionary of version strings to :class:`ApplicationVersion`.
#: See also :meth:`makeVersion`.
versions = None
Makes/retrieves a singleton :class:`ApplicationVersion` from
a``app`` and ``version`` string.
"""
- try:
- # defer to the application for version creation to enforce
- # singletons
- return applications()[app].makeVersion(version)
- except KeyError:
- raise NoSuchApplication(app)
+ # defer to the application for version creation to enforce
+ # singletons
+ return getApplication(app).makeVersion(version)
def expand_re(val):
"""
return subs
return h
-def backup_database(outdir, deployment):
+@decorator.decorator
+def throws_database_errors(f, self, *args, **kwargs):
"""
- Generic database backup function for MySQL.
+ Decorator that takes database errors from :mod:`wizard.sql` and
+ converts them into application script failures from
+ :mod:`wizard.app`. We can't throw application errors directly from
+ :mod:`wizard.sql` because that would result in a cyclic import;
+ also, it's cleaner to distinguish between a database error and an
+ application script failure.
"""
- # XXX: Change this once deployments support multiple dbs
- if deployment.application.database == "mysql":
- return backup_mysql_database(outdir, deployment)
- else:
- raise NotImplementedError
-
-def backup_mysql_database(outdir, deployment):
- """
- Database backups for MySQL using the :command:`mysqldump` utility.
- """
- outfile = os.path.join(outdir, "db.sql")
try:
- shell.call("mysqldump", "--compress", "-r", outfile, *get_mysql_args(deployment.dsn))
- shell.call("gzip", "--best", outfile)
- except shell.CallError as e:
- raise BackupFailure(e.stderr)
-
-def restore_database(backup_dir, deployment):
- """
- Generic database restoration function for MySQL.
- """
- # XXX: see backup_database
- if deployment.application.database == "mysql":
- return restore_mysql_database(backup_dir, deployment)
- else:
- raise NotImplementedError
-
-def restore_mysql_database(backup_dir, deployment):
- """
- Database restoration for MySQL by piping SQL commands into :command:`mysql`.
- """
- if not os.path.exists(backup_dir):
- raise RestoreFailure("Backup %s doesn't exist", backup_dir.rpartition("/")[2])
- sql = open(os.path.join(backup_dir, "db.sql"), 'w+')
- shell.call("gunzip", "-c", os.path.join(backup_dir, "db.sql.gz"), stdout=sql)
- sql.seek(0)
- shell.call("mysql", *get_mysql_args(deployment.dsn), stdin=sql)
- sql.close()
-
-# XXX: SCRIPTS
-def remove_database(deployment):
- """
- Generic database removal function. Actually, not so generic because we
- go and check if we're on scripts and if we are run a different command.
- """
- if deployment.dsn.host == "sql.mit.edu":
- try:
- shell.call("/mit/scripts/sql/bin/drop-database", deployment.dsn.database)
- return
- except shell.CallError:
- pass
- engine = sqlalchemy.create_engine(deployment.dsn)
- engine.execute("DROP DATABASE `%s`" % deployment.dsn.database)
-
-def get_mysql_args(dsn):
- """
- Extracts arguments that would be passed to the command line mysql utility
- from a deployment.
- """
- args = []
- if dsn.host:
- args += ["-h", dsn.host]
- if dsn.username:
- args += ["-u", dsn.username]
- if dsn.password:
- args += ["-p" + dsn.password]
- args += [dsn.database]
- return args
+ return f(self, *args, **kwargs)
+ except sql.BackupDatabaseError:
+ raise BackupFailure(traceback.format_exc())
+ except sql.RestoreDatabaseError:
+ raise RestoreFailure(traceback.format_exc())
+ except sql.RemoveDatabaseError:
+ raise RemoveFailure(traceback.format_exc())
class Error(wizard.Error):
"""Generic error class for this module."""
pass
-class UnknownApplicationError(Error):
- """
- We don't know about this application."""
- #: Name of the unknown application.
- app = None
- def __init__(self, app):
- self.app = app
- def __str__(self):
- return """Unknown application %s""" % self.app
-
class NoRepositoryError(Error):
"""
:class:`Application` does not appear to have a Git repository
location = None
def __init__(self, value):
self.value = value
+ def __str__(self):
+ return "Could not parse '%s' from versions store in '%s'" % (self.value, self.location)
class NoSuchApplication(Error):
"""
location = None
def __init__(self, app):
self.app = app
+ def __str__(self):
+ return "Wizard doesn't know about an application named '%s'." % self.app
class Failure(Error):
"""