HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/__pycache__/fileclient.cpython-310.pyc
o

�N�g*��@s�dZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddl
ZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Zddl!Zddl"m#Z#ddl$m%Z%m&Z&m'Z'ddl(m)Z)m*Z*m+Z+ddl,m-Z-e�.e/�Z0dZ1dd	d
�Z2dd�Z3Gd
d�d�Z4Gdd�de4�Z5Gdd�de4�Z6Gdd�de6�Z7e7Z8Gdd�d�Z9Gdd�d�Z:dS)z"
Classes that manage file clients
�N)�DEFAULT_HASH_TYPE)�CommandExecutionError�MinionError�SaltClientError)�HTTPHeaders�HTTPInputError�parse_response_start_line)�	SaltSwift�FcCs4|�dd�}|r|dkrd}tttd��|t�|�S)z[
    Read in the ``file_client`` option and return the correct type of file
    server
    �file_client�remote�local�pillar)rr
r)�get�RemoteClient�FSClient�PillarClient)�optsr�client�r�C/opt/saltstack/salt/lib/python3.10/site-packages/salt/fileclient.py�get_file_client1s��rc	CsZt|t�s|Si}|��D]\}}t|t�r&z|��}Wn	ty%Ynw|||<q
|S)z�
    Convert top level keys from bytes to strings if possible.
    This is necessary because Python 3 makes a distinction
    between these types.
    )�
isinstance�dict�items�bytes�decode�UnicodeError)�src�output�key�valrrr�decode_dict_keys_to_str?s

�
r"c@s8eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Ze	j
d<dd��Zd=dd�Z	
d>dd�Z
d?dd�Z		
	
		d@dd�Zd<dd�Zd<dd�Z			
	
	
dAdd �Zd!d"�ZdBd#d$�Zd?d%d&�Zd?d'd(�Zd?d)d*�Zd<d+d,�Zd<d-d.�Zd/d0�Zd=d1d2�ZdCd3d4�Z				
	
		dDd5d6�Z	7			
dEd8d9�Zd=d:d;�Zd
S)F�Clientz/
    Base class for Salt file interactions
    cCs||_tj�|j�|_dS�N)r�salt�loader�utils��selfrrrr�__init__XszClient.__init__cCs|�|d�dS�Nr)r*)r)�staterrr�__setstate__aszClient.__setstate__cCs
d|jiSr+�r�r)rrr�__getstate__f�
zClient.__getstate__cCs.|�d�std|����tjj�|�\}}|S)zV
        Make sure that this path is intended for the salt master and trim it
        �salt://zUnsupported path: )�
startswithrr%r'�url�parse)r)�pathZ	file_path�saltenvrrr�_check_protois
zClient._check_protoc	Cshtj�|�r	|}ntj�|�}t�}tjjj|dd�D]\}}}|D]}tj�||�}|�	|�q"q|S)zF
        Helper util to return a list of files in a directory
        T��followlinks)
�osr6�isdir�dirname�setr%r'�os_walk�join�add)	r)�dest�destdirZfilelist�root�dirs�files�namer6rrr�_file_local_listrs�zClient._file_local_list�baseNccs��|�|�}tjj�|d||�}tj�|�}tjj�d��8tj�	|�r)t�
|�zt�|�WntyI}z
|j
t
jkr?�WYd}~nd}~ww|VWd�dS1sXwYdS)zV
        Return the local location to cache the file, cache dirs will be made
        rF�?N)�get_cachedirr%r'r6r@r;r=rFZ	set_umask�isfile�remove�makedirs�OSError�errno�EEXIST)r)r6r7�cachedirrBrC�excrrr�
_cache_loc�s"�

���"�zClient._cache_loccCs:|dur|jd}|Stj�|�stj�|jd|�}|S)NrR)rr;r6�isabsr@)r)rRrrrrK�s
�zClient.get_cachedir�FcC�t�)zb
        Copies a file from the local files or master depending on
        implementation
        ��NotImplementedError)r)r6rBrNr7�gziprRrrr�get_file�szClient.get_filecCrW)z%
        List the empty dirs
        rX�r)r7�prefixrrr�file_list_emptydirs��zClient.file_list_emptydirsTc
