Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/shellutils.cpython-37.pyc

B

�d{T8�@sdZddlmZdZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlmZmZmZmZmZddlmZddlmZmZddlmZmZdd	lm Z dd
l!m"Z"yddl#m$Z$m%Z%Wn.e&k
�rGdd
�d
e'�Z%dd�Z$YnXGdd�de(�Z)Gdd�de(�Z*d9dd�Z+ej,fdd�Z-dd�Z.dd�Z/defdd�Z0efdd �Z1d!d"�Z2Gd#d$�d$�Z3e"d%�e3�Z3d:d(d)�Z4d*d+�Z5Gd,d-�d-e(�Z6Gd.d/�d/e(�Z7e(�Z8Gd0d1�d1e(�Z9Gd2d3�d3e(�Z:e:�Z;d4d5�Z<d6e
j=e
j>fd7d8�Z?dS);zTshell/term utilities, useful to write some python scripts instead of shell
scripts.
�)�print_functionzrestructuredtext enN)�exists�isdir�islink�basename�join)�string_types)�range�input)�
STD_BLACKLIST�_handle_blacklist)�str_to_bytes)�
deprecated)�ProcInfo�
NoSuchProcessc@seZdZdS)rN)�__name__�
__module__�__qualname__�rr�J/opt/alt/python37/lib/python3.7/site-packages/logilab/common/shellutils.pyr3srcCs
t��dS)N)r)�pidrrrr5src@seZdZdd�Zdd�ZdS)�tempdircCst��|_|jS)N)�tempfile�mkdtemp�path)�selfrrr�	__enter__;s
ztempdir.__enter__cCst�|j�|dkS)N)�shutil�rmtreer)r�exctype�value�	tracebackrrr�__exit__?sztempdir.__exit__N)rrrrr"rrrrr9src@s$eZdZdd�Zdd�Zdd�ZdS)�pushdcCs
||_dS)N)�	directory)rr$rrr�__init__Fszpushd.__init__cCst��|_t�|j�|jS)N)�os�getcwd�cwd�chdirr$)rrrrrIs
zpushd.__enter__cCst�|j�dS)N)r&r)r()rrr r!rrrr"Nszpushd.__exit__N)rrrr%rr"rrrrr#Esr#cCs�|dkrd}n6yt|�}Wn(tk
rBddl}|�|�j}YnX|dkrRd}n6yt|�}Wn(tk
r�ddl}|�|�j}YnXt�	|||�dS)z�Same as `os.chown` function but accepting user login or group name as
    argument. If login or group is omitted, it's left unchanged.

    Note: you must own the file to chown it (or be root). Otherwise OSError is raised.
    N���r)
�int�
ValueError�pwd�getpwnam�pw_uid�grp�getgrnam�gr_gidr&�chown)rZlogin�group�uidr-�gidr0rrrr3Rsr3c
Cs�t�|�}t|�dkrFt|�s"t�x�|D]}||t|t|���q(Wn�y|d}Wn tk
rrtd|��YnXt|�r�t|�r�t|t|��}y|||�Wn4tk
r�}ztd|||f��Wdd}~XYnXdS)z+A shell-like mv, supporting wildcards.
    �rzNo file matching %szUnable to move %r to %r (%s)N)	�glob�lenr�AssertionErrorrr�
IndexError�OSErrorr)�source�destination�_action�sources�filename�exrrr�mvjs 

rCcGsXxR|D]J}xDt�|�D]6}t|�r.t�|�qt|�rBt�|�qt�|�qWqWdS)z+A shell-like rm, supporting wildcards.
    N)r8rr&�removerrr)�filesZwfilerArrr�rms
rFcCst||tjd�dS)z+A shell-like cp, supporting wildcards.
    )r?N)rCr�copy)r=r>rrr�cp�srHFcs�t�t�r�f�|rdd��ndd��g}xRt�|�D]D\�}}t|||�t��}||kr^q6|����fdd�|D��q6W|S)a�Recursively find files ending with the given extensions from the directory.

    :type directory: str
    :param directory:
      directory where the search should start

    :type exts: basestring or list or tuple
    :param exts:
      extensions or lists or extensions to search

    :type exclude: boolean
    :param exts:
      if this argument is True, returning files NOT ending with the given
      extensions

    :type blacklist: list or tuple
    :param blacklist:
      optional list of files or directory to ignore, default to the value of
      `logilab.common.STD_BLACKLIST`

    :rtype: list
    :return:
      the list of all matching files
    cSs x|D]}|�|�rdSqWdS)NFT)�endswith)rA�exts�extrrr�match�s

