
                         L   S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSK	Jr  SS	KJr  SS
KJr  SrSrSrSr " S S\
R(                  5      rS r " S S\5      rS rS rS rSS jr " S S\5      rS r\" \\5      \" \\5      \" \\5      \" \\5      S.rg)z,Library for safe migrations of config files.    N)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                       \ rS rSrSrg)MigrationError&    N)__name__
__module____qualname____firstlineno____static_attributes__r       4lib/googlecloudsdk/command_lib/app/migrate_config.pyr   r   &   s    r   r   c                     U S-   $ )Nz.bakr   )	file_paths    r   _Bakifyr   *   s    	V	r   c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)MigrationResult.   zThe 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                     Xl         g N	new_files)selfr   s     r   __init__MigrationResult.__init__6   s    Nr   c                 4    U R                   UR                   :H  $ r   r   r    others     r   __eq__MigrationResult.__eq__9   s    >>U__,,r   c                     X:X  + $ r   r   r$   s     r   __ne__MigrationResult.__ne__<   s    r   c                    U R                   R                  5        H  n[        U5      n[        R                  R                  U5      (       d  M4  [        R                  R                  U5      (       a  [        SR                  U5      5      e[        R                  R                  SR                  X5      5        [        R                  " X5        M     g )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   _BackupMigrationResult._Backup?   s    ##%hWW^^D!!		!	!3::8DF 	F	ggmm*11$ABll4" &r   c                    U R                   R                  5        H  u  pUcF  [        R                  R	                  SR                  U5      5        [        R                  " U5        MN  [        R                  R	                  SR                  [        R                  R                  U5      (       a  SOSU5      5        [        R                  " X5        M     g )NzDeleting [{}]z{} [{}]OverwritingWriting)r   itemsr   r2   r3   r1   r-   remover.   r0   r
   WriteFileContents)r    r.   new_contentss      r   _WriteFilesMigrationResult._WriteFilesJ   s    "nn224		o,,T23
		$i&&WW^^D11My$H 	I3 5r   c                 D    U R                  5         U R                  5         g)z>Backs up first, then deletes, overwrites and writes new files.N)r7   r@   )r    s    r   ApplyMigrationResult.ApplyT   s    LLNr   r   N)r   r   r   r   __doc__r!   r&   r)   r7   r@   rC   r   r   r   r   r   r   .   s%    -	#4r   r   c                 |    [         R                  " U 5      n[        R                  " SU5      nU SX0n[	        U5      $ )zMigration script for cron.xml.N)r
   ReadFileContentsr   GetCronYamlr   srcdstxml_stryaml_contentsr   s        r   _MigrateCronXMLrN   Z   s;    ""3''!--dG<-D#-)		##r   c                 |    [         R                  " U 5      n[        R                  " SU5      nU SX0n[	        U5      $ )zMigration script for queue.xml.N)r
   rG   r   GetQueueYamlr   rI   s        r   _MigrateQueueXMLrQ   b   s;    ""3''"//g>-D#-)		##r   c                 |    [         R                  " U 5      n[        R                  " SU5      nU SX0n[	        U5      $ )z"Migration script for dispatch.xml.N)r
   rG   r   GetDispatchYamlr   rI   s        r   _MigrateDispatchXMLrT   j   s;    ""3''%55dGD-D#-)		##r   c                 H   [         R                  " U 5      n[        R                  " U5      nU S0nU(       aO  [         R                  " U5      n[        R                  " U5      nU=R                  UR                  -  sl        SXR'   UR                  5       XQ'   [        U5      $ )z+Migration script for datastore-indexes.xml.N)r
   rG   r   IndexesXmlToIndexDefinitionsindexesToYAMLr   )rJ   rK   auto_srcrL   rW   r   	xml_str_2auto_indexess           r   _MigrateDatastoreIndexXMLr\   r   s    ""3''<<WE'Dk)&&x0I&CCINLOO|+++OI>>#).		##r   c                       \ rS rSrSrS rSrg)MigrationScript   zObject 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                     Xl         X l        g r   )
migrate_fndescription)r    ra   rb   s      r   r!   MigrationScript.__init__   s     O"r   )rb   ra   N)r   r   r   r   rE   r!   r   r   r   r   r^   r^      s    #r   r^   c                     U R                   " S0 UD6n[        R                  " S5        [        R                  " U R
                  SSSS9  UR                  5         g)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   )ra   r   warningr	   PromptContinuerb   rC   )entrykwargsresults      r   Runrm      sN     %f%&++23B 	,,.r   )zcron-xml-to-yamlzqueue-xml-to-yamlzdispatch-xml-to-yamlzdatastore-indexes-xml-to-yamlr   ) rE   r-   r4   "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   rN   rQ   rT   r\   r^   rm   REGISTRYr   r   r   <module>rz      s     3 
  B : > ; * # 2 * 5
7@CZ%% )f )X$$$$#f #$ (D()9;G+,?,:<%45N5@&Br   