Cs|j|dd|||||d�S)ze
        Pull a file down from the file server and store it in the minion
        file cache
        rVT)rR�source_hash�
verify_ssl�use_etag)�get_url)r)r6r7rRr`rarbrrr�
cache_file�s
�zClient.cache_filecCs<g}t|t�r|�d�}|D]
}|�|j|||d��q|S)zm
        Download a list of files stored on the master and put them in the
        minion file cache
        �,�rR)r�str�split�appendrd)r)�pathsr7rR�retr6rrr�cache_files�s

zClient.cache_filescCs8g}|�|�D]}|�|jtjj�|�||d��q|S)zU
        Download and cache all files on a master in a specified environment
        rf)�	file_listrirdr%r'r4�create)r)r7rRrkr6rrr�cache_master�s�zClient.cache_mastercCsg}|�tjj�|��}|�d�s|d}t�d||�|�|�D]/}tjj�|�}|�	�rP|�
|�rPtjj�|||�rP|j
tjj�|�||d�}|rP|�|�q!|r�|�|�}tjj�|d|�}	|�|�D]%}tjj�|�}|�
|�r�|	�d|��}
tj�|
�s�t�|
�|�|
�qf|S)zE
        Download all of the files in a subdir of the master
        �/z+Caching directory '%s' for environment '%s'rfrF)r8r%r'�datar�endswith�log�inform�stripr3�stringutilsZcheck_include_excluderdr4rnrirKr6r@r^r;r<rN)r)r6r7Z
include_emptyZinclude_patZexclude_patrRrk�fn_rBZ
minion_dirrrr�	cache_dir�s:
��
�




�zClient.cache_dircKsNtj�|jdd|�d��}tj�|�}tj�|�st�|�t�	||�|S)zJ
        Cache a local file on the minion in the localfiles cache
        rR�
localfilesrp)
r;r6r@r�lstripr=r<rN�shutil�copyfile)r)r6�kwargsrBrCrrr�cache_local_files
zClient.cache_local_filecCsLtj�|jdd|�}tj�|jdd�}|�|�}|�|�}t|�|��S)zL
        List files in the local minion files and localfiles caches
        rRrFry)r;r6r@rrH�sorted�union)r)r7�	filesdestZlocalfilesdestZfdestZldestrrr�file_local_list's


zClient.file_local_listcC�gS�z3
        This function must be overwritten
        rr\rrrrm2r_zClient.file_listcCr�r�rr\rrr�dir_list8r_zClient.dir_listcC�iSr�rr\rrr�symlink_list>r_zClient.symlink_listc	Cs�|�d�rtjj�|�\}}|r|}tjj�|�rdnd}tj�|j	dd|�
d��}tj�|j	dd||�
d��}|j|||d�}tj�|�rS|rQtjj�
|�S|Stj�|�rd|rbtjj�
|�S|Stj�|�rl|Sd	S)
z�
        Returns the full path to a file if it is cached locally on the minion
        otherwise returns a blank string
        r2TFrRry�|/rFrfrV)r3r%r'r4r5�
is_escapedr;r6r@rrz�_extrn_path�exists�escape)	r)r6r7rR�senv�escapedZlocalsfilesdestr�Z	extrndestrrr�	is_cachedDs,
����zClient.is_cachedcCsltj�|�j}|dkr
|S|dkr.tjj�|�\}}|r|}tjj�|j	dd||�
d��S|j|||d�S)zc
        Return the expected cache location for the specified URL and
        environment.
        rVr%rRrFr�rf)�urllibr5�urlparse�schemer%r'r4r6r@rrzr�)r)r4r7rR�protor�rrr�
cache_destds�zClient.cache_destcCs�t�}|�|�D]3}tjj��r|�dd�}|�d�r;|�d�r.|�|�dd�dd��q|�|�dd�dd��qt	|�S)	zj
        Return a list of all available sls modules on the master for a given
        environment
        �\rp�.sls�	/init.sls�.Ni�������)
r>rmr%r'�platform�
is_windows�replacerrrAr)r)r7Zstatesr6rrr�list_statesys

�zClient.list_statescCsld|vr
|�dd�}tjj�|d�}tjj�|d�}||fD]}|j|||d�}|r3||d�Sq iS)z�
        Get a state file from the master and store it in the local minion
        cache; return the location of the file
        r�rpr�r�rf)�sourcerB)r�r%r'r4rnrd)r)Zslsr7rRZsls_urlZinit_urlr6rBrrr�	get_state�s�zClient.get_statec
Csdg}|�|��d�}|�dd�}t|�dkrd}n|d}|j||d�D]9}	z|	t|�dkr1Wq$Wn	ty;Yq$w|	t|�d��d�}
|�|�t	j
j�|	�|�d|
��d||��q$zD|j
||d�D]:}	z|	t|�dkrsWqfWn	ty}Yqfw|	t|�d��d�}
|�d|
��}tj�|�s�t�|�|�|�qfWn	ty�Ynw|��|S)	zB
        Get a directory recursively from the salt-master
        rp��rVr)r]NT)r8�rstrip�rsplit�lenrm�
IndexErrorrzrir[r%r'r4rnr^r;r6r<rN�	TypeError�sort)r)r6rBr7rZrRrk�	separatedr]rwZminion_relpathZminion_mkdirrrr�get_dir�sX����
��
��zClient.get_dirc
$s�tjj|dd�}
|
j}tj�|
j|
j��tj	�}|durUtj�
|�s'|�d�rU|
js7t
|
j�dkr?|
j�d�s?|�d�d}
nd}
tjj��rNtjj�|
�}
tj�||
�}|rg|��tjvrgd	�||f�}d
}|dvr�tj�|�sytd|�d
���|dur�tjj�|d��
}|��}Wd�|S1s�wY|S|S|dkrӈj|||||d���r�|dur�tjj��d��
}|��}Wd�|S1s�wY|S�S|r�tj�|�}tj�
|�s�|r�t�|�n[dSnX|�sD�j|||d�}|du�r2z(|�d�d}tjjj t
|�}tjj!�"||�|k�r#t#�$d|||�|WSWn
t%t&t'f�y1Ynwtj�|�}tj�
|��sDt�|�|
jdk�r�z5dI�fdd�	}�jdd|
j|
jdd�d|d|d�|d�|d�|dd�|d�|dd�|d d�d!�
|WSt(�y�}z
t)d"|�d#|����d}~ww|
jd$k�r�zKt*�+�}|
j,}|�s�d%}|�-|
j.|�|�/|
j0|
j1�|
j�2d�}tjj�|d&��}|�3d'|��|j4�Wd�n	1�s�wY|�5�|WSt(�y�}zt)d(�6||���d}~ww|
jd)k�r=z)�fd*d+�}t7|d,d�|d-d�|d.d�|d/d��}|�8|
j|
jdd�|�|WSt(�y<t)d"|����wi}|
j0du�rz|
jd0v�rz|
j}|�9d1�}|dk�r`||dd�}tj�:|
j||
j|
j;|
j|
j<f�}|
j0|
j1f|d2<n|}d�|�d3���z7z�gd4�����fd5d6�}|�r�g���fd7d8�}n|�d9�}tjj�|d&����fd:d8�}i} ��r�tj�=���r�tj�=|��r�tjj��d;��}!|!���>d<d��?�}"Wd�n	1�s�wY|"| d=<tjj@j|fd|||
j0|
j1�jA|| d>�|��}#��r+|#�Bd?�d@k�r+|�s��C�d�t�D|�|WW�du�r*��C�SSdA|#v�r;t)dB�6|#dC|
j���|�re�dD�rTd���WW�du�rS��C�SSdE���WW�du�rd��C�SS��C�d�tjj�E||�|WW�du�r���C�SStjFjG�y�}zt)dFj6|jH|gt@jIjJjK|jH�R���d}~wtjFjL�y�}zt)dG|�dH|jM����d}~ww�du�rƈ�C�ww)Jz/
        Get a single file from a URL.
        F)�allow_fragmentsN�rpr�r�rp���z
