
                         p   S r SSKJr  SSKJr  SSKJ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\R0                  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.    )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                       \ 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
)MigrationResult1   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__9   s    Nr   c                 4    U R                   UR                   :H  $ r    r!   r#   others     r   __eq__MigrationResult.__eq__<   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#   r1   bak_paths      r   _BackupMigrationResult._BackupB   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   r5   r6   r4   r0   remover1   r3   r   WriteFileContents)r#   r1   new_contentss      r   _WriteFilesMigrationResult._WriteFilesM   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)r:   rC   )r#   s    r   ApplyMigrationResult.ApplyW   s    LLNr   r!   N)r   r   r   r   __doc__r$   r)   r,   r:   rC   rF   r   r   r   r   r   r   1   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   _MigrateCronXMLrQ   ]   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   rJ   r	   GetQueueYamlr   rL   s        r   _MigrateQueueXMLrT   e   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   rJ   r   GetDispatchYamlr   rL   s        r   _MigrateDispatchXMLrW   m   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   rJ   r   IndexesXmlToIndexDefinitionsindexesToYAMLr   )rM   rN   auto_srcrO   rZ   r"   	xml_str_2auto_indexess           r   _MigrateDatastoreIndexXMLr_   u   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#   rd   re   s      r   r$   MigrationScript.__init__   s     O"r   )re   rd   N)r   r   r   r   rH   r$   r   r   r   r   ra   ra      s    #r   ra   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   )rd   r   warningr   PromptContinuere   rF   )entrykwargsresults      r   Runrp      sN     %f%&++23B 	,,.r   )zcron-xml-to-yamlzqueue-xml-to-yamlzdispatch-xml-to-yamlzdatastore-indexes-xml-to-yamlr    )$rH   
__future__r   r   r   r0   r7   "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   rQ   rT   rW   r_   ra   rp   REGISTRYr   r   r   <module>r~      s     3 &  ' 	  B : > ; * # 2 * 5
7@CZ%% )f )X$$$$#f #$ (D()9;G+,?,:<%45N5@&Br   