zfind.<locals>.matchcSs x|D]}|�|�rdSqWdS)NTF)rI)rArJrKrrrrL�s

cs g|]}�|��rt�|��qSr)r)�.0�f)�dirpathrJrLrr�
<listcomp>�szfind.<locals>.<listcomp>)�
isinstancerr&�walkrr�extend)r$rJ�exclude�	blacklistrE�dirnames�	filenames�dirnamer)rOrJrLr�find�s

 rYccsLxFt�|�D]8\}}}t|||�x t�||�D]}t||�Vq0WqWdS)aLRecursively finds files matching glob `pattern` under `directory`.

    This is an alternative to `logilab.common.shellutils.find`.

    :type directory: str
    :param directory:
      directory where the search should start

    :type pattern: basestring
    :param pattern:
      the glob pattern (e.g *.py, foo*.py, etc.)

    :type blacklist: list or tuple
    :param blacklist:
      optional list of files or directory to ignore, default to the value of
      `logilab.common.STD_BLACKLIST`

    :rtype: iterator
    :return:
      iterator over the list of all matching files
    N)r&rRr�fnmatch�filterr)r$�patternrU�curdirrVrW�fnamerrr�globfind�sr_cCs~ddl}t|�st�|�|�|�}xT|��D]H}|�d�rNt�t||��q.tt||�d�}|�	|�
|��|��q.WdS)Nr�/�wb)�zipfilerr&�mkdir�ZipFile�namelistrIr�open�write�read�close)�archiveZdestdirrbZzfobj�name�outfilerrr�unzip�s


rmc@seZdZdZdd�ZdS)�ExecutezsThis is a deadlock safe version of popen2 (no stdin), that returns
    an object with errorlevel, out and err.
    cCs8tj|dtjtjd�}|��\|_|_t�|j�|_	dS)NT)�shell�stdout�stderr)
�
subprocess�Popen�PIPE�communicate�out�errr&�WEXITSTATUS�
returncode�status)r�command�cmdrrrr%�szExecute.__init__N)rrr�__doc__r%rrrrrn�srnzUse subprocess.Popen instead�
�c
Cs\t|�}�xL|�rJy@t�|tjtjBtjB�}t�|ttt�	����t�
|�dStk
�rF}z�|jtj
k�r"ybt|d�}t|���}t|�}t��t�|�tj}	|	|dkr�td|��|||	df��WnHtk
r��Yn4tk
�r
t�|�Yntk
�rYnXn�|d8}t�|�Wdd}~XYqXqWtd|��dS)z�Acquire a lock represented by a file on the file system

    If the process written in lock file doesn't exist anymore, we remove the
    lock file immediately
    If age of the lock_file is greater than max_delay, then we raise a UserWarning
    T�rr7z=Command '%s' (pid %s) has locked the file '%s' for %s minutes�<NzUnable to acquire %s)�absr&rf�O_EXCL�O_RDWR�O_CREATrgr
�str�getpidrir<�errno�EEXISTr+�readliner�time�stat�ST_MTIME�UserWarningrkrrD�	Exception�sleep)
�	lock_fileZmax_try�delayZ	max_delay�count�fd�er�piZagerrr�acquire_lock�s6


 r�cCst�|�dS)z8Release a lock represented by a file on the file system.N)r&rD)r�rrr�release_lock"sr�c@sbeZdZdZdejdfdd�Zdd�Zdd	d
�Zdd�Z	e
eee	�Zddd�Zdd�Z
dd�ZdS)�ProgressBarzA simple text progression bar.��cCsT|rd|t|�f|_ndt|�|_||_||_||_d|_d|_d|_d|_dS)Nz
%s [%%-%ss]z	
[%%-%ss]r)	r+�_fstr�_stream�_total�_size�_current�	_progress�
_current_text�_last_text_write_size)r�nbops�size�stream�titlerrrr%*szProgressBar.__init__cCs|jS)N)r�)rrrr�	_get_text7szProgressBar._get_textNcCs||jkr||_|��dS)N)r��refresh)r�textrrr�	_set_text:s
zProgressBar._set_textcCs
d|_dS)N)r�)rrrr�	_del_text?szProgressBar._del_textr7FcCsT|r||_n|j|7_tt|j�t|j�|j�}||jkrP||_|��dS)z�Move FORWARD to new cursor position (cursor will never go backward).

        :offset: fraction of ``size``

        :exact:

          - False: offset relative to current cursor position if True
          - True: offset as an asbsolute position

        N)r�r+�floatr�r�r�r�)r�offset�exact�progressrrr�updateDs