index.html�:�file)r�rVzPath 'z' is not absolute�rbr%rfrV�=z@Cached copy of %s (%s) matches source_hash %s, skipping download�s3c	sLd|�jvr�jd|Sz
�jdd|WSttfy%|YSw)za
                    Get value of s3.<key> from Minion config or from Pillar
                    zs3.rr��r�KeyErrorr��r �defaultr/rr�s3_opt-s�zClient.get_url.<locals>.s3_optzs3.queryZGETr �keyid�service_urlraT�location�
path_style�https_enable)
�methodZbucketr6Z
return_binZ
local_file�actionr r�r�rar�r�r�zCould not fetch from z
. Exception: �ftp��wbzRETR z4Could not retrieve {} from FTP server. Exception: {}Zswiftc	s@|�jvr
�j|Sz�jd|WSttfy|YSw)z^
                    Get value of <key> from Minion config or from Pillar
                    rr�r�r/rr�	swift_optas

�z!Client.get_url.<locals>.swift_optz
keystone.userzkeystone.tenantzkeystone.auth_urlzkeystone.password)�http�https�@�authz.etag)NFNNcs��ddurԈddus�rԈddur�|��s4d�dvr4�ddur(d�d<�r.�dr2d�d<dS�d�|��rvdttj�d�vrvd	d
��d��D�d}�d<tjj�	�d��
}|�
|�}Wd�n1spwYn^d�dvrԈd�d�}|�d�s�d�d<�r��dr�d�d<n%d
}|�
d�}|D]
}d|vr�|�
d�d}q�|�d<�r��dr�d�d<�d�dur�dur�nnd�d<�d<�ddur�zt|�}Wn
ty�YdSw|jdv�d<t��d<dSdS)Nr�Fr��zContent-TyperT�etagcSs g|]\}}|��dkr|�qS)r�)�lower)�.0r r!rrr�
<listcomp>�s
�z5Client.get_url.<locals>.on_header.<locals>.<listcomp>�w�textzutf-8�;�encodingz	encoding=r�)i-i.i/i3)ruZ
parse_line�maprgr�rr%r'rF�fopen�writerr3rhrr�coder)�hdrr��etagfp�content_typer��fields�field)�	dest_etagrb�
write_bodyrr�	on_header�s`
�����
�
� 	��z!Client.get_url.<locals>.on_headercs0�dr�dr|��d�}��|�dSdS)Nrr�)rri��chunk)�resultr�rr�on_chunk�s
�z Client.get_url.<locals>.on_chunkz.partcs�dr��|�dSdS)Nr)r�r�)�destfpr�rrr�s��r�
z
If-None-Match)�streamZstreaming_callbackZheader_callback�username�passwordrra�header_dict�statusi0�handlezError: {} reading {}�errorr��zHTTP error {0} reading {1}: {3}zError reading z: r$)Nr�r5r�r�r;r6r@�netlocr��sepr<rr�queryr�rhr%r'r�r��sanitize_win_pathr��string�ascii_lowercaserUrrFr��readr[r=rNr�Z
HASHES_REVMAP�	hashutils�get_hashrs�debug�AttributeErrorr�rO�	Exceptionr�ftplibZFTP�port�connect�hostnameZloginr�r�rzZ
retrbinaryr��quit�formatr	Z
get_object�rfind�
urlunparse�params�fragmentr�r�rur�rr�closerM�renamer��	HTTPErrorr�ZserverZBaseHTTPRequestHandler�	responses�URLError�reason)$r)r4rBrNr7Zno_cacherRr`rarb�url_dataZ
url_schemeZurl_pathZstrpathZfp_rqrC�formr�rSr�Zftp_portZremote_file_pathr�Z
swift_connZ
get_kwargsr�Zat_sign_posZ	fixed_urlr�r�Zdest_tmpr�r�r�r�r)r�r�r�r)rbr�rrc�s��
�

