ó
@5Mc           @   s  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l m Z m Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z d	   Z d
   Z d e j f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d S(   i˙˙˙˙N(   t   plugint   shellc         C   s2   t  j |   } t  j   } | | _ | j   | S(   s6   Convenience method for connecting to a MySQL database.(   t
   sqlalchemyt   create_enginet   MetaDatat   bindt   reflect(   t   urlt   enginet   meta(    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyt   connect   s
    	
c         C   sÇ   |  s
 d S|  j s |  St |  j |  j |  j f  r9 |  SxH t j d  D]7 } | j   } | t	 j	 |    } | d k	 rI | SqI Wt
 j d  } | rĂ |  } t j j j |  }  | j |  _ n  |  S(   sÚ   
    If the URL has a database name but no other values, it will
    use the global configuration, and then try the database name.

    This function implements a plugin interface named
    :ref:`wizard.sql.auth`.
    s   wizard.sql.autht
   WIZARD_DSNN(   t   Nonet   databaset   anyt   hostt   usernamet   passwordt   pkg_resourcest   iter_entry_pointst   loadt   copyt   ost   getenvR   R   R   t   make_url(   R   t   entryt   funct   rt   env_dsnt   old_url(    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyt   auth   s"    	c         C   s)   | j  j d k r t |  |  St  d S(   s+   
    Generic database backup function.
    t   mysqlN(   t   applicationR   t   backup_mysqlt   NotImplementedError(   t   outdirt
   deployment(    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyt   backup2   s    c         C   sz   t  j j |  d  } y9 t j d d d | t | j   t j d d |  Wn% t j k
 ru } t | j	   n Xd S(   sL   
    Database backups for MySQL using the :command:`mysqldump` utility.
    s   db.sqlt	   mysqldumps
   --compresss   -rt   gzips   --bestN(
   R   t   patht   joinR   t   callt   get_mysql_argst   dsnt	   CallErrort   BackupDatabaseErrort   stderr(   R#   R$   t   outfilet   e(    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyR!   <   s    "c         C   s)   | j  j d k r t |  |  St  d S(   s0   
    Generic database restoration function.
    R   N(   R    R   t   restore_mysqlR"   (   t
   backup_dirR$   (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyt   restoreG   s    c         C   s˛   t  j j |   s2 t d |  j d  d   n  t t  j j |  d  d  } t j d d t  j j |  d  d	 | | j	 d
  t j d d | t
 | j  | j   d S(   sV   
    Database restoration for MySQL by piping SQL commands into :command:`mysql`.
    s   Backup %s doesn't existt   /i   s   db.sqls   w+t   gunzips   -cs	   db.sql.gzt   stdouti    R   t   stdinN(   R   R(   t   existst   RestoreDatabaseErrort
   rpartitiont   openR)   R   R*   t   seekR+   R,   t   close(   R3   R$   t   sql(    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyR2   Q   s     (c         C   sL   t  j d |  g  } | d k	 r% d St j |   } | j d |  j  d S(   sĚ   
    Generic drop database function.  Attempts to run ``DROP
    DATABASE`` on the database if no plugins succeed.

    This function implements the plugin interface named
    :ref:`wizard.sql.drop`.
    s   wizard.sql.dropNs   DROP DATABASE `%s`(   R    t   hookR   R   R   t   executeR   (   R   R   R   (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyt   drop]   s
    c         C   sx   g  } |  j  r% | d |  j  g 7} n  |  j rD | d |  j g 7} n  |  j rd | d |  j g 7} n  | |  j g 7} | S(   sj   
    Extracts arguments that would be passed to the command line mysql utility
    from a deployment.
    s   -hs   -us   -p(   R   R   R   R   (   R,   t   args(    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyR+   k   s    			t   Errorc           B   s   e  Z d  Z RS(   s$   Generic error class for this module.(   t   __name__t
   __module__t   __doc__(    (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyRD   z   s   R.   c           B   s&   e  Z d  Z d Z d   Z d   Z RS(   s   Backup script failed.c         C   s   | |  _  d  S(   N(   t   details(   t   selfRH   (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyt   __init__   s    c         C   s   d |  j  S(   Ns5   

ERROR: Backing up the database failed, details:

%s(   RH   (   RI   (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyt   __str__   s    N(   RE   RF   RG   R   RH   RJ   RK   (    (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyR.   ~   s   	R:   c           B   s&   e  Z d  Z d Z d   Z d   Z RS(   s   Restore script failed.c         C   s   | |  _  d  S(   N(   RH   (   RI   RH   (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyRJ      s    c         C   s   d |  j  S(   Ns4   

ERROR: Restoring the database failed, details:

%s(   RH   (   RI   (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyRK      s    N(   RE   RF   RG   R   RH   RJ   RK   (    (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyR:      s   	t   RemoveDatabaseErrorc           B   s&   e  Z d  Z d Z d   Z d   Z RS(   s   Removing the database failed.c         C   s   | |  _  d  S(   N(   RH   (   RI   RH   (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyRJ      s    c         C   s   d |  j  S(   Ns3   

ERROR: Removing the database failed, details:

%s(   RH   (   RI   (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyRK      s    N(   RE   RF   RG   R   RH   RJ   RK   (    (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyRL      s   	(   R   R   R   R   t	   decoratort   wizardR    R   R
   R   R%   R!   R4   R2   RB   R+   RD   R.   R:   RL   (    (    (    s8   /afs/athena.mit.edu/contrib/scripts/wizard/wizard/sql.pyt   <module>   s$   			
		
			