
    \e                        d Z ddlZddlZddlZddl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 ej                  ej                  z  ej                  z  ZefdZej"                  fdZej&                  efd       ZddZddZd	 Zd
 Zd Zy)z(
File utilities.

.. versionadded:: 1.8
    N)excutilsc                     	 t        j                  | |       y# t        $ rH}|j                  t        j                  k(  r t         j
                  j                  |       s  Y d}~yd}~ww xY w)zCreate a directory (and any ancestor directories required)

    :param path: Directory to create
    :param mode: Directory creation permissions
    N)osmakedirsOSErrorerrnoEEXISTpathisdir)r
   modeexcs      6/usr/lib/python3/dist-packages/oslo_utils/fileutils.pyensure_treer   %   sP    
D$ 99$77==& 's    	A*>A%%A*c                 ~    	  ||        y# t         $ r(}|j                  t        j                  k7  r Y d}~yd}~ww xY w)zDelete a file, but ignore file not found error.

    :param path: File to delete
    :param remove: Optional function to remove passed path
    N)r   r   ENOENT)r
   removees      r   delete_if_existsr   5   s6    t 77ell" #s    	<7<c              #      K   	 d y# t         $ r5 t        j                         5   ||        ddd       Y y# 1 sw Y   Y yxY ww xY ww)zProtect code that wants to operate on PATH atomically.
    Any exception will cause PATH to be removed.

    :param path: File to work with
    :param remove: Optional function to remove passed path
    N)	Exceptionr   save_and_reraise_exception)r
   r   s     r   remove_path_on_errorr   C   sD      002 	4L	 	 	s=   A
	 A
A	9AA
A	AA
AA
c                     |rt        |       t        j                  |||      \  }}	 t        j                  ||        t        j
                  |       |S # t        j
                  |       w xY w)a  Create a temporary file containing data.

    Create a temporary file containing specified content, with a specified
    filename suffix and prefix. The tempfile will be created in a default
    location, or in the directory `path`, if it is not None. `path` and its
    parent directories will be created if they don't exist.

    :param content: bytestring to write to the file
    :param path: same as parameter 'dir' for mkstemp
    :param suffix: same as parameter 'suffix' for mkstemp
    :param prefix: same as parameter 'prefix' for mkstemp

    For example: it can be used in database tests for creating
    configuration files.

    .. versionadded:: 1.9
    )suffixdirprefix)r   tempfilemkstempr   writeclose)contentr
   r   r   fds        r   write_to_tempfiler#   S   sY    $ D!!T&IJR
W
K 	s   A A.c                 (   t        j                  |      }t        | d      5 t        fdd      D ](  }|j	                  |       t        j                  d       * 	 ddd       |j                         S # 1 sw Y   |j                         S xY w)a  Compute checksum of a file's contents.

    :param path: Path to the file
    :param read_chunksize: Maximum number of bytes to be read from the file
     at once. Default is 65536 bytes or 64KB
    :param algorithm: The hash algorithm name to use. For example, 'md5',
     'sha256', 'sha512' and so on. Default is 'sha256'. Refer to
     hashlib.algorithms_available for available algorithms
    :return: Hex digest string of the checksum

    .. versionadded:: 3.31.0
    rbc                  &     j                        S )N)read)fread_chunksizes   r   <lambda>z'compute_file_checksum.<locals>.<lambda>   s    !&&"8     r+   r   N)hashlibnewopeniterupdatetimesleep	hexdigest)r
   r)   	algorithmchecksumchunkr(   s    `   @r   compute_file_checksumr7   p   s     {{9%H	dD	 Q8#> 	EOOE"JJqM	
 
 s   <A99Bc                 x   t        | d      5 }	 |j                  | t        j                         |j                         }|j                         |fcddd       S # t        $ rI}|j
                  t
        j                  k(  r!|j                  dt        j                         n Y d}~xd}~ww xY w# 1 sw Y   yxY w)a  Return num bytes from the end of the file and unread byte count.

    Returns a tuple containing some content from the file and the
    number of bytes that appear in the file before the point at which
    reading started. The content will be at most ``num`` bytes, taken
    from the end of the file. If the file is smaller than ``num``
    bytes the entire content of the file is returned.

    :param path: The file path to read
    :param num: The number of bytes to return

    :returns: (data, unread_bytes)

    r%   r   N)
r.   seekr   SEEK_ENDIOErrorr   EINVALSEEK_SETtellr'   )r
   numfpr   unread_bytess        r   
last_bytesrB      s      
dD	 )R		GGSD"++& wwy	<() )  	 ww%,,&2;;' (	) )s3   B0!A!B0	B-$?B(#B0(B--B00B9c                     t        | d      5 }|j                         }ddd       	 t        j                         y# 1 sw Y    xY w# t        $ r Y yw xY w)zCheck if file is of json type or not.

    This function try to load the input file using json.loads()
    and return False if ValueError otherwise True.

    :param file_path: The file path to check

    :returns: bool

    rNTF)r.   r'   jsonloads
ValueError)	file_pathfhdatas      r   is_jsonrK      sW     
i	 wwy

4	 
  s   =A	 A		AAc                 0   t        | d      5 }|j                         }d}	 t        j                  |       |cddd       S # t        $ r? 	 t        j                  |       d}n## t
        j                  j                  $ r Y nw xY wY Rw xY w# 1 sw Y   yxY w)ah  Check if file is of yaml type or not.

    This function try to load the input file using yaml.safe_load()
    and return True if loadable. Because every json file can be loadable
    in yaml, so this function return False if file is loadable using
    json.loads() means it is json file.

    :param file_path: The file path to check

    :returns: bool

    rD   FTN)	r.   r'   rE   rF   rG   yaml	safe_loadscannerScannerError)rH   rI   rJ   is_yamls       r   rQ   rQ      s     
i	 wwy	JJt  
  	t$<<,, 		 sQ   BAB	B	A#"B	#B B	BB	BB		BB)N tmp)i   sha256)__doc__
contextlibr   r,   rE   r   statr   r1   rM   
oslo_utilsr   S_IRWXUS_IRWXGS_IRWXO_DEFAULT_MODEr   unlinkr   contextmanagerr   r#   r7   rB   rK   rQ    r+   r   <module>r`      s         	     t||+dll: )   #%))  &6  : ,)>(r+   