��

���
��	�
����������

��

F
"���
�


�
��


��

��

��������

�zClient.get_url�jinjacKs�d|vr	|�d�||d<|j|||d�}|rtj�|�sdS|tjjjvr4tjjj||fi|��}	nt	�
d|�dS|	dsJt	�
d|	d�dS|sV|j|||d�}d	}tj�|�}
tj�
|
�su|rjt�|
�ntjj�|	d�dSt�|	d|�|S)
z<
        Cache a file then process it as a template
        �envr7rfrVz7Attempted to render template with unavailable engine %sr�z(Failed to render template with error: %srqT)�poprdr;r6r�r%r'Z	templatesZTEMPLATE_REGISTRYrsr�r�r=r<rNrFZsafe_rmr{�move)r)r4rB�templaterNr7rRr}ZsfnrqrCrrr�get_templateBs4
�zClient.get_templatec	Cs*tj�|�}tjj��rtjj�|j	�}n|j	}|�
d�d}z|jr)|�dd�}Wn	t
y3Ynw|dur>|jd}ntj�|�sNtj�|jd|�}|jr[d�|j|jg�}n|j}tjj�|d||�}tjj�||g�}tjjj||d	d
�s}dSt|�tkr�tjj�|�}tjj�|d|||�S)z;
        Return the extrn_filepath for a given url
        r�r�r�rVNrR�-Zextrn_filesT)ZsubdirzInvalid path)r�r5r�r%r'r�r�r6r�r�rhr�r��
ValueErrorrr;rUr@r�r�ZverifyZ
clean_pathr��MAX_FILENAME_LENGTHr�Z
sha256_digest)	r)r4r7rRrr��	file_nameZ	root_path�new_pathrrrr�rs4��zClient._extrn_path)rINr$�rVFrINN�rIrV)rINNTF)rIFNNN�rI)rVrINN)FrIFNNTF)rFrIN) �__name__�
__module__�__qualname__�__doc__r*r-r0r8rH�
contextlib�contextmanagerrTrKr[r^rdrlrorxr~r�rmr�r�r�r�r�r�r�rcr
r�rrrrr#Ssj		

�
		
�


�:





 

@
�s
�0r#c@s�eZdZdZddd�Z	ddd	�Zd d
d�Zd dd
�Zd dd�Zddd�Z	ddd�Z
ddd�Zddd�Zdd�Z
dd�Zdd�ZdS)!rz6
    Used by pillar to handle fileclient requests
    rIcCstddd�}tjj�|�rtjj�|�}|jd�|g�D]}tj�	||�}tj�
|�r7||d<||d<|Sq|S)z&
        Locate the file path
        rV)r6�rel�pillar_rootsr6r)r%r'r4r�Zunescaperrr;r6r@rL)r)r6r7�fndrD�fullrrr�
_find_file�s
�zPillarClient._find_filerVFNc	Cs,|�|�}|�||�}|�d�}|sdS|S)z�
        Copies a file from the local files directory into :param:`dest`
        gzip compression settings are ignored for local files
        r6rV)r8r r)	r)r6rBrNr7rZrRr�fnd_pathrrrr[�s

zPillarClient.get_filec
	s�g}|�d�}�jd�|g�D]>}tjjjtj�||�dd�D]-\}}}�fdd�|D�|dd�<|D]}tj�	tj�||�|�}	|�
tjj�|	��q4q q|S)z�
        Return a list of files in the given environment
        with optional relative prefix path to limit directory traversal
        rprTr9c� g|]}tj��j|�s|�qSr�r%�
fileserverZis_file_ignoredr�r��dr/rrr���
��z*PillarClient.file_list.<locals>.<listcomp>N)
rurrr%r'r6r?r;r@�relpathrirqr)
r)r7r]rkr6rDrErF�fnamer(rr/rrm�s
�
���
zPillarClient.file_listc
s�g}|�d�}�jd�|g�D]6}tjjjtj�||�dd�D]%\}}}�fdd�|D�|dd�<|sE|sE|�	tjj
�tj�||���q q|S)z�
        List the empty dirs in the pillar_roots
        with optional relative prefix path to limit directory traversal
        rprTr9cr"rr#r%r/rrr��r'z4PillarClient.file_list_emptydirs.<locals>.<listcomp>N�
