
                            d Z ddlm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	 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
ZddZddZ	 	 	 	 	 	 ddZy)z+Utilities for exporting diagnostic results.    )annotationsN)info_holder)errors)
diagnostic)utils)config)logc                *   | t        j                         j                  } t        j                  j	                         j                  d      }d| dt        j                         j                   d}t        j                  j                  | |      }|S )z,Returns a unique path for the export bundle.z%Y-%m-%d_%H-%M-%Sdiagnostic_bundle__z.tar.gz)r   Pathslogs_dirdatetimenowstrftimeuuiduuid4hexospathjoin)destination	timestamptarfile_nametarfile_paths       >lib/googlecloudsdk/command_lib/storage/diagnose/export_util.py_get_export_bundle_pathr   !   st    ,,.))K##%../BC)%i[$**,2B2B1C7K,k<8,	    c                    t        j                  dd      5 }|j                  |        ddd       |j                  S # 1 sw Y   j                  S xY w)zGenerates a temporary file with the given content.

  Args:
    content: The string content to be written to the file.

  Returns:
    The path of the generated file.
  Fw)deletemodeN)tempfileNamedTemporaryFilewritename)contentfiles     r   _generate_temp_filer)   ,   sL     "" JJw	
 

 
s	   >Ac                    | D ]  }	 |rt        j                  |        y# t        t        f$ r.}t	        j
                  dj                  |             Y d}~Vd}~ww xY w)zCleans up the temporary files.z%Failed to clean up temporary file. {}N)r   removeOSErrorEnvironmentErrorr	   warningformat)
temp_files	temp_filees      r   _clean_up_temp_filesr3   =   sW    iE	
		)  %& E	kk9@@CDDEs   !A$AAc                D   t        |      }d}d}	 t        j                  |d      5 }|j                  t	        j
                         j                  d       dj                  d | D              }t        |      }|j                  |d       t        t        j                  t        j                                     }t        j                  d	d
g      \  }}	|r||z  }|	r||	z  }t        |      }|j                  |d       ddd       t/        ||g       |S # 1 sw Y   xY w# t        t         t        j"                  t        j$                  t        j&                  f$ r*}
t)        j*                  dj-                  ||
            d}
~
ww xY w# t/        ||g       w xY w)a  Exports a diagnostic bundle to the destination file path.

  A diagnostic bundle consists of result of running the diagnose command, output
  of gcloud info command and recent logs.

  Args:
    test_results: List of test results to be included in the bundle.
    destination: The destination file path. If None, the default logs directory
      is used.

  Returns:
    The path of the exported diagnostic bundle.

  Raises:
    command_errors.FatalError: If the export fails.
  Nzw:gzgcloud_logs)arcname
c              3  2   K   | ]  }t        |        y w)N)str).0test_results     r   	<genexpr>z+export_diagnostic_bundle.<locals>.<genexpr>e   s      ,.:{#k
ls   zstorage_diagnostic_results.txt)
anonymizerinfoz--run-diagnosticszgcloud_info.txtz1Failed to export diagnostic bundle at path {}, {})r   tarfileopenaddr   r   r   r   r)   r9   r   
InfoHolder
Anonymizerr   
run_gcloudr,   r-   	ReadErrorCompressionErrorExtractErrorcommand_errors
FatalErrorr/   r3   )test_resultsr   r   diagnostic_result_file_nameinfo_file_nametardiagnostic_result_contentinfo_contentstdoutstderrr2   s              r   export_diagnostic_bundlerR   G   s   ( )5, $..H	lF	+s	ggflln%%}g= #')) ,.:, # %8
#%! 
gg
%/O  
 

 
 K,B,B,D
El ''1D(EFnff		*<8n	ggn&7g8; 
,Z 5~FG	_ 
,	+@ 
  
#
#;BB!	
  5~FGs;   D' CDD' D$ D' '<F#%FFF F)r   
str | Nonereturnr9   )r'   r9   rT   r9   )r0   z	list[str]rT   None)rJ   z!list[diagnostic.DiagnosticResult]r   rS   rT   r9   )__doc__
__future__r   r   r   r?   r#   r   googlecloudsdk.command_libr   "googlecloudsdk.command_lib.storager   rH   +googlecloudsdk.command_lib.storage.diagnoser   r   googlecloudsdk.corer   r	   r   r)   r3   rR    r   r   <module>r]      sa    2 "  	    2 G B = & #"EH3HH 	Hr   