%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/utils/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/utils/files.pyc

�
���^c@@sxdZddlmZmZmZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlZddlmZmZmZddlmZddlmZddlmZddlm Z yddl!Z!e"Z#Wne$k
rae%Z#nXej&e'�Z(dBZ)dCZ*dDe*Z+dZ,idd6dd6dd6dd6dd6dd6Z-e.gej/e-�D]\Z0Z1e1e0f^q��Z2d�Z3d�Z4d �Z5d!�Z6d	d	d"�Z7d#�Z8dd$�Z:ej;dd%d&dd'��Z<d(�Z=ej;d)��Z>d*�Z?ej;d+��Z@ej;d,��ZAe"de"dd-�ZBd.�ZCd/�ZDed0�d1��ZEe%d2�ZFd3�ZGdd4�ZHed5�d6d7��ZIed8�d9��ZJd:�ZKed;�d<��ZLd=�ZMd>�ZNd?�ZOd@�ZPdA�ZQdS(Eu"
Functions for working with files
i(tabsolute_importtunicode_literalstprint_functionN(tCommandExecutionErrort
FileLockErrortMinionError(tjinja_filter(tsix(trange(tquoteuufileuhttpuhttpsuftpuswiftus3usaltu__salt.tmp.i�usha512i`usha384i@usha256i8usha224i(usha1i umd5cC@s&yt|�Wntk
r!nXdS(u 
    Remove temporary files
    N(trm_rft	Exception(ttmp((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyt__clean_tmp:s
c
C@s`|j�}x%dD]}|jd|�rdSqWx%dD]}|jd|�r;|Sq;WdS(uH
    Guess an archive type (tar, zip, or rar) by its file extension
    utarutar.gzutgzutar.bz2utbz2utbzutar.xzutxzutar.lzmautlzu.uzipurar(
utarutar.gzutgzutar.bz2utbz2utbzutar.xzutxzutar.lzmautlz(uzipurarN(tlowertendswithtNone(tnametending((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytguess_archive_typeDs

cO@smd|krd|d<n|jdt�}tj||�\}}|tkrY||fStj|�~|S(u
    Helper function which does exactly what ``tempfile.mkstemp()`` does but
    accepts another argument, ``close_fd``, which, by default, is true and closes
    the fd before returning the file path. Something commonly done throughout
    Salt's code.
    uprefixu__salt.tmp.uclose_fd(tpoptTruettempfiletmkstemptFalsetostclose(targstkwargstclose_fdtfd_tf_path((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyRUs


c
C@s�x�tjjj|�D]�\}}}|j|d�jtj�}tjj||�}tjj	|�sztj
|�nxK|D]C}tjj|||�}tjj||�}	tj||	�q�WqWdS(u�
    Recursively copy the source directory to the destination,
    leaving files with the source does not explicitly overwrite.

    (identical to cp -r on a unix machine)
    uN(
tsalttutilstpathtos_walktreplacetlstripRtseptjointexiststmakedirstshutiltcopyfile(
tsourcetdesttroott_tfilestpath_from_sourcettarget_directoryRtfile_path_from_sourcettarget_path((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytrecursive_copygs%
c
C@s�tjj|�s*tdj|���ntjjtjj|��s`tdj|���ntjj|�}tjjtjj|��}t	d|d|�}t
j||�d}|r�tjj|d�}n|dks�|dkr|rtjj
|�rt||�qn|dks=|dkr=|r=nd}tjjj�sytj|�}Wqtk
r{qXnyt
j||�Wntk
r�t|��nX|dk	r�tj||j|j�tj||j�ntjjjd	�}	|	r�t}
ytj j!j"�}
Wnt#t$fk
r?nX|
d
kr�t%tj&d��,}|	|g}t'j(|d|d
|�WdQXq�ntjj|�r�t|�ndS(uj
    Copy files from a source to a destination in an atomic way, and if
    specified cache the file.
    u([Errno 2] No such file or directory: {0}tprefixtdiruufile_backupuminionubothumasteru
restoreconu	EnforcinguwtstdouttstderrN()RR"tisfiletIOErrortformattisdirtdirnametbasenametabspathRR*R+R'R(t
backup_minionRR R!tplatformt
is_windowststattOSErrortmoveRR
tchowntst_uidtst_gidtchmodtst_modetwhichRtmodulestselinuxt
getenforcetImportErrorRtfopentdevnullt
subprocesstcall(
R,R-tbackup_modetcachedirtbnametdnamettgttbkroottfstattrcontpolicytdev_nulltcmd((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyR+ysZ


%cC@s�ytj||�Wn�tk
r�}|jtjkr>�nytj|�WnCtk
r�}|jtjkr�tdj||j	���q�nXtj||�nXdS(u�
    On Windows, os.rename() will fail with a WindowsError exception if a file
    exists at the destination path. This function checks for this error and if
    found, it deletes the destination path first.
    u Error: Unable to remove {0}: {1}N(
RtrenameREterrnotEEXISTtremovetENOENTRR<tstrerror(tsrctdsttexc((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyR`�scC@s�|dk	r-t|tj�r3|f}q3nd}|j|krFdS|jtjkrptdj|���nK|jtjkr�tdj|���n!tdj|j||j	���dS(u�
    Common code for raising exceptions when reading a file fails

    The ignore argument can be an iterable of integer error codes (or a single
    integer error code) that should be ignored.
    Nu{0} does not existu"Permission denied reading from {0}u+Error {0} encountered reading from {1}: {2}((
Rt
isinstanceRt
integer_typesRaRdRR<tEACCESRe(RhR"tignore((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytprocess_read_exception�sig�������?c
#@s%t|tj�s!td��n|dkr:|d}n�dkrUtj��nt}t�fd�}z�yDtjj	|�r�tjj
|�r�|dj|��ntjtj
BtjB}x�tj��|kr�ytj||�}Wnmttfk
rc}	|	jtjkr@|dj|	j||	j��ntjd||�tj|�q�Xtj|d��WdQXtjd	|�t}dVPq�W|d
j||��Wn<tk
r��n)tk
r�}	|dj||	��nXWd|r tj|�tjd||�nXdS(
uJ
    Obtain a write lock. If one exists, wait for it to release first
    upath must be a stringu.wc@st|d���dS(u'
        Raise a FileLockError
        t
time_startN(R(tmsgtrace(Rn(s4/usr/lib/python2.7/site-packages/salt/utils/files.pyt_raise_error�su$lock_fn {0} exists and is not a fileu2Error {0} encountered obtaining file lock {1}: {2}u(Lock file %s exists, sleeping %f secondsuwNuWrite lock %s obtaineduFTimeout of {0} seconds exceeded waiting for lock_fn {1} to be releasedu.Error encountered obtaining file lock {0}: {1}uWrite lock for %s (%s) released(RiRtstring_typesRRttimeRRR"R(R:R<tO_CREATtO_EXCLtO_WRONLYtopenR;RERaRbRetlogttracetsleeptfdopenRRRc(
R"tlock_fnttimeoutRzRnt
obtained_lockRqt
open_flagstfh_Rh((Rns4/usr/lib/python2.7/site-packages/salt/utils/files.pyt	wait_lock�sX
%


cC@s tjd�}tj|�|S(u#
    Returns the current umask
    i(Rtumask(tret((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyt	get_umask;s
cc@sS|dkstjjj�r&dVn)ztj|�}dVWdtj|�XdS(uM
    Temporarily set the umask and restore once the contextmanager exits
    N(RR R!RBRCRR�(tmaskt	orig_mask((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyt	set_umaskDs	cO@s�tjrPy0|dd
kr8tdj|d���nWqPtk
rLqPXnd}tjr}tjj	j
�r}d|ks�|jdt�rot
|�dkrt|�}d|dkrl|djdd�|d<d|dkr�|dcd7<q�qlq|jd�rbd|dkrl|djdd�|d<d|dkr_|dcd7<q_qlqd	|d<n�tjrd
|krt}t
|�dkr�t|�}d|dkr�t}q�n|jdd�r�d|dkr�t}q�n|st|d
<qntjr8|r8|jdd�r8d|d<nt||�}t�r�y
tj}Wntk
rvd}nXtj|j�tj�}tj|j�tj||B�n|S(u�
    Wrapper around open() built-in to set CLOEXEC on the fd.

    This flag specifies that the file descriptor should be closed when an exec
    function is invoked;

    When a file descriptor is allocated (as with open or dup), this bit is
    initially cleared on the new file descriptor, meaning that descriptor will
    survive into the new program after exec.

    NB! We still have small race condition between open and fcntl.
    iiiu&{0} is not a permitted file descriptorubinaryubutumodeurbuencodingunewlineu(iiiN(RtPY3t	TypeErrorR<t
IndexErrorRtPY2R R!RBRCRRtlentlistR$tgetRt__salt_system_encoding__Rwtis_fcntl_availabletfcntlt
FD_CLOEXECtAttributeErrortfilenotF_GETFDtF_SETFD(RRtbinarytf_handleR�t	old_flags((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyRQTsX
	
'
#
	


#co@s�|d|d}}d}t|||���}zutdt�r�tj}|r�tg|D]}||dk^q[�r�tj}ntj|j�|�n|VWdtdt�r�tj|j�tj	�nXWdQXdS(u;
    Shortcut for fopen with lock and context manager.
    iiuwatcheck_sunosN(
RQR�RR�tLOCK_SHtanytLOCK_EXtflockR�tLOCK_UN(RRtfilenametwritingR�t	lock_typetwrite((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytflopen�s	/	c

o@s,|jdd�}|jdd�}|jdd�}t||���}|d}tj|�}ttd�r�|j|ks�|j|kr�g||fD]}|dkr�|^q�r�tj|||�q�n|dk	rtj	|j
�}	|	|krtj||j
|	A|B�qn|VWdQXdS(u�
    Shortcut for fopen with extra uid, gid, and mode options.

    Supported optional Keyword Arguments:

    mode
        Explicit mode to set. Mode is anything os.chmod would accept
        as input for mode. Works only on unix/unix-like systems.

    uid
        The uid to set, if not set, or it is None or -1 no changes are
        made. Same applies if the path is already owned by this uid.
        Must be int. Works only on unix/unix-like systems.

    gid
        The gid to set, if not set, or it is None or -1 no changes are
        made. Same applies if the path is already owned by this gid.
        Must be int. Works only on unix/unix-like systems.

    uuidi����ugidumodeiuchownN(RRRQRRDthasattrRHRIRGtS_IMODERKRJ(
RRtuidtgidtmodeR�R"td_stattit	mode_part((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytfpopen�s
+!cc@s�|dkrt�}nytj|�}Wn0tjk
r]}|dk	rY||�ndSX|r�tj|�}|jdkr�|j|jf}||kr�dS|j|�q�ngg}	}
xR|D]J}tj	j
||�}tj	j|�r|	j|�q�|
j|�q�W|r0||	|
fVnxg|	D]_}tj	j
||�}
|sktj	j
|
�r7x(t|
||||�D]}|Vq�Wq7q7W|s�||	|
fVndS(u�
    A clone of the python os.walk function with some checks for recursive
    symlinks. Unlike os.walk this follows symlinks by default.
    Ni(RtsetRtlistdirterrorRDtst_inotst_devtaddR"R'R=tappendtislinkt	safe_walk(ttopttopdowntonerrortfollowlinkst_seentnamesterrtstatustnodetdirstnondirsRt	full_pathtnew_pathtx((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyR��s<



cC@s/ytj|�Wnttfk
r*nXdS(u
    Safely remove a file
    N(RRcR;RE(RY((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytsafe_rmscC@s�d�}tjj|�s.tjj|�r>tj|�nUtjjj�r�ytjj	j
|�}Wq�tk
r|q�Xntj
|d|�dS(uj
    Platform-independent recursive delete. Includes code from
    http://stackoverflow.com/a/2656405
    cS@sOtjjj�rHtj|tj�rHtj|tj	�||�n�dS(u/
        Error handler for `shutil.rmtree`.

        If the error is due to an access error (read only file)
        it attempts to add write permission and then retries.

        If the error is for another reason it re-raises the error.

        Usage : `shutil.rmtree(path, onerror=onerror)`
        N(
R R!RBRCRtaccesstW_OKRJRDtS_IWUSR(tfuncR"texc_info((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyt_onerror s(
R�N(RR"R�R=RcR R!RBRCtstringutilst
to_unicodeR�R*trmtree(R"R�((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyR
s	%
uis_emptycC@s3ytj|�jdkSWntk
r.tSXdS(u
    Is a file empty?
    iN(RRDtst_sizeRER(R�((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytis_empty<s
cC@s |rtjjj�rtStS(u�
    Simple function to check if the ``fcntl`` module is available or not.

    If ``check_sunos`` is passed as ``True`` an additional check to see if host is
    SunOS is also made. For additional information see: http://goo.gl/159FF8
    (R R!RBtis_sunosRt	HAS_FCNTL(R�((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyR�HscC@sbd�}t|tj�sFtjd|tj|d�jdd��Stjd||dtj�S(u�
    Input the basename of a file, without the directory tree, and returns a safe name to use
    i.e. only the required characters are converted by urllib.quote
    If the input is a PY2 String, output a PY2 String. If input is Unicode output Unicode.
    For consistency all platforms are treated the same. Hard coded to utf8 as its ascii compatible
    windows is \ / : * ? " < > | posix is /

    .. versionadded:: 2017.7.2

    :codeauthor: Damon Atkins <https://github.com/damon-atkins>
    cS@st|jd�dd�S(Nitsafeu(R	tgroup(tre_obj((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyt_replace`su[\\:/*?"<>|]uutf8uasciiubackslashreplacetflags(RiRt	text_typetretsubtencodetUNICODE(t
file_basenameR�((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytsafe_filename_leafTs		cC@s�|stj}nt|tj�rIt|tj�rI|jd�}ntjj|�\}}|jg|j	|�D]}t
|�^qw�}|r�|j||g�}n|S(u3
    Input the full path and filename, splits on directory separator and calls safe_filename_leaf for
    each part of the path. dir_sep allows coder to force a directory separate to a particular character

    .. versionadded:: 2017.7.2

    :codeauthor: Damon Atkins <https://github.com/damon-atkins>
    uascii(RR&RiRR�R�R"t
splitdriveR'trsplitR�(tfile_path_nametdir_septdriveR"tfile_section((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyt
safe_filepathks	%1uis_text_fileic@s tjrd�nt�dj�fd�tdd�D��d}y|j|�}WnQtk
r�y+t|d��}|j|�}WdQXWq�tk
r�t	SXnXd	|kr�t	S|s�t
Sy|jd
�t
SWntk
r�nX|j
d|�}tt|��t|�dkS(u
    Uses heuristics to guess whether the given file is text or binary,
    by reading a single block of bytes from the file.
    If more than 30% of the chars in the block are non-text, or there
    are NUL ('') bytes in the block, assume this is a binary file.
    cS@s
t|f�S(N(tbytes(R�((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyt<lambda>�stc3@s|]}�|�VqdS(N((t.0R�(tint2byte(s4/usr/lib/python2.7/site-packages/salt/utils/files.pys	<genexpr>�si is

	urbNtuutf-8g333333�?(RR�tchrR'RtreadR�RQR;RRtdecodetUnicodeDecodeErrort	translateRtfloatR�(tfp_t	blocksizettext_characterstblocktfp2_tnontext((R�s4/usr/lib/python2.7/site-packages/salt/utils/files.pytis_text�s,%

	

uis_bin_filec	C@s�tjj|�stSyrt|d��]}yA|jd�}tjrX|jt	�}nt
jjj
|�SWntk
r�tSXWdQXWntjk
r�tSXdS(u�
    Detects if the file is a binary, returns bool. Returns True if the file is
    a bin, False if the file is not and None if the file is not available.
    urbiN(RR"R:RRQR�RR�R�R�R R!R�t	is_binaryR�RR�(R"R�tdata((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyR��s	
cC@sCytj|�Wn+tk
r>}|jtjkr?�q?nXdS(uS
    Runs os.remove(path) and suppresses the OSError if the file doesn't exist
    N(RRcRERaRd(R"Rh((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyRc�s
u
list_filescC@s�t�}|j|�xwt|�D]i\}}}x*|D]"}|jtjj||��q9Wx*|D]"}|jtjj||��qfWq#Wt|�S(uS
    Return a list of all files found under directory (and its subdirectories)
    (R�R�R�RR"R'R�(t	directoryR�R.R�R0R((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyt
list_files�s	

 
$cC@s1yt|�dSWnttfk
r,dSXdS(uj
    Convert the st_mode value from a stat(2) call (as returned from os.stat())
    to an octal mode.
    i����uN(toctR�R�(R�((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytst_mode_to_octal�scC@sz|dkrdSt|tj�s4tj|�}ntjrR|jdd�}n|jd�jd�jd�j	d�S(u�
    Return a mode value, normalized to a string and containing a leading zero
    if it does not have one.

    Allow "keep" as a valid mode (used by file state/module to preserve mode
    from the Salt fileserver in file states).
    u0ou0u"u'iN(
RRiRRrR�R�R$tstripR%tzfill(R�((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytnormalize_mode�s	cC@s�idd6dd6dd6dd6d	d
6}tj|�}tjd|�}|s_td��nt|jd��}d
|j|jd�d�}||S(u/
    Convert human-readable units to bytes
    iuKiuMiuGiuTiuPu^(\d+)([KMGTP])?$uFSize must be all digits, with an optional unit type (K, M, G, T, or P)ii(RR�R�tmatcht
ValueErrortintR�R�(t
human_sizetsize_exp_mapthuman_size_strRtsize_numtunit_multiplier((s4/usr/lib/python2.7/site-packages/salt/utils/files.pythuman_size_to_bytes�s)c	C@s�tjj|�\}}tjjj�r?|jdd�}n
|d}tjjj�smtj|�}nt	j
ttj�d��d}tjjj�r�tj
d�}ntj
d�}dj|d	 ||d	�}tjj||d
j||��}tjjtjj|��s>tjtjj|��ntj||�tjjj�s�tj||j|j�tj||j�ndS(u%
    Backup a file on the minion
    u:u_ii@Bi����u%a_%b_%d_%H-%M-%S_%Yu%a_%b_%d_%H:%M:%S_%Yu
{0}{1}_{2}i����u{0}_{1}N(RR"tsplitR R!RBRCR$RDRR�RRststrftimeR<R'R=R>R)R*R+RGRHRIRJRK(	R"RZRXRWtsrc_dirR[tmsecststamptbkpath((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyRAs(
#cC@s�d�}d�}d�}d�}tjj|�sEtd��ny+t|d��}|jd�}WdQXWn tjk
r�td	��nX||�}|r�|S||�r�d
S||�r�tS||�r�dStd��dS(
uo
    Detect a file's encoding using the following:
    - Check for Byte Order Marks (BOM)
    - Check for UTF-8 Markers
    - Check System Encoding
    - Check for ascii

    Args:

        path (str): The path to the file to check

    Returns:
        str: The encoding of the file

    Raises:
        CommandExecutionError: If the encoding cannot be detected
    cS@s;y|jd�tjd�Wntk
r2tSXtSdS(NuASCIIuFound ASCII(R�RxtdebugR�RR(t_data((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytcheck_ascii7s

cS@sCdtjjjtj�fdtjjjtj�fdtjjjtj�fdtjjjtj�fdtjjjtj	�fdtjjjd�fdtjjjd�fdtjjjd	�fdtjjjd
�fdtjjjd�fg
}x7|D]/\}}|j
|�rtjd|�|SqWt
S(
Nu	UTF-32-BEu	UTF-32-LEu	UTF-16-BEu	UTF-16-LEuUTF-8uUTF-7u+/v8-u+/v8u+/v9u+/v+u+/v/uFound BOM for %s(R R!R�tto_bytestcodecstBOM_UTF32_BEtBOM_UTF32_LEtBOM_UTF16_BEtBOM_UTF16_LEtBOM_UTF8t
startswithRxRR(Rtbomst	_encodingtbom((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyt	check_bomAscS@ssy|jd�}Wntk
r'tSXtjrkx7|D],}dt|�ko[dknr8tSq8WntSdS(NuUTF-8i�i��(R�R�RRR�tordR(Rtdecodedtchar((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytcheck_utf8_markersWs
	
"cS@s.y|jt�Wntk
r%tSXtSdS(N(R�R�R�RR(R((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytcheck_system_encodingds

u
Not a fileurbiNuFailed to open fileuUTF-8uASCIIuCould not detect file encoding(RR"R:RRQR�R�R�(R"RRR#R$R�R�tencoding((s4/usr/lib/python2.7/site-packages/salt/utils/files.pytget_encoding%s*	
		
	(uufile(uhttpuhttpsuftpuswiftus3(usaltufile(Rt__doc__t
__future__RRRRt
contextlibRatloggingRR�R*RDRSRRstsalt.utils.pathR tsalt.utils.platformtsalt.utils.stringutilstsalt.modules.selinuxtsalt.exceptionsRRRtsalt.utils.decorators.jinjaRtsalt.extRtsalt.ext.six.movesRtsalt.ext.six.moves.urllib.parseR	R�RR�RPRt	getLoggert__name__RxtLOCAL_PROTOSt
REMOTE_PROTOStVALID_PROTOStTEMPFILE_PREFIXtHASHEStdictt	iteritemsR�tyt
HASHES_REVMAPR
RRR5R+R`RRmtcontextmanagerR�R�R�RQR�R�R�R�R
R�R�R�R�R�R�RcR�R�RR
RAR&(((s4/usr/lib/python2.7/site-packages/salt/utils/files.pyt<module>s�




4	
			?	L			J-2	
	!		&					

Zerion Mini Shell 1.0