rurrr%r'r6r?r;r@rirqrr(�r)r7r]rkr6rDrErFrr/rr^�s
�
���	z PillarClient.file_list_emptydirsc
Cspg}|�d�}|jd�|g�D]%}tjjjtj�||�dd�D]\}}}|�	tjj
�tj�||���q q|S)z{
        List the dirs in the pillar_roots
        with optional relative prefix path to limit directory traversal
        rprTr9r*r+rrrr��s
� �zPillarClient.dir_listc
Cslz|�|�}Wn(ty/}ztj�|�s#t�d||�WYd}~dS|WYd}~Sd}~ww|�||�S)zU
        Return either a file path or the result of a remote find_file call.
        �5specified file %s is not present to generate hash: %sN)r8rr;r6rLrs�warningr )r)r6r7�errrrrZ__get_file_path�s���	zPillarClient.__get_file_pathcCspi}|�||�}|dur|Sz|d}Wnty|}Ynw|j�dt�}tjjj||d�|d<||d<|S)z�
        Return the hash of a file, to get the hash of a file in the pillar_roots
        prepend the path with salt://<file on server> otherwise, prepend the
        file with / for a local file.
        Nr6�	hash_type�r�hsum)	�_PillarClient__get_file_pathr�rrrr%r'r�r�)r)r6r7rkrr!r/rrr�	hash_file	s�zPillarClient.hash_filecCs�i}|�||�}|dur|dfSz|d}|�d�}Wn ty;|}z	tt�|��}Wnty8d}YnwYnw|j�dt�}t	j
jj||d�|d<||d<||fS)a7
        Return the hash of a file, to get the hash of a file in the pillar_roots
        prepend the path with salt://<file on server> otherwise, prepend the
        file with / for a local file.

        Additionally, return the stat result of the file, or None if no stat
        results were found.
        Nr6�statr/r0r1)
r2rr��listr;r4r�rrr%r'r�r�)r)r6r7rkrr!Zfnd_statr/rrr�hash_and_stat_file s(	���zPillarClient.hash_and_stat_filecCs
|�|�S)�W
        Return a list of the files in the file server's specified environment
        )rm)r)r7rrr�list_env?s
zPillarClient.list_envcCs|jS)�-
        Return the master opts data
        r.r/rrr�master_optsEszPillarClient.master_optscCs"g}|jdD]}|�|�q|S)z3
        Return the available environments
        r)rri)r)rkr7rrr�envsKszPillarClient.envscCr�)aX
        Originally returned information via the external_nodes subsystem.
        External_nodes was deprecated and removed in
        2014.1.6 in favor of master_tops (which had been around since pre-0.17).
             salt-call --local state.show_top
        ends up here, but master_tops has not been extended to support
        show_top in a completely local environment yet.  It's worth noting
        that originally this fn started with
            if 'external_nodes' not in opts: return {}
        So since external_nodes is gone now, we are just returning the
        empty dict.
        rr/rrr�master_topsTs
zPillarClient.master_topsrrr)rrrrr r[rmr^r�r2r3r6r8r:r;r<rrrrr�s

�






	rc@s�eZdZdZdd�Zdd�Zd*dd�Zd	d
�Z	
d+dd�Zd,dd�Z	d,dd�Z
d,dd�Zd,dd�Zd-dd�Z
d-dd�Zd-dd�Zd-dd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd
S).rz4
    Interact with the salt master file server.
    cCsJt�||�d|_tjjj�|j�|_t	|jd�r |jj
|_
dSd|_
dS)NFr�rV)r#r*�_closingr%�channelr�
ReqChannel�factoryr�hasattrr�r(rrrr*is
zRemoteClient.__init__cCs$|j��tjjj�|j�|_|jS)zD
        Reset the channel, in the event of an interruption
        )r>rr%rr?r@rr/rrr�_refresh_channelrs
