
    	                         d Z ddlm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Z G d de	      Z
ej                  d
d	       Zy)z~Alternate tempfile.NamedTemporaryFile that's easier to use on Windows.

(Windows locks files from simultaneous writes/reads).
    )absolute_import)division)print_function)unicode_literalsNc                   "    e Zd ZdZd Zd Zd Zy)_WindowsNamedTempFilezWrapper around named temporary file for Windows.

  NamedTemporaryFiles cannot be read by other processes on windows because
  only one process can open a file at a time. This file will be unlinked
  at the end of the context.
  c                     |j                  dd      | _        || _        |j                         | _        d| j                  d<   d | _        y )NdeleteTF)get_requested_delete_argscopy_kwargs_f)selfargskwargss      ?lib/googlecloudsdk/command_lib/code/cross_platform_temp_file.py__init__z_WindowsNamedTempFile.__init__&   s>    #ZZ$7DDJ;;=DL"DLLDG    c                 v    t        j                  | j                  i | j                  | _        | j                  S N)tempfileNamedTemporaryFiler   r   r   )r   s    r   	__enter__z_WindowsNamedTempFile.__enter__-   s*    ))4::FFDG77Nr   c                     | j                   r8| j                  r+	 t        j                  | j                  j                         y y y # t
        $ r Y y w xY wr   )r   r   osunlinknameOSError)r   exc_type	exc_valuetbs       r   __exit__z_WindowsNamedTempFile.__exit__1   sF    $''
		$'',, #*  s   )A 	AAN)__name__
__module____qualname____doc__r   r   r$    r   r   r   r      s    r   r   c              #   z  K   t        d|||      }t        j                  dk(  r:t        di |5 }|j	                  |        |j                          | ddd       yt        j                  di |5 }|j	                  |        |j                          | ddd       y# 1 sw Y   yxY w# 1 sw Y   yxY ww)zWrite a named temporary with given contents.

  Args:
    contents: (str) File contents.
    prefix: (str) File base name prefix.
    suffix: (str) Filename suffix.
    delete: (bool) Delete file on __exit__.

  Yields:
    The temporary file object.
  zw+t)modeprefixsuffixr
   ntNr)   )	dictr   r   r   writecloser   r   flush)contentsr,   r-   r
   common_argsfs         r   NamedTempFiler6   :   s      %vfM+WW_		-	-gghggig 
.	-
 
	$	$	3{	3qgghggig 
4	3 
.	-
 
4	3s4   .B;&B#B;4&B/	B;#B,(B;/B84B;)tmp T)r(   
__future__r   r   r   r   
contextlibr   r   objectr   contextmanagerr6   r)   r   r   <module>r=      sG   
 '  % '  	 F 8  r   