%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/fileserver/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyo

�
���^c@@smdZddlmZmZmZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
m
Z
ddlmZd*Zd+Zdd
lmZyddlZeZWnek
r�eZnXddlZddlZddlZddlZddlZddlZddl Zddl!Zddl"m#Z#e
j$e%�Z&dZ'd�Z(d�Z)d�Z*d�Z+d�Z,d�Z-d�Z.d�Z/d�Z0d�Z1d�Z2d�Z3dd�Z5dd�Z6d�Z7d�Z8ed �Z9d	d!�Z:d"�Z;d#�Z<d$�Z=d%�Z>d&�Z?d'�Z@d(�ZAd)�ZBdS(,u�
Mercurial Fileserver Backend

To enable, add ``hgfs`` to the :conf_master:`fileserver_backend` option in the
Master config file.

.. code-block:: yaml

    fileserver_backend:
      - hgfs

.. note::
    ``hg`` also works here. Prior to the 2018.3.0 release, *only* ``hg`` would
    work.

After enabling this backend, branches, bookmarks, and tags in a remote
mercurial repository are exposed to salt as different environments. This
feature is managed by the :conf_master:`fileserver_backend` option in the salt
master config file.

This fileserver has an additional option :conf_master:`hgfs_branch_method` that
will set the desired branch method. Possible values are: ``branches``,
``bookmarks``, or ``mixed``. If using ``branches`` or ``mixed``, the
``default`` branch will be mapped to ``base``.


.. versionchanged:: 2014.1.0
    The :conf_master:`hgfs_base` master config parameter was added, allowing
    for a branch other than ``default`` to be used for the ``base``
    environment, and allowing for a ``base`` environment to be specified when
    using an :conf_master:`hgfs_branch_method` of ``bookmarks``.


:depends:   - mercurial
            - python bindings for mercurial (``python-hglib``)
i(tabsolute_importtprint_functiontunicode_literalsN(tdatetime(tFileserverConfigErrorubranchesu	bookmarksumixedubaseu
branch_methodu
mountpointuroot(tsix(ttagifyuhgcC@sZttdkrtSts+tjd�tStdtkrVtjdtdt�tStS(u-
    Only load if mercurial is available
    ufileserver_backendueMercurial fileserver backend is enabled in configuration but could not be loaded, is hglib installed?uhgfs_branch_methodu6Invalid hgfs_branch_method '%s'. Valid methods are: %s(t__virtualname__t__opts__tFalsetHAS_HGtlogterrortVALID_BRANCH_METHODS(((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt__virtual__Rs
cC@s
|j�S(u5
    Returns all branches for the specified repo
    (tbranches(trepo((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt
_all_branchesescC@sNy5gt|�D]}|d|kr|^qdSWntk
rItSXdS(u9
    Find the requested branch in the specified repo
    iN(Rt
IndexErrorR	(Rtnametx((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt_get_branchos5
cC@s|j�dS(u6
    Returns all bookmarks for the specified repo
    i(t	bookmarks(R((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt_all_bookmarksyscC@sNy5gt|�D]}|d|kr|^qdSWntk
rItSXdS(u;
    Find the requested bookmark in the specified repo
    iN(RRR	(RRR((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt
_get_bookmark�s5
cC@s-g|j�D]}|ddkr
|^q
S(u1
    Returns all tags for the specified repo
    iutip(ttags(RR((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt	_all_tags�s	cC@sNy5gt|�D]}|d|kr|^qdSWntk
rItSXdS(u6
    Find the requested tag in the specified repo
    iN(RRR	(RRR((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt_get_tag�s5
cC@s�|dkr|d}n||dks8|t�kr�|ddkrlt|d|�pkt|d|�S|ddkr�t|d|�p�t|d|�S|ddkr�t|d|�p�t|d|�p�t|d|�SntS(u1
    Return ref tuple if ref is in the repo.
    ubaseu
branch_methodubranchesurepou	bookmarksumixed(tenvsRRRR	(RR((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt_get_ref�s
cC@std��dS(uB
    Fatal fileserver configuration issue, raise an exception
    u$Failed to load hg fileserver backendN(R(((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt	_failhard�scC@stjjtdd�}t}g}i}x.tD]&}tjtdj|��||<q2Wx�tdD]�}t	j
|�}t|t�r�t
t|��}tgtjtjjj||��D]!\}}	|tj|	�f^q��}
|
s
tjd|�t�n|
jd|d�}|tkrXtjd||djt��t�nt}x=d	�|
D�D]+}tjd
||djt��t}qoW|r�t�n|j|
�n|}t|tj�s�tjd|�t�ny!tjjj|d�|d<Wntk
r$nXtt tjd
d��}
|
|�j!�}tjj||�}tjj"|�s�tj#|�ntj$|�s�t%j&|�t}nyt%j'|�}Wn]t%jj(k
r�tjd||�t�n-t)k
r }tjd||�t�nXy|j*dd�}Wnt%jj+k
rVd}nX|s�tjj|dd�}tjj-j.|d��9}|j/d�|j/tjj0j1dj|���WdQXn|ji|d6|d6|d6|d6tjjtdddj|��d6�|j2|�|j3�qgW|rtjjtdd�}y�tjj-j.|d��s}t4j5�j6d�}|j/d j|��x>|D]6}|j/tjj0j1d!j|d|d���q�WWdQXWnt7k
r�qXtj8d"|�n|S(#uE
    Return a list of hglib objects for the various hgfs remotes
    ucachediruhgfsuhgfs_{0}uhgfs_remotesu�Invalid per-remote configuration for hgfs remote %s. If no per-remote parameters are being specified, there may be a trailing colon after the URL, which should be removed. Check the master configuration file.u
branch_methodudInvalid branch_method '%s' for remote %s. Valid branch methods are: %s. This remote will be ignored.u, cs@s!|]}|tkr|VqdS(N(tPER_REMOTE_OVERRIDES(t.0R((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pys	<genexpr>�su|Invalid configuration parameter '%s' for remote %s. Valid parameters are: %s. See the documentation for further information.uZInvalid hgfs remote %s. Remotes must be strings, you may need to enclose the URL in quotesu
mountpointu	hash_typeumd5u�Cache path %s (corresponding remote: %s) exists but is not a valid mercurial repository. You will need to manually delete this directory on the master to continue to use this hgfs remote.u<Exception '%s' encountered while initializing hgfs remote %stnamesupathsu.hguhgrcuw+u[paths]
udefault = {0}
Nurepouurluhashu
{0}.update.lkulockfileuhgfs/remote_map.txtu%d %b %Y %H:%M:%S.%fu# hgfs_remote map as of {0}
u
{0} = {1}
uWrote new hgfs_remote map to %s(9tostpathtjoinRR	RRt	text_typetformattcopytdeepcopyt
isinstancetdicttnexttitert	iteritemstsalttutilstdatatrepack_dictlistRRRtgetR
tTruetupdatetstring_typesturltstrip_protot	TypeErrortgetattrthashlibt	hexdigesttisdirtmakedirstlistdirthglibtinittopentServerErrort	ExceptiontconfigtCommandErrortNonetfilestfopentwritetstringutilstto_strtappendtcloseRtnowtstrftimetOSErrortinfo(tbp_t
new_remotetrepostper_remote_defaultstparamtremotet	repo_conftrepo_urltkeytvaltper_remote_conft
branch_methodtper_remote_errorst	hash_typet	repo_hashtrp_Rtexctrefst
hgconfpaththgconfigt
remote_maptfp_t	timestamp((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyR@�s�
$J
	






	




	

,
cC@s�tjjtdd�}ytj|�}Wntk
rEg}nXt�}x7|D]/}y|j|d�WqVtk
r�qVXqVWg}xW|D]O}|dkr�q�ntjj||�}tjj	|�r�|j
|�q�q�Wg}|r`xh|D]]}ytj|�Wn3tk
rH}	t
jd||	�|j
|�q�Xt
jd|�q�Wnx|D]}
|j|
�qgWt|�|fS(uC
    Remove cache directories for remotes no longer configured
    ucachediruhgfsuhashurefsu0Unable to remove old hgfs remote cachedir %s: %suhgfs removed old cachedir %s(uhashurefs(R"R#R$RR>RPR@tremovet
ValueErrorR<RLtshutiltrmtreeRRtdebugtbool(RRtcachedir_lsRTRt	to_removetitemR#tfailedtrdirRbtfdir((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt_clear_old_remotesXs@

	





cC@s�tjjtdd�}tjjtdd�}g}xi||fD][}tjj|�rEytj|�Wq�tk
r�}|jdj	||��q�XqEqEW|S(u%
    Completely clear hgfs cache
    ucachediruhgfsufile_lists/hgfsuUnable to delete {0}: {1}(
R"R#R$RtexistsRkRlRPRLR&(tfsb_cachedirt
list_cachedirterrorsRsRb((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pytclear_cache�s$cC@s�d�}t|t�r"||�Sg}g}x�t�D]�}|r�y tj|d|�scw8nWq�tk
r�tj|dtj|��s�q8q�q�Xn||�\}}|j|�|j|�q8W||fS(u�
    Clear update.lk

    ``remote`` can either be a dictionary containing repo configuration
    information, or a pattern. If the latter, then remotes for which the URL
    matches the pattern will be locked.
    cS@s�d�}g}g}tjj|d�r�ytj|d�Wnstk
r�}|jtjkr�ytj|d�Wq�tk
r�}||||�q�Xq�||||�q�Xdj	|d�}t
j|�|j|�n||fS(NcS@s;dj|d|d|�}tj|�|j|�dS(Nu0Unable to remove update lock for {0} ({1}): {2} uurlulockfile(R&RRmRL(terrlistRRbtmsg((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt
_add_error�s
ulockfileuRemoved lock for {0}uurl(
R"R#RvRiRPterrnotEISDIRRkRlR&RRmRL(RR}tsuccessRrRbR|((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt_do_clear_lock�s"	
uurl(R)R*R@tfnmatchR8RR%textend(RWR�tclearedRyRR�Rr((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt
clear_lock�s"	




cC@s�d�}t|t�r"||�Sg}g}x�t�D]�}|r�y tj|d|�scw8nWq�tk
r�tj|dtj|��s�q8q�q�Xn||�\}}|j|�|j|�q8W||fS(u�
    Place an update.lk

    ``remote`` can either be a dictionary containing repo configuration
    information, or a pattern. If the latter, then remotes for which the URL
    matches the pattern will be locked.
    cS@s�g}g}tjj|d�s�y'tjjj|dd��WdQXWnPttfk
r�}dj	|d|d|�}t
j|�|j|�q�Xdj	|d�}t
j|�|j|�n||fS(Nulockfileuwu-Unable to set update lock for {0} ({1}): {2} uurluSet lock for {0}(
R"R#RvR.R/RGRHtIOErrorRPR&RRmRL(RR�RrRbR|((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt_do_lock�s


uurl(R)R*R@R�R8RR%R�(RWR�tlockedRyRR�Rr((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pytlock�s"	




c
C@s�itd6dd6}t�\|d<}x:|D]2}tjj|d�rktjd|d|d�q.nt|�\}}|r�tjd|d�q.ntj	d|d�|d	j
�|d	j�}y|d	j�Wn3t
k
r}tjd
||ddtj�n2X|d	j�}|d|dkrHt|d<n|d	j�t|�q.Wtjjtd
d�}|jdt�tks�tjj|�rGtjj|�}	tjj|	�s�tj|	�ntdt�}
tjjt�}tjjj|d��-}|j |j!|
��tj"d|�WdQXntjdt�r�tjj#j$dtdtddtdt�}
|
j%|t&ddgdd��ny*tj'j(tjjtd
d�t)�Wnt*t+fk
r�nXdS(u0
    Execute an hg pull on all of the repos
    uchangeduhgfsubackendulockfileu�Update lockfile is present for hgfs remote %s, skipping. If this warning persists, it is possible that the update process was interrupted. Removing %s or running 'salt-run fileserver.clear_lock hgfs' will allow updates to continue for this remote.uurlu7Unable to set update lock for hgfs remote %s, skipping.uhgfs is fetching from %surepou1Exception %s caught while updating hgfs remote %stexc_info_on_logleveliucachediruhgfs/envs.ptignore_cacheuwb+uWrote env cache data to %sNufileserver_eventsumasterusock_diru	transporttoptstlistenuupdatetprefixu
fileserveru	hgfs/hash(,R	RuR"R#RvRtwarningR�RRmRAttiptpullRCtloggingtDEBUGR3RMR�R$RR2tisfiletdirnameR=RR.tpayloadtSerialR/RGRHRItdumpsttraceteventt	get_eventt
fire_eventRt
fileservertreap_fileserver_cache_dirt	find_fileR�RP(R0RTRt_RytcurtipRbtnewtipt	env_cachetenv_cachedirtnew_envstserialRgR�((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyR4�sj



+
	%	cC@s�tdr-tjjjdd�td}n
td}tdrdtjjjdd�td}n
td}tjjj|d|d	|�S(
uc
    Check if an environment is exposed by comparing it against a whitelist and
    blacklist.
    uhgfs_env_whitelistuNeonurThe hgfs_env_whitelist config option has been renamed to hgfs_saltenv_whitelist. Please update your configuration.uhgfs_saltenv_whitelistuhgfs_env_blacklisturThe hgfs_env_blacklist config option has been renamed to hgfs_saltenv_blacklist. Please update your configuration.uhgfs_saltenv_blacklistt	whitelistt	blacklist(RR.R/tversionst
warn_untilRJtcheck_whitelist_blacklist(tenvR�R�((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt_env_is_exposedBs 





c
C@s�|sGtjjtdd�}tjjt|�}|d
k	rG|Snt�}xt	�D]}|dj
�|ddkr�xKt|d�D]6}|d}||dkr�d}n|j|�q�Wn|ddkr*xKt
|d�D]6}|d}||dkrd}n|j|�q�Wn|jgt|d�D]}	|	d^qA�|dj�qZWgt|�D]}	t|	�rw|	^qwS(
u@
    Return a list of refs that can be used as environments
    ucachediruhgfs/envs.purepou
branch_methodubranchesumixediubaseu	bookmarksN(ubranchesumixed(u	bookmarksumixed(R"R#R$RR.R�tcheck_env_cacheRFtsetR@RARtaddRR4RRMtsortedR�(
R�R�tcache_matchtretRtbranchtbranch_nametbookmarkt
bookmark_nameR((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyRbs.	
	
	.c
K@sjidd6dd6}tjj|�s5|t�kr9|Stjjtdd||�}tjjtdd|dj|��}tjjtdd|dj|��}tjjtdd|d	j|��}tjj|�}tjj|�}	tjj|�sHytj	|�WqHt
k
rDtj|�tj	|�qHXntjj|	�s�ytj	|	�Wq�t
k
r�tj|	�tj	|	�q�Xnx�t�D]�}
|
d
r�|j
|
d
tjj�r�q�n|t|
d
�jtjj�}|
dr#tjj|
d|�}n|
dj�t|
|�}|sZ|
dj�q�ntjj||�tjj|�r�tjj|�r�tjjj|d
��I}
|
j�}||dkr�||d<||d<|
dj�|SWdQXny1|
djdj|�gd|dd|�Wn(tjjk
rR|
dj�q�nXtjjj|d��WdQXx<t j |�D]+}ytj|�Wq�t!k
r�q�Xq�Wtjjj|d��}
|
j"|d�WdQXytj|�Wnt
t#fk
r
nX||d<||d<yt$tj%|��|d<Wnt!k
rRnX|
dj�|SW|S(u
    Find the first file to match the path and ref, read the file out of hg
    and send the path to the newly cached file
    uupathurelucachediru	hgfs/refsu	hgfs/hashu
{0}.hash.*u{0}.hash.blob_sha1u{0}.lku
mountpointurooturepouriNupath:{0}trevtoutputuwuw+ustat(&R"R#tisabsRR$RR&R�R<R=RPRiR@t
startswithtseptlentlstripRARRMR.R�t	wait_lockR�R/RGRHtreadtcatR?RREtglobRCRIR�tlisttstat(R#ttgt_envtkwargstfndtdestthashes_globtblobshadesttlk_fntdestdirthashdirRt	repo_pathtrefRgtshatfilename((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyR�s�

!




#
$



'




c
@sOd�kr�jd�nidd6dd6}t�fd�dD��sP|S|ds^|S|d	|d<�jd
d
�}tjj|d�}tjj	j
|d���}|j�d�|jt
d�}|r
tjr
tjj	j|�r
|jt�}n|r;|r;tjjj||�}||d
<n||d<Wd
QX|S(u?
    Return a chunk from a file based on the data received
    uenvuudataudestc3@s|]}|�kVqdS(N((R R(tload(s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pys	<genexpr>�supathulocusaltenvurelugzipurbufile_buffer_sizeN(upathulocusaltenv(tpoptallR2RFR"R#tnormpathR.R/RGRHtseekR�RRtPY3t	is_binarytdecodet__salt_system_encoding__t	gzip_utiltcompress(R�R�R�tgziptfpathRgR0((R�s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt
serve_file�s*


%
c@sDd�kr�jd�nt�fd�dD��s<dSitdd6}|d}|d}tjjtdd	�dd
j|td��}tjj|�s�tj	j
j|td�|d<tj	jj
|d��}|j|d�Wd
QX|Stj	jj
|d��&}tj	jj|j��|d<Wd
QX|Sd
S(uL
    Return a file hash, the hash type is set in the master config file
    uenvc3@s|]}|�kVqdS(N((R R(R�(s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pys	<genexpr>supathusaltenvuu	hash_typeurelucachediru	hgfs/hashu{0}.hash.{1}uhsumuw+Nurb(upathusaltenv(R�R�RR"R#R$R&R�R.R/t	hashutilstget_hashRGRHRIRJt
to_unicodeR�(R�R�R�trelpathR#thashdestRg((R�s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt	file_hashs(

	 %c	C@s]d|kr|jd�ntjjtdd�}tjj|�s�ytj|�Wq�tjk
rtj	d|�gSXntjj|dj
|d��}tjj|dj
|d��}tjj
t|||�\}}}|d
k	r�|S|rYi}t|�|d<t|�|d	<|rItjjt|||�n|j|g�SgS(uD
    Return a dict containing the file lists for files and dirs
    uenvucachedirufile_lists/hgfsuUnable to make cachedir %su{0}.pusaltenvu.{0}.wufilesudirsN(R�R"R#R$RR<R=RRtcriticalR&R.R�tcheck_file_list_cacheRFt_get_file_listt
_get_dir_listtwrite_file_list_cacheR2(	R�tformRxt
list_cachetw_lockR�t
refresh_cachet
save_cacheR�((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt_file_lists#s0""		cC@s
t|d�S(uR
    Return a list of all files on the file server in a specified environment
    ufiles(R�(R�((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt	file_listGscC@sd|kr|jd�nd|ks;|dt�kr?gSt�}x�t�D]�}|dj�t||d�}|r�|djd|d�}x`|D]U}tjj	|d|d�}|j
d�s�|jtjj|d	|��q�q�Wn|dj
�qRWt|�S(
uO
    Get a list of all files on the file server in a specified environment
    uenvusaltenvurepoR�iiurootu../u
mountpoint(R�RR�R@RARtmanifestR"R#R�R�R�R$RMR�(R�R�RR�R�ttupR�((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyR�Ns 	
*cC@sgS(u>
    Return a list of all empty directories on the master
    ((R�((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pytfile_list_emptydirsgscC@s
t|d�S(u8
    Return a list of all directories on the master
    udirs(R�(R�((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pytdir_listosc	C@s�d|kr|jd�nd|ks;|dt�kr?gSt�}xt�D]}|dj�t||d�}|rV|djd|d�}x�|D]�}|d}|jdd�}x�t|�dkrNt	j
j|d|d	�}|d
kr5|jd�s5|j
t	j
j|d|��q5n|djdd�}q�Wq�Wn|dj�qRW|dr�|j
|d�nt|�S(
u5
    Get a list of all directories on the master
    uenvusaltenvurepoR�iiu/iurootu.u../u
mountpoint(R�RR�R@RARR�trsplitR�R"R#R�R�R�R$RMR�(	R�R�RR�R�R�tfilepathtsplitR�((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyR�vs.	

&!
(ubranchesu	bookmarksumixed(ubaseu
branch_methodu
mountpointuroot(Ct__doc__t
__future__RRRR'R~R�R�R:R�R"RkRtsalt.exceptionsRR
Rtsalt.extRR?R3R
tImportErrorR	tsalt.utils.dataR.tsalt.utils.filestsalt.utils.gzip_utiltsalt.utils.hashutilstsalt.utils.stringutilstsalt.utils.urltsalt.utils.versionstsalt.fileservertsalt.utils.eventRt	getLoggert__name__RRRRRRRRRRRR@RuRzRFR�R�R4R�RR�R�R�R�R�R�R�R�R�(((s8/usr/lib/python2.7/site-packages/salt/fileserver/hgfs.pyt<module>%sp


		
	
		
		
				�	)	8/	J	 j			$				

Zerion Mini Shell 1.0