zRemoteClient._refresh_channelFcCsJt��}z	|jj||d�WStjjy$tdtt��|��d���w)N��rawzFile client timed out after z seconds)	�time�	monotonicr>�sendr%�
exceptionsZSaltReqTimeoutErrorr�int)r)�loadrD�startrrr�
_channel_send|s���zRemoteClient._channel_sendcCsJ|jrdSd|_d}z|j}Wn	tyYnw|dur#|��dSdS)NT)r=r>r�r)r)r>rrr�destroy�s
��zRemoteClient.destroyrVrINcCs�tjj�|�\}}|r
|}tjj��s|�||�\}}	n|�||�}|dkr/t�	d||�dS|durUt
j�|�s>|�
d�rUt
j�|t
j�|��}t�	d|t
j�|�|�|}
|
s�|�|�}t�	d|||�|j|||d��
}|}
Wd�n1s{wYt�	d	||
|�|
r�t
j�|
�r�tjj��s�|�|
|�\}
}n|�|
|�}
|
|kr�|
St�	d
||�d}d}|�|�}||dd
�}|r�t|�}||d<d}|�r
t
j�|�}t
j�|��s|�rzt
�|�Wnt�y}z
|jtjkr��WYd}~nd}~wwdStjj�|d�}nt�	d�	|�sd|d<n|��|d<|j|dd�}t|�}z�|d�s�|�sw|d�rw|j|d||d��*}|}tjj�|d��}|�|d�Wd�n	1�sbwYWd�n	1�srwYd|v�r�|dk�r�|d7}tjj� |tjj!�"|�#dt$���}||dk�r�t�%d||�W�qWn�|�s�|j|d||d�� }|}t
j�|��r�tjj�&|�tjj'�(|d�}Wd�n	1�s�wY|�#dd��r�tjj)�*|d�}n|d}t+|t,��r�|�-�}|�|�WnQt.t/f�yU}zBzt0|�j1}Wnt2�y&t,t0|��}Ynw|d7}t�%d||||�|�3�|dk�rKt�4d|||�WYd}~n
WYd}~nd}~ww�q|�rh|�5�t�6d||�|St�	d||�|S)z�
        Get a single file from the salt-master
        path must be a salt server location, aka, salt://path/to/file, if
        dest is omitted, then the downloaded file will be placed in the minion
        cache
        rVz(Could not find file '%s' in saltenv '%s'FNr�z;In saltenv '%s', '%s' is a directory. Changing dest to '%s'z9In saltenv '%s', looking at rel_path '%s' to resolve '%s'rfz<In saltenv '%s', ** considering ** path '%s' to resolve '%s'z6Fetching file from saltenv '%s', ** attempting ** '%s'rZ_serve_file�r6r7�cmdrZzwb+zNo dest file foundT�locrCrqrBr1r�r�r/z(Bad download of file %s, attempt %d of 3zKData transport is broken, got: %s, type: %s, exception: %s, attempt %d of 3zTData transport is broken, got: %s, type: %s, exception: %s, retry attempts exhaustedz0Fetching file from saltenv '%s', ** done ** '%s'z3In saltenv '%s', we are ** missing ** the file '%s')7r%r'r4Z	split_envr�r�r6r3rsr�r;r6r<rrr@�basenamer=r8rTrLrIrNrOrPrQrFr��tellrLr"r�r�r�rvZto_strrrr-Zrm_rfZ
atomicfileZatomic_openZ	gzip_utilZ
uncompressrrg�encoder�r��typerr�rBr�rrt)r)r6rBrNr7rZrRr�Zhash_serverZstat_serverZ
dest2checkZrel_pathr�Z
hash_localZ
stat_localZd_triesZtransport_triesrJrwrCrSrqZofiler1Z	data_typerrrr[�s8	
�	
����
���

�	

����
���
��	��
�����V��zRemoteClient.get_filecC�||dd�}|�|�S)z.
        List the files on the master
        �
_file_list�r7r]rO�rL�r)r7r]rJrrrrmU��zRemoteClient.file_listcCrU)z3
        List the empty dirs on the master
        Z_file_list_emptydirsrWrXrYrrrr^^rZz RemoteClient.file_list_emptydirscCrU)z-
        List the dirs on the master
        Z	_dir_listrWrXrYrrrr�grZzRemoteClient.dir_listcCrU)z=
        List symlinked files and dirs on the master
        Z
_symlink_listrWrXrYrrrr�prZzRemoteClient.symlink_listc
Cs�z|�|�}WnCtyJ}z7tj�|�s&t�d||�idfWYd}~Si}|j�dt	�}t
jjj
||d�|d<||d<|WYd}~Sd}~ww||dd�}|�|�S)z;
        Common code for hashing and stating files
        r,Nr/r0r1Z
