
                         n   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddl	m
Z
 ddl	mZ dd	l	mZ dd
lmZ ddlmZ ddlmZ ddlmZ dZdZdZdZ G d dej0                        Zd Z G d de      Zd Zd Zd ZddZ  G d de      Z!d Z" e!ee       e!ee       e!ee       e!e e      dZ#y) z,Library for safe migrations of config files.    )absolute_import)division)unicode_literalsN)datastore_index_xml)cron_xml_parser)dispatch_xml_parser)queue_xml_parser)
exceptions)log)
console_io)filesz%Translates a cron.xml into cron.yaml.z'Translates a queue.xml into queue.yaml.z-Translates a dispatch.xml into dispatch.yaml.z3Translates a datastore-indexes.xml into index.yaml.c                       e Zd Zy)MigrationErrorN)__name__
__module____qualname__     4lib/googlecloudsdk/command_lib/app/migrate_config.pyr   r   )   s    r   r   c                     | dz   S )Nz.bakr   )	file_paths    r   _Bakifyr   -   s    	V	r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	MigrationResultzThe changes that are about to be applied on a declarative form.

  Args:
    new_files: {str, str} a mapping from absolute file path to new contents of
      the file, or None if the file should be deleted.
  c                     || _         y N	new_files)selfr   s     r   __init__zMigrationResult.__init__9   s	    DNr   c                 4    | j                   |j                   k(  S r   r   r   others     r   __eq__zMigrationResult.__eq__<   s    >>U__,,r   c                     | |k(   S r   r   r"   s     r   __ne__zMigrationResult.__ne__?   s    u}r   c                    | j                   j                         D ]  }t        |      }t        j                  j                  |      s.t        j                  j                  |      rt        dj                  |            t        j                  j                  dj                  ||             t        j                  ||        y )Nz%Backup file path [{}] already exists.zCopying [{}] to [{}])r   keysr   ospathisfileexistsr   formatr   errPrintshutilcopy2)r   r*   bak_paths      r   _BackupzMigrationResult._BackupB   s    ##%hWW^^D!		!3::8DF 	F	ggmm*11$ABll4" &r   c                    | j                   j                         D ]  \  }}|Dt        j                  j	                  dj                  |             t        j                  |       Lt        j                  j	                  dj                  t        j                  j                  |      rdnd|             t        j                  ||        y )NzDeleting [{}]z{} [{}]OverwritingWriting)r   itemsr   r.   r/   r-   r)   remover*   r,   r   WriteFileContents)r   r*   new_contentss      r   _WriteFileszMigrationResult._WriteFilesM   s    "nn224l		o,,T23
		$i&&WW^^D1My$H 	Il3 5r   c                 D    | j                          | j                          y)z>Backs up first, then deletes, overwrites and writes new files.N)r3   r;   )r   s    r   ApplyzMigrationResult.ApplyW   s    LLNr   N)
r   r   r   __doc__r    r$   r&   r3   r;   r=   r   r   r   r   r   1   s%    -	#4r   r   c                 z    t        j                  |       }t        j                  d|      }| d||i}t	        |      S )zMigration script for cron.xml.N)r   ReadFileContentsr   GetCronYamlr   srcdstxml_stryaml_contentsr   s        r   _MigrateCronXMLrG   ]   s=    ""3''!--dG<-D#}-)		##r   c                 z    t        j                  |       }t        j                  d|      }| d||i}t	        |      S )zMigration script for queue.xml.N)r   r@   r	   GetQueueYamlr   rB   s        r   _MigrateQueueXMLrJ   e   s=    ""3''"//g>-D#}-)		##r   c                 z    t        j                  |       }t        j                  d|      }| d||i}t	        |      S )z"Migration script for dispatch.xml.N)r   r@   r   GetDispatchYamlr   rB   s        r   _MigrateDispatchXMLrM   m   s=    ""3''%55dGD-D#}-)		##r   c                 :   t        j                  |       }t        j                  |      }| di}|rNt        j                  |      }t        j                  |      }|xj                  |j                  z  c_        d||<   |j                         ||<   t        |      S )z+Migration script for datastore-indexes.xml.N)r   r@   r   IndexesXmlToIndexDefinitionsindexesToYAMLr   )rC   rD   auto_srcrE   rP   r   	xml_str_2auto_indexess           r   _MigrateDatastoreIndexXMLrU   u   s    ""3''<<WE'Dk)&&x0I&CCINLOO|+++OIh>>#)C.		##r   c                       e Zd ZdZd Zy)MigrationScriptzObject representing a migration script and its metadata.

  Attributes:
    migrate_fn: a function which accepts a variable number of self-defined
      kwargs and returns a MigrationResult.
    description: str, description for help texts and prompts.
  c                      || _         || _        y r   )
migrate_fndescription)r   rY   rZ   s      r   r    zMigrationScript.__init__   s     DO"Dr   N)r   r   r   r>   r    r   r   r   rW   rW      s    #r   rW   c                      | j                   di |}t        j                  d       t        j                  | j
                  ddd       |j                          y)zRun a migration entry, with prompts and warnings.

  Args:
    entry: MigrationScript, the entry to run.
    **kwargs: keyword args for the migration function.
  z!Please *back up* existing files.
Tz,Do you want to run the migration script now?)defaultprompt_stringcancel_on_noNr   )rY   r   warningr   PromptContinuerZ   r=   )entrykwargsresults      r   Runrd      sP     5%f%&++23B 	,,.r   )zcron-xml-to-yamlzqueue-xml-to-yamlzdispatch-xml-to-yamlzdatastore-indexes-xml-to-yamlr   )$r>   
__future__r   r   r   r)   r0   "googlecloudsdk.appengine.datastorer   googlecloudsdk.appengine.toolsr   r   r	   googlecloudsdk.corer
   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   
_CRON_DESC_QUEUE_DESC_DISPATCH_DESC_INDEX_DESCErrorr   r   objectr   rG   rJ   rM   rU   rW   rd   REGISTRYr   r   r   <module>rr      s     3 &  ' 	  B : > ; * # 2 * 5
7@CZ%% )f )X$$$$#f #$ (D()9;G+,?,:<%45N5@&Br   