zProgressBar.updatecCst|j�|jdt|j|j��|js,|jrfd|j}|j}|dkrHd}|j�||�t|�	��|_|j�
�dS)z$Refresh the progression bar display.�=z %%-%isNr�)r�rgr��minr�r�r�r�r9�rstrip�flush)r�templater�rrrr�Ys 
zProgressBar.refreshcCs|j�d�|j��dS)N�
)r�rgr�)rrrr�finisheszProgressBar.finish)N)r7F)rrrr}�sysrpr%r�r�r��propertyr�r�r�r�rrrrr�'s


r�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�DummyProgressBar)r�cCsdS)Nr)rrrrr�mszDummyProgressBar.refreshcCsdS)Nr)rrrrr�oszDummyProgressBar.updatecCsdS)Nr)rrrrr�qszDummyProgressBar.finishN)rrrZ__slot__r�r�r�rrrrr�jsr�c@s0eZdZeeeedfdd�Zdd�Zdd�ZdS)	r�TcCs"||_||_||_||_||_dS)N)r�r�r�r��enabled)rr�r�r�r�r�rrrr%xs
zprogress.__init__cCsN|jr@i}x&dD]}t||�}|tk	r|||<qWtf|�|_nt�|_|jS)N)r�r�r�r�)r��getattr�_MARKERr��pbr�)r�kwargs�attrr rrrrs

zprogress.__enter__cCs|j��dS)N)r�r�)r�exc_type�exc_val�exc_tbrrrr"�szprogress.__exit__N)rrrr�r%rr"rrrrr�vsr�c@s(eZdZd	dd�Zdd�Zd
dd�ZdS)�RawInputNcCs|pt|_||_dS)N)�	raw_input�_input�_print)rr
�printerrrrr%�s
zRawInput.__init__cs:||kst�g}x`|D]X}||kr0|d��}n|d��}t|�dkr`|d|dd���7}|�||f�qWd|d�dd�|D��f}d}x�|dk�r,|�|�������s�|S�fd	d�|D�}	t|	�dkr�|	dSt|	�dkr�d
�}
nd�d�|	�f}
|j�r|�|
�nt	|
�|d8}q�Wt
d
��dS)Nrr7z(%s)z	%s [%s]: r`cSsg|]}|d�qS)r7r)rM�optrrrrP�sz RawInput.ask.<locals>.<listcomp>�cs"g|]\}}|�����r|�qSr)�lower�
startswith)rM�option�label)�answerrrrP�sz%s is not an option.z+%s is an ambiguous answer, do you mean %s ?z or zunable to get a sensible answer)r:�upperr�r9�appendrr��stripr��printr�)r�question�options�default�choicesr�r��promptZtries�possible�msgr)r�r�ask�s8

zRawInput.askTcCs"|rdp
d}|�|d|�}|dkS)N�y�n)r�r�)r�)rr�Zdefault_is_yesr�r�rrr�confirm�szRawInput.confirm)NN)T)rrrr%r�r�rrrrr��s
"r�cCs2tjdkr$ddl}|�t���dStjdSdS)z�avoid using os.getlogin() because of strange tty / stdin problems
    (man 3 getlogin)
    Another solution would be to use $LOGNAME, $USER or $USERNAME
    �win32rNZUSERNAME)r��platformr-�getpwuidr&�getuid�environ)r-rrr�getlogin�s
r��cCs(d}xt|�D]}|t�|�7}qW|S)z!dumb password generation functionr�)r	�random�choice)�lengthZvocabr-�irrr�generate_password�sr�)NN)r~r~r)@r}�
__future__r�
__docformat__r&r8rr�r�rr�rZr��stringr�rr�os.pathrrrrrZsixrZ	six.movesr	r
r�Zlogilab.commonrrZlogilab.common.compatr
Zlogilab.common.deprecationrZlogilab.common.procrr�ImportErrorr��objectrr#r3�moverCrFrHrYr_rmrnr�r�r�r�r�r�r�ZASKr��
ascii_letters�digitsr�rrrr�<module>s\
2

*C-