_file_hashrN)r8rr;r6rLrsr-rrrr%r'r�r�rL)r)r6r7r.rkr/rJrrrZ__hash_and_stat_fileys&����z!RemoteClient.__hash_and_stat_filecCs|�||�S)z�
        Return the hash of a file, to get the hash of a file on the salt
        master file server prepend the path with salt://<file on server>
        otherwise, prepend the file with / for a local file.
        )�!_RemoteClient__hash_and_stat_file)r)r6r7rrrr3�szRemoteClient.hash_filecCs�|�||�}z|�|�}Wn@tyM}z4tj�|�s%|dfWYd}~Sz|tt�|��fWWYd}~StyH|dfYWYd}~Swd}~ww||dd�}|�	|�}z
|�
d�}W||fStyqd}Y||fSw)zu
        The same as hash_file, but also return the file's mode, or None if no
        mode data is present.
        NZ
_file_findrNr4)r3r8rr;r6rLr5r4r�rLrr�)r)r6r7Zhash_resultr.rJr�stat_resultrrrr6�s0 ������zRemoteClient.hash_and_stat_filecCs|dd�}|�|�S)r7rV)r7rOrX)r)r7rJrrrr8�s
�zRemoteClient.list_envcC�ddi}|�|�S)z9
        Return a list of available environments
        rOZ
_file_envsrX�r)rJrrrr;���zRemoteClient.envscCr])r9rOZ_master_optsrXr^rrrr:�r_zRemoteClient.master_optscCs4d|jd|jd�}|jr|j�d�|d<|�|�S)zI
        Return the metadata derived from the master_tops system
        Z_master_tops�id)rOr`rssalt�tok)rr��	gen_tokenrLr^rrrr<�s�zRemoteClient.master_topscC�|Sr$rr/rrr�	__enter__��zRemoteClient.__enter__cGs|��dSr$)rM)r)�argsrrr�__exit__��zRemoteClient.__exit__�Frrr)rrrrr*rBrLrMr[rmr^r�r�r[r3r6r8r;r:r<rdrgrrrrrds,	


�
A
	
	
	
	


			rc@�eZdZdZdd�ZdS)rzi
    A local client that uses the RemoteClient but substitutes the channel for
    the FSChan object
    cCs,t�||�d|_tj�|�|_t�|_dS)NF)	r#r*r=r%r$ZFSChanr>�DumbAuthr�r(rrrr*�szFSClient.__init__N)rrrrr*rrrrr��rc@rj)rkza
    The dumbauth class is used to stub out auth calls fired from the FSClient
    subsystem
    cCs|Sr$r)r)Z	clear_tokrrrrb�rezDumbAuth.gen_tokenN)rrrrrbrrrrrk�rlrkc@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�ContextlessFileClientcCs
||_dSr$)r)r)rrrrr*�r1zContextlessFileClient.__init__cCst|j|�Sr$)�getattrr)r)r rrr�__getattr__�rhz!ContextlessFileClient.__getattr__cGsdSr$r)r)�_rrrrgrezContextlessFileClient.__exit__cCrcr$rr/rrrrdrezContextlessFileClient.__enter__N)rrrr*rorgrdrrrrrm�s
rmri);rrrPr�Zhttp.serverr��loggingr;r{r�rE�urllib.errorr��urllib.parseZsalt.channel.clientr%Zsalt.clientZ
salt.cryptZsalt.fileserverZsalt.loaderZsalt.payloadZsalt.utils.atomicfileZsalt.utils.dataZsalt.utils.filesZsalt.utils.gzip_utilZsalt.utils.hashutilsZsalt.utils.httpZsalt.utils.pathZsalt.utils.platformZsalt.utils.stringutilsZsalt.utils.templatesZsalt.utils.urlZsalt.utils.verifyZsalt.utils.versionsZsalt.configrZsalt.exceptionsrrrZsalt.ext.tornado.httputilrrrZsalt.utils.openstack.swiftr	�	getLoggerrrsrrr"r#rrrZLocalClientrkrmrrrr�<module